1 /* Skeleton for a conversion module.
2 Copyright (C) 1998-2014 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, see
18 <http://www.gnu.org/licenses/>. */
20 /* This file can be included to provide definitions of several things
21 many modules have in common. It can be customized using the following
24 DEFINE_INIT define the default initializer. This requires the
25 following symbol to be defined.
27 CHARSET_NAME string with official name of the coded character
30 DEFINE_FINI define the default destructor function.
32 MIN_NEEDED_FROM minimal number of bytes needed for the from-charset.
33 MIN_NEEDED_TO likewise for the to-charset.
35 MAX_NEEDED_FROM maximal number of bytes needed for the from-charset.
36 This macro is optional, it defaults to MIN_NEEDED_FROM.
37 MAX_NEEDED_TO likewise for the to-charset.
39 FROM_LOOP_MIN_NEEDED_FROM
40 FROM_LOOP_MAX_NEEDED_FROM
41 minimal/maximal number of bytes needed on input
42 of one round through the FROM_LOOP. Defaults
43 to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
44 FROM_LOOP_MIN_NEEDED_TO
45 FROM_LOOP_MAX_NEEDED_TO
46 minimal/maximal number of bytes needed on output
47 of one round through the FROM_LOOP. Defaults
48 to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
49 TO_LOOP_MIN_NEEDED_FROM
50 TO_LOOP_MAX_NEEDED_FROM
51 minimal/maximal number of bytes needed on input
52 of one round through the TO_LOOP. Defaults
53 to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
56 minimal/maximal number of bytes needed on output
57 of one round through the TO_LOOP. Defaults
58 to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
60 FROM_DIRECTION this macro is supposed to return a value != 0
61 if we convert from the current character set,
62 otherwise it return 0.
64 EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
65 defines some code which writes out a sequence
66 of bytes which bring the current state into
69 FROM_LOOP name of the function implementing the conversion
70 from the current character set.
71 TO_LOOP likewise for the other direction
73 ONE_DIRECTION optional. If defined to 1, only one conversion
74 direction is defined instead of two. In this
75 case, FROM_DIRECTION should be defined to 1, and
76 FROM_LOOP and TO_LOOP should have the same value.
78 SAVE_RESET_STATE in case of an error we must reset the state for
79 the rerun so this macro must be defined for
80 stateful encodings. It takes an argument which
81 is nonzero when saving.
83 RESET_INPUT_BUFFER If the input character sets allow this the macro
84 can be defined to reset the input buffer pointers
85 to cover only those characters up to the error.
87 FUNCTION_NAME if not set the conversion function is named `gconv'.
89 PREPARE_LOOP optional code preparing the conversion loop. Can
90 contain variable definitions.
91 END_LOOP also optional, may be used to store information
93 EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
96 STORE_REST optional, needed only when MAX_NEEDED_FROM > 4.
97 This macro stores the seen but unconverted input bytes
100 FROM_ONEBYTE optional. If defined, should be the name of a
101 specialized conversion function for a single byte
102 from the current character set to INTERNAL. This
103 function has prototype
105 FROM_ONEBYTE (struct __gconv_step *, unsigned char);
106 and does a special conversion:
107 - The input is a single byte.
108 - The output is a single uint32_t.
109 - The state before the conversion is the initial state;
110 the state after the conversion is irrelevant.
111 - No transliteration.
112 - __invocation_counter = 0.
113 - __internal_use = 1.
116 Modules can use mbstate_t to store conversion state as follows:
118 * Bits 2..0 of '__count' contain the number of lookahead input bytes
119 stored in __value.__wchb. Always zero if the converter never
120 returns __GCONV_INCOMPLETE_INPUT.
122 * Bits 31..3 of '__count' are module dependent shift state.
124 * __value: When STORE_REST/UNPACK_BYTES aren't defined and when the
125 converter has returned __GCONV_INCOMPLETE_INPUT, this contains
126 at most 4 lookahead bytes. Converters with an mb_cur_max > 4
127 (currently only UTF-8) must find a way to store their state
128 in __value.__wch and define STORE_REST/UNPACK_BYTES appropriately.
130 When __value contains lookahead, __count must not be zero, because
131 the converter is not in the initial state then, and mbsinit() --
132 defined as a (__count == 0) test -- must reflect this.
138 #define __need_size_t
150 # define DL_CALL_FCT(fct, args) fct args
153 /* The direction objects. */
155 # ifndef FROM_DIRECTION
156 # define FROM_DIRECTION_VAL NULL
157 # define TO_DIRECTION_VAL ((void *) ~((uintptr_t) 0))
158 # define FROM_DIRECTION (step->__data == FROM_DIRECTION_VAL)
161 # ifndef FROM_DIRECTION
162 # error "FROM_DIRECTION must be provided if non-default init is used"
166 #ifndef ONE_DIRECTION
167 # define ONE_DIRECTION 0
171 /* How many bytes are needed at most for the from-charset. */
172 #ifndef MAX_NEEDED_FROM
173 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
176 /* Same for the to-charset. */
177 #ifndef MAX_NEEDED_TO
178 # define MAX_NEEDED_TO MIN_NEEDED_TO
181 /* Defaults for the per-direction min/max constants. */
182 #ifndef FROM_LOOP_MIN_NEEDED_FROM
183 # define FROM_LOOP_MIN_NEEDED_FROM MIN_NEEDED_FROM
185 #ifndef FROM_LOOP_MAX_NEEDED_FROM
186 # define FROM_LOOP_MAX_NEEDED_FROM MAX_NEEDED_FROM
188 #ifndef FROM_LOOP_MIN_NEEDED_TO
189 # define FROM_LOOP_MIN_NEEDED_TO MIN_NEEDED_TO
191 #ifndef FROM_LOOP_MAX_NEEDED_TO
192 # define FROM_LOOP_MAX_NEEDED_TO MAX_NEEDED_TO
194 #ifndef TO_LOOP_MIN_NEEDED_FROM
195 # define TO_LOOP_MIN_NEEDED_FROM MIN_NEEDED_TO
197 #ifndef TO_LOOP_MAX_NEEDED_FROM
198 # define TO_LOOP_MAX_NEEDED_FROM MAX_NEEDED_TO
200 #ifndef TO_LOOP_MIN_NEEDED_TO
201 # define TO_LOOP_MIN_NEEDED_TO MIN_NEEDED_FROM
203 #ifndef TO_LOOP_MAX_NEEDED_TO
204 # define TO_LOOP_MAX_NEEDED_TO MAX_NEEDED_FROM
208 /* Define macros which can access unaligned buffers. These macros are
209 supposed to be used only in code outside the inner loops. For the inner
210 loops we have other definitions which allow optimized access. */
211 #if _STRING_ARCH_unaligned
212 /* We can handle unaligned memory access. */
213 # define get16u(addr) *((const uint16_t *) (addr))
214 # define get32u(addr) *((const uint32_t *) (addr))
216 /* We need no special support for writing values either. */
217 # define put16u(addr, val) *((uint16_t *) (addr)) = (val)
218 # define put32u(addr, val) *((uint32_t *) (addr)) = (val)
220 /* Distinguish between big endian and little endian. */
221 # if __BYTE_ORDER == __LITTLE_ENDIAN
222 # define get16u(addr) \
223 (((const unsigned char *) (addr))[1] << 8 \
224 | ((const unsigned char *) (addr))[0])
225 # define get32u(addr) \
226 (((((const unsigned char *) (addr))[3] << 8 \
227 | ((const unsigned char *) (addr))[2]) << 8 \
228 | ((const unsigned char *) (addr))[1]) << 8 \
229 | ((const unsigned char *) (addr))[0])
231 # define put16u(addr, val) \
232 ({ uint16_t __val = (val); \
233 ((unsigned char *) (addr))[0] = __val; \
234 ((unsigned char *) (addr))[1] = __val >> 8; \
236 # define put32u(addr, val) \
237 ({ uint32_t __val = (val); \
238 ((unsigned char *) (addr))[0] = __val; \
240 ((unsigned char *) (addr))[1] = __val; \
242 ((unsigned char *) (addr))[2] = __val; \
244 ((unsigned char *) (addr))[3] = __val; \
247 # define get16u(addr) \
248 (((const unsigned char *) (addr))[0] << 8 \
249 | ((const unsigned char *) (addr))[1])
250 # define get32u(addr) \
251 (((((const unsigned char *) (addr))[0] << 8 \
252 | ((const unsigned char *) (addr))[1]) << 8 \
253 | ((const unsigned char *) (addr))[2]) << 8 \
254 | ((const unsigned char *) (addr))[3])
256 # define put16u(addr, val) \
257 ({ uint16_t __val = (val); \
258 ((unsigned char *) (addr))[1] = __val; \
259 ((unsigned char *) (addr))[0] = __val >> 8; \
261 # define put32u(addr, val) \
262 ({ uint32_t __val = (val); \
263 ((unsigned char *) (addr))[3] = __val; \
265 ((unsigned char *) (addr))[2] = __val; \
267 ((unsigned char *) (addr))[1] = __val; \
269 ((unsigned char *) (addr))[0] = __val; \
275 /* For conversions from a fixed width character set to another fixed width
276 character set we can define RESET_INPUT_BUFFER in a very fast way. */
277 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
278 # if FROM_LOOP_MIN_NEEDED_FROM == FROM_LOOP_MAX_NEEDED_FROM \
279 && FROM_LOOP_MIN_NEEDED_TO == FROM_LOOP_MAX_NEEDED_TO \
280 && TO_LOOP_MIN_NEEDED_FROM == TO_LOOP_MAX_NEEDED_FROM \
281 && TO_LOOP_MIN_NEEDED_TO == TO_LOOP_MAX_NEEDED_TO
282 /* We have to use these `if's here since the compiler cannot know that
283 (outbuf - outerr) is always divisible by FROM/TO_LOOP_MIN_NEEDED_TO.
284 The ?:1 avoids division by zero warnings that gcc 3.2 emits even for
285 obviously unreachable code. */
286 # define RESET_INPUT_BUFFER \
287 if (FROM_DIRECTION) \
289 if (FROM_LOOP_MIN_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_TO == 0) \
290 *inptrp -= (outbuf - outerr) \
291 * (FROM_LOOP_MIN_NEEDED_FROM / FROM_LOOP_MIN_NEEDED_TO); \
292 else if (FROM_LOOP_MIN_NEEDED_TO % FROM_LOOP_MIN_NEEDED_FROM == 0) \
293 *inptrp -= (outbuf - outerr) \
294 / (FROM_LOOP_MIN_NEEDED_TO / FROM_LOOP_MIN_NEEDED_FROM \
297 *inptrp -= ((outbuf - outerr) / FROM_LOOP_MIN_NEEDED_TO) \
298 * FROM_LOOP_MIN_NEEDED_FROM; \
302 if (TO_LOOP_MIN_NEEDED_FROM % TO_LOOP_MIN_NEEDED_TO == 0) \
303 *inptrp -= (outbuf - outerr) \
304 * (TO_LOOP_MIN_NEEDED_FROM / TO_LOOP_MIN_NEEDED_TO); \
305 else if (TO_LOOP_MIN_NEEDED_TO % TO_LOOP_MIN_NEEDED_FROM == 0) \
306 *inptrp -= (outbuf - outerr) \
307 / (TO_LOOP_MIN_NEEDED_TO / TO_LOOP_MIN_NEEDED_FROM ? : 1); \
309 *inptrp -= ((outbuf - outerr) / TO_LOOP_MIN_NEEDED_TO) \
310 * TO_LOOP_MIN_NEEDED_FROM; \
316 /* The default init function. It simply matches the name and initializes
317 the step data to point to one of the objects above. */
319 # ifndef CHARSET_NAME
320 # error "CHARSET_NAME not defined"
323 extern int gconv_init (struct __gconv_step
*step
);
325 gconv_init (struct __gconv_step
*step
)
327 /* Determine which direction. */
328 if (strcmp (step
->__from_name
, CHARSET_NAME
) == 0)
330 step
->__data
= FROM_DIRECTION_VAL
;
332 step
->__min_needed_from
= FROM_LOOP_MIN_NEEDED_FROM
;
333 step
->__max_needed_from
= FROM_LOOP_MAX_NEEDED_FROM
;
334 step
->__min_needed_to
= FROM_LOOP_MIN_NEEDED_TO
;
335 step
->__max_needed_to
= FROM_LOOP_MAX_NEEDED_TO
;
338 step
->__btowc_fct
= FROM_ONEBYTE
;
341 else if (__builtin_expect (strcmp (step
->__to_name
, CHARSET_NAME
), 0) == 0)
343 step
->__data
= TO_DIRECTION_VAL
;
345 step
->__min_needed_from
= TO_LOOP_MIN_NEEDED_FROM
;
346 step
->__max_needed_from
= TO_LOOP_MAX_NEEDED_FROM
;
347 step
->__min_needed_to
= TO_LOOP_MIN_NEEDED_TO
;
348 step
->__max_needed_to
= TO_LOOP_MAX_NEEDED_TO
;
351 return __GCONV_NOCONV
;
353 #ifdef SAVE_RESET_STATE
354 step
->__stateful
= 1;
356 step
->__stateful
= 0;
364 /* The default destructor function does nothing in the moment and so
365 we don't define it at all. But we still provide the macro just in
366 case we need it some day. */
371 /* If no arguments have to passed to the loop function define the macro
373 #ifndef EXTRA_LOOP_ARGS
374 # define EXTRA_LOOP_ARGS
378 /* This is the actual conversion function. */
379 #ifndef FUNCTION_NAME
380 # define FUNCTION_NAME gconv
383 /* The macros are used to access the function to convert single characters. */
384 #define SINGLE(fct) SINGLE2 (fct)
385 #define SINGLE2(fct) fct##_single
388 extern int FUNCTION_NAME (struct __gconv_step
*step
,
389 struct __gconv_step_data
*data
,
390 const unsigned char **inptrp
,
391 const unsigned char *inend
,
392 unsigned char **outbufstart
, size_t *irreversible
,
393 int do_flush
, int consume_incomplete
);
395 FUNCTION_NAME (struct __gconv_step
*step
, struct __gconv_step_data
*data
,
396 const unsigned char **inptrp
, const unsigned char *inend
,
397 unsigned char **outbufstart
, size_t *irreversible
, int do_flush
,
398 int consume_incomplete
)
400 struct __gconv_step
*next_step
= step
+ 1;
401 struct __gconv_step_data
*next_data
= data
+ 1;
402 __gconv_fct fct
= NULL
;
405 if ((data
->__flags
& __GCONV_IS_LAST
) == 0)
407 fct
= next_step
->__fct
;
409 if (next_step
->__shlib_handle
!= NULL
)
414 /* If the function is called with no input this means we have to reset
415 to the initial state. The possibly partly converted input is
417 if (__glibc_unlikely (do_flush
))
419 /* This should never happen during error handling. */
420 assert (outbufstart
== NULL
);
424 #ifdef EMIT_SHIFT_TO_INIT
427 /* We preserve the initial values of the pointer variables. */
428 unsigned char *outbuf
= data
->__outbuf
;
429 unsigned char *outstart
= outbuf
;
430 unsigned char *outend
= data
->__outbufend
;
436 # ifdef SAVE_RESET_STATE
437 SAVE_RESET_STATE (1);
440 /* Emit the escape sequence to reset the state. */
443 /* Call the steps down the chain if there are any but only if we
444 successfully emitted the escape sequence. This should only
445 fail if the output buffer is full. If the input is invalid
446 it should be discarded since the user wants to start from a
448 if (status
== __GCONV_OK
)
450 if (data
->__flags
& __GCONV_IS_LAST
)
451 /* Store information about how many bytes are available. */
452 data
->__outbuf
= outbuf
;
455 /* Write out all output which was produced. */
456 if (outbuf
> outstart
)
458 const unsigned char *outerr
= outstart
;
461 result
= DL_CALL_FCT (fct
, (next_step
, next_data
,
462 &outerr
, outbuf
, NULL
,
464 consume_incomplete
));
466 if (result
!= __GCONV_EMPTY_INPUT
)
468 if (__glibc_unlikely (outerr
!= outbuf
))
470 /* We have a problem. Undo the conversion. */
473 /* Restore the state. */
474 # ifdef SAVE_RESET_STATE
475 SAVE_RESET_STATE (0);
479 /* Change the status. */
484 if (status
== __GCONV_OK
)
485 /* Now flush the remaining steps. */
486 status
= DL_CALL_FCT (fct
, (next_step
, next_data
, NULL
,
487 NULL
, NULL
, irreversible
, 1,
488 consume_incomplete
));
495 /* Clear the state object. There might be bytes in there from
496 previous calls with CONSUME_INCOMPLETE == 1. But don't emit
498 memset (data
->__statep
, '\0', sizeof (*data
->__statep
));
500 if (! (data
->__flags
& __GCONV_IS_LAST
))
501 /* Now flush the remaining steps. */
502 status
= DL_CALL_FCT (fct
, (next_step
, next_data
, NULL
, NULL
,
503 NULL
, irreversible
, do_flush
,
504 consume_incomplete
));
509 /* We preserve the initial values of the pointer variables. */
510 const unsigned char *inptr
= *inptrp
;
511 unsigned char *outbuf
= (__builtin_expect (outbufstart
== NULL
, 1)
512 ? data
->__outbuf
: *outbufstart
);
513 unsigned char *outend
= data
->__outbufend
;
514 unsigned char *outstart
;
515 /* This variable is used to count the number of characters we
516 actually converted. */
517 size_t lirreversible
= 0;
518 size_t *lirreversiblep
= irreversible
? &lirreversible
: NULL
;
520 /* The following assumes that encodings, which have a variable length
521 what might unalign a buffer even though it is an aligned in the
522 beginning, either don't have the minimal number of bytes as a divisor
523 of the maximum length or have a minimum length of 1. This is true
524 for all known and supported encodings.
525 We use && instead of || to combine the subexpression for the FROM
526 encoding and for the TO encoding, because usually one of them is
527 INTERNAL, for which the subexpression evaluates to 1, but INTERNAL
528 buffers are always aligned correctly. */
529 #define POSSIBLY_UNALIGNED \
530 (!_STRING_ARCH_unaligned \
531 && (((FROM_LOOP_MIN_NEEDED_FROM != 1 \
532 && FROM_LOOP_MAX_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_FROM == 0) \
533 && (FROM_LOOP_MIN_NEEDED_TO != 1 \
534 && FROM_LOOP_MAX_NEEDED_TO % FROM_LOOP_MIN_NEEDED_TO == 0)) \
535 || ((TO_LOOP_MIN_NEEDED_FROM != 1 \
536 && TO_LOOP_MAX_NEEDED_FROM % TO_LOOP_MIN_NEEDED_FROM == 0) \
537 && (TO_LOOP_MIN_NEEDED_TO != 1 \
538 && TO_LOOP_MAX_NEEDED_TO % TO_LOOP_MIN_NEEDED_TO == 0))))
539 #if POSSIBLY_UNALIGNED
541 # define GEN_unaligned(name) GEN_unaligned2 (name)
542 # define GEN_unaligned2(name) name##_unaligned
551 #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
552 /* If the function is used to implement the mb*towc*() or wc*tomb*()
553 functions we must test whether any bytes from the last call are
554 stored in the `state' object. */
555 if (((FROM_LOOP_MAX_NEEDED_FROM
> 1 && TO_LOOP_MAX_NEEDED_FROM
> 1)
556 || (FROM_LOOP_MAX_NEEDED_FROM
> 1 && FROM_DIRECTION
)
557 || (TO_LOOP_MAX_NEEDED_FROM
> 1 && !FROM_DIRECTION
))
558 && consume_incomplete
&& (data
->__statep
->__count
& 7) != 0)
560 /* Yep, we have some bytes left over. Process them now.
561 But this must not happen while we are called from an
563 assert (outbufstart
== NULL
);
565 # if FROM_LOOP_MAX_NEEDED_FROM > 1
566 if (TO_LOOP_MAX_NEEDED_FROM
== 1 || FROM_DIRECTION
)
567 status
= SINGLE(FROM_LOOP
) (step
, data
, inptrp
, inend
, &outbuf
,
568 outend
, lirreversiblep
572 # if FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1
575 # if TO_LOOP_MAX_NEEDED_FROM > 1
576 status
= SINGLE(TO_LOOP
) (step
, data
, inptrp
, inend
, &outbuf
,
577 outend
, lirreversiblep EXTRA_LOOP_ARGS
);
581 if (__builtin_expect (status
, __GCONV_OK
) != __GCONV_OK
)
586 #if POSSIBLY_UNALIGNED
589 && ((uintptr_t) inptr
% FROM_LOOP_MIN_NEEDED_FROM
!= 0
590 || ((data
->__flags
& __GCONV_IS_LAST
)
591 && (uintptr_t) outbuf
% FROM_LOOP_MIN_NEEDED_TO
!= 0)))
593 && (((data
->__flags
& __GCONV_IS_LAST
)
594 && (uintptr_t) outbuf
% TO_LOOP_MIN_NEEDED_TO
!= 0)
595 || (uintptr_t) inptr
% TO_LOOP_MIN_NEEDED_FROM
!= 0)));
600 struct __gconv_trans_data
*trans
;
602 /* Remember the start value for this round. */
604 /* The outbuf buffer is empty. */
607 #ifdef SAVE_RESET_STATE
608 SAVE_RESET_STATE (1);
611 if (__glibc_likely (!unaligned
))
614 /* Run the conversion loop. */
615 status
= FROM_LOOP (step
, data
, inptrp
, inend
, &outbuf
, outend
,
616 lirreversiblep EXTRA_LOOP_ARGS
);
618 /* Run the conversion loop. */
619 status
= TO_LOOP (step
, data
, inptrp
, inend
, &outbuf
, outend
,
620 lirreversiblep EXTRA_LOOP_ARGS
);
622 #if POSSIBLY_UNALIGNED
626 /* Run the conversion loop. */
627 status
= GEN_unaligned (FROM_LOOP
) (step
, data
, inptrp
, inend
,
632 /* Run the conversion loop. */
633 status
= GEN_unaligned (TO_LOOP
) (step
, data
, inptrp
, inend
,
640 /* If we were called as part of an error handling module we
641 don't do anything else here. */
642 if (__glibc_unlikely (outbufstart
!= NULL
))
644 *outbufstart
= outbuf
;
648 /* Give the transliteration module the chance to store the
649 original text and the result in case it needs a context. */
650 for (trans
= data
->__trans
; trans
!= NULL
; trans
= trans
->__next
)
651 if (trans
->__trans_context_fct
!= NULL
)
652 DL_CALL_FCT (trans
->__trans_context_fct
,
653 (trans
->__data
, inptr
, *inptrp
, outstart
, outbuf
));
655 /* We finished one use of the loops. */
656 ++data
->__invocation_counter
;
658 /* If this is the last step leave the loop, there is nothing
660 if (__glibc_unlikely (data
->__flags
& __GCONV_IS_LAST
))
662 /* Store information about how many bytes are available. */
663 data
->__outbuf
= outbuf
;
665 /* Remember how many non-identical characters we
666 converted in an irreversible way. */
667 *irreversible
+= lirreversible
;
672 /* Write out all output which was produced. */
673 if (__glibc_likely (outbuf
> outstart
))
675 const unsigned char *outerr
= data
->__outbuf
;
678 result
= DL_CALL_FCT (fct
, (next_step
, next_data
, &outerr
,
679 outbuf
, NULL
, irreversible
, 0,
680 consume_incomplete
));
682 if (result
!= __GCONV_EMPTY_INPUT
)
684 if (__glibc_unlikely (outerr
!= outbuf
))
686 #ifdef RESET_INPUT_BUFFER
689 /* We have a problem in one of the functions below.
690 Undo the conversion upto the error point. */
693 /* Reload the pointers. */
697 /* Restore the state. */
698 # ifdef SAVE_RESET_STATE
699 SAVE_RESET_STATE (0);
702 if (__glibc_likely (!unaligned
))
705 /* Run the conversion loop. */
706 nstatus
= FROM_LOOP (step
, data
, inptrp
, inend
,
711 /* Run the conversion loop. */
712 nstatus
= TO_LOOP (step
, data
, inptrp
, inend
,
717 # if POSSIBLY_UNALIGNED
721 /* Run the conversion loop. */
722 nstatus
= GEN_unaligned (FROM_LOOP
) (step
, data
,
729 /* Run the conversion loop. */
730 nstatus
= GEN_unaligned (TO_LOOP
) (step
, data
,
738 /* We must run out of output buffer space in this
740 assert (outbuf
== outerr
);
741 assert (nstatus
== __GCONV_FULL_OUTPUT
);
743 /* If we haven't consumed a single byte decrement
744 the invocation counter. */
745 if (__glibc_unlikely (outbuf
== outstart
))
746 --data
->__invocation_counter
;
747 #endif /* reset input buffer */
750 /* Change the status. */
754 /* All the output is consumed, we can make another run
755 if everything was ok. */
756 if (status
== __GCONV_FULL_OUTPUT
)
759 outbuf
= data
->__outbuf
;
763 if (status
!= __GCONV_OK
)
766 /* Reset the output buffer pointer for the next round. */
767 outbuf
= data
->__outbuf
;
774 /* If we are supposed to consume all character store now all of the
775 remaining characters in the `state' object. */
776 #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
777 if (((FROM_LOOP_MAX_NEEDED_FROM
> 1 && TO_LOOP_MAX_NEEDED_FROM
> 1)
778 || (FROM_LOOP_MAX_NEEDED_FROM
> 1 && FROM_DIRECTION
)
779 || (TO_LOOP_MAX_NEEDED_FROM
> 1 && !FROM_DIRECTION
))
780 && __builtin_expect (consume_incomplete
, 0)
781 && status
== __GCONV_INCOMPLETE_INPUT
)
784 mbstate_t *state
= data
->__statep
;
788 /* Make sure the remaining bytes fit into the state objects
790 assert (inend
- *inptrp
< 4);
793 for (cnt
= 0; *inptrp
< inend
; ++cnt
)
794 data
->__statep
->__value
.__wchb
[cnt
] = *(*inptrp
)++;
795 data
->__statep
->__count
&= ~7;
796 data
->__statep
->__count
|= cnt
;
801 #undef POSSIBLY_UNALIGNED
810 #undef MIN_NEEDED_FROM
812 #undef MAX_NEEDED_FROM
814 #undef FROM_LOOP_MIN_NEEDED_FROM
815 #undef FROM_LOOP_MAX_NEEDED_FROM
816 #undef FROM_LOOP_MIN_NEEDED_TO
817 #undef FROM_LOOP_MAX_NEEDED_TO
818 #undef TO_LOOP_MIN_NEEDED_FROM
819 #undef TO_LOOP_MAX_NEEDED_FROM
820 #undef TO_LOOP_MIN_NEEDED_TO
821 #undef TO_LOOP_MAX_NEEDED_TO
822 #undef FROM_DIRECTION
823 #undef EMIT_SHIFT_TO_INIT
827 #undef SAVE_RESET_STATE
828 #undef RESET_INPUT_BUFFER
832 #undef EXTRA_LOOP_ARGS