]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/skeleton.c
Enumerate tests with special rules in tests-special variable.
[thirdparty/glibc.git] / iconv / skeleton.c
CommitLineData
09376451 1/* Skeleton for a conversion module.
d4697bc9 2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
8619129f
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
8619129f
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
8619129f 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
8619129f
UD
19
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
22 macros:
23
24 DEFINE_INIT define the default initializer. This requires the
25 following symbol to be defined.
26
27 CHARSET_NAME string with official name of the coded character
28 set (in all-caps)
29
30 DEFINE_FINI define the default destructor function.
31
32 MIN_NEEDED_FROM minimal number of bytes needed for the from-charset.
33 MIN_NEEDED_TO likewise for the to-charset.
34
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.
38
faaa6f62
UD
39 FROM_LOOP_MIN_NEEDED_FROM
40 FROM_LOOP_MAX_NEEDED_FROM
a784e502
UD
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.
faaa6f62
UD
44 FROM_LOOP_MIN_NEEDED_TO
45 FROM_LOOP_MAX_NEEDED_TO
a784e502
UD
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.
faaa6f62
UD
49 TO_LOOP_MIN_NEEDED_FROM
50 TO_LOOP_MAX_NEEDED_FROM
a784e502
UD
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.
faaa6f62
UD
54 TO_LOOP_MIN_NEEDED_TO
55 TO_LOOP_MAX_NEEDED_TO
a784e502
UD
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.
faaa6f62 59
8619129f
UD
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.
63
64 EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
65 defines some code which writes out a sequence
faaa6f62 66 of bytes which bring the current state into
8619129f
UD
67 the initial state.
68
69 FROM_LOOP name of the function implementing the conversion
faaa6f62 70 from the current character set.
8619129f
UD
71 TO_LOOP likewise for the other direction
72
f6ad4726
UD
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.
77
78 SAVE_RESET_STATE in case of an error we must reset the state for
8619129f
UD
79 the rerun so this macro must be defined for
80 stateful encodings. It takes an argument which
81 is nonzero when saving.
82
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.
86
87 FUNCTION_NAME if not set the conversion function is named `gconv'.
28f1c862
UD
88
89 PREPARE_LOOP optional code preparing the conversion loop. Can
90 contain variable definitions.
918b9d72 91 END_LOOP also optional, may be used to store information
28f1c862
UD
92
93 EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
94 to loop function.
faaa6f62 95
f9ad060c
UD
96 STORE_REST optional, needed only when MAX_NEEDED_FROM > 4.
97 This macro stores the seen but unconverted input bytes
98 in the state.
99
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
104 wint_t
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.
114 - do_flush = 0.
115
faaa6f62
UD
116 Modules can use mbstate_t to store conversion state as follows:
117
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.
121
122 * Bits 31..3 of '__count' are module dependent shift state.
123
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.
129
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.
8619129f
UD
133 */
134
135#include <assert.h>
136#include <gconv.h>
137#include <string.h>
138#define __need_size_t
139#define __need_NULL
140#include <stddef.h>
c66dbe00
UD
141
142#ifndef STATIC_GCONV
143# include <dlfcn.h>
c66dbe00 144#endif
8619129f 145
915a6c51 146#include <sysdep.h>
e054f494 147#include <stdint.h>
915a6c51 148
a808d541
UD
149#ifndef DL_CALL_FCT
150# define DL_CALL_FCT(fct, args) fct args
151#endif
152
8619129f 153/* The direction objects. */
45541bdf 154#if DEFINE_INIT
8619129f 155# ifndef FROM_DIRECTION
45541bdf
UD
156# define FROM_DIRECTION_VAL NULL
157# define TO_DIRECTION_VAL ((void *) ~((uintptr_t) 0))
158# define FROM_DIRECTION (step->__data == FROM_DIRECTION_VAL)
8619129f
UD
159# endif
160#else
161# ifndef FROM_DIRECTION
dbc92dc6 162# error "FROM_DIRECTION must be provided if non-default init is used"
8619129f
UD
163# endif
164#endif
165
166
167/* How many bytes are needed at most for the from-charset. */
168#ifndef MAX_NEEDED_FROM
169# define MAX_NEEDED_FROM MIN_NEEDED_FROM
170#endif
171
172/* Same for the to-charset. */
173#ifndef MAX_NEEDED_TO
174# define MAX_NEEDED_TO MIN_NEEDED_TO
175#endif
176
faaa6f62
UD
177/* Defaults for the per-direction min/max constants. */
178#ifndef FROM_LOOP_MIN_NEEDED_FROM
179# define FROM_LOOP_MIN_NEEDED_FROM MIN_NEEDED_FROM
180#endif
181#ifndef FROM_LOOP_MAX_NEEDED_FROM
182# define FROM_LOOP_MAX_NEEDED_FROM MAX_NEEDED_FROM
183#endif
184#ifndef FROM_LOOP_MIN_NEEDED_TO
185# define FROM_LOOP_MIN_NEEDED_TO MIN_NEEDED_TO
186#endif
187#ifndef FROM_LOOP_MAX_NEEDED_TO
188# define FROM_LOOP_MAX_NEEDED_TO MAX_NEEDED_TO
189#endif
190#ifndef TO_LOOP_MIN_NEEDED_FROM
191# define TO_LOOP_MIN_NEEDED_FROM MIN_NEEDED_TO
192#endif
193#ifndef TO_LOOP_MAX_NEEDED_FROM
194# define TO_LOOP_MAX_NEEDED_FROM MAX_NEEDED_TO
195#endif
196#ifndef TO_LOOP_MIN_NEEDED_TO
197# define TO_LOOP_MIN_NEEDED_TO MIN_NEEDED_FROM
198#endif
199#ifndef TO_LOOP_MAX_NEEDED_TO
200# define TO_LOOP_MAX_NEEDED_TO MAX_NEEDED_FROM
201#endif
202
8619129f 203
77e1d15a
UD
204/* Define macros which can access unaligned buffers. These macros are
205 supposed to be used only in code outside the inner loops. For the inner
206 loops we have other definitions which allow optimized access. */
207#ifdef _STRING_ARCH_unaligned
208/* We can handle unaligned memory access. */
a784e502
UD
209# define get16u(addr) *((const uint16_t *) (addr))
210# define get32u(addr) *((const uint32_t *) (addr))
77e1d15a
UD
211
212/* We need no special support for writing values either. */
c1db8b0d
UD
213# define put16u(addr, val) *((uint16_t *) (addr)) = (val)
214# define put32u(addr, val) *((uint32_t *) (addr)) = (val)
77e1d15a
UD
215#else
216/* Distinguish between big endian and little endian. */
217# if __BYTE_ORDER == __LITTLE_ENDIAN
c1db8b0d 218# define get16u(addr) \
a784e502
UD
219 (((const unsigned char *) (addr))[1] << 8 \
220 | ((const unsigned char *) (addr))[0])
c1db8b0d 221# define get32u(addr) \
a784e502
UD
222 (((((const unsigned char *) (addr))[3] << 8 \
223 | ((const unsigned char *) (addr))[2]) << 8 \
224 | ((const unsigned char *) (addr))[1]) << 8 \
225 | ((const unsigned char *) (addr))[0])
77e1d15a 226
c1db8b0d 227# define put16u(addr, val) \
77e1d15a 228 ({ uint16_t __val = (val); \
cb2c5501
UD
229 ((unsigned char *) (addr))[0] = __val; \
230 ((unsigned char *) (addr))[1] = __val >> 8; \
77e1d15a 231 (void) 0; })
c1db8b0d 232# define put32u(addr, val) \
cb2c5501
UD
233 ({ uint32_t __val = (val); \
234 ((unsigned char *) (addr))[0] = __val; \
77e1d15a 235 __val >>= 8; \
cb2c5501 236 ((unsigned char *) (addr))[1] = __val; \
77e1d15a 237 __val >>= 8; \
cb2c5501 238 ((unsigned char *) (addr))[2] = __val; \
77e1d15a 239 __val >>= 8; \
cb2c5501 240 ((unsigned char *) (addr))[3] = __val; \
77e1d15a
UD
241 (void) 0; })
242# else
c1db8b0d 243# define get16u(addr) \
a784e502
UD
244 (((const unsigned char *) (addr))[0] << 8 \
245 | ((const unsigned char *) (addr))[1])
c1db8b0d 246# define get32u(addr) \
a784e502
UD
247 (((((const unsigned char *) (addr))[0] << 8 \
248 | ((const unsigned char *) (addr))[1]) << 8 \
249 | ((const unsigned char *) (addr))[2]) << 8 \
250 | ((const unsigned char *) (addr))[3])
77e1d15a 251
c1db8b0d 252# define put16u(addr, val) \
77e1d15a 253 ({ uint16_t __val = (val); \
cb2c5501 254 ((unsigned char *) (addr))[1] = __val; \
a1303dc8 255 ((unsigned char *) (addr))[0] = __val >> 8; \
77e1d15a 256 (void) 0; })
c1db8b0d 257# define put32u(addr, val) \
cb2c5501
UD
258 ({ uint32_t __val = (val); \
259 ((unsigned char *) (addr))[3] = __val; \
77e1d15a 260 __val >>= 8; \
cb2c5501 261 ((unsigned char *) (addr))[2] = __val; \
77e1d15a 262 __val >>= 8; \
cb2c5501 263 ((unsigned char *) (addr))[1] = __val; \
77e1d15a 264 __val >>= 8; \
cb2c5501 265 ((unsigned char *) (addr))[0] = __val; \
77e1d15a
UD
266 (void) 0; })
267# endif
268#endif
269
270
f6ad4726
UD
271/* For conversions from a fixed width character set to another fixed width
272 character set we can define RESET_INPUT_BUFFER in a very fast way. */
8619129f 273#if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
faaa6f62
UD
274# if FROM_LOOP_MIN_NEEDED_FROM == FROM_LOOP_MAX_NEEDED_FROM \
275 && FROM_LOOP_MIN_NEEDED_TO == FROM_LOOP_MAX_NEEDED_TO \
276 && TO_LOOP_MIN_NEEDED_FROM == TO_LOOP_MAX_NEEDED_FROM \
277 && TO_LOOP_MIN_NEEDED_TO == TO_LOOP_MAX_NEEDED_TO
278/* We have to use these `if's here since the compiler cannot know that
279 (outbuf - outerr) is always divisible by FROM/TO_LOOP_MIN_NEEDED_TO.
280 The ?:1 avoids division by zero warnings that gcc 3.2 emits even for
281 obviously unreachable code. */
282# define RESET_INPUT_BUFFER \
283 if (FROM_DIRECTION) \
284 { \
285 if (FROM_LOOP_MIN_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_TO == 0) \
286 *inptrp -= (outbuf - outerr) \
287 * (FROM_LOOP_MIN_NEEDED_FROM / FROM_LOOP_MIN_NEEDED_TO); \
288 else if (FROM_LOOP_MIN_NEEDED_TO % FROM_LOOP_MIN_NEEDED_FROM == 0) \
289 *inptrp -= (outbuf - outerr) \
290 / (FROM_LOOP_MIN_NEEDED_TO / FROM_LOOP_MIN_NEEDED_FROM \
291 ? : 1); \
292 else \
293 *inptrp -= ((outbuf - outerr) / FROM_LOOP_MIN_NEEDED_TO) \
294 * FROM_LOOP_MIN_NEEDED_FROM; \
295 } \
296 else \
297 { \
298 if (TO_LOOP_MIN_NEEDED_FROM % TO_LOOP_MIN_NEEDED_TO == 0) \
299 *inptrp -= (outbuf - outerr) \
300 * (TO_LOOP_MIN_NEEDED_FROM / TO_LOOP_MIN_NEEDED_TO); \
301 else if (TO_LOOP_MIN_NEEDED_TO % TO_LOOP_MIN_NEEDED_FROM == 0) \
302 *inptrp -= (outbuf - outerr) \
303 / (TO_LOOP_MIN_NEEDED_TO / TO_LOOP_MIN_NEEDED_FROM ? : 1); \
304 else \
305 *inptrp -= ((outbuf - outerr) / TO_LOOP_MIN_NEEDED_TO) \
306 * TO_LOOP_MIN_NEEDED_FROM; \
307 }
8619129f
UD
308# endif
309#endif
310
311
312/* The default init function. It simply matches the name and initializes
313 the step data to point to one of the objects above. */
314#if DEFINE_INIT
315# ifndef CHARSET_NAME
316# error "CHARSET_NAME not defined"
317# endif
318
a1620a4c 319extern int gconv_init (struct __gconv_step *step);
8619129f 320int
d64b6ad0 321gconv_init (struct __gconv_step *step)
8619129f
UD
322{
323 /* Determine which direction. */
d64b6ad0 324 if (strcmp (step->__from_name, CHARSET_NAME) == 0)
918b9d72 325 {
45541bdf 326 step->__data = FROM_DIRECTION_VAL;
390955cb 327
faaa6f62
UD
328 step->__min_needed_from = FROM_LOOP_MIN_NEEDED_FROM;
329 step->__max_needed_from = FROM_LOOP_MAX_NEEDED_FROM;
330 step->__min_needed_to = FROM_LOOP_MIN_NEEDED_TO;
331 step->__max_needed_to = FROM_LOOP_MAX_NEEDED_TO;
f9ad060c
UD
332
333#ifdef FROM_ONEBYTE
334 step->__btowc_fct = FROM_ONEBYTE;
335#endif
918b9d72 336 }
365afefc 337 else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0)
918b9d72 338 {
45541bdf 339 step->__data = TO_DIRECTION_VAL;
390955cb 340
faaa6f62
UD
341 step->__min_needed_from = TO_LOOP_MIN_NEEDED_FROM;
342 step->__max_needed_from = TO_LOOP_MAX_NEEDED_FROM;
343 step->__min_needed_to = TO_LOOP_MIN_NEEDED_TO;
344 step->__max_needed_to = TO_LOOP_MAX_NEEDED_TO;
918b9d72 345 }
390955cb 346 else
d64b6ad0 347 return __GCONV_NOCONV;
8619129f 348
f6ad4726 349#ifdef SAVE_RESET_STATE
d64b6ad0 350 step->__stateful = 1;
9ce5071a 351#else
d64b6ad0 352 step->__stateful = 0;
9ce5071a
UD
353#endif
354
d64b6ad0 355 return __GCONV_OK;
8619129f
UD
356}
357#endif
358
359
360/* The default destructor function does nothing in the moment and so
f6ad4726
UD
361 we don't define it at all. But we still provide the macro just in
362 case we need it some day. */
8619129f
UD
363#if DEFINE_FINI
364#endif
365
366
28f1c862
UD
367/* If no arguments have to passed to the loop function define the macro
368 as empty. */
369#ifndef EXTRA_LOOP_ARGS
370# define EXTRA_LOOP_ARGS
371#endif
372
373
8619129f
UD
374/* This is the actual conversion function. */
375#ifndef FUNCTION_NAME
376# define FUNCTION_NAME gconv
377#endif
378
fd1b5c0f
UD
379/* The macros are used to access the function to convert single characters. */
380#define SINGLE(fct) SINGLE2 (fct)
381#define SINGLE2(fct) fct##_single
382
383
a1620a4c
UD
384extern int FUNCTION_NAME (struct __gconv_step *step,
385 struct __gconv_step_data *data,
386 const unsigned char **inptrp,
387 const unsigned char *inend,
388 unsigned char **outbufstart, size_t *irreversible,
389 int do_flush, int consume_incomplete);
8619129f 390int
d64b6ad0 391FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
fd1b5c0f 392 const unsigned char **inptrp, const unsigned char *inend,
f1d5c60d 393 unsigned char **outbufstart, size_t *irreversible, int do_flush,
55985355 394 int consume_incomplete)
8619129f 395{
d64b6ad0
UD
396 struct __gconv_step *next_step = step + 1;
397 struct __gconv_step_data *next_data = data + 1;
915a6c51 398 __gconv_fct fct = NULL;
8619129f
UD
399 int status;
400
915a6c51
UD
401 if ((data->__flags & __GCONV_IS_LAST) == 0)
402 {
403 fct = next_step->__fct;
404#ifdef PTR_DEMANGLE
405 if (next_step->__shlib_handle != NULL)
406 PTR_DEMANGLE (fct);
407#endif
408 }
85830c4c 409
8619129f
UD
410 /* If the function is called with no input this means we have to reset
411 to the initial state. The possibly partly converted input is
412 dropped. */
a1ffb40e 413 if (__glibc_unlikely (do_flush))
8619129f 414 {
f1d5c60d
UD
415 /* This should never happen during error handling. */
416 assert (outbufstart == NULL);
417
2373b30e
UD
418 status = __GCONV_OK;
419
390955cb 420#ifdef EMIT_SHIFT_TO_INIT
2373b30e
UD
421 if (do_flush == 1)
422 {
423 /* We preserve the initial values of the pointer variables. */
424 unsigned char *outbuf = data->__outbuf;
425 unsigned char *outstart = outbuf;
426 unsigned char *outend = data->__outbufend;
427
428# ifdef PREPARE_LOOP
429 PREPARE_LOOP
430# endif
431
432# ifdef SAVE_RESET_STATE
433 SAVE_RESET_STATE (1);
434# endif
435
436 /* Emit the escape sequence to reset the state. */
437 EMIT_SHIFT_TO_INIT;
438
439 /* Call the steps down the chain if there are any but only if we
440 successfully emitted the escape sequence. This should only
441 fail if the output buffer is full. If the input is invalid
442 it should be discarded since the user wants to start from a
443 clean state. */
444 if (status == __GCONV_OK)
445 {
446 if (data->__flags & __GCONV_IS_LAST)
447 /* Store information about how many bytes are available. */
448 data->__outbuf = outbuf;
449 else
450 {
451 /* Write out all output which was produced. */
452 if (outbuf > outstart)
453 {
454 const unsigned char *outerr = outstart;
455 int result;
456
457 result = DL_CALL_FCT (fct, (next_step, next_data,
458 &outerr, outbuf, NULL,
459 irreversible, 0,
460 consume_incomplete));
461
462 if (result != __GCONV_EMPTY_INPUT)
463 {
a1ffb40e 464 if (__glibc_unlikely (outerr != outbuf))
2373b30e
UD
465 {
466 /* We have a problem. Undo the conversion. */
467 outbuf = outstart;
468
469 /* Restore the state. */
470# ifdef SAVE_RESET_STATE
471 SAVE_RESET_STATE (0);
472# endif
473 }
474
475 /* Change the status. */
476 status = result;
477 }
478 }
479
480 if (status == __GCONV_OK)
481 /* Now flush the remaining steps. */
482 status = DL_CALL_FCT (fct, (next_step, next_data, NULL,
483 NULL, NULL, irreversible, 1,
484 consume_incomplete));
485 }
486 }
487 }
488 else
8619129f 489#endif
2373b30e
UD
490 {
491 /* Clear the state object. There might be bytes in there from
492 previous calls with CONSUME_INCOMPLETE == 1. But don't emit
493 escape sequences. */
494 memset (data->__statep, '\0', sizeof (*data->__statep));
495
496 if (! (data->__flags & __GCONV_IS_LAST))
497 /* Now flush the remaining steps. */
498 status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
499 NULL, irreversible, do_flush,
500 consume_incomplete));
501 }
8619129f
UD
502 }
503 else
504 {
8619129f 505 /* We preserve the initial values of the pointer variables. */
fd1b5c0f 506 const unsigned char *inptr = *inptrp;
f1d5c60d
UD
507 unsigned char *outbuf = (__builtin_expect (outbufstart == NULL, 1)
508 ? data->__outbuf : *outbufstart);
d64b6ad0 509 unsigned char *outend = data->__outbufend;
b117f744 510 unsigned char *outstart;
3aaad0b6
UD
511 /* This variable is used to count the number of characters we
512 actually converted. */
38677ace 513 size_t lirreversible = 0;
b572c2da 514 size_t *lirreversiblep = irreversible ? &lirreversible : NULL;
faaa6f62
UD
515
516 /* The following assumes that encodings, which have a variable length
c0c3f78a 517 what might unalign a buffer even though it is an aligned in the
faaa6f62
UD
518 beginning, either don't have the minimal number of bytes as a divisor
519 of the maximum length or have a minimum length of 1. This is true
520 for all known and supported encodings.
521 We use && instead of || to combine the subexpression for the FROM
522 encoding and for the TO encoding, because usually one of them is
523 INTERNAL, for which the subexpression evaluates to 1, but INTERNAL
524 buffers are always aligned correctly. */
525#define POSSIBLY_UNALIGNED \
526 (!defined _STRING_ARCH_unaligned \
527 && (((FROM_LOOP_MIN_NEEDED_FROM != 1 \
528 && FROM_LOOP_MAX_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_FROM == 0) \
529 && (FROM_LOOP_MIN_NEEDED_TO != 1 \
530 && FROM_LOOP_MAX_NEEDED_TO % FROM_LOOP_MIN_NEEDED_TO == 0)) \
531 || ((TO_LOOP_MIN_NEEDED_FROM != 1 \
532 && TO_LOOP_MAX_NEEDED_FROM % TO_LOOP_MIN_NEEDED_FROM == 0) \
533 && (TO_LOOP_MIN_NEEDED_TO != 1 \
534 && TO_LOOP_MAX_NEEDED_TO % TO_LOOP_MIN_NEEDED_TO == 0))))
535#if POSSIBLY_UNALIGNED
3aaad0b6 536 int unaligned;
77e1d15a
UD
537# define GEN_unaligned(name) GEN_unaligned2 (name)
538# define GEN_unaligned2(name) name##_unaligned
faaa6f62
UD
539#else
540# define unaligned 0
77e1d15a 541#endif
8619129f 542
28f1c862
UD
543#ifdef PREPARE_LOOP
544 PREPARE_LOOP
545#endif
546
faaa6f62 547#if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
fd1b5c0f
UD
548 /* If the function is used to implement the mb*towc*() or wc*tomb*()
549 functions we must test whether any bytes from the last call are
550 stored in the `state' object. */
faaa6f62
UD
551 if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
552 || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
553 || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
fd1b5c0f
UD
554 && consume_incomplete && (data->__statep->__count & 7) != 0)
555 {
f1d5c60d 556 /* Yep, we have some bytes left over. Process them now.
a784e502
UD
557 But this must not happen while we are called from an
558 error handler. */
f1d5c60d 559 assert (outbufstart == NULL);
fd1b5c0f 560
faaa6f62
UD
561# if FROM_LOOP_MAX_NEEDED_FROM > 1
562 if (TO_LOOP_MAX_NEEDED_FROM == 1 || FROM_DIRECTION)
55985355 563 status = SINGLE(FROM_LOOP) (step, data, inptrp, inend, &outbuf,
b572c2da 564 outend, lirreversiblep
85830c4c 565 EXTRA_LOOP_ARGS);
fd1b5c0f 566# endif
faaa6f62
UD
567# if !ONE_DIRECTION
568# if FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1
fd1b5c0f 569 else
faaa6f62
UD
570# endif
571# if TO_LOOP_MAX_NEEDED_FROM > 1
55985355 572 status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf,
b572c2da 573 outend, lirreversiblep EXTRA_LOOP_ARGS);
faaa6f62 574# endif
fd1b5c0f
UD
575# endif
576
365afefc 577 if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
fd1b5c0f
UD
578 return status;
579 }
580#endif
581
faaa6f62
UD
582#if POSSIBLY_UNALIGNED
583 unaligned =
584 ((FROM_DIRECTION
585 && ((uintptr_t) inptr % FROM_LOOP_MIN_NEEDED_FROM != 0
586 || ((data->__flags & __GCONV_IS_LAST)
587 && (uintptr_t) outbuf % FROM_LOOP_MIN_NEEDED_TO != 0)))
588 || (!FROM_DIRECTION
589 && (((data->__flags & __GCONV_IS_LAST)
590 && (uintptr_t) outbuf % TO_LOOP_MIN_NEEDED_TO != 0)
591 || (uintptr_t) inptr % TO_LOOP_MIN_NEEDED_FROM != 0)));
3aaad0b6
UD
592#endif
593
f1813b56 594 while (1)
8619129f 595 {
d6204268
UD
596 struct __gconv_trans_data *trans;
597
8619129f 598 /* Remember the start value for this round. */
fd1b5c0f 599 inptr = *inptrp;
8619129f 600 /* The outbuf buffer is empty. */
b117f744 601 outstart = outbuf;
8619129f 602
8619129f
UD
603#ifdef SAVE_RESET_STATE
604 SAVE_RESET_STATE (1);
605#endif
606
a1ffb40e 607 if (__glibc_likely (!unaligned))
77e1d15a
UD
608 {
609 if (FROM_DIRECTION)
610 /* Run the conversion loop. */
55985355 611 status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend,
b572c2da 612 lirreversiblep EXTRA_LOOP_ARGS);
77e1d15a
UD
613 else
614 /* Run the conversion loop. */
55985355 615 status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend,
b572c2da 616 lirreversiblep EXTRA_LOOP_ARGS);
77e1d15a 617 }
faaa6f62 618#if POSSIBLY_UNALIGNED
8619129f 619 else
77e1d15a
UD
620 {
621 if (FROM_DIRECTION)
622 /* Run the conversion loop. */
55985355
UD
623 status = GEN_unaligned (FROM_LOOP) (step, data, inptrp, inend,
624 &outbuf, outend,
b572c2da 625 lirreversiblep
77e1d15a
UD
626 EXTRA_LOOP_ARGS);
627 else
628 /* Run the conversion loop. */
55985355
UD
629 status = GEN_unaligned (TO_LOOP) (step, data, inptrp, inend,
630 &outbuf, outend,
b572c2da 631 lirreversiblep
77e1d15a
UD
632 EXTRA_LOOP_ARGS);
633 }
634#endif
8619129f 635
f1d5c60d
UD
636 /* If we were called as part of an error handling module we
637 don't do anything else here. */
a1ffb40e 638 if (__glibc_unlikely (outbufstart != NULL))
f1d5c60d
UD
639 {
640 *outbufstart = outbuf;
641 return status;
642 }
643
4d37e19c
UD
644 /* Give the transliteration module the chance to store the
645 original text and the result in case it needs a context. */
d6204268
UD
646 for (trans = data->__trans; trans != NULL; trans = trans->__next)
647 if (trans->__trans_context_fct != NULL)
648 DL_CALL_FCT (trans->__trans_context_fct,
649 (trans->__data, inptr, *inptrp, outstart, outbuf));
4d37e19c 650
0aece08d
UD
651 /* We finished one use of the loops. */
652 ++data->__invocation_counter;
653
390955cb 654 /* If this is the last step leave the loop, there is nothing
a784e502 655 we can do. */
a1ffb40e 656 if (__glibc_unlikely (data->__flags & __GCONV_IS_LAST))
8619129f
UD
657 {
658 /* Store information about how many bytes are available. */
d64b6ad0 659 data->__outbuf = outbuf;
bf979eef 660
38677ace 661 /* Remember how many non-identical characters we
c0c3f78a 662 converted in an irreversible way. */
38677ace 663 *irreversible += lirreversible;
bf979eef 664
8619129f
UD
665 break;
666 }
667
668 /* Write out all output which was produced. */
a1ffb40e 669 if (__glibc_likely (outbuf > outstart))
8619129f 670 {
d64b6ad0 671 const unsigned char *outerr = data->__outbuf;
8619129f
UD
672 int result;
673
4bae5567 674 result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
f1d5c60d 675 outbuf, NULL, irreversible, 0,
fd1b5c0f 676 consume_incomplete));
8619129f 677
d64b6ad0 678 if (result != __GCONV_EMPTY_INPUT)
8619129f 679 {
a1ffb40e 680 if (__glibc_unlikely (outerr != outbuf))
8619129f
UD
681 {
682#ifdef RESET_INPUT_BUFFER
683 RESET_INPUT_BUFFER;
684#else
dbc92dc6
UD
685 /* We have a problem in one of the functions below.
686 Undo the conversion upto the error point. */
8619129f
UD
687 size_t nstatus;
688
689 /* Reload the pointers. */
fd1b5c0f 690 *inptrp = inptr;
b117f744 691 outbuf = outstart;
8619129f 692
2373b30e 693 /* Restore the state. */
8619129f
UD
694# ifdef SAVE_RESET_STATE
695 SAVE_RESET_STATE (0);
696# endif
697
a1ffb40e 698 if (__glibc_likely (!unaligned))
f6ad4726
UD
699 {
700 if (FROM_DIRECTION)
701 /* Run the conversion loop. */
702 nstatus = FROM_LOOP (step, data, inptrp, inend,
17427edd 703 &outbuf, outerr,
f6ad4726
UD
704 lirreversiblep
705 EXTRA_LOOP_ARGS);
706 else
707 /* Run the conversion loop. */
708 nstatus = TO_LOOP (step, data, inptrp, inend,
17427edd 709 &outbuf, outerr,
f6ad4726
UD
710 lirreversiblep
711 EXTRA_LOOP_ARGS);
712 }
faaa6f62 713# if POSSIBLY_UNALIGNED
8619129f 714 else
f6ad4726
UD
715 {
716 if (FROM_DIRECTION)
717 /* Run the conversion loop. */
718 nstatus = GEN_unaligned (FROM_LOOP) (step, data,
719 inptrp, inend,
d1700665 720 &outbuf,
17427edd 721 outerr,
f6ad4726
UD
722 lirreversiblep
723 EXTRA_LOOP_ARGS);
724 else
725 /* Run the conversion loop. */
726 nstatus = GEN_unaligned (TO_LOOP) (step, data,
727 inptrp, inend,
17427edd 728 &outbuf, outerr,
f6ad4726
UD
729 lirreversiblep
730 EXTRA_LOOP_ARGS);
731 }
732# endif
8619129f
UD
733
734 /* We must run out of output buffer space in this
735 rerun. */
5aa8ff62 736 assert (outbuf == outerr);
d64b6ad0 737 assert (nstatus == __GCONV_FULL_OUTPUT);
0aece08d
UD
738
739 /* If we haven't consumed a single byte decrement
740 the invocation counter. */
a1ffb40e 741 if (__glibc_unlikely (outbuf == outstart))
0aece08d 742 --data->__invocation_counter;
8619129f
UD
743#endif /* reset input buffer */
744 }
745
746 /* Change the status. */
747 status = result;
748 }
749 else
750 /* All the output is consumed, we can make another run
751 if everything was ok. */
d64b6ad0 752 if (status == __GCONV_FULL_OUTPUT)
f1813b56
UD
753 {
754 status = __GCONV_OK;
755 outbuf = data->__outbuf;
756 }
8619129f 757 }
f1813b56
UD
758
759 if (status != __GCONV_OK)
760 break;
761
762 /* Reset the output buffer pointer for the next round. */
763 outbuf = data->__outbuf;
8619129f 764 }
8619129f 765
918b9d72
UD
766#ifdef END_LOOP
767 END_LOOP
768#endif
fd1b5c0f
UD
769
770 /* If we are supposed to consume all character store now all of the
771 remaining characters in the `state' object. */
faaa6f62
UD
772#if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
773 if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
774 || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
775 || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
365afefc
UD
776 && __builtin_expect (consume_incomplete, 0)
777 && status == __GCONV_INCOMPLETE_INPUT)
fd1b5c0f
UD
778 {
779# ifdef STORE_REST
780 mbstate_t *state = data->__statep;
781
782 STORE_REST
783# else
fd1b5c0f 784 /* Make sure the remaining bytes fit into the state objects
a784e502 785 buffer. */
fd1b5c0f
UD
786 assert (inend - *inptrp < 4);
787
dbc92dc6 788 size_t cnt;
fd1b5c0f
UD
789 for (cnt = 0; *inptrp < inend; ++cnt)
790 data->__statep->__value.__wchb[cnt] = *(*inptrp)++;
791 data->__statep->__count &= ~7;
792 data->__statep->__count |= cnt;
793# endif
794 }
795#endif
faaa6f62
UD
796#undef unaligned
797#undef POSSIBLY_UNALIGNED
8619129f
UD
798 }
799
800 return status;
801}
802
803#undef DEFINE_INIT
804#undef CHARSET_NAME
805#undef DEFINE_FINI
806#undef MIN_NEEDED_FROM
807#undef MIN_NEEDED_TO
808#undef MAX_NEEDED_FROM
809#undef MAX_NEEDED_TO
faaa6f62
UD
810#undef FROM_LOOP_MIN_NEEDED_FROM
811#undef FROM_LOOP_MAX_NEEDED_FROM
812#undef FROM_LOOP_MIN_NEEDED_TO
813#undef FROM_LOOP_MAX_NEEDED_TO
814#undef TO_LOOP_MIN_NEEDED_FROM
815#undef TO_LOOP_MAX_NEEDED_FROM
816#undef TO_LOOP_MIN_NEEDED_TO
817#undef TO_LOOP_MAX_NEEDED_TO
8619129f
UD
818#undef FROM_DIRECTION
819#undef EMIT_SHIFT_TO_INIT
820#undef FROM_LOOP
821#undef TO_LOOP
f9ad060c 822#undef ONE_DIRECTION
f6ad4726 823#undef SAVE_RESET_STATE
8619129f
UD
824#undef RESET_INPUT_BUFFER
825#undef FUNCTION_NAME
918b9d72
UD
826#undef PREPARE_LOOP
827#undef END_LOOP
f9ad060c 828#undef EXTRA_LOOP_ARGS
fd1b5c0f 829#undef STORE_REST
f9ad060c 830#undef FROM_ONEBYTE