1 /* Skeleton for a conversion module.
2 Copyright (C) 1998, 1999, 2000 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
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
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
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
91 # define DL_CALL_FCT(fct, args) fct args
94 /* The direction objects. */
95 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
96 static int from_object;
99 # ifndef FROM_DIRECTION
100 # define FROM_DIRECTION (step->__data == &from_object)
103 # ifndef FROM_DIRECTION
104 # error "FROM_DIRECTION must be provided if direction objects are not used"
109 /* How many bytes are needed at most for the from-charset. */
110 #ifndef MAX_NEEDED_FROM
111 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
114 /* Same for the to-charset. */
115 #ifndef MAX_NEEDED_TO
116 # define MAX_NEEDED_TO MIN_NEEDED_TO
120 /* For conversions from a fixed width character sets to another fixed width
121 character set we we can define RESET_INPUT_BUFFER is necessary. */
122 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
123 # if MIN_NEEDED_FROM == MAX_NEEDED_FROM && MIN_NEEDED_TO == MAX_NEEDED_TO
124 /* We have to use these `if's here since the compiler cannot know that
125 (outbuf - outerr) is always divisible by MIN_NEEDED_TO. */
126 # define RESET_INPUT_BUFFER \
127 if (MIN_NEEDED_FROM % MIN_NEEDED_TO == 0) \
128 *inbuf -= (outbuf - outerr) * (MIN_NEEDED_FROM / MIN_NEEDED_TO); \
129 else if (MIN_NEEDED_TO % MIN_NEEDED_FROM == 0) \
130 *inbuf -= (outbuf - outerr) / (MIN_NEEDED_TO / MIN_NEEDED_FROM); \
132 *inbuf -= ((outbuf - outerr) / MIN_NEEDED_TO) * MIN_NEEDED_FROM
137 /* The default init function. It simply matches the name and initializes
138 the step data to point to one of the objects above. */
140 # ifndef CHARSET_NAME
141 # error "CHARSET_NAME not defined"
145 gconv_init (struct __gconv_step *step)
147 /* Determine which direction. */
148 if (strcmp (step->__from_name, CHARSET_NAME) == 0)
150 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 if (strcmp (step->__to_name, CHARSET_NAME) == 0)
159 step->__data = &to_object;
161 step->__min_needed_from = MIN_NEEDED_TO;
162 step->__max_needed_from = MAX_NEEDED_TO;
163 step->__min_needed_to = MIN_NEEDED_FROM;
164 step->__max_needed_to = MAX_NEEDED_FROM;
167 return __GCONV_NOCONV;
170 step->__stateful = 1;
172 step->__stateful = 0;
180 /* The default destructor function does nothing in the moment and so
181 be define it at all. But we still provide the macro just in case
182 we need it some day. */
187 /* If no arguments have to passed to the loop function define the macro
189 #ifndef EXTRA_LOOP_ARGS
190 # define EXTRA_LOOP_ARGS
194 /* This is the actual conversion function. */
195 #ifndef FUNCTION_NAME
196 # define FUNCTION_NAME gconv
200 FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
201 const unsigned char **inbuf, const unsigned char *inbufend,
202 size_t *written, int do_flush)
204 struct __gconv_step *next_step = step + 1;
205 struct __gconv_step_data *next_data = data + 1;
206 __gconv_fct fct = data->__is_last ? NULL : next_step->__fct;
209 /* If the function is called with no input this means we have to reset
210 to the initial state. The possibly partly converted input is
216 #ifdef EMIT_SHIFT_TO_INIT
217 /* Emit the escape sequence to reset the state. */
220 /* Call the steps down the chain if there are any but only if we
221 successfully emitted the escape sequence. */
222 if (status == __GCONV_OK && ! data->__is_last)
223 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
228 /* We preserve the initial values of the pointer variables. */
229 const unsigned char *inptr = *inbuf;
230 unsigned char *outbuf = data->__outbuf;
231 unsigned char *outend = data->__outbufend;
232 unsigned char *outstart;
234 /* This variable is used to count the number of characters we
235 actually converted. */
236 size_t converted = 0;
244 /* Remember the start value for this round. */
246 /* The outbuf buffer is empty. */
249 #ifdef SAVE_RESET_STATE
250 SAVE_RESET_STATE (1);
254 /* Run the conversion loop. */
255 status = FROM_LOOP (inbuf, inbufend, &outbuf, outend,
256 data->__statep, step->__data, &converted
259 /* Run the conversion loop. */
260 status = TO_LOOP (inbuf, inbufend, &outbuf, outend,
261 data->__statep, step->__data, &converted
264 /* If this is the last step leave the loop, there is nothing
268 /* Store information about how many bytes are available. */
269 data->__outbuf = outbuf;
271 /* Remember how many non-identical characters we converted. */
272 *written += converted;
277 /* Write out all output which was produced. */
278 if (outbuf > outstart)
280 const unsigned char *outerr = data->__outbuf;
283 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
284 outbuf, written, 0));
286 if (result != __GCONV_EMPTY_INPUT)
288 if (outerr != outbuf)
290 #ifdef RESET_INPUT_BUFFER
293 /* We have a problem with the in on of the functions
294 below. Undo the conversion upto the error point. */
297 /* Reload the pointers. */
301 /* Reset the state. */
302 # ifdef SAVE_RESET_STATE
303 SAVE_RESET_STATE (0);
307 /* Run the conversion loop. */
308 nstatus = FROM_LOOP ((const unsigned char **) inbuf,
309 (const unsigned char *) inbufend,
310 (unsigned char **) &outbuf,
311 (unsigned char *) outerr,
312 data->__statep, step->__data,
313 &converted EXTRA_LOOP_ARGS);
315 /* Run the conversion loop. */
316 nstatus = TO_LOOP ((const unsigned char **) inbuf,
317 (const unsigned char *) inbufend,
318 (unsigned char **) &outbuf,
319 (unsigned char *) outerr,
320 data->__statep, step->__data,
321 &converted EXTRA_LOOP_ARGS);
323 /* We must run out of output buffer space in this
325 assert (outbuf == outerr);
326 assert (nstatus == __GCONV_FULL_OUTPUT);
327 #endif /* reset input buffer */
330 /* Change the status. */
334 /* All the output is consumed, we can make another run
335 if everything was ok. */
336 if (status == __GCONV_FULL_OUTPUT)
340 /* We finished one use of the loops. */
341 ++data->__invocation_counter;
343 while (status == __GCONV_OK);
356 #undef MIN_NEEDED_FROM
358 #undef MAX_NEEDED_FROM
360 #undef DEFINE_DIRECTION_OBJECTS
361 #undef FROM_DIRECTION
362 #undef EMIT_SHIFT_TO_INIT
366 #undef RESET_INPUT_BUFFER