1 /* Conversion loop frame work.
2 Copyright (C) 1998-2020 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 <https://www.gnu.org/licenses/>. */
20 /* This file provides a frame for the reader loop in all conversion modules.
21 The actual code must (of course) be provided in the actual module source
22 code but certain actions can be written down generically, with some
23 customization options which are these:
25 MIN_NEEDED_INPUT minimal number of input bytes needed for the next
27 MIN_NEEDED_OUTPUT minimal number of bytes produced by the next round
30 MAX_NEEDED_INPUT you guess it, this is the maximal number of input
31 bytes needed. It defaults to MIN_NEEDED_INPUT
32 MAX_NEEDED_OUTPUT likewise for output bytes.
34 LOOPFCT name of the function created. If not specified
35 the name is `loop' but this prevents the use
36 of multiple functions in the same file.
38 BODY this is supposed to expand to the body of the loop.
39 The user must provide this.
41 EXTRA_LOOP_DECLS extra arguments passed from conversion loop call.
43 INIT_PARAMS code to define and initialize variables from params.
44 UPDATE_PARAMS code to store result in params.
46 ONEBYTE_BODY body of the specialized conversion function for a
47 single byte from the current character set to INTERNAL.
52 #include <iconv/gconv_int.h>
56 #include <sys/param.h> /* For MIN. */
59 #include <libc-diag.h>
61 /* We have to provide support for machines which are not able to handled
62 unaligned memory accesses. Some of the character encodings have
63 representations with a fixed width of 2 or 4 bytes. But if we cannot
64 access unaligned memory we still have to read byte-wise. */
66 #if _STRING_ARCH_unaligned || !defined DEFINE_UNALIGNED
67 /* We can handle unaligned memory access. */
68 # define get16(addr) *((const uint16_t *) (addr))
69 # define get32(addr) *((const uint32_t *) (addr))
71 /* We need no special support for writing values either. */
72 # define put16(addr, val) *((uint16_t *) (addr)) = (val)
73 # define put32(addr, val) *((uint32_t *) (addr)) = (val)
75 # define FCTNAME2(name) name
77 /* Distinguish between big endian and little endian. */
78 # if __BYTE_ORDER == __LITTLE_ENDIAN
79 # define get16(addr) \
80 (((const unsigned char *) (addr))[1] << 8 \
81 | ((const unsigned char *) (addr))[0])
82 # define get32(addr) \
83 (((((const unsigned char *) (addr))[3] << 8 \
84 | ((const unsigned char *) (addr))[2]) << 8 \
85 | ((const unsigned char *) (addr))[1]) << 8 \
86 | ((const unsigned char *) (addr))[0])
88 # define put16(addr, val) \
89 ({ uint16_t __val = (val); \
90 ((unsigned char *) (addr))[0] = __val; \
91 ((unsigned char *) (addr))[1] = __val >> 8; \
93 # define put32(addr, val) \
94 ({ uint32_t __val = (val); \
95 ((unsigned char *) (addr))[0] = __val; \
97 ((unsigned char *) (addr))[1] = __val; \
99 ((unsigned char *) (addr))[2] = __val; \
101 ((unsigned char *) (addr))[3] = __val; \
104 # define get16(addr) \
105 (((const unsigned char *) (addr))[0] << 8 \
106 | ((const unsigned char *) (addr))[1])
107 # define get32(addr) \
108 (((((const unsigned char *) (addr))[0] << 8 \
109 | ((const unsigned char *) (addr))[1]) << 8 \
110 | ((const unsigned char *) (addr))[2]) << 8 \
111 | ((const unsigned char *) (addr))[3])
113 # define put16(addr, val) \
114 ({ uint16_t __val = (val); \
115 ((unsigned char *) (addr))[1] = __val; \
116 ((unsigned char *) (addr))[0] = __val >> 8; \
118 # define put32(addr, val) \
119 ({ uint32_t __val = (val); \
120 ((unsigned char *) (addr))[3] = __val; \
122 ((unsigned char *) (addr))[2] = __val; \
124 ((unsigned char *) (addr))[1] = __val; \
126 ((unsigned char *) (addr))[0] = __val; \
130 # define FCTNAME2(name) name##_unaligned
132 #define FCTNAME(name) FCTNAME2(name)
135 /* We need at least one byte for the next round. */
136 #ifndef MIN_NEEDED_INPUT
137 # error "MIN_NEEDED_INPUT definition missing"
138 #elif MIN_NEEDED_INPUT < 1
139 # error "MIN_NEEDED_INPUT must be >= 1"
142 /* Let's see how many bytes we produce. */
143 #ifndef MAX_NEEDED_INPUT
144 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
147 /* We produce at least one byte in the next round. */
148 #ifndef MIN_NEEDED_OUTPUT
149 # error "MIN_NEEDED_OUTPUT definition missing"
150 #elif MIN_NEEDED_OUTPUT < 1
151 # error "MIN_NEEDED_OUTPUT must be >= 1"
154 /* Let's see how many bytes we produce. */
155 #ifndef MAX_NEEDED_OUTPUT
156 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
159 /* Default name for the function. */
161 # define LOOPFCT loop
164 /* Make sure we have a loop body. */
166 # error "Definition of BODY missing for function" LOOPFCT
170 /* If no arguments have to passed to the loop function define the macro
172 #ifndef EXTRA_LOOP_DECLS
173 # define EXTRA_LOOP_DECLS
176 /* Allow using UPDATE_PARAMS in macros where #ifdef UPDATE_PARAMS test
178 #ifndef UPDATE_PARAMS
179 # define UPDATE_PARAMS do { } while (0)
181 #ifndef REINIT_PARAMS
182 # define REINIT_PARAMS do { } while (0)
186 /* To make it easier for the writers of the modules, we define a macro
187 to test whether we have to ignore errors. */
188 #define ignore_errors_p() \
189 (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
192 /* Error handling for the FROM_LOOP direction, with ignoring of errors.
193 Note that we cannot use the do while (0) trick since `break' and
194 `continue' must reach certain points. */
195 #define STANDARD_FROM_LOOP_ERR_HANDLER(Incr) \
197 result = __GCONV_ILLEGAL_INPUT; \
199 if (! ignore_errors_p ()) \
202 /* We ignore the invalid input byte sequence. */ \
205 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
206 that "iconv -c" must give the same exitcode as "iconv". */ \
210 /* Error handling for the TO_LOOP direction, with use of transliteration/
211 transcription functions and ignoring of errors. Note that we cannot use
212 the do while (0) trick since `break' and `continue' must reach certain
214 #define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
216 result = __GCONV_ILLEGAL_INPUT; \
218 if (irreversible == NULL) \
219 /* This means we are in call from __gconv_transliterate. In this \
220 case we are not doing any error recovery outself. */ \
223 /* If needed, flush any conversion state, so that __gconv_transliterate \
224 starts with current shift state. */ \
227 /* First try the transliteration methods. */ \
228 if ((step_data->__flags & __GCONV_TRANSLIT) != 0) \
229 result = __gconv_transliterate \
230 (step, step_data, *inptrp, \
231 &inptr, inend, &outptr, irreversible); \
235 /* If any of them recognized the input continue with the loop. */ \
236 if (result != __GCONV_ILLEGAL_INPUT) \
238 if (__glibc_unlikely (result == __GCONV_FULL_OUTPUT)) \
244 /* Next see whether we have to ignore the error. If not, stop. */ \
245 if (! ignore_errors_p ()) \
248 /* When we come here it means we ignore the character. */ \
251 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
252 that "iconv -c" must give the same exitcode as "iconv". */ \
257 /* With GCC 7 when compiling with -Os for 32-bit s390 the compiler
258 warns that the variable 'ch', in the definition of BODY in
259 sysdeps/s390/multiarch/8bit-generic.c, may be used uninitialized in
260 the call to UNICODE_TAG_HANDLER in that macro. This variable is
261 actually always initialized before use, in the prior loop if INDEX
262 is nonzero and in the following 'if' if INDEX is zero. That code
263 has a comment referencing this diagnostic disabling; updates in one
264 place may require updates in the other. */
265 DIAG_PUSH_NEEDS_COMMENT
;
266 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
267 /* Handling of Unicode 3.1 TAG characters. Unicode recommends
268 "If language codes are not relevant to the particular processing
269 operation, then they should be ignored." This macro is usually
270 called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
271 #define UNICODE_TAG_HANDLER(Character, Incr) \
273 /* TAG characters are those in the range U+E0000..U+E007F. */ \
274 if (((Character) >> 7) == (0xe0000 >> 7)) \
280 DIAG_POP_NEEDS_COMMENT
;
283 /* The function returns the status, as defined in gconv.h. */
285 __attribute ((always_inline
))
286 FCTNAME (LOOPFCT
) (struct __gconv_step
*step
,
287 struct __gconv_step_data
*step_data
,
288 const unsigned char **inptrp
, const unsigned char *inend
,
289 unsigned char **outptrp
, const unsigned char *outend
,
290 size_t *irreversible EXTRA_LOOP_DECLS
)
292 #ifdef LOOP_NEED_STATE
293 mbstate_t *state
= step_data
->__statep
;
295 #ifdef LOOP_NEED_FLAGS
296 int flags
= step_data
->__flags
;
298 #ifdef LOOP_NEED_DATA
299 void *data
= step
->__data
;
301 int result
= __GCONV_EMPTY_INPUT
;
302 const unsigned char *inptr
= *inptrp
;
303 unsigned char *outptr
= *outptrp
;
309 while (inptr
!= inend
)
311 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
312 compiler generating better code. They will be optimized away
313 since MIN_NEEDED_OUTPUT is always a constant. */
314 if (MIN_NEEDED_INPUT
> 1
315 && __builtin_expect (inptr
+ MIN_NEEDED_INPUT
> inend
, 0))
317 /* We don't have enough input for another complete input
319 result
= __GCONV_INCOMPLETE_INPUT
;
322 if ((MIN_NEEDED_OUTPUT
!= 1
323 && __builtin_expect (outptr
+ MIN_NEEDED_OUTPUT
> outend
, 0))
324 || (MIN_NEEDED_OUTPUT
== 1
325 && __builtin_expect (outptr
>= outend
, 0)))
327 /* Overflow in the output buffer. */
328 result
= __GCONV_FULL_OUTPUT
;
332 /* Here comes the body the user provides. It can stop with
333 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
334 input characters vary in size), GCONV_ILLEGAL_INPUT, or
335 GCONV_FULL_OUTPUT (if the output characters vary in size). */
339 /* Update the pointers pointed to by the parameters. */
348 /* Include the file a second time to define the function to handle
350 #if !defined DEFINE_UNALIGNED && !_STRING_ARCH_unaligned \
351 && MIN_NEEDED_INPUT != 1 && MAX_NEEDED_INPUT % MIN_NEEDED_INPUT == 0 \
352 && MIN_NEEDED_OUTPUT != 1 && MAX_NEEDED_OUTPUT % MIN_NEEDED_OUTPUT == 0
359 # define DEFINE_UNALIGNED
361 # undef DEFINE_UNALIGNED
363 # if MAX_NEEDED_INPUT > 1
364 # define SINGLE(fct) SINGLE2 (fct)
365 # define SINGLE2(fct) fct##_single
367 __attribute ((always_inline
))
368 SINGLE(LOOPFCT
) (struct __gconv_step
*step
,
369 struct __gconv_step_data
*step_data
,
370 const unsigned char **inptrp
, const unsigned char *inend
,
371 unsigned char **outptrp
, unsigned char *outend
,
372 size_t *irreversible EXTRA_LOOP_DECLS
)
374 mbstate_t *state
= step_data
->__statep
;
375 # ifdef LOOP_NEED_FLAGS
376 int flags
= step_data
->__flags
;
378 # ifdef LOOP_NEED_DATA
379 void *data
= step
->__data
;
381 int result
= __GCONV_OK
;
382 unsigned char bytebuf
[MAX_NEEDED_INPUT
];
383 const unsigned char *inptr
= *inptrp
;
384 unsigned char *outptr
= *outptrp
;
394 /* Add the bytes from the state to the input buffer. */
395 assert ((state
->__count
& 7) <= sizeof (state
->__value
));
396 for (inlen
= 0; inlen
< (size_t) (state
->__count
& 7); ++inlen
)
397 bytebuf
[inlen
] = state
->__value
.__wchb
[inlen
];
400 /* Are there enough bytes in the input buffer? */
401 if (MIN_NEEDED_INPUT
> 1
402 && __builtin_expect (inptr
+ (MIN_NEEDED_INPUT
- inlen
) > inend
, 0))
407 /* Building with -O3 GCC emits a `array subscript is above array
408 bounds' warning. GCC BZ #64739 has been opened for this. */
409 DIAG_PUSH_NEEDS_COMMENT
;
410 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Warray-bounds");
411 while (inptr
< inend
)
412 bytebuf
[inlen
++] = *inptr
++;
413 DIAG_POP_NEEDS_COMMENT
;
417 inend
= &bytebuf
[inlen
];
421 /* We don't have enough input for another complete input
423 size_t inlen_after
= inlen
+ (inend
- inptr
);
424 assert (inlen_after
<= sizeof (state
->__value
.__wchb
));
425 for (; inlen
< inlen_after
; inlen
++)
426 state
->__value
.__wchb
[inlen
] = *inptr
++;
429 return __GCONV_INCOMPLETE_INPUT
;
432 /* Enough space in output buffer. */
433 if ((MIN_NEEDED_OUTPUT
!= 1 && outptr
+ MIN_NEEDED_OUTPUT
> outend
)
434 || (MIN_NEEDED_OUTPUT
== 1 && outptr
>= outend
))
435 /* Overflow in the output buffer. */
436 return __GCONV_FULL_OUTPUT
;
438 /* Now add characters from the normal input buffer. */
440 bytebuf
[inlen
++] = *inptr
++;
441 while (inlen
< MAX_NEEDED_INPUT
&& inptr
< inend
);
444 inend
= &bytebuf
[inlen
];
452 /* Now we either have produced an output character and consumed all the
453 bytes from the state and at least one more, or the character is still
454 incomplete, or we have some other error (like illegal input character,
455 no space in output buffer). */
456 if (__glibc_likely (inptr
!= bytebuf
))
458 /* We found a new character. */
459 assert (inptr
- bytebuf
> (state
->__count
& 7));
461 *inptrp
+= inptr
- bytebuf
- (state
->__count
& 7);
466 /* Clear the state buffer. */
470 state
->__count
&= ~7;
473 else if (result
== __GCONV_INCOMPLETE_INPUT
)
475 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
477 assert (inend
!= &bytebuf
[MAX_NEEDED_INPUT
]);
479 *inptrp
+= inend
- bytebuf
- (state
->__count
& 7);
485 /* We don't have enough input for another complete input
487 assert (inend
- inptr
> (state
->__count
& ~7));
488 assert (inend
- inptr
<= sizeof (state
->__value
.__wchb
));
489 state
->__count
= (state
->__count
& ~7) | (inend
- inptr
);
490 for (inlen
= 0; inlen
< inend
- inptr
; inlen
++)
491 state
->__value
.__wchb
[inlen
] = inptr
[inlen
];
504 /* Define the shortcut function for btowc. */
506 gconv_btowc (struct __gconv_step
*step
, unsigned char c
)
508 # define FROM_ONEBYTE gconv_btowc
513 /* We remove the macro definitions so that we can include this file again
514 for the definition of another function. */
515 #undef MIN_NEEDED_INPUT
516 #undef MAX_NEEDED_INPUT
517 #undef MIN_NEEDED_OUTPUT
518 #undef MAX_NEEDED_OUTPUT
522 #undef EXTRA_LOOP_DECLS
529 #undef LOOP_NEED_STATE
530 #undef LOOP_NEED_FLAGS
531 #undef LOOP_NEED_DATA