Update.
[glibc.git] / iconv / skeleton.c
blobedcd92eb8713c1649c3b6a2fea110a25c6a2ec92
1 /* Skeleton for a conversion module.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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 FROM_LOOP_MIN_NEEDED_FROM
41 FROM_LOOP_MAX_NEEDED_FROM
42 minimal/maximal number of bytes needed on input
43 of one round through the FROM_LOOP. Defaults
44 to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
45 FROM_LOOP_MIN_NEEDED_TO
46 FROM_LOOP_MAX_NEEDED_TO
47 minimal/maximal number of bytes needed on output
48 of one round through the FROM_LOOP. Defaults
49 to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
50 TO_LOOP_MIN_NEEDED_FROM
51 TO_LOOP_MAX_NEEDED_FROM
52 minimal/maximal number of bytes needed on input
53 of one round through the TO_LOOP. Defaults
54 to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
55 TO_LOOP_MIN_NEEDED_TO
56 TO_LOOP_MAX_NEEDED_TO
57 minimal/maximal number of bytes needed on output
58 of one round through the TO_LOOP. Defaults
59 to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
61 DEFINE_DIRECTION_OBJECTS
62 two objects will be defined to be used when the
63 `gconv' function must only distinguish two
64 directions. This is implied by DEFINE_INIT.
65 If this macro is not defined the following
66 macro must be available.
68 FROM_DIRECTION this macro is supposed to return a value != 0
69 if we convert from the current character set,
70 otherwise it return 0.
72 EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
73 defines some code which writes out a sequence
74 of bytes which bring the current state into
75 the initial state.
77 FROM_LOOP name of the function implementing the conversion
78 from the current character set.
79 TO_LOOP likewise for the other direction
81 ONE_DIRECTION optional. If defined to 1, only one conversion
82 direction is defined instead of two. In this
83 case, FROM_DIRECTION should be defined to 1, and
84 FROM_LOOP and TO_LOOP should have the same value.
86 SAVE_RESET_STATE in case of an error we must reset the state for
87 the rerun so this macro must be defined for
88 stateful encodings. It takes an argument which
89 is nonzero when saving.
91 RESET_INPUT_BUFFER If the input character sets allow this the macro
92 can be defined to reset the input buffer pointers
93 to cover only those characters up to the error.
95 FUNCTION_NAME if not set the conversion function is named `gconv'.
97 PREPARE_LOOP optional code preparing the conversion loop. Can
98 contain variable definitions.
99 END_LOOP also optional, may be used to store information
101 EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
102 to loop function.
104 Modules can use mbstate_t to store conversion state as follows:
106 * Bits 2..0 of '__count' contain the number of lookahead input bytes
107 stored in __value.__wchb. Always zero if the converter never
108 returns __GCONV_INCOMPLETE_INPUT.
110 * Bits 31..3 of '__count' are module dependent shift state.
112 * __value: When STORE_REST/UNPACK_BYTES aren't defined and when the
113 converter has returned __GCONV_INCOMPLETE_INPUT, this contains
114 at most 4 lookahead bytes. Converters with an mb_cur_max > 4
115 (currently only UTF-8) must find a way to store their state
116 in __value.__wch and define STORE_REST/UNPACK_BYTES appropriately.
118 When __value contains lookahead, __count must not be zero, because
119 the converter is not in the initial state then, and mbsinit() --
120 defined as a (__count == 0) test -- must reflect this.
123 #include <assert.h>
124 #include <gconv.h>
125 #include <string.h>
126 #define __need_size_t
127 #define __need_NULL
128 #include <stddef.h>
130 #ifndef STATIC_GCONV
131 # include <dlfcn.h>
132 #endif
134 #ifndef DL_CALL_FCT
135 # define DL_CALL_FCT(fct, args) fct args
136 #endif
138 /* The direction objects. */
139 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
140 static int from_object;
141 static int to_object;
143 # ifndef FROM_DIRECTION
144 # define FROM_DIRECTION (step->__data == &from_object)
145 # endif
146 #else
147 # ifndef FROM_DIRECTION
148 # error "FROM_DIRECTION must be provided if direction objects are not used"
149 # endif
150 #endif
153 /* How many bytes are needed at most for the from-charset. */
154 #ifndef MAX_NEEDED_FROM
155 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
156 #endif
158 /* Same for the to-charset. */
159 #ifndef MAX_NEEDED_TO
160 # define MAX_NEEDED_TO MIN_NEEDED_TO
161 #endif
163 /* Defaults for the per-direction min/max constants. */
164 #ifndef FROM_LOOP_MIN_NEEDED_FROM
165 # define FROM_LOOP_MIN_NEEDED_FROM MIN_NEEDED_FROM
166 #endif
167 #ifndef FROM_LOOP_MAX_NEEDED_FROM
168 # define FROM_LOOP_MAX_NEEDED_FROM MAX_NEEDED_FROM
169 #endif
170 #ifndef FROM_LOOP_MIN_NEEDED_TO
171 # define FROM_LOOP_MIN_NEEDED_TO MIN_NEEDED_TO
172 #endif
173 #ifndef FROM_LOOP_MAX_NEEDED_TO
174 # define FROM_LOOP_MAX_NEEDED_TO MAX_NEEDED_TO
175 #endif
176 #ifndef TO_LOOP_MIN_NEEDED_FROM
177 # define TO_LOOP_MIN_NEEDED_FROM MIN_NEEDED_TO
178 #endif
179 #ifndef TO_LOOP_MAX_NEEDED_FROM
180 # define TO_LOOP_MAX_NEEDED_FROM MAX_NEEDED_TO
181 #endif
182 #ifndef TO_LOOP_MIN_NEEDED_TO
183 # define TO_LOOP_MIN_NEEDED_TO MIN_NEEDED_FROM
184 #endif
185 #ifndef TO_LOOP_MAX_NEEDED_TO
186 # define TO_LOOP_MAX_NEEDED_TO MAX_NEEDED_FROM
187 #endif
190 /* Define macros which can access unaligned buffers. These macros are
191 supposed to be used only in code outside the inner loops. For the inner
192 loops we have other definitions which allow optimized access. */
193 #ifdef _STRING_ARCH_unaligned
194 /* We can handle unaligned memory access. */
195 # define get16u(addr) *((__const uint16_t *) (addr))
196 # define get32u(addr) *((__const uint32_t *) (addr))
198 /* We need no special support for writing values either. */
199 # define put16u(addr, val) *((uint16_t *) (addr)) = (val)
200 # define put32u(addr, val) *((uint32_t *) (addr)) = (val)
201 #else
202 /* Distinguish between big endian and little endian. */
203 # if __BYTE_ORDER == __LITTLE_ENDIAN
204 # define get16u(addr) \
205 (((__const unsigned char *) (addr))[1] << 8 \
206 | ((__const unsigned char *) (addr))[0])
207 # define get32u(addr) \
208 (((((__const unsigned char *) (addr))[3] << 8 \
209 | ((__const unsigned char *) (addr))[2]) << 8 \
210 | ((__const unsigned char *) (addr))[1]) << 8 \
211 | ((__const unsigned char *) (addr))[0])
213 # define put16u(addr, val) \
214 ({ uint16_t __val = (val); \
215 ((unsigned char *) (addr))[0] = __val; \
216 ((unsigned char *) (addr))[1] = __val >> 8; \
217 (void) 0; })
218 # define put32u(addr, val) \
219 ({ uint32_t __val = (val); \
220 ((unsigned char *) (addr))[0] = __val; \
221 __val >>= 8; \
222 ((unsigned char *) (addr))[1] = __val; \
223 __val >>= 8; \
224 ((unsigned char *) (addr))[2] = __val; \
225 __val >>= 8; \
226 ((unsigned char *) (addr))[3] = __val; \
227 (void) 0; })
228 # else
229 # define get16u(addr) \
230 (((__const unsigned char *) (addr))[0] << 8 \
231 | ((__const unsigned char *) (addr))[1])
232 # define get32u(addr) \
233 (((((__const unsigned char *) (addr))[0] << 8 \
234 | ((__const unsigned char *) (addr))[1]) << 8 \
235 | ((__const unsigned char *) (addr))[2]) << 8 \
236 | ((__const unsigned char *) (addr))[3])
238 # define put16u(addr, val) \
239 ({ uint16_t __val = (val); \
240 ((unsigned char *) (addr))[1] = __val; \
241 ((unsigned char *) (addr))[0] = __val >> 8; \
242 (void) 0; })
243 # define put32u(addr, val) \
244 ({ uint32_t __val = (val); \
245 ((unsigned char *) (addr))[3] = __val; \
246 __val >>= 8; \
247 ((unsigned char *) (addr))[2] = __val; \
248 __val >>= 8; \
249 ((unsigned char *) (addr))[1] = __val; \
250 __val >>= 8; \
251 ((unsigned char *) (addr))[0] = __val; \
252 (void) 0; })
253 # endif
254 #endif
257 /* For conversions from a fixed width character set to another fixed width
258 character set we can define RESET_INPUT_BUFFER in a very fast way. */
259 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
260 # if FROM_LOOP_MIN_NEEDED_FROM == FROM_LOOP_MAX_NEEDED_FROM \
261 && FROM_LOOP_MIN_NEEDED_TO == FROM_LOOP_MAX_NEEDED_TO \
262 && TO_LOOP_MIN_NEEDED_FROM == TO_LOOP_MAX_NEEDED_FROM \
263 && TO_LOOP_MIN_NEEDED_TO == TO_LOOP_MAX_NEEDED_TO
264 /* We have to use these `if's here since the compiler cannot know that
265 (outbuf - outerr) is always divisible by FROM/TO_LOOP_MIN_NEEDED_TO.
266 The ?:1 avoids division by zero warnings that gcc 3.2 emits even for
267 obviously unreachable code. */
268 # define RESET_INPUT_BUFFER \
269 if (FROM_DIRECTION) \
271 if (FROM_LOOP_MIN_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_TO == 0) \
272 *inptrp -= (outbuf - outerr) \
273 * (FROM_LOOP_MIN_NEEDED_FROM / FROM_LOOP_MIN_NEEDED_TO); \
274 else if (FROM_LOOP_MIN_NEEDED_TO % FROM_LOOP_MIN_NEEDED_FROM == 0) \
275 *inptrp -= (outbuf - outerr) \
276 / (FROM_LOOP_MIN_NEEDED_TO / FROM_LOOP_MIN_NEEDED_FROM \
277 ? : 1); \
278 else \
279 *inptrp -= ((outbuf - outerr) / FROM_LOOP_MIN_NEEDED_TO) \
280 * FROM_LOOP_MIN_NEEDED_FROM; \
282 else \
284 if (TO_LOOP_MIN_NEEDED_FROM % TO_LOOP_MIN_NEEDED_TO == 0) \
285 *inptrp -= (outbuf - outerr) \
286 * (TO_LOOP_MIN_NEEDED_FROM / TO_LOOP_MIN_NEEDED_TO); \
287 else if (TO_LOOP_MIN_NEEDED_TO % TO_LOOP_MIN_NEEDED_FROM == 0) \
288 *inptrp -= (outbuf - outerr) \
289 / (TO_LOOP_MIN_NEEDED_TO / TO_LOOP_MIN_NEEDED_FROM ? : 1); \
290 else \
291 *inptrp -= ((outbuf - outerr) / TO_LOOP_MIN_NEEDED_TO) \
292 * TO_LOOP_MIN_NEEDED_FROM; \
294 # endif
295 #endif
298 /* The default init function. It simply matches the name and initializes
299 the step data to point to one of the objects above. */
300 #if DEFINE_INIT
301 # ifndef CHARSET_NAME
302 # error "CHARSET_NAME not defined"
303 # endif
305 extern int gconv_init (struct __gconv_step *step);
307 gconv_init (struct __gconv_step *step)
309 /* Determine which direction. */
310 if (strcmp (step->__from_name, CHARSET_NAME) == 0)
312 step->__data = &from_object;
314 step->__min_needed_from = FROM_LOOP_MIN_NEEDED_FROM;
315 step->__max_needed_from = FROM_LOOP_MAX_NEEDED_FROM;
316 step->__min_needed_to = FROM_LOOP_MIN_NEEDED_TO;
317 step->__max_needed_to = FROM_LOOP_MAX_NEEDED_TO;
319 else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0)
321 step->__data = &to_object;
323 step->__min_needed_from = TO_LOOP_MIN_NEEDED_FROM;
324 step->__max_needed_from = TO_LOOP_MAX_NEEDED_FROM;
325 step->__min_needed_to = TO_LOOP_MIN_NEEDED_TO;
326 step->__max_needed_to = TO_LOOP_MAX_NEEDED_TO;
328 else
329 return __GCONV_NOCONV;
331 #ifdef SAVE_RESET_STATE
332 step->__stateful = 1;
333 #else
334 step->__stateful = 0;
335 #endif
337 return __GCONV_OK;
339 #endif
342 /* The default destructor function does nothing in the moment and so
343 we don't define it at all. But we still provide the macro just in
344 case we need it some day. */
345 #if DEFINE_FINI
346 #endif
349 /* If no arguments have to passed to the loop function define the macro
350 as empty. */
351 #ifndef EXTRA_LOOP_ARGS
352 # define EXTRA_LOOP_ARGS
353 #endif
356 /* This is the actual conversion function. */
357 #ifndef FUNCTION_NAME
358 # define FUNCTION_NAME gconv
359 #endif
361 /* The macros are used to access the function to convert single characters. */
362 #define SINGLE(fct) SINGLE2 (fct)
363 #define SINGLE2(fct) fct##_single
366 extern int FUNCTION_NAME (struct __gconv_step *step,
367 struct __gconv_step_data *data,
368 const unsigned char **inptrp,
369 const unsigned char *inend,
370 unsigned char **outbufstart, size_t *irreversible,
371 int do_flush, int consume_incomplete);
373 FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
374 const unsigned char **inptrp, const unsigned char *inend,
375 unsigned char **outbufstart, size_t *irreversible, int do_flush,
376 int consume_incomplete)
378 struct __gconv_step *next_step = step + 1;
379 struct __gconv_step_data *next_data = data + 1;
380 __gconv_fct fct;
381 int status;
383 fct = (data->__flags & __GCONV_IS_LAST) ? NULL : next_step->__fct;
385 /* If the function is called with no input this means we have to reset
386 to the initial state. The possibly partly converted input is
387 dropped. */
388 if (__builtin_expect (do_flush, 0))
390 /* This should never happen during error handling. */
391 assert (outbufstart == NULL);
393 status = __GCONV_OK;
395 #ifdef EMIT_SHIFT_TO_INIT
396 if (do_flush == 1)
398 /* We preserve the initial values of the pointer variables. */
399 unsigned char *outbuf = data->__outbuf;
400 unsigned char *outstart = outbuf;
401 unsigned char *outend = data->__outbufend;
403 # ifdef PREPARE_LOOP
404 PREPARE_LOOP
405 # endif
407 # ifdef SAVE_RESET_STATE
408 SAVE_RESET_STATE (1);
409 # endif
411 /* Emit the escape sequence to reset the state. */
412 EMIT_SHIFT_TO_INIT;
414 /* Call the steps down the chain if there are any but only if we
415 successfully emitted the escape sequence. This should only
416 fail if the output buffer is full. If the input is invalid
417 it should be discarded since the user wants to start from a
418 clean state. */
419 if (status == __GCONV_OK)
421 if (data->__flags & __GCONV_IS_LAST)
422 /* Store information about how many bytes are available. */
423 data->__outbuf = outbuf;
424 else
426 /* Write out all output which was produced. */
427 if (outbuf > outstart)
429 const unsigned char *outerr = outstart;
430 int result;
432 result = DL_CALL_FCT (fct, (next_step, next_data,
433 &outerr, outbuf, NULL,
434 irreversible, 0,
435 consume_incomplete));
437 if (result != __GCONV_EMPTY_INPUT)
439 if (__builtin_expect (outerr != outbuf, 0))
441 /* We have a problem. Undo the conversion. */
442 outbuf = outstart;
444 /* Restore the state. */
445 # ifdef SAVE_RESET_STATE
446 SAVE_RESET_STATE (0);
447 # endif
450 /* Change the status. */
451 status = result;
455 if (status == __GCONV_OK)
456 /* Now flush the remaining steps. */
457 status = DL_CALL_FCT (fct, (next_step, next_data, NULL,
458 NULL, NULL, irreversible, 1,
459 consume_incomplete));
463 else
464 #endif
466 /* Clear the state object. There might be bytes in there from
467 previous calls with CONSUME_INCOMPLETE == 1. But don't emit
468 escape sequences. */
469 memset (data->__statep, '\0', sizeof (*data->__statep));
471 if (! (data->__flags & __GCONV_IS_LAST))
472 /* Now flush the remaining steps. */
473 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
474 NULL, irreversible, do_flush,
475 consume_incomplete));
478 else
480 /* We preserve the initial values of the pointer variables. */
481 const unsigned char *inptr = *inptrp;
482 unsigned char *outbuf = (__builtin_expect (outbufstart == NULL, 1)
483 ? data->__outbuf : *outbufstart);
484 unsigned char *outend = data->__outbufend;
485 unsigned char *outstart;
486 /* This variable is used to count the number of characters we
487 actually converted. */
488 size_t lirreversible = 0;
489 size_t *lirreversiblep = irreversible ? &lirreversible : NULL;
491 /* The following assumes that encodings, which have a variable length
492 what might unalign a buffer even though it is a aligned in the
493 beginning, either don't have the minimal number of bytes as a divisor
494 of the maximum length or have a minimum length of 1. This is true
495 for all known and supported encodings.
496 We use && instead of || to combine the subexpression for the FROM
497 encoding and for the TO encoding, because usually one of them is
498 INTERNAL, for which the subexpression evaluates to 1, but INTERNAL
499 buffers are always aligned correctly. */
500 #define POSSIBLY_UNALIGNED \
501 (!defined _STRING_ARCH_unaligned \
502 && (((FROM_LOOP_MIN_NEEDED_FROM != 1 \
503 && FROM_LOOP_MAX_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_FROM == 0) \
504 && (FROM_LOOP_MIN_NEEDED_TO != 1 \
505 && FROM_LOOP_MAX_NEEDED_TO % FROM_LOOP_MIN_NEEDED_TO == 0)) \
506 || ((TO_LOOP_MIN_NEEDED_FROM != 1 \
507 && TO_LOOP_MAX_NEEDED_FROM % TO_LOOP_MIN_NEEDED_FROM == 0) \
508 && (TO_LOOP_MIN_NEEDED_TO != 1 \
509 && TO_LOOP_MAX_NEEDED_TO % TO_LOOP_MIN_NEEDED_TO == 0))))
510 #if POSSIBLY_UNALIGNED
511 int unaligned;
512 # define GEN_unaligned(name) GEN_unaligned2 (name)
513 # define GEN_unaligned2(name) name##_unaligned
514 #else
515 # define unaligned 0
516 #endif
518 #ifdef PREPARE_LOOP
519 PREPARE_LOOP
520 #endif
522 #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
523 /* If the function is used to implement the mb*towc*() or wc*tomb*()
524 functions we must test whether any bytes from the last call are
525 stored in the `state' object. */
526 if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
527 || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
528 || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
529 && consume_incomplete && (data->__statep->__count & 7) != 0)
531 /* Yep, we have some bytes left over. Process them now.
532 But this must not happen while we are called from an
533 error handler. */
534 assert (outbufstart == NULL);
536 # if FROM_LOOP_MAX_NEEDED_FROM > 1
537 if (TO_LOOP_MAX_NEEDED_FROM == 1 || FROM_DIRECTION)
538 status = SINGLE(FROM_LOOP) (step, data, inptrp, inend, &outbuf,
539 outend, lirreversiblep
540 EXTRA_LOOP_ARGS);
541 # endif
542 # if !ONE_DIRECTION
543 # if FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1
544 else
545 # endif
546 # if TO_LOOP_MAX_NEEDED_FROM > 1
547 status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf,
548 outend, lirreversiblep EXTRA_LOOP_ARGS);
549 # endif
550 # endif
552 if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
553 return status;
555 #endif
557 #if POSSIBLY_UNALIGNED
558 unaligned =
559 ((FROM_DIRECTION
560 && ((uintptr_t) inptr % FROM_LOOP_MIN_NEEDED_FROM != 0
561 || ((data->__flags & __GCONV_IS_LAST)
562 && (uintptr_t) outbuf % FROM_LOOP_MIN_NEEDED_TO != 0)))
563 || (!FROM_DIRECTION
564 && (((data->__flags & __GCONV_IS_LAST)
565 && (uintptr_t) outbuf % TO_LOOP_MIN_NEEDED_TO != 0)
566 || (uintptr_t) inptr % TO_LOOP_MIN_NEEDED_FROM != 0)));
567 #endif
569 while (1)
571 struct __gconv_trans_data *trans;
573 /* Remember the start value for this round. */
574 inptr = *inptrp;
575 /* The outbuf buffer is empty. */
576 outstart = outbuf;
578 #ifdef SAVE_RESET_STATE
579 SAVE_RESET_STATE (1);
580 #endif
582 if (__builtin_expect (!unaligned, 1))
584 if (FROM_DIRECTION)
585 /* Run the conversion loop. */
586 status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend,
587 lirreversiblep EXTRA_LOOP_ARGS);
588 else
589 /* Run the conversion loop. */
590 status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend,
591 lirreversiblep EXTRA_LOOP_ARGS);
593 #if POSSIBLY_UNALIGNED
594 else
596 if (FROM_DIRECTION)
597 /* Run the conversion loop. */
598 status = GEN_unaligned (FROM_LOOP) (step, data, inptrp, inend,
599 &outbuf, outend,
600 lirreversiblep
601 EXTRA_LOOP_ARGS);
602 else
603 /* Run the conversion loop. */
604 status = GEN_unaligned (TO_LOOP) (step, data, inptrp, inend,
605 &outbuf, outend,
606 lirreversiblep
607 EXTRA_LOOP_ARGS);
609 #endif
611 /* If we were called as part of an error handling module we
612 don't do anything else here. */
613 if (__builtin_expect (outbufstart != NULL, 0))
615 *outbufstart = outbuf;
616 return status;
619 /* Give the transliteration module the chance to store the
620 original text and the result in case it needs a context. */
621 for (trans = data->__trans; trans != NULL; trans = trans->__next)
622 if (trans->__trans_context_fct != NULL)
623 DL_CALL_FCT (trans->__trans_context_fct,
624 (trans->__data, inptr, *inptrp, outstart, outbuf));
626 /* We finished one use of the loops. */
627 ++data->__invocation_counter;
629 /* If this is the last step leave the loop, there is nothing
630 we can do. */
631 if (__builtin_expect (data->__flags & __GCONV_IS_LAST, 0))
633 /* Store information about how many bytes are available. */
634 data->__outbuf = outbuf;
636 /* Remember how many non-identical characters we
637 converted in a irreversible way. */
638 *irreversible += lirreversible;
640 break;
643 /* Write out all output which was produced. */
644 if (__builtin_expect (outbuf > outstart, 1))
646 const unsigned char *outerr = data->__outbuf;
647 int result;
649 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
650 outbuf, NULL, irreversible, 0,
651 consume_incomplete));
653 if (result != __GCONV_EMPTY_INPUT)
655 if (__builtin_expect (outerr != outbuf, 0))
657 #ifdef RESET_INPUT_BUFFER
658 RESET_INPUT_BUFFER;
659 #else
660 /* We have a problem with the in on of the functions
661 below. Undo the conversion upto the error point. */
662 size_t nstatus;
664 /* Reload the pointers. */
665 *inptrp = inptr;
666 outbuf = outstart;
668 /* Restore the state. */
669 # ifdef SAVE_RESET_STATE
670 SAVE_RESET_STATE (0);
671 # endif
673 if (__builtin_expect (!unaligned, 1))
675 if (FROM_DIRECTION)
676 /* Run the conversion loop. */
677 nstatus = FROM_LOOP (step, data, inptrp, inend,
678 &outbuf, outerr,
679 lirreversiblep
680 EXTRA_LOOP_ARGS);
681 else
682 /* Run the conversion loop. */
683 nstatus = TO_LOOP (step, data, inptrp, inend,
684 &outbuf, outerr,
685 lirreversiblep
686 EXTRA_LOOP_ARGS);
688 # if POSSIBLY_UNALIGNED
689 else
691 if (FROM_DIRECTION)
692 /* Run the conversion loop. */
693 nstatus = GEN_unaligned (FROM_LOOP) (step, data,
694 inptrp, inend,
695 &outbuf,
696 outerr,
697 lirreversiblep
698 EXTRA_LOOP_ARGS);
699 else
700 /* Run the conversion loop. */
701 nstatus = GEN_unaligned (TO_LOOP) (step, data,
702 inptrp, inend,
703 &outbuf, outerr,
704 lirreversiblep
705 EXTRA_LOOP_ARGS);
707 # endif
709 /* We must run out of output buffer space in this
710 rerun. */
711 assert (outbuf == outerr);
712 assert (nstatus == __GCONV_FULL_OUTPUT);
714 /* If we haven't consumed a single byte decrement
715 the invocation counter. */
716 if (__builtin_expect (outbuf == outstart, 0))
717 --data->__invocation_counter;
718 #endif /* reset input buffer */
721 /* Change the status. */
722 status = result;
724 else
725 /* All the output is consumed, we can make another run
726 if everything was ok. */
727 if (status == __GCONV_FULL_OUTPUT)
729 status = __GCONV_OK;
730 outbuf = data->__outbuf;
734 if (status != __GCONV_OK)
735 break;
737 /* Reset the output buffer pointer for the next round. */
738 outbuf = data->__outbuf;
741 #ifdef END_LOOP
742 END_LOOP
743 #endif
745 /* If we are supposed to consume all character store now all of the
746 remaining characters in the `state' object. */
747 #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
748 if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
749 || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
750 || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
751 && __builtin_expect (consume_incomplete, 0)
752 && status == __GCONV_INCOMPLETE_INPUT)
754 # ifdef STORE_REST
755 mbstate_t *state = data->__statep;
757 STORE_REST
758 # else
759 size_t cnt;
761 /* Make sure the remaining bytes fit into the state objects
762 buffer. */
763 assert (inend - *inptrp < 4);
765 for (cnt = 0; *inptrp < inend; ++cnt)
766 data->__statep->__value.__wchb[cnt] = *(*inptrp)++;
767 data->__statep->__count &= ~7;
768 data->__statep->__count |= cnt;
769 # endif
771 #endif
772 #undef unaligned
773 #undef POSSIBLY_UNALIGNED
776 return status;
779 #undef DEFINE_INIT
780 #undef CHARSET_NAME
781 #undef DEFINE_FINI
782 #undef MIN_NEEDED_FROM
783 #undef MIN_NEEDED_TO
784 #undef MAX_NEEDED_FROM
785 #undef MAX_NEEDED_TO
786 #undef FROM_LOOP_MIN_NEEDED_FROM
787 #undef FROM_LOOP_MAX_NEEDED_FROM
788 #undef FROM_LOOP_MIN_NEEDED_TO
789 #undef FROM_LOOP_MAX_NEEDED_TO
790 #undef TO_LOOP_MIN_NEEDED_FROM
791 #undef TO_LOOP_MAX_NEEDED_FROM
792 #undef TO_LOOP_MIN_NEEDED_TO
793 #undef TO_LOOP_MAX_NEEDED_TO
794 #undef DEFINE_DIRECTION_OBJECTS
795 #undef FROM_DIRECTION
796 #undef EMIT_SHIFT_TO_INIT
797 #undef FROM_LOOP
798 #undef TO_LOOP
799 #undef SAVE_RESET_STATE
800 #undef RESET_INPUT_BUFFER
801 #undef FUNCTION_NAME
802 #undef PREPARE_LOOP
803 #undef END_LOOP
804 #undef ONE_DIRECTION
805 #undef STORE_REST