1 /* This file is taken from the glibc sources (release 2.2.4).
2 Only this initial comment has been added. The next comment
3 gives the original copyright notice.
6 /* Skeleton for a conversion module.
7 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
8 This file is part of the GNU C Library.
9 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
11 The GNU C Library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 The GNU C Library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with the GNU C Library; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 /* This file can be included to provide definitions of several things
27 many modules have in common. It can be customized using the following
30 DEFINE_INIT define the default initializer. This requires the
31 following symbol to be defined.
33 CHARSET_NAME string with official name of the coded character
36 DEFINE_FINI define the default destructor function.
38 MIN_NEEDED_FROM minimal number of bytes needed for the from-charset.
39 MIN_NEEDED_TO likewise for the to-charset.
41 MAX_NEEDED_FROM maximal number of bytes needed for the from-charset.
42 This macro is optional, it defaults to MIN_NEEDED_FROM.
43 MAX_NEEDED_TO likewise for the to-charset.
45 DEFINE_DIRECTION_OBJECTS
46 two objects will be defined to be used when the
47 `gconv' function must only distinguish two
48 directions. This is implied by DEFINE_INIT.
49 If this macro is not defined the following
50 macro must be available.
52 FROM_DIRECTION this macro is supposed to return a value != 0
53 if we convert from the current character set,
54 otherwise it return 0.
56 EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
57 defines some code which writes out a sequence
58 of characters which bring the current state into
61 FROM_LOOP name of the function implementing the conversion
62 from the current characters.
63 TO_LOOP likewise for the other direction
65 ONE_DIRECTION optional. If defined to 1, only one conversion
66 direction is defined instead of two. In this
67 case, FROM_DIRECTION should be defined to 1, and
68 FROM_LOOP and TO_LOOP should have the same value.
70 SAVE_RESET_STATE in case of an error we must reset the state for
71 the rerun so this macro must be defined for
72 stateful encodings. It takes an argument which
73 is nonzero when saving.
75 RESET_INPUT_BUFFER If the input character sets allow this the macro
76 can be defined to reset the input buffer pointers
77 to cover only those characters up to the error.
79 FUNCTION_NAME if not set the conversion function is named `gconv'.
81 PREPARE_LOOP optional code preparing the conversion loop. Can
82 contain variable definitions.
83 END_LOOP also optional, may be used to store information
85 EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
101 # define DL_CALL_FCT(fct, args) fct args
104 /* The direction objects. */
105 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
106 static int from_object;
107 static int to_object;
109 # ifndef FROM_DIRECTION
110 # define FROM_DIRECTION (step->__data == &from_object)
113 # ifndef FROM_DIRECTION
114 # error "FROM_DIRECTION must be provided if direction objects are not used"
119 /* How many bytes are needed at most for the from-charset. */
120 #ifndef MAX_NEEDED_FROM
121 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
124 /* Same for the to-charset. */
125 #ifndef MAX_NEEDED_TO
126 # define MAX_NEEDED_TO MIN_NEEDED_TO
130 /* Define macros which can access unaligned buffers. These macros are
131 supposed to be used only in code outside the inner loops. For the inner
132 loops we have other definitions which allow optimized access. */
133 #ifdef _STRING_ARCH_unaligned
134 /* We can handle unaligned memory access. */
135 # define get16u(addr) *((__const uint16_t *) (addr))
136 # define get32u(addr) *((__const uint32_t *) (addr))
138 /* We need no special support for writing values either. */
139 # define put16u(addr, val) *((uint16_t *) (addr)) = (val)
140 # define put32u(addr, val) *((uint32_t *) (addr)) = (val)
142 /* Distinguish between big endian and little endian. */
143 # if __BYTE_ORDER == __LITTLE_ENDIAN
144 # define get16u(addr) \
145 (((__const unsigned char *) (addr))[1] << 8 \
146 | ((__const unsigned char *) (addr))[0])
147 # define get32u(addr) \
148 (((((__const unsigned char *) (addr))[3] << 8 \
149 | ((__const unsigned char *) (addr))[2]) << 8 \
150 | ((__const unsigned char *) (addr))[1]) << 8 \
151 | ((__const unsigned char *) (addr))[0])
153 # define put16u(addr, val) \
154 ({ uint16_t __val = (val); \
155 ((unsigned char *) (addr))[0] = __val; \
156 ((unsigned char *) (addr))[1] = __val >> 8; \
158 # define put32u(addr, val) \
159 ({ uint32_t __val = (val); \
160 ((unsigned char *) (addr))[0] = __val; \
162 ((unsigned char *) (addr))[1] = __val; \
164 ((unsigned char *) (addr))[2] = __val; \
166 ((unsigned char *) (addr))[3] = __val; \
169 # define get16u(addr) \
170 (((__const unsigned char *) (addr))[0] << 8 \
171 | ((__const unsigned char *) (addr))[1])
172 # define get32u(addr) \
173 (((((__const unsigned char *) (addr))[0] << 8 \
174 | ((__const unsigned char *) (addr))[1]) << 8 \
175 | ((__const unsigned char *) (addr))[2]) << 8 \
176 | ((__const unsigned char *) (addr))[3])
178 # define put16u(addr, val) \
179 ({ uint16_t __val = (val); \
180 ((unsigned char *) (addr))[1] = __val; \
181 ((unsigned char *) (addr))[0] = __val >> 8; \
183 # define put32u(addr, val) \
184 ({ uint32_t __val = (val); \
185 ((unsigned char *) (addr))[3] = __val; \
187 ((unsigned char *) (addr))[2] = __val; \
189 ((unsigned char *) (addr))[1] = __val; \
191 ((unsigned char *) (addr))[0] = __val; \
197 /* For conversions from a fixed width character set to another fixed width
198 character set we can define RESET_INPUT_BUFFER in a very fast way. */
199 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
200 # if MIN_NEEDED_FROM == MAX_NEEDED_FROM && MIN_NEEDED_TO == MAX_NEEDED_TO
201 /* We have to use these `if's here since the compiler cannot know that
202 (outbuf - outerr) is always divisible by MIN_NEEDED_TO. */
203 # define RESET_INPUT_BUFFER \
204 if (MIN_NEEDED_FROM % MIN_NEEDED_TO == 0) \
205 *inptrp -= (outbuf - outerr) * (MIN_NEEDED_FROM / MIN_NEEDED_TO); \
206 else if (MIN_NEEDED_TO % MIN_NEEDED_FROM == 0) \
207 *inptrp -= (outbuf - outerr) / (MIN_NEEDED_TO / MIN_NEEDED_FROM); \
209 *inptrp -= ((outbuf - outerr) / MIN_NEEDED_TO) * MIN_NEEDED_FROM
214 /* The default init function. It simply matches the name and initializes
215 the step data to point to one of the objects above. */
217 # ifndef CHARSET_NAME
218 # error "CHARSET_NAME not defined"
221 extern int gconv_init (struct __gconv_step *step);
223 gconv_init (struct __gconv_step *step)
225 /* Determine which direction. */
226 if (strcmp (step->__from_name, CHARSET_NAME) == 0)
228 step->__data = &from_object;
230 step->__min_needed_from = MIN_NEEDED_FROM;
231 step->__max_needed_from = MAX_NEEDED_FROM;
232 step->__min_needed_to = MIN_NEEDED_TO;
233 step->__max_needed_to = MAX_NEEDED_TO;
235 else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0)
237 step->__data = &to_object;
239 step->__min_needed_from = MIN_NEEDED_TO;
240 step->__max_needed_from = MAX_NEEDED_TO;
241 step->__min_needed_to = MIN_NEEDED_FROM;
242 step->__max_needed_to = MAX_NEEDED_FROM;
245 return __GCONV_NOCONV;
247 #ifdef SAVE_RESET_STATE
248 step->__stateful = 1;
250 step->__stateful = 0;
258 /* The default destructor function does nothing in the moment and so
259 we don't define it at all. But we still provide the macro just in
260 case we need it some day. */
265 /* If no arguments have to passed to the loop function define the macro
267 #ifndef EXTRA_LOOP_ARGS
268 # define EXTRA_LOOP_ARGS
272 /* This is the actual conversion function. */
273 #ifndef FUNCTION_NAME
274 # define FUNCTION_NAME gconv
277 /* The macros are used to access the function to convert single characters. */
278 #define SINGLE(fct) SINGLE2 (fct)
279 #define SINGLE2(fct) fct##_single
282 extern int FUNCTION_NAME (struct __gconv_step *step,
283 struct __gconv_step_data *data,
284 const unsigned char **inptrp,
285 const unsigned char *inend,
286 unsigned char **outbufstart, size_t *irreversible,
287 int do_flush, int consume_incomplete);
289 FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
290 const unsigned char **inptrp, const unsigned char *inend,
291 unsigned char **outbufstart, size_t *irreversible, int do_flush,
292 int consume_incomplete)
294 struct __gconv_step *next_step = step + 1;
295 struct __gconv_step_data *next_data = data + 1;
299 fct = (data->__flags & __GCONV_IS_LAST) ? NULL : next_step->__fct;
301 /* If the function is called with no input this means we have to reset
302 to the initial state. The possibly partly converted input is
304 if (__builtin_expect (do_flush, 0))
306 /* This should never happen during error handling. */
307 assert (outbufstart == NULL);
311 #ifdef EMIT_SHIFT_TO_INIT
314 /* We preserve the initial values of the pointer variables. */
315 unsigned char *outbuf = data->__outbuf;
316 unsigned char *outstart = outbuf;
317 unsigned char *outend = data->__outbufend;
323 # ifdef SAVE_RESET_STATE
324 SAVE_RESET_STATE (1);
327 /* Emit the escape sequence to reset the state. */
330 /* Call the steps down the chain if there are any but only if we
331 successfully emitted the escape sequence. This should only
332 fail if the output buffer is full. If the input is invalid
333 it should be discarded since the user wants to start from a
335 if (status == __GCONV_OK)
337 if (data->__flags & __GCONV_IS_LAST)
338 /* Store information about how many bytes are available. */
339 data->__outbuf = outbuf;
342 /* Write out all output which was produced. */
343 if (outbuf > outstart)
345 const unsigned char *outerr = outstart;
348 result = DL_CALL_FCT (fct, (next_step, next_data,
349 &outerr, outbuf, NULL,
351 consume_incomplete));
353 if (result != __GCONV_EMPTY_INPUT)
355 if (__builtin_expect (outerr != outbuf, 0))
357 /* We have a problem. Undo the conversion. */
360 /* Restore the state. */
361 # ifdef SAVE_RESET_STATE
362 SAVE_RESET_STATE (0);
366 /* Change the status. */
371 if (status == __GCONV_OK)
372 /* Now flush the remaining steps. */
373 status = DL_CALL_FCT (fct, (next_step, next_data, NULL,
374 NULL, NULL, irreversible, 1,
375 consume_incomplete));
382 /* Clear the state object. There might be bytes in there from
383 previous calls with CONSUME_INCOMPLETE == 1. But don't emit
385 memset (data->__statep, '\0', sizeof (*data->__statep));
387 if (! (data->__flags & __GCONV_IS_LAST))
388 /* Now flush the remaining steps. */
389 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
390 NULL, irreversible, do_flush,
391 consume_incomplete));
396 /* We preserve the initial values of the pointer variables. */
397 const unsigned char *inptr = *inptrp;
398 unsigned char *outbuf = (__builtin_expect (outbufstart == NULL, 1)
399 ? data->__outbuf : *outbufstart);
400 unsigned char *outend = data->__outbufend;
401 unsigned char *outstart;
402 /* This variable is used to count the number of characters we
403 actually converted. */
404 size_t lirreversible = 0;
405 size_t *lirreversiblep = irreversible ? &lirreversible : NULL;
406 #if defined _STRING_ARCH_unaligned \
407 || MIN_NEEDED_FROM == 1 || MAX_NEEDED_FROM % MIN_NEEDED_FROM != 0 \
408 || MIN_NEEDED_TO == 1 || MAX_NEEDED_TO % MIN_NEEDED_TO != 0
412 # define GEN_unaligned(name) GEN_unaligned2 (name)
413 # define GEN_unaligned2(name) name##_unaligned
420 #if MAX_NEEDED_FROM > 1 || MAX_NEEDED_TO > 1
421 /* If the function is used to implement the mb*towc*() or wc*tomb*()
422 functions we must test whether any bytes from the last call are
423 stored in the `state' object. */
424 if (((MAX_NEEDED_FROM > 1 && MAX_NEEDED_TO > 1)
425 || (MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
426 || (MAX_NEEDED_TO > 1 && !FROM_DIRECTION))
427 && consume_incomplete && (data->__statep->__count & 7) != 0)
429 /* Yep, we have some bytes left over. Process them now.
430 But this must not happen while we are called from an
432 assert (outbufstart == NULL);
434 # if MAX_NEEDED_FROM > 1
435 if (MAX_NEEDED_TO == 1 || FROM_DIRECTION)
436 status = SINGLE(FROM_LOOP) (step, data, inptrp, inend, &outbuf,
437 outend, lirreversiblep
440 # if MAX_NEEDED_FROM > 1 && MAX_NEEDED_TO > 1 && !ONE_DIRECTION
443 # if MAX_NEEDED_TO > 1 && !ONE_DIRECTION
444 status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf,
445 outend, lirreversiblep EXTRA_LOOP_ARGS);
448 if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
453 #if !defined _STRING_ARCH_unaligned \
454 && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
455 && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
456 /* The following assumes that encodings, which have a variable length
457 what might unalign a buffer even though it is a aligned in the
458 beginning, either don't have the minimal number of bytes as a divisor
459 of the maximum length or have a minimum length of 1. This is true
460 for all known and supported encodings. */
461 unaligned = ((FROM_DIRECTION
462 && ((uintptr_t) inptr % MIN_NEEDED_FROM != 0
463 || ((data->__flags & __GCONV_IS_LAST)
464 && (uintptr_t) outbuf % MIN_NEEDED_TO != 0)))
466 && (((data->__flags & __GCONV_IS_LAST)
467 && (uintptr_t) outbuf % MIN_NEEDED_FROM != 0)
468 || (uintptr_t) inptr % MIN_NEEDED_TO != 0)));
473 struct __gconv_trans_data *trans;
475 /* Remember the start value for this round. */
477 /* The outbuf buffer is empty. */
480 #ifdef SAVE_RESET_STATE
481 SAVE_RESET_STATE (1);
484 if (__builtin_expect (!unaligned, 1))
487 /* Run the conversion loop. */
488 status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend,
489 lirreversiblep EXTRA_LOOP_ARGS);
491 /* Run the conversion loop. */
492 status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend,
493 lirreversiblep EXTRA_LOOP_ARGS);
495 #if !defined _STRING_ARCH_unaligned \
496 && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
497 && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
501 /* Run the conversion loop. */
502 status = GEN_unaligned (FROM_LOOP) (step, data, inptrp, inend,
507 /* Run the conversion loop. */
508 status = GEN_unaligned (TO_LOOP) (step, data, inptrp, inend,
515 /* If we were called as part of an error handling module we
516 don't do anything else here. */
517 if (__builtin_expect (outbufstart != NULL, 0))
519 *outbufstart = outbuf;
523 /* Give the transliteration module the chance to store the
524 original text and the result in case it needs a context. */
525 for (trans = data->__trans; trans != NULL; trans = trans->__next)
526 if (trans->__trans_context_fct != NULL)
527 DL_CALL_FCT (trans->__trans_context_fct,
528 (trans->__data, inptr, *inptrp, outstart, outbuf));
530 /* We finished one use of the loops. */
531 ++data->__invocation_counter;
533 /* If this is the last step leave the loop, there is nothing
535 if (__builtin_expect (data->__flags & __GCONV_IS_LAST, 0))
537 /* Store information about how many bytes are available. */
538 data->__outbuf = outbuf;
540 /* Remember how many non-identical characters we
541 converted in a irreversible way. */
542 *irreversible += lirreversible;
547 /* Write out all output which was produced. */
548 if (__builtin_expect (outbuf > outstart, 1))
550 const unsigned char *outerr = data->__outbuf;
553 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
554 outbuf, NULL, irreversible, 0,
555 consume_incomplete));
557 if (result != __GCONV_EMPTY_INPUT)
559 if (__builtin_expect (outerr != outbuf, 0))
561 #ifdef RESET_INPUT_BUFFER
564 /* We have a problem with the in on of the functions
565 below. Undo the conversion upto the error point. */
568 /* Reload the pointers. */
572 /* Restore the state. */
573 # ifdef SAVE_RESET_STATE
574 SAVE_RESET_STATE (0);
577 if (__builtin_expect (!unaligned, 1))
580 /* Run the conversion loop. */
581 nstatus = FROM_LOOP (step, data, inptrp, inend,
586 /* Run the conversion loop. */
587 nstatus = TO_LOOP (step, data, inptrp, inend,
592 # if !defined _STRING_ARCH_unaligned \
593 && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
594 && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
598 /* Run the conversion loop. */
599 nstatus = GEN_unaligned (FROM_LOOP) (step, data,
606 /* Run the conversion loop. */
607 nstatus = GEN_unaligned (TO_LOOP) (step, data,
615 /* We must run out of output buffer space in this
617 assert (outbuf == outerr);
618 assert (nstatus == __GCONV_FULL_OUTPUT);
620 /* If we haven't consumed a single byte decrement
621 the invocation counter. */
622 if (__builtin_expect (outbuf == outstart, 0))
623 --data->__invocation_counter;
624 #endif /* reset input buffer */
627 /* Change the status. */
631 /* All the output is consumed, we can make another run
632 if everything was ok. */
633 if (status == __GCONV_FULL_OUTPUT)
636 outbuf = data->__outbuf;
640 if (status != __GCONV_OK)
643 /* Reset the output buffer pointer for the next round. */
644 outbuf = data->__outbuf;
651 /* If we are supposed to consume all character store now all of the
652 remaining characters in the `state' object. */
653 #if MAX_NEEDED_FROM > 1 || MAX_NEEDED_TO > 1
654 if (((MAX_NEEDED_FROM > 1 && MAX_NEEDED_TO > 1)
655 || (MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
656 || (MAX_NEEDED_TO > 1 && !FROM_DIRECTION))
657 && __builtin_expect (consume_incomplete, 0)
658 && status == __GCONV_INCOMPLETE_INPUT)
661 mbstate_t *state = data->__statep;
667 /* Make sure the remaining bytes fit into the state objects
669 assert (inend - *inptrp < 4);
671 for (cnt = 0; *inptrp < inend; ++cnt)
672 data->__statep->__value.__wchb[cnt] = *(*inptrp)++;
673 data->__statep->__count &= ~7;
674 data->__statep->__count |= cnt;
686 #undef MIN_NEEDED_FROM
688 #undef MAX_NEEDED_FROM
690 #undef DEFINE_DIRECTION_OBJECTS
691 #undef FROM_DIRECTION
692 #undef EMIT_SHIFT_TO_INIT
695 #undef SAVE_RESET_STATE
696 #undef RESET_INPUT_BUFFER