Update.
[glibc.git] / iconv / skeleton.c
blob0bcad9e6d0809f16ea2d138346bc999867ab5b01
1 /* Skeleton for a conversion module.
2 Copyright (C) 1998, 1999 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 <gconv.h>
81 #include <string.h>
82 #define __need_size_t
83 #define __need_NULL
84 #include <stddef.h>
86 #ifndef STATIC_GCONV
87 # include <dlfcn.h>
88 #endif
90 /* The direction objects. */
91 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
92 static int from_object;
93 static int to_object;
95 # ifndef FROM_DIRECTION
96 # define FROM_DIRECTION (step->__data == &from_object)
97 # endif
98 #else
99 # ifndef FROM_DIRECTION
100 # error "FROM_DIRECTION must be provided if direction objects are not used"
101 # endif
102 #endif
105 /* How many bytes are needed at most for the from-charset. */
106 #ifndef MAX_NEEDED_FROM
107 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
108 #endif
110 /* Same for the to-charset. */
111 #ifndef MAX_NEEDED_TO
112 # define MAX_NEEDED_TO MIN_NEEDED_TO
113 #endif
116 /* For conversions from a fixed width character sets to another fixed width
117 character set we we can define RESET_INPUT_BUFFER is necessary. */
118 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
119 # if MIN_NEEDED_FROM == MAX_NEEDED_FROM && MIN_NEEDED_TO == MAX_NEEDED_TO
120 /* We have to use these `if's here since the compiler cannot know that
121 (outbuf - outerr) is always divisible by MIN_NEEDED_TO. */
122 # define RESET_INPUT_BUFFER \
123 if (MIN_NEEDED_FROM % MIN_NEEDED_TO == 0) \
124 *inbuf -= (outbuf - outerr) * (MIN_NEEDED_FROM / MIN_NEEDED_TO); \
125 else if (MIN_NEEDED_TO % MIN_NEEDED_FROM == 0) \
126 *inbuf -= (outbuf - outerr) / (MIN_NEEDED_TO / MIN_NEEDED_FROM); \
127 else \
128 *inbuf -= ((outbuf - outerr) / MIN_NEEDED_TO) * MIN_NEEDED_FROM
129 # endif
130 #endif
133 /* The default init function. It simply matches the name and initializes
134 the step data to point to one of the objects above. */
135 #if DEFINE_INIT
136 # ifndef CHARSET_NAME
137 # error "CHARSET_NAME not defined"
138 # endif
141 gconv_init (struct __gconv_step *step)
143 /* Determine which direction. */
144 if (strcmp (step->__from_name, CHARSET_NAME) == 0)
146 step->__data = &from_object;
148 step->__min_needed_from = MIN_NEEDED_FROM;
149 step->__max_needed_from = MAX_NEEDED_FROM;
150 step->__min_needed_to = MIN_NEEDED_TO;
151 step->__max_needed_to = MAX_NEEDED_TO;
153 else if (strcmp (step->__to_name, CHARSET_NAME) == 0)
155 step->__data = &to_object;
157 step->__min_needed_from = MIN_NEEDED_TO;
158 step->__max_needed_from = MAX_NEEDED_TO;
159 step->__min_needed_to = MIN_NEEDED_FROM;
160 step->__max_needed_to = MAX_NEEDED_FROM;
162 else
163 return __GCONV_NOCONV;
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 unsigned char **inbuf, const unsigned char *inbufend,
198 size_t *written, 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 status = __GCONV_OK;
212 #ifdef EMIT_SHIFT_TO_INIT
213 /* Emit the escape sequence to reset the state. */
214 EMIT_SHIFT_TO_INIT;
215 #endif
216 /* Call the steps down the chain if there are any but only if we
217 successfully emitted the escape sequence. */
218 if (status == __GCONV_OK && ! data->__is_last)
219 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
220 written, 1));
222 else
224 /* We preserve the initial values of the pointer variables. */
225 const unsigned char *inptr = *inbuf;
226 unsigned char *outbuf = data->__outbuf;
227 unsigned char *outend = data->__outbufend;
228 unsigned char *outstart;
230 /* This variable is used to count the number of characters we
231 actually converted. */
232 size_t converted = 0;
234 #ifdef PREPARE_LOOP
235 PREPARE_LOOP
236 #endif
240 /* Remember the start value for this round. */
241 inptr = *inbuf;
242 /* The outbuf buffer is empty. */
243 outstart = outbuf;
245 #ifdef SAVE_RESET_STATE
246 SAVE_RESET_STATE (1);
247 #endif
249 if (FROM_DIRECTION)
250 /* Run the conversion loop. */
251 status = FROM_LOOP (inbuf, inbufend, &outbuf, outend,
252 data->__statep, step->__data, &converted
253 EXTRA_LOOP_ARGS);
254 else
255 /* Run the conversion loop. */
256 status = TO_LOOP (inbuf, inbufend, &outbuf, outend,
257 data->__statep, step->__data, &converted
258 EXTRA_LOOP_ARGS);
260 /* If this is the last step leave the loop, there is nothing
261 we can do. */
262 if (data->__is_last)
264 /* Store information about how many bytes are available. */
265 data->__outbuf = outbuf;
267 /* Remember how many characters we converted. */
268 *written += converted;
270 break;
273 /* Write out all output which was produced. */
274 if (outbuf > outstart)
276 const unsigned char *outerr = data->__outbuf;
277 int result;
279 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
280 outbuf, written, 0));
282 if (result != __GCONV_EMPTY_INPUT)
284 if (outerr != outbuf)
286 #ifdef RESET_INPUT_BUFFER
287 RESET_INPUT_BUFFER;
288 #else
289 /* We have a problem with the in on of the functions
290 below. Undo the conversion upto the error point. */
291 size_t nstatus;
293 /* Reload the pointers. */
294 *inbuf = inptr;
295 outbuf = outstart;
297 /* Reset the state. */
298 # ifdef SAVE_RESET_STATE
299 SAVE_RESET_STATE (0);
300 # endif
302 if (FROM_DIRECTION)
303 /* Run the conversion loop. */
304 nstatus = FROM_LOOP ((const unsigned char **) inbuf,
305 (const unsigned char *) inbufend,
306 (unsigned char **) &outbuf,
307 (unsigned char *) outerr,
308 data->__statep, step->__data,
309 &converted EXTRA_LOOP_ARGS);
310 else
311 /* Run the conversion loop. */
312 nstatus = TO_LOOP ((const unsigned char **) inbuf,
313 (const unsigned char *) inbufend,
314 (unsigned char **) &outbuf,
315 (unsigned char *) outerr,
316 data->__statep, step->__data,
317 &converted EXTRA_LOOP_ARGS);
319 /* We must run out of output buffer space in this
320 rerun. */
321 assert (outbuf == outerr);
322 assert (nstatus == __GCONV_FULL_OUTPUT);
323 #endif /* reset input buffer */
326 /* Change the status. */
327 status = result;
329 else
330 /* All the output is consumed, we can make another run
331 if everything was ok. */
332 if (status == __GCONV_FULL_OUTPUT)
333 status = __GCONV_OK;
336 while (status == __GCONV_OK);
338 #ifdef END_LOOP
339 END_LOOP
340 #endif
342 /* We finished one use of this step. */
343 ++data->__invocation_counter;
346 return status;
349 #undef DEFINE_INIT
350 #undef CHARSET_NAME
351 #undef DEFINE_FINI
352 #undef MIN_NEEDED_FROM
353 #undef MIN_NEEDED_TO
354 #undef MAX_NEEDED_FROM
355 #undef MAX_NEEDED_TO
356 #undef DEFINE_DIRECTION_OBJECTS
357 #undef FROM_DIRECTION
358 #undef EMIT_SHIFT_TO_INIT
359 #undef FROM_LOOP
360 #undef TO_LOOP
361 #undef RESET_STATE
362 #undef RESET_INPUT_BUFFER
363 #undef FUNCTION_NAME
364 #undef PREPARE_LOOP
365 #undef END_LOOP