1 /* Conversion loop frame work.
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 provides a frame for the reader loop in all conversion modules.
20 The actual code must (of course) be provided in the actual module source
21 code but certain actions can be written down generically, with some
22 customization options which are these:
24 MIN_NEEDED_INPUT minimal number of input bytes needed for the next
26 MIN_NEEDED_OUTPUT minimal number of bytes produced by the next round
29 MAX_NEEDED_INPUT you guess it, this is the maximal number of input
30 bytes needed. It defaults to MIN_NEEDED_INPUT
31 MAX_NEEDED_OUTPUT likewise for output bytes.
33 LOOPFCT name of the function created. If not specified
34 the name is `loop' but this prevents the use
35 of multiple functions in the same file.
37 BODY this is supposed to expand to the body of the loop.
38 The user must provide this.
40 EXTRA_LOOP_DECLS extra arguments passed from conversion loop call.
42 INIT_PARAMS code to define and initialize variables from params.
43 UPDATE_PARAMS code to store result in params.
45 ONEBYTE_BODY body of the specialized conversion function for a
46 single byte from the current character set to INTERNAL.
51 #include <iconv/gconv_int.h>
55 #include <sys/param.h> /* For MIN. */
58 #include <libc-diag.h>
60 /* We have to provide support for machines which are not able to handled
61 unaligned memory accesses. Some of the character encodings have
62 representations with a fixed width of 2 or 4 bytes. But if we cannot
63 access unaligned memory we still have to read byte-wise. */
65 #if _STRING_ARCH_unaligned || !defined DEFINE_UNALIGNED
66 /* We can handle unaligned memory access. */
67 # define get16(addr) *((const uint16_t *) (addr))
68 # define get32(addr) *((const uint32_t *) (addr))
70 /* We need no special support for writing values either. */
71 # define put16(addr, val) *((uint16_t *) (addr)) = (val)
72 # define put32(addr, val) *((uint32_t *) (addr)) = (val)
74 # define FCTNAME2(name) name
76 /* Distinguish between big endian and little endian. */
77 # if __BYTE_ORDER == __LITTLE_ENDIAN
78 # define get16(addr) \
79 (((const unsigned char *) (addr))[1] << 8 \
80 | ((const unsigned char *) (addr))[0])
81 # define get32(addr) \
82 (((((const unsigned char *) (addr))[3] << 8 \
83 | ((const unsigned char *) (addr))[2]) << 8 \
84 | ((const unsigned char *) (addr))[1]) << 8 \
85 | ((const unsigned char *) (addr))[0])
87 # define put16(addr, val) \
88 ({ uint16_t __val = (val); \
89 ((unsigned char *) (addr))[0] = __val; \
90 ((unsigned char *) (addr))[1] = __val >> 8; \
92 # define put32(addr, val) \
93 ({ uint32_t __val = (val); \
94 ((unsigned char *) (addr))[0] = __val; \
96 ((unsigned char *) (addr))[1] = __val; \
98 ((unsigned char *) (addr))[2] = __val; \
100 ((unsigned char *) (addr))[3] = __val; \
103 # define get16(addr) \
104 (((const unsigned char *) (addr))[0] << 8 \
105 | ((const unsigned char *) (addr))[1])
106 # define get32(addr) \
107 (((((const unsigned char *) (addr))[0] << 8 \
108 | ((const unsigned char *) (addr))[1]) << 8 \
109 | ((const unsigned char *) (addr))[2]) << 8 \
110 | ((const unsigned char *) (addr))[3])
112 # define put16(addr, val) \
113 ({ uint16_t __val = (val); \
114 ((unsigned char *) (addr))[1] = __val; \
115 ((unsigned char *) (addr))[0] = __val >> 8; \
117 # define put32(addr, val) \
118 ({ uint32_t __val = (val); \
119 ((unsigned char *) (addr))[3] = __val; \
121 ((unsigned char *) (addr))[2] = __val; \
123 ((unsigned char *) (addr))[1] = __val; \
125 ((unsigned char *) (addr))[0] = __val; \
129 # define FCTNAME2(name) name##_unaligned
131 #define FCTNAME(name) FCTNAME2(name)
134 /* We need at least one byte for the next round. */
135 #ifndef MIN_NEEDED_INPUT
136 # error "MIN_NEEDED_INPUT definition missing"
137 #elif MIN_NEEDED_INPUT < 1
138 # error "MIN_NEEDED_INPUT must be >= 1"
141 /* Let's see how many bytes we produce. */
142 #ifndef MAX_NEEDED_INPUT
143 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
146 /* We produce at least one byte in the next round. */
147 #ifndef MIN_NEEDED_OUTPUT
148 # error "MIN_NEEDED_OUTPUT definition missing"
149 #elif MIN_NEEDED_OUTPUT < 1
150 # error "MIN_NEEDED_OUTPUT must be >= 1"
153 /* Let's see how many bytes we produce. */
154 #ifndef MAX_NEEDED_OUTPUT
155 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
158 /* Default name for the function. */
160 # define LOOPFCT loop
163 /* Make sure we have a loop body. */
165 # error "Definition of BODY missing for function" LOOPFCT
169 /* If no arguments have to passed to the loop function define the macro
171 #ifndef EXTRA_LOOP_DECLS
172 # define EXTRA_LOOP_DECLS
175 /* Allow using UPDATE_PARAMS in macros where #ifdef UPDATE_PARAMS test
177 #ifndef UPDATE_PARAMS
178 # define UPDATE_PARAMS do { } while (0)
180 #ifndef REINIT_PARAMS
181 # define REINIT_PARAMS do { } while (0)
185 /* To make it easier for the writers of the modules, we define a macro
186 to test whether we have to ignore errors. */
187 #define ignore_errors_p() \
188 (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
191 /* Error handling for the FROM_LOOP direction, with ignoring of errors.
192 Note that we cannot use the do while (0) trick since `break' and
193 `continue' must reach certain points. */
194 #define STANDARD_FROM_LOOP_ERR_HANDLER(Incr) \
196 result = __GCONV_ILLEGAL_INPUT; \
198 if (! ignore_errors_p ()) \
201 /* We ignore the invalid input byte sequence. */ \
204 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
205 that "iconv -c" must give the same exitcode as "iconv". */ \
209 /* Error handling for the TO_LOOP direction, with use of transliteration/
210 transcription functions and ignoring of errors. Note that we cannot use
211 the do while (0) trick since `break' and `continue' must reach certain
213 #define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
215 result = __GCONV_ILLEGAL_INPUT; \
217 if (irreversible == NULL) \
218 /* This means we are in call from __gconv_transliterate. In this \
219 case we are not doing any error recovery outself. */ \
222 /* If needed, flush any conversion state, so that __gconv_transliterate \
223 starts with current shift state. */ \
226 /* First try the transliteration methods. */ \
227 if ((step_data->__flags & __GCONV_TRANSLIT) != 0) \
228 result = __gconv_transliterate \
229 (step, step_data, *inptrp, \
230 &inptr, inend, &outptr, irreversible); \
234 /* If any of them recognized the input continue with the loop. */ \
235 if (result != __GCONV_ILLEGAL_INPUT) \
237 if (__glibc_unlikely (result == __GCONV_FULL_OUTPUT)) \
243 /* Next see whether we have to ignore the error. If not, stop. */ \
244 if (! ignore_errors_p ()) \
247 /* When we come here it means we ignore the character. */ \
250 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
251 that "iconv -c" must give the same exitcode as "iconv". */ \
256 /* With GCC 7 when compiling with -Os for 32-bit s390 the compiler
257 warns that the variable 'ch', in the definition of BODY in
258 sysdeps/s390/multiarch/8bit-generic.c, may be used uninitialized in
259 the call to UNICODE_TAG_HANDLER in that macro. This variable is
260 actually always initialized before use, in the prior loop if INDEX
261 is nonzero and in the following 'if' if INDEX is zero. That code
262 has a comment referencing this diagnostic disabling; updates in one
263 place may require updates in the other. */
264 DIAG_PUSH_NEEDS_COMMENT
;
265 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
266 /* Handling of Unicode 3.1 TAG characters. Unicode recommends
267 "If language codes are not relevant to the particular processing
268 operation, then they should be ignored." This macro is usually
269 called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
270 #define UNICODE_TAG_HANDLER(Character, Incr) \
272 /* TAG characters are those in the range U+E0000..U+E007F. */ \
273 if (((Character) >> 7) == (0xe0000 >> 7)) \
279 DIAG_POP_NEEDS_COMMENT
;
282 /* The function returns the status, as defined in gconv.h. */
284 __attribute ((always_inline
))
285 FCTNAME (LOOPFCT
) (struct __gconv_step
*step
,
286 struct __gconv_step_data
*step_data
,
287 const unsigned char **inptrp
, const unsigned char *inend
,
288 unsigned char **outptrp
, const unsigned char *outend
,
289 size_t *irreversible EXTRA_LOOP_DECLS
)
291 #ifdef LOOP_NEED_STATE
292 mbstate_t *state
= step_data
->__statep
;
294 #ifdef LOOP_NEED_FLAGS
295 int flags
= step_data
->__flags
;
297 #ifdef LOOP_NEED_DATA
298 void *data
= step
->__data
;
300 int result
= __GCONV_EMPTY_INPUT
;
301 const unsigned char *inptr
= *inptrp
;
302 unsigned char *outptr
= *outptrp
;
308 while (inptr
!= inend
)
310 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
311 compiler generating better code. They will be optimized away
312 since MIN_NEEDED_OUTPUT is always a constant. */
313 if (MIN_NEEDED_INPUT
> 1
314 && __builtin_expect (inptr
+ MIN_NEEDED_INPUT
> inend
, 0))
316 /* We don't have enough input for another complete input
318 result
= __GCONV_INCOMPLETE_INPUT
;
321 if ((MIN_NEEDED_OUTPUT
!= 1
322 && __builtin_expect (outptr
+ MIN_NEEDED_OUTPUT
> outend
, 0))
323 || (MIN_NEEDED_OUTPUT
== 1
324 && __builtin_expect (outptr
>= outend
, 0)))
326 /* Overflow in the output buffer. */
327 result
= __GCONV_FULL_OUTPUT
;
331 /* Here comes the body the user provides. It can stop with
332 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
333 input characters vary in size), GCONV_ILLEGAL_INPUT, or
334 GCONV_FULL_OUTPUT (if the output characters vary in size). */
338 /* Update the pointers pointed to by the parameters. */
347 /* Include the file a second time to define the function to handle
349 #if !defined DEFINE_UNALIGNED && !_STRING_ARCH_unaligned \
350 && MIN_NEEDED_INPUT != 1 && MAX_NEEDED_INPUT % MIN_NEEDED_INPUT == 0 \
351 && MIN_NEEDED_OUTPUT != 1 && MAX_NEEDED_OUTPUT % MIN_NEEDED_OUTPUT == 0
358 # define DEFINE_UNALIGNED
360 # undef DEFINE_UNALIGNED
362 # if MAX_NEEDED_INPUT > 1
363 # define SINGLE(fct) SINGLE2 (fct)
364 # define SINGLE2(fct) fct##_single
366 __attribute ((always_inline
))
367 SINGLE(LOOPFCT
) (struct __gconv_step
*step
,
368 struct __gconv_step_data
*step_data
,
369 const unsigned char **inptrp
, const unsigned char *inend
,
370 unsigned char **outptrp
, unsigned char *outend
,
371 size_t *irreversible EXTRA_LOOP_DECLS
)
373 mbstate_t *state
= step_data
->__statep
;
374 # ifdef LOOP_NEED_FLAGS
375 int flags
= step_data
->__flags
;
377 # ifdef LOOP_NEED_DATA
378 void *data
= step
->__data
;
380 int result
= __GCONV_OK
;
381 unsigned char bytebuf
[MAX_NEEDED_INPUT
];
382 const unsigned char *inptr
= *inptrp
;
383 unsigned char *outptr
= *outptrp
;
393 /* Add the bytes from the state to the input buffer. */
394 assert ((state
->__count
& 7) <= sizeof (state
->__value
));
395 for (inlen
= 0; inlen
< (size_t) (state
->__count
& 7); ++inlen
)
396 bytebuf
[inlen
] = state
->__value
.__wchb
[inlen
];
399 /* Are there enough bytes in the input buffer? */
400 if (MIN_NEEDED_INPUT
> 1
401 && __builtin_expect (inptr
+ (MIN_NEEDED_INPUT
- inlen
) > inend
, 0))
406 /* Building with -O3 GCC emits a `array subscript is above array
407 bounds' warning. GCC BZ #64739 has been opened for this. */
408 DIAG_PUSH_NEEDS_COMMENT
;
409 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Warray-bounds");
410 while (inptr
< inend
)
411 bytebuf
[inlen
++] = *inptr
++;
412 DIAG_POP_NEEDS_COMMENT
;
416 inend
= &bytebuf
[inlen
];
420 /* We don't have enough input for another complete input
422 size_t inlen_after
= inlen
+ (inend
- inptr
);
423 assert (inlen_after
<= sizeof (state
->__value
.__wchb
));
424 for (; inlen
< inlen_after
; inlen
++)
425 state
->__value
.__wchb
[inlen
] = *inptr
++;
428 return __GCONV_INCOMPLETE_INPUT
;
431 /* Enough space in output buffer. */
432 if ((MIN_NEEDED_OUTPUT
!= 1 && outptr
+ MIN_NEEDED_OUTPUT
> outend
)
433 || (MIN_NEEDED_OUTPUT
== 1 && outptr
>= outend
))
434 /* Overflow in the output buffer. */
435 return __GCONV_FULL_OUTPUT
;
437 /* Now add characters from the normal input buffer. */
438 if (inlen
>= MAX_NEEDED_INPUT
)
439 /* Avoid a -Wstringop-overflow= warning when this loop is
440 unrolled. The compiler cannot otherwise see that this is
441 unreachable because it depends on (state->__count & 7) not
442 being too large after a previous conversion step. */
443 __builtin_unreachable ();
445 bytebuf
[inlen
++] = *inptr
++;
446 while (inlen
< MAX_NEEDED_INPUT
&& inptr
< inend
);
449 inend
= &bytebuf
[inlen
];
457 /* Now we either have produced an output character and consumed all the
458 bytes from the state and at least one more, or the character is still
459 incomplete, or we have some other error (like illegal input character,
460 no space in output buffer). */
461 if (__glibc_likely (inptr
!= bytebuf
))
463 /* We found a new character. */
464 assert (inptr
- bytebuf
> (state
->__count
& 7));
466 *inptrp
+= inptr
- bytebuf
- (state
->__count
& 7);
471 /* Clear the state buffer. */
475 state
->__count
&= ~7;
478 else if (result
== __GCONV_INCOMPLETE_INPUT
)
480 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
482 assert (inend
!= &bytebuf
[MAX_NEEDED_INPUT
]);
484 *inptrp
+= inend
- bytebuf
- (state
->__count
& 7);
490 /* We don't have enough input for another complete input
492 assert (inend
- inptr
> (state
->__count
& ~7));
493 assert (inend
- inptr
<= sizeof (state
->__value
.__wchb
));
494 state
->__count
= (state
->__count
& ~7) | (inend
- inptr
);
495 for (inlen
= 0; inlen
< inend
- inptr
; inlen
++)
496 state
->__value
.__wchb
[inlen
] = inptr
[inlen
];
509 /* Define the shortcut function for btowc. */
511 gconv_btowc (struct __gconv_step
*step
, unsigned char c
)
513 # define FROM_ONEBYTE gconv_btowc
518 /* We remove the macro definitions so that we can include this file again
519 for the definition of another function. */
520 #undef MIN_NEEDED_INPUT
521 #undef MAX_NEEDED_INPUT
522 #undef MIN_NEEDED_OUTPUT
523 #undef MAX_NEEDED_OUTPUT
527 #undef EXTRA_LOOP_DECLS
534 #undef LOOP_NEED_STATE
535 #undef LOOP_NEED_FLAGS
536 #undef LOOP_NEED_DATA