linux: use statx for fstat if neither newfstatat nor fstatat64 is present
[glibc.git] / iconv / skeleton.c
blob0356dbf92b7da1e6b761595bdad96ad084614233
1 /* Skeleton for a conversion module.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* This file can be included to provide definitions of several things
20 many modules have in common. It can be customized using the following
21 macros:
23 DEFINE_INIT define the default initializer. This requires the
24 following symbol to be defined.
26 CHARSET_NAME string with official name of the coded character
27 set (in all-caps)
29 DEFINE_FINI define the default destructor function.
31 MIN_NEEDED_FROM minimal number of bytes needed for the from-charset.
32 MIN_NEEDED_TO likewise for the to-charset.
34 MAX_NEEDED_FROM maximal number of bytes needed for the from-charset.
35 This macro is optional, it defaults to MIN_NEEDED_FROM.
36 MAX_NEEDED_TO likewise for the to-charset.
38 FROM_LOOP_MIN_NEEDED_FROM
39 FROM_LOOP_MAX_NEEDED_FROM
40 minimal/maximal number of bytes needed on input
41 of one round through the FROM_LOOP. Defaults
42 to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
43 FROM_LOOP_MIN_NEEDED_TO
44 FROM_LOOP_MAX_NEEDED_TO
45 minimal/maximal number of bytes needed on output
46 of one round through the FROM_LOOP. Defaults
47 to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
48 TO_LOOP_MIN_NEEDED_FROM
49 TO_LOOP_MAX_NEEDED_FROM
50 minimal/maximal number of bytes needed on input
51 of one round through the TO_LOOP. Defaults
52 to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
53 TO_LOOP_MIN_NEEDED_TO
54 TO_LOOP_MAX_NEEDED_TO
55 minimal/maximal number of bytes needed on output
56 of one round through the TO_LOOP. Defaults
57 to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
59 FROM_DIRECTION this macro is supposed to return a value != 0
60 if we convert from the current character set,
61 otherwise it return 0.
63 EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
64 defines some code which writes out a sequence
65 of bytes which bring the current state into
66 the initial state.
68 FROM_LOOP name of the function implementing the conversion
69 from the current character set.
70 TO_LOOP likewise for the other direction
72 ONE_DIRECTION optional. If defined to 1, only one conversion
73 direction is defined instead of two. In this
74 case, FROM_DIRECTION should be defined to 1, and
75 FROM_LOOP and TO_LOOP should have the same value.
77 SAVE_RESET_STATE in case of an error we must reset the state for
78 the rerun so this macro must be defined for
79 stateful encodings. It takes an argument which
80 is nonzero when saving.
82 RESET_INPUT_BUFFER If the input character sets allow this the macro
83 can be defined to reset the input buffer pointers
84 to cover only those characters up to the error.
85 Note that if the conversion has skipped over
86 irreversible characters (due to
87 __GCONV_IGNORE_ERRORS) there is no longer a direct
88 correspondence between input and output pointers,
89 and this macro is not called.
91 FUNCTION_NAME if not set the conversion function is named `gconv'.
93 PREPARE_LOOP optional code preparing the conversion loop. Can
94 contain variable definitions.
95 END_LOOP also optional, may be used to store information
97 EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
98 to loop function.
100 STORE_REST optional, needed only when MAX_NEEDED_FROM > 4.
101 This macro stores the seen but unconverted input bytes
102 in the state.
104 FROM_ONEBYTE optional. If defined, should be the name of a
105 specialized conversion function for a single byte
106 from the current character set to INTERNAL. This
107 function has prototype
108 wint_t
109 FROM_ONEBYTE (struct __gconv_step *, unsigned char);
110 and does a special conversion:
111 - The input is a single byte.
112 - The output is a single uint32_t.
113 - The state before the conversion is the initial state;
114 the state after the conversion is irrelevant.
115 - No transliteration.
116 - __invocation_counter = 0.
117 - __internal_use = 1.
118 - do_flush = 0.
120 Modules can use mbstate_t to store conversion state as follows:
122 * Bits 2..0 of '__count' contain the number of lookahead input bytes
123 stored in __value.__wchb. Always zero if the converter never
124 returns __GCONV_INCOMPLETE_INPUT.
126 * Bits 31..3 of '__count' are module dependent shift state.
128 * __value: When STORE_REST/UNPACK_BYTES aren't defined and when the
129 converter has returned __GCONV_INCOMPLETE_INPUT, this contains
130 at most 4 lookahead bytes. Converters with an mb_cur_max > 4
131 (currently only UTF-8) must find a way to store their state
132 in __value.__wch and define STORE_REST/UNPACK_BYTES appropriately.
134 When __value contains lookahead, __count must not be zero, because
135 the converter is not in the initial state then, and mbsinit() --
136 defined as a (__count == 0) test -- must reflect this.
139 #include <assert.h>
140 #include <iconv/gconv_int.h>
141 #include <string.h>
142 #define __need_size_t
143 #define __need_NULL
144 #include <stddef.h>
146 #ifndef STATIC_GCONV
147 # include <dlfcn.h>
148 #endif
150 #include <sysdep.h>
151 #include <stdint.h>
153 #ifndef DL_CALL_FCT
154 # define DL_CALL_FCT(fct, args) fct args
155 #endif
157 /* The direction objects. */
158 #if DEFINE_INIT
159 # ifndef FROM_DIRECTION
160 # define FROM_DIRECTION_VAL NULL
161 # define TO_DIRECTION_VAL ((void *) ~((uintptr_t) 0))
162 # define FROM_DIRECTION (step->__data == FROM_DIRECTION_VAL)
163 # endif
164 #else
165 # ifndef FROM_DIRECTION
166 # error "FROM_DIRECTION must be provided if non-default init is used"
167 # endif
168 #endif
170 /* How many bytes are needed at most for the from-charset. */
171 #ifndef MAX_NEEDED_FROM
172 # define MAX_NEEDED_FROM MIN_NEEDED_FROM
173 #endif
175 /* Same for the to-charset. */
176 #ifndef MAX_NEEDED_TO
177 # define MAX_NEEDED_TO MIN_NEEDED_TO
178 #endif
180 /* Defaults for the per-direction min/max constants. */
181 #ifndef FROM_LOOP_MIN_NEEDED_FROM
182 # define FROM_LOOP_MIN_NEEDED_FROM MIN_NEEDED_FROM
183 #endif
184 #ifndef FROM_LOOP_MAX_NEEDED_FROM
185 # define FROM_LOOP_MAX_NEEDED_FROM MAX_NEEDED_FROM
186 #endif
187 #ifndef FROM_LOOP_MIN_NEEDED_TO
188 # define FROM_LOOP_MIN_NEEDED_TO MIN_NEEDED_TO
189 #endif
190 #ifndef FROM_LOOP_MAX_NEEDED_TO
191 # define FROM_LOOP_MAX_NEEDED_TO MAX_NEEDED_TO
192 #endif
193 #ifndef TO_LOOP_MIN_NEEDED_FROM
194 # define TO_LOOP_MIN_NEEDED_FROM MIN_NEEDED_TO
195 #endif
196 #ifndef TO_LOOP_MAX_NEEDED_FROM
197 # define TO_LOOP_MAX_NEEDED_FROM MAX_NEEDED_TO
198 #endif
199 #ifndef TO_LOOP_MIN_NEEDED_TO
200 # define TO_LOOP_MIN_NEEDED_TO MIN_NEEDED_FROM
201 #endif
202 #ifndef TO_LOOP_MAX_NEEDED_TO
203 # define TO_LOOP_MAX_NEEDED_TO MAX_NEEDED_FROM
204 #endif
207 /* Define macros which can access unaligned buffers. These macros are
208 supposed to be used only in code outside the inner loops. For the inner
209 loops we have other definitions which allow optimized access. */
210 #if _STRING_ARCH_unaligned
211 /* We can handle unaligned memory access. */
212 # define get16u(addr) *((const uint16_t *) (addr))
213 # define get32u(addr) *((const uint32_t *) (addr))
215 /* We need no special support for writing values either. */
216 # define put16u(addr, val) *((uint16_t *) (addr)) = (val)
217 # define put32u(addr, val) *((uint32_t *) (addr)) = (val)
218 #else
219 /* Distinguish between big endian and little endian. */
220 # if __BYTE_ORDER == __LITTLE_ENDIAN
221 # define get16u(addr) \
222 (((const unsigned char *) (addr))[1] << 8 \
223 | ((const unsigned char *) (addr))[0])
224 # define get32u(addr) \
225 (((((const unsigned char *) (addr))[3] << 8 \
226 | ((const unsigned char *) (addr))[2]) << 8 \
227 | ((const unsigned char *) (addr))[1]) << 8 \
228 | ((const unsigned char *) (addr))[0])
230 # define put16u(addr, val) \
231 ({ uint16_t __val = (val); \
232 ((unsigned char *) (addr))[0] = __val; \
233 ((unsigned char *) (addr))[1] = __val >> 8; \
234 (void) 0; })
235 # define put32u(addr, val) \
236 ({ uint32_t __val = (val); \
237 ((unsigned char *) (addr))[0] = __val; \
238 __val >>= 8; \
239 ((unsigned char *) (addr))[1] = __val; \
240 __val >>= 8; \
241 ((unsigned char *) (addr))[2] = __val; \
242 __val >>= 8; \
243 ((unsigned char *) (addr))[3] = __val; \
244 (void) 0; })
245 # else
246 # define get16u(addr) \
247 (((const unsigned char *) (addr))[0] << 8 \
248 | ((const unsigned char *) (addr))[1])
249 # define get32u(addr) \
250 (((((const unsigned char *) (addr))[0] << 8 \
251 | ((const unsigned char *) (addr))[1]) << 8 \
252 | ((const unsigned char *) (addr))[2]) << 8 \
253 | ((const unsigned char *) (addr))[3])
255 # define put16u(addr, val) \
256 ({ uint16_t __val = (val); \
257 ((unsigned char *) (addr))[1] = __val; \
258 ((unsigned char *) (addr))[0] = __val >> 8; \
259 (void) 0; })
260 # define put32u(addr, val) \
261 ({ uint32_t __val = (val); \
262 ((unsigned char *) (addr))[3] = __val; \
263 __val >>= 8; \
264 ((unsigned char *) (addr))[2] = __val; \
265 __val >>= 8; \
266 ((unsigned char *) (addr))[1] = __val; \
267 __val >>= 8; \
268 ((unsigned char *) (addr))[0] = __val; \
269 (void) 0; })
270 # endif
271 #endif
274 /* For conversions from a fixed width character set to another fixed width
275 character set we can define RESET_INPUT_BUFFER in a very fast way. */
276 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
277 # if FROM_LOOP_MIN_NEEDED_FROM == FROM_LOOP_MAX_NEEDED_FROM \
278 && FROM_LOOP_MIN_NEEDED_TO == FROM_LOOP_MAX_NEEDED_TO \
279 && TO_LOOP_MIN_NEEDED_FROM == TO_LOOP_MAX_NEEDED_FROM \
280 && TO_LOOP_MIN_NEEDED_TO == TO_LOOP_MAX_NEEDED_TO
281 /* We have to use these `if's here since the compiler cannot know that
282 (outbuf - outerr) is always divisible by FROM/TO_LOOP_MIN_NEEDED_TO.
283 The ?:1 avoids division by zero warnings that gcc 3.2 emits even for
284 obviously unreachable code. */
285 # define RESET_INPUT_BUFFER \
286 if (FROM_DIRECTION) \
288 if (FROM_LOOP_MIN_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_TO == 0) \
289 *inptrp -= (outbuf - outerr) \
290 * (FROM_LOOP_MIN_NEEDED_FROM / FROM_LOOP_MIN_NEEDED_TO); \
291 else if (FROM_LOOP_MIN_NEEDED_TO % FROM_LOOP_MIN_NEEDED_FROM == 0) \
292 *inptrp -= (outbuf - outerr) \
293 / (FROM_LOOP_MIN_NEEDED_TO / FROM_LOOP_MIN_NEEDED_FROM \
294 ? : 1); \
295 else \
296 *inptrp -= ((outbuf - outerr) / FROM_LOOP_MIN_NEEDED_TO) \
297 * FROM_LOOP_MIN_NEEDED_FROM; \
299 else \
301 if (TO_LOOP_MIN_NEEDED_FROM % TO_LOOP_MIN_NEEDED_TO == 0) \
302 *inptrp -= (outbuf - outerr) \
303 * (TO_LOOP_MIN_NEEDED_FROM / TO_LOOP_MIN_NEEDED_TO); \
304 else if (TO_LOOP_MIN_NEEDED_TO % TO_LOOP_MIN_NEEDED_FROM == 0) \
305 *inptrp -= (outbuf - outerr) \
306 / (TO_LOOP_MIN_NEEDED_TO / TO_LOOP_MIN_NEEDED_FROM ? : 1); \
307 else \
308 *inptrp -= ((outbuf - outerr) / TO_LOOP_MIN_NEEDED_TO) \
309 * TO_LOOP_MIN_NEEDED_FROM; \
311 # endif
312 #endif
315 /* The default init function. It simply matches the name and initializes
316 the step data to point to one of the objects above. */
317 #if DEFINE_INIT
318 # ifndef CHARSET_NAME
319 # error "CHARSET_NAME not defined"
320 # endif
322 extern int gconv_init (struct __gconv_step *step);
324 gconv_init (struct __gconv_step *step)
326 /* Determine which direction. */
327 if (strcmp (step->__from_name, CHARSET_NAME) == 0)
329 step->__data = FROM_DIRECTION_VAL;
331 step->__min_needed_from = FROM_LOOP_MIN_NEEDED_FROM;
332 step->__max_needed_from = FROM_LOOP_MAX_NEEDED_FROM;
333 step->__min_needed_to = FROM_LOOP_MIN_NEEDED_TO;
334 step->__max_needed_to = FROM_LOOP_MAX_NEEDED_TO;
336 #ifdef FROM_ONEBYTE
337 step->__btowc_fct = FROM_ONEBYTE;
338 #endif
340 else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0)
342 step->__data = TO_DIRECTION_VAL;
344 step->__min_needed_from = TO_LOOP_MIN_NEEDED_FROM;
345 step->__max_needed_from = TO_LOOP_MAX_NEEDED_FROM;
346 step->__min_needed_to = TO_LOOP_MIN_NEEDED_TO;
347 step->__max_needed_to = TO_LOOP_MAX_NEEDED_TO;
349 else
350 return __GCONV_NOCONV;
352 #ifdef SAVE_RESET_STATE
353 step->__stateful = 1;
354 #else
355 step->__stateful = 0;
356 #endif
358 return __GCONV_OK;
360 #endif
363 /* The default destructor function does nothing in the moment and so
364 we don't define it at all. But we still provide the macro just in
365 case we need it some day. */
366 #if DEFINE_FINI
367 #endif
370 /* If no arguments have to passed to the loop function define the macro
371 as empty. */
372 #ifndef EXTRA_LOOP_ARGS
373 # define EXTRA_LOOP_ARGS
374 #endif
377 /* This is the actual conversion function. */
378 #ifndef FUNCTION_NAME
379 # define FUNCTION_NAME gconv
380 #endif
382 /* The macros are used to access the function to convert single characters. */
383 #define SINGLE(fct) SINGLE2 (fct)
384 #define SINGLE2(fct) fct##_single
387 extern int FUNCTION_NAME (struct __gconv_step *step,
388 struct __gconv_step_data *data,
389 const unsigned char **inptrp,
390 const unsigned char *inend,
391 unsigned char **outbufstart, size_t *irreversible,
392 int do_flush, int consume_incomplete);
394 FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
395 const unsigned char **inptrp, const unsigned char *inend,
396 unsigned char **outbufstart, size_t *irreversible, int do_flush,
397 int consume_incomplete)
399 struct __gconv_step *next_step = step + 1;
400 struct __gconv_step_data *next_data = data + 1;
401 __gconv_fct fct = NULL;
402 int status;
404 if ((data->__flags & __GCONV_IS_LAST) == 0)
406 fct = next_step->__fct;
407 #ifdef PTR_DEMANGLE
408 if (next_step->__shlib_handle != NULL)
409 PTR_DEMANGLE (fct);
410 #endif
413 /* If the function is called with no input this means we have to reset
414 to the initial state. The possibly partly converted input is
415 dropped. */
416 if (__glibc_unlikely (do_flush))
418 /* This should never happen during error handling. */
419 assert (outbufstart == NULL);
421 status = __GCONV_OK;
423 #ifdef EMIT_SHIFT_TO_INIT
424 if (do_flush == 1)
426 /* We preserve the initial values of the pointer variables. */
427 unsigned char *outbuf = data->__outbuf;
428 unsigned char *outstart = outbuf;
429 unsigned char *outend = data->__outbufend;
431 # ifdef PREPARE_LOOP
432 PREPARE_LOOP
433 # endif
435 # ifdef SAVE_RESET_STATE
436 SAVE_RESET_STATE (1);
437 # endif
439 /* Emit the escape sequence to reset the state. */
440 EMIT_SHIFT_TO_INIT;
442 /* Call the steps down the chain if there are any but only if we
443 successfully emitted the escape sequence. This should only
444 fail if the output buffer is full. If the input is invalid
445 it should be discarded since the user wants to start from a
446 clean state. */
447 if (status == __GCONV_OK)
449 if (data->__flags & __GCONV_IS_LAST)
450 /* Store information about how many bytes are available. */
451 data->__outbuf = outbuf;
452 else
454 /* Write out all output which was produced. */
455 if (outbuf > outstart)
457 const unsigned char *outerr = outstart;
458 int result;
460 result = DL_CALL_FCT (fct, (next_step, next_data,
461 &outerr, outbuf, NULL,
462 irreversible, 0,
463 consume_incomplete));
465 if (result != __GCONV_EMPTY_INPUT)
467 if (__glibc_unlikely (outerr != outbuf))
469 /* We have a problem. Undo the conversion. */
470 outbuf = outstart;
472 /* Restore the state. */
473 # ifdef SAVE_RESET_STATE
474 SAVE_RESET_STATE (0);
475 # endif
478 /* Change the status. */
479 status = result;
483 if (status == __GCONV_OK)
484 /* Now flush the remaining steps. */
485 status = DL_CALL_FCT (fct, (next_step, next_data, NULL,
486 NULL, NULL, irreversible, 1,
487 consume_incomplete));
491 else
492 #endif
494 /* Clear the state object. There might be bytes in there from
495 previous calls with CONSUME_INCOMPLETE == 1. But don't emit
496 escape sequences. */
497 memset (data->__statep, '\0', sizeof (*data->__statep));
499 if (! (data->__flags & __GCONV_IS_LAST))
500 /* Now flush the remaining steps. */
501 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
502 NULL, irreversible, do_flush,
503 consume_incomplete));
506 else
508 /* We preserve the initial values of the pointer variables,
509 but only some conversion modules need it. */
510 const unsigned char *inptr __attribute__ ((__unused__)) = *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
540 int unaligned;
541 # define GEN_unaligned(name) GEN_unaligned2 (name)
542 # define GEN_unaligned2(name) name##_unaligned
543 #else
544 # define unaligned 0
545 #endif
547 #ifdef PREPARE_LOOP
548 PREPARE_LOOP
549 #endif
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
562 error handler. */
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
569 EXTRA_LOOP_ARGS);
570 # endif
571 # if !ONE_DIRECTION
572 # if FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1
573 else
574 # endif
575 # if TO_LOOP_MAX_NEEDED_FROM > 1
576 status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf,
577 outend, lirreversiblep EXTRA_LOOP_ARGS);
578 # endif
579 # endif
581 if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
582 return status;
584 #endif
586 #if POSSIBLY_UNALIGNED
587 unaligned =
588 ((FROM_DIRECTION
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)))
592 || (!FROM_DIRECTION
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)));
596 #endif
598 while (1)
600 /* Remember the start value for this round. */
601 inptr = *inptrp;
602 /* The outbuf buffer is empty. */
603 outstart = outbuf;
604 #ifdef RESET_INPUT_BUFFER
605 /* Remember how many irreversible characters were skipped before
606 this round. */
607 size_t loop_irreversible
608 = lirreversible + (irreversible ? *irreversible : 0);
609 #endif
611 #ifdef SAVE_RESET_STATE
612 SAVE_RESET_STATE (1);
613 #endif
615 if (__glibc_likely (!unaligned))
617 if (FROM_DIRECTION)
618 /* Run the conversion loop. */
619 status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend,
620 lirreversiblep EXTRA_LOOP_ARGS);
621 else
622 /* Run the conversion loop. */
623 status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend,
624 lirreversiblep EXTRA_LOOP_ARGS);
626 #if POSSIBLY_UNALIGNED
627 else
629 if (FROM_DIRECTION)
630 /* Run the conversion loop. */
631 status = GEN_unaligned (FROM_LOOP) (step, data, inptrp, inend,
632 &outbuf, outend,
633 lirreversiblep
634 EXTRA_LOOP_ARGS);
635 else
636 /* Run the conversion loop. */
637 status = GEN_unaligned (TO_LOOP) (step, data, inptrp, inend,
638 &outbuf, outend,
639 lirreversiblep
640 EXTRA_LOOP_ARGS);
642 #endif
644 /* If we were called as part of an error handling module we
645 don't do anything else here. */
646 if (__glibc_unlikely (outbufstart != NULL))
648 *outbufstart = outbuf;
649 return status;
652 /* We finished one use of the loops. */
653 ++data->__invocation_counter;
655 /* If this is the last step leave the loop, there is nothing
656 we can do. */
657 if (__glibc_unlikely (data->__flags & __GCONV_IS_LAST))
659 /* Store information about how many bytes are available. */
660 data->__outbuf = outbuf;
662 /* Remember how many non-identical characters we
663 converted in an irreversible way. */
664 *irreversible += lirreversible;
666 break;
669 /* Write out all output which was produced. */
670 if (__glibc_likely (outbuf > outstart))
672 const unsigned char *outerr = data->__outbuf;
673 int result;
675 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
676 outbuf, NULL, irreversible, 0,
677 consume_incomplete));
679 if (result != __GCONV_EMPTY_INPUT)
681 if (__glibc_unlikely (outerr != outbuf))
683 #ifdef RESET_INPUT_BUFFER
684 /* RESET_INPUT_BUFFER can only work when there were
685 no new irreversible characters skipped during
686 this round. */
687 if (loop_irreversible
688 == lirreversible + (irreversible ? *irreversible : 0))
690 RESET_INPUT_BUFFER;
691 goto done_reset;
693 #endif
694 /* We have a problem in one of the functions below.
695 Undo the conversion upto the error point. */
696 size_t nstatus __attribute__ ((unused));
698 /* Reload the pointers. */
699 *inptrp = inptr;
700 outbuf = outstart;
702 /* Restore the state. */
703 #ifdef SAVE_RESET_STATE
704 SAVE_RESET_STATE (0);
705 #endif
707 if (__glibc_likely (!unaligned))
709 if (FROM_DIRECTION)
710 /* Run the conversion loop. */
711 nstatus = FROM_LOOP (step, data, inptrp, inend,
712 &outbuf, outerr,
713 lirreversiblep
714 EXTRA_LOOP_ARGS);
715 else
716 /* Run the conversion loop. */
717 nstatus = TO_LOOP (step, data, inptrp, inend,
718 &outbuf, outerr,
719 lirreversiblep
720 EXTRA_LOOP_ARGS);
722 #if POSSIBLY_UNALIGNED
723 else
725 if (FROM_DIRECTION)
726 /* Run the conversion loop. */
727 nstatus = GEN_unaligned (FROM_LOOP) (step, data,
728 inptrp, inend,
729 &outbuf,
730 outerr,
731 lirreversiblep
732 EXTRA_LOOP_ARGS);
733 else
734 /* Run the conversion loop. */
735 nstatus = GEN_unaligned (TO_LOOP) (step, data,
736 inptrp, inend,
737 &outbuf, outerr,
738 lirreversiblep
739 EXTRA_LOOP_ARGS);
741 #endif
743 /* We must run out of output buffer space in this
744 rerun. */
745 assert (outbuf == outerr);
746 assert (nstatus == __GCONV_FULL_OUTPUT);
748 /* If we haven't consumed a single byte decrement
749 the invocation counter. */
750 if (__glibc_unlikely (outbuf == outstart))
751 --data->__invocation_counter;
754 #ifdef RESET_INPUT_BUFFER
755 done_reset:
756 #endif
757 /* Change the status. */
758 status = result;
760 else
761 /* All the output is consumed, we can make another run
762 if everything was ok. */
763 if (status == __GCONV_FULL_OUTPUT)
765 status = __GCONV_OK;
766 outbuf = data->__outbuf;
770 if (status != __GCONV_OK)
771 break;
773 /* Reset the output buffer pointer for the next round. */
774 outbuf = data->__outbuf;
777 #ifdef END_LOOP
778 END_LOOP
779 #endif
781 /* If we are supposed to consume all character store now all of the
782 remaining characters in the `state' object. */
783 #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
784 if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
785 || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
786 || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
787 && __builtin_expect (consume_incomplete, 0)
788 && status == __GCONV_INCOMPLETE_INPUT)
790 # ifdef STORE_REST
791 mbstate_t *state = data->__statep;
793 STORE_REST
794 # else
795 /* Make sure the remaining bytes fit into the state objects
796 buffer. */
797 size_t cnt_after = inend - *inptrp;
798 assert (cnt_after <= sizeof (data->__statep->__value.__wchb));
800 size_t cnt;
801 for (cnt = 0; cnt < cnt_after; ++cnt)
802 data->__statep->__value.__wchb[cnt] = (*inptrp)[cnt];
803 *inptrp = inend;
804 data->__statep->__count &= ~7;
805 data->__statep->__count |= cnt;
806 # endif
808 #endif
809 #undef unaligned
810 #undef POSSIBLY_UNALIGNED
813 return status;
816 #undef DEFINE_INIT
817 #undef CHARSET_NAME
818 #undef DEFINE_FINI
819 #undef MIN_NEEDED_FROM
820 #undef MIN_NEEDED_TO
821 #undef MAX_NEEDED_FROM
822 #undef MAX_NEEDED_TO
823 #undef FROM_LOOP_MIN_NEEDED_FROM
824 #undef FROM_LOOP_MAX_NEEDED_FROM
825 #undef FROM_LOOP_MIN_NEEDED_TO
826 #undef FROM_LOOP_MAX_NEEDED_TO
827 #undef TO_LOOP_MIN_NEEDED_FROM
828 #undef TO_LOOP_MAX_NEEDED_FROM
829 #undef TO_LOOP_MIN_NEEDED_TO
830 #undef TO_LOOP_MAX_NEEDED_TO
831 #undef FROM_DIRECTION
832 #undef EMIT_SHIFT_TO_INIT
833 #undef FROM_LOOP
834 #undef TO_LOOP
835 #undef ONE_DIRECTION
836 #undef SAVE_RESET_STATE
837 #undef RESET_INPUT_BUFFER
838 #undef FUNCTION_NAME
839 #undef PREPARE_LOOP
840 #undef END_LOOP
841 #undef EXTRA_LOOP_ARGS
842 #undef STORE_REST
843 #undef FROM_ONEBYTE