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
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
86 #include <elf/ldsodefs.h>
89 /* The direction objects. */
90 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
91 static int from_object
;
94 # ifndef FROM_DIRECTION
95 # define FROM_DIRECTION (step->data == &from_object)
98 # ifndef FROM_DIRECTION
99 # error "FROM_DIRECTION must be provided if direction objects are not used"
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
109 /* Same for the to-charset. */
110 #ifndef MAX_NEEDED_TO
111 # define MAX_NEEDED_TO MIN_NEEDED_TO
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); \
127 *inbuf -= ((outbuf - outerr) / MIN_NEEDED_TO) * MIN_NEEDED_FROM
132 /* The default init function. It simply matches the name and initializes
133 the step data to point to one of the objects above. */
135 # ifndef CHARSET_NAME
136 # error "CHARSET_NAME not defined"
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
;
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
;
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
;
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. */
183 /* If no arguments have to passed to the loop function define the macro
185 #ifndef EXTRA_LOOP_ARGS
186 # define EXTRA_LOOP_ARGS
190 /* This is the actual conversion function. */
191 #ifndef FUNCTION_NAME
192 # define FUNCTION_NAME gconv
196 FUNCTION_NAME (struct gconv_step
*step
, struct gconv_step_data
*data
,
197 const char **inbuf
, const char *inbufend
, size_t *written
,
200 struct gconv_step
*next_step
= step
+ 1;
201 struct gconv_step_data
*next_data
= data
+ 1;
202 gconv_fct fct
= next_step
->fct
;
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
210 /* Call the steps down the chain if there are any. */
215 #ifdef EMIT_SHIFT_TO_INIT
220 if (status
== GCONV_OK
)
222 /* Give the modules below the same chance. */
223 status
= DL_CALL_FCT (fct
, (next_step
, next_data
, NULL
, NULL
,
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
;
235 /* This variable is used to count the number of characters we
236 actually converted. */
237 size_t converted
= 0;
245 /* Remember the start value for this round. */
247 /* The outbuf buffer is empty. */
250 #ifdef SAVE_RESET_STATE
251 SAVE_RESET_STATE (1);
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
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
271 /* If this is the last step leave the loop, there is nothgin
275 /* Store information about how many bytes are available. */
276 data
->outbuf
= outbuf
;
278 /* Remember how many characters we converted. */
279 *written
+= converted
;
284 /* Write out all output which was produced. */
287 const char *outerr
= data
->outbuf
;
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
300 /* We have a problem with the in on of the functions
301 below. Undo the conversion upto the error point. */
304 /* Reload the pointers. */
308 /* Reset the state. */
309 # ifdef SAVE_RESET_STATE
310 SAVE_RESET_STATE (0);
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
);
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
332 assert (outbuf
== outerr
);
333 assert (nstatus
== GCONV_FULL_OUTPUT
);
334 #endif /* reset input buffer */
337 /* Change the status. */
341 /* All the output is consumed, we can make another run
342 if everything was ok. */
343 if (status
== GCONV_FULL_OUTPUT
)
347 while (status
== GCONV_OK
);
353 /* We finished one use of this step. */
354 ++data
->invocation_counter
;
363 #undef MIN_NEEDED_FROM
365 #undef MAX_NEEDED_FROM
367 #undef DEFINE_DIRECTION_OBJECTS
368 #undef FROM_DIRECTION
369 #undef EMIT_SHIFT_TO_INIT
373 #undef RESET_INPUT_BUFFER