Extra files for Solaris/SPARC.
[glibc/history.git] / iconv / skeleton.c
blob90203213ef0e4243469cc1f5f08863de6cab165c
1 /* Skeleton for a converison module.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This file can be included to provide definitions of several things
22 many modules have in common. It can be customized using the following
23 macros:
25 DEFINE_INIT define the default initializer. This requires the
26 following symbol to be defined.
28 CHARSET_NAME string with official name of the coded character
29 set (in all-caps)
31 DEFINE_FINI define the default destructor function.
33 MIN_NEEDED_FROM minimal number of bytes needed for the from-charset.
34 MIN_NEEDED_TO likewise for the to-charset.
36 MAX_NEEDED_FROM maximal number of bytes needed for the from-charset.
37 This macro is optional, it defaults to MIN_NEEDED_FROM.
38 MAX_NEEDED_TO likewise for the to-charset.
40 DEFINE_DIRECTION_OBJECTS
41 two objects will be defined to be used when the
42 `gconv' function must only distinguish two
43 directions. This is implied by DEFINE_INIT.
44 If this macro is not defined the following
45 macro must be available.
47 FROM_DIRECTION this macro is supposed to return a value != 0
48 if we convert from the current character set,
49 otherwise it return 0.
51 EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
52 defines some code which writes out a sequence
53 of characters which bring the current state into
54 the initial state.
56 FROM_LOOP name of the function implementing the conversion
57 from the current characters.
58 TO_LOOP likewise for the other direction
60 RESET_STATE in case of an error we must reset the state for
61 the rerun so this macro must be defined for
62 stateful encodings. It takes an argument which
63 is nonzero when saving.
65 RESET_INPUT_BUFFER If the input character sets allow this the macro
66 can be defined to reset the input buffer pointers
67 to cover only those characters up to the error.
69 FUNCTION_NAME if not set the conversion function is named `gconv'.
71 PREPARE_LOOP optional code preparing the conversion loop. Can
72 contain variable definitions.
73 END_LOOP also optional, may be used to store information
75 EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
76 to loop function.
79 #include <assert.h>
80 #include <dlfcn.h>
81 #include <gconv.h>
82 #include <string.h>
83 #define __need_size_t
84 #define __need_NULL
85 #include <stddef.h>
86 #include <elf/ldsodefs.h>
89 /* The direction objects. */
90 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
91 static int from_object;
92 static int to_object;
94 # ifndef FROM_DIRECTION
95 # define FROM_DIRECTION (step->data == &from_object)
96 # endif
97 #else
98 # ifndef FROM_DIRECTION
99 # error "FROM_DIRECTION must be provided if direction objects are not used"
100 # endif
101 #endif
104 /* How many bytes are needed at most for the from-charset. */
105 #ifndef MAX_NEEDED_FROM
106 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
107 #endif
109 /* Same for the to-charset. */
110 #ifndef MAX_NEEDED_TO
111 # define MAX_NEEDED_TO MIN_NEEDED_TO
112 #endif
115 /* For conversions from a fixed width character sets to another fixed width
116 character set we we can define RESET_INPUT_BUFFER is necessary. */
117 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
118 # if MIN_NEEDED_FROM == MAX_NEEDED_FROM && MIN_NEEDED_TO == MAX_NEEDED_TO
119 /* We have to used these `if's here since the compiler cannot know that
120 (outbuf - outerr) is always divisible by MIN_NEEDED_TO. */
121 # define RESET_INPUT_BUFFER \
122 if (MIN_NEEDED_FROM % MIN_NEEDED_TO == 0) \
123 *inbuf -= (outbuf - outerr) * (MIN_NEEDED_FROM / MIN_NEEDED_TO); \
124 else if (MIN_NEEDED_TO % MIN_NEEDED_FROM == 0) \
125 *inbuf -= (outbuf - outerr) / (MIN_NEEDED_TO / MIN_NEEDED_FROM); \
126 else \
127 *inbuf -= ((outbuf - outerr) / MIN_NEEDED_TO) * MIN_NEEDED_FROM
128 # endif
129 #endif
132 /* The default init function. It simply matches the name and initializes
133 the step data to point to one of the objects above. */
134 #if DEFINE_INIT
135 # ifndef CHARSET_NAME
136 # error "CHARSET_NAME not defined"
137 # endif
140 gconv_init (struct gconv_step *step)
142 /* Determine which direction. */
143 if (__strcasecmp (step->from_name, CHARSET_NAME) == 0)
144 step->data = &from_object;
145 else if (__strcasecmp (step->to_name, CHARSET_NAME) == 0)
146 step->data = &to_object;
147 else
148 return GCONV_NOCONV;
150 if (step->data == &from_object)
152 step->min_needed_from = MIN_NEEDED_FROM;
153 step->max_needed_from = MAX_NEEDED_FROM;
154 step->min_needed_to = MIN_NEEDED_TO;
155 step->max_needed_to = MAX_NEEDED_TO;
157 else
159 step->min_needed_from = MIN_NEEDED_TO;
160 step->max_needed_from = MAX_NEEDED_TO;
161 step->min_needed_to = MIN_NEEDED_FROM;
162 step->max_needed_to = MAX_NEEDED_FROM;
165 #ifdef RESET_STATE
166 step->stateful = 1;
167 #else
168 step->stateful = 0;
169 #endif
171 return GCONV_OK;
173 #endif
176 /* The default destructor function does nothing in the moment and so
177 be define it at all. But we still provide the macro just in case
178 we need it some day. */
179 #if DEFINE_FINI
180 #endif
183 /* If no arguments have to passed to the loop function define the macro
184 as empty. */
185 #ifndef EXTRA_LOOP_ARGS
186 # define EXTRA_LOOP_ARGS
187 #endif
190 /* This is the actual conversion function. */
191 #ifndef FUNCTION_NAME
192 # define FUNCTION_NAME gconv
193 #endif
196 FUNCTION_NAME (struct gconv_step *step, struct gconv_step_data *data,
197 const char **inbuf, const char *inbufend, size_t *written,
198 int do_flush)
200 struct gconv_step *next_step = step + 1;
201 struct gconv_step_data *next_data = data + 1;
202 gconv_fct fct = next_step->fct;
203 int status;
205 /* If the function is called with no input this means we have to reset
206 to the initial state. The possibly partly converted input is
207 dropped. */
208 if (do_flush)
210 /* Call the steps down the chain if there are any. */
211 if (data->is_last)
212 status = GCONV_OK;
213 else
215 #ifdef EMIT_SHIFT_TO_INIT
216 status = GCONV_OK;
218 EMIT_SHIFT_TO_INIT;
220 if (status == GCONV_OK)
221 #endif
222 /* Give the modules below the same chance. */
223 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
224 written, 1));
227 else
229 /* We preserve the initial values of the pointer variables. */
230 const char *inptr = *inbuf;
231 char *outbuf = data->outbuf;
232 char *outend = data->outbufend;
233 char *outptr;
235 /* This variable is used to count the number of characters we
236 actually converted. */
237 size_t converted = 0;
239 #ifdef PREPARE_LOOP
240 PREPARE_LOOP
241 #endif
245 /* Remember the start value for this round. */
246 inptr = *inbuf;
247 /* The outbuf buffer is empty. */
248 outptr = outbuf;
250 #ifdef SAVE_RESET_STATE
251 SAVE_RESET_STATE (1);
252 #endif
254 if (FROM_DIRECTION)
255 /* Run the conversion loop. */
256 status = FROM_LOOP ((const unsigned char **) inbuf,
257 (const unsigned char *) inbufend,
258 (unsigned char **) &outbuf,
259 (unsigned char *) outend,
260 data->statep, step->data, &converted
261 EXTRA_LOOP_ARGS);
262 else
263 /* Run the conversion loop. */
264 status = TO_LOOP ((const unsigned char **) inbuf,
265 (const unsigned char *) inbufend,
266 (unsigned char **) &outbuf,
267 (unsigned char *) outend,
268 data->statep, step->data, &converted
269 EXTRA_LOOP_ARGS);
271 /* If this is the last step leave the loop, there is nothgin
272 we can do. */
273 if (data->is_last)
275 /* Store information about how many bytes are available. */
276 data->outbuf = outbuf;
278 /* Remember how many characters we converted. */
279 *written += converted;
281 break;
284 /* Write out all output which was produced. */
285 if (outbuf > outptr)
287 const char *outerr = data->outbuf;
288 int result;
290 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
291 outbuf, written, 0));
293 if (result != GCONV_EMPTY_INPUT)
295 if (outerr != outbuf)
297 #ifdef RESET_INPUT_BUFFER
298 RESET_INPUT_BUFFER;
299 #else
300 /* We have a problem with the in on of the functions
301 below. Undo the conversion upto the error point. */
302 size_t nstatus;
304 /* Reload the pointers. */
305 *inbuf = inptr;
306 outbuf = outptr;
308 /* Reset the state. */
309 # ifdef SAVE_RESET_STATE
310 SAVE_RESET_STATE (0);
311 # endif
313 if (FROM_DIRECTION)
314 /* Run the conversion loop. */
315 nstatus = FROM_LOOP ((const unsigned char **) inbuf,
316 (const unsigned char *) inbufend,
317 (unsigned char **) &outbuf,
318 (unsigned char *) outerr,
319 data->statep, step->data,
320 &converted EXTRA_LOOP_ARGS);
321 else
322 /* Run the conversion loop. */
323 nstatus = TO_LOOP ((const unsigned char **) inbuf,
324 (const unsigned char *) inbufend,
325 (unsigned char **) &outbuf,
326 (unsigned char *) outerr,
327 data->statep, step->data,
328 &converted EXTRA_LOOP_ARGS);
330 /* We must run out of output buffer space in this
331 rerun. */
332 assert (outbuf == outerr);
333 assert (nstatus == GCONV_FULL_OUTPUT);
334 #endif /* reset input buffer */
337 /* Change the status. */
338 status = result;
340 else
341 /* All the output is consumed, we can make another run
342 if everything was ok. */
343 if (status == GCONV_FULL_OUTPUT)
344 status = GCONV_OK;
347 while (status == GCONV_OK);
349 #ifdef END_LOOP
350 END_LOOP
351 #endif
353 /* We finished one use of this step. */
354 ++data->invocation_counter;
357 return status;
360 #undef DEFINE_INIT
361 #undef CHARSET_NAME
362 #undef DEFINE_FINI
363 #undef MIN_NEEDED_FROM
364 #undef MIN_NEEDED_TO
365 #undef MAX_NEEDED_FROM
366 #undef MAX_NEEDED_TO
367 #undef DEFINE_DIRECTION_OBJECTS
368 #undef FROM_DIRECTION
369 #undef EMIT_SHIFT_TO_INIT
370 #undef FROM_LOOP
371 #undef TO_LOOP
372 #undef RESET_STATE
373 #undef RESET_INPUT_BUFFER
374 #undef FUNCTION_NAME
375 #undef PREPARE_LOOP
376 #undef END_LOOP