]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/loop.c
alloc_buffer: Return unqualified pointer type in alloc_buffer_next
[thirdparty/glibc.git] / iconv / loop.c
CommitLineData
8619129f 1/* Conversion loop frame work.
04277e02 2 Copyright (C) 1998-2019 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 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:
24
25 MIN_NEEDED_INPUT minimal number of input bytes needed for the next
26 conversion.
27 MIN_NEEDED_OUTPUT minimal number of bytes produced by the next round
28 of conversion.
29
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.
33
8619129f
UD
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.
37
8619129f
UD
38 BODY this is supposed to expand to the body of the loop.
39 The user must provide this.
28f1c862 40
382466e0 41 EXTRA_LOOP_DECLS extra arguments passed from conversion loop call.
66175fa8
UD
42
43 INIT_PARAMS code to define and initialize variables from params.
44 UPDATE_PARAMS code to store result in params.
f9ad060c
UD
45
46 ONEBYTE_BODY body of the specialized conversion function for a
47 single byte from the current character set to INTERNAL.
8619129f
UD
48*/
49
fd1b5c0f 50#include <assert.h>
b35e58e4 51#include <endian.h>
8619129f 52#include <gconv.h>
b35e58e4
UD
53#include <stdint.h>
54#include <string.h>
d64b6ad0 55#include <wchar.h>
8619129f
UD
56#include <sys/param.h> /* For MIN. */
57#define __need_size_t
58#include <stddef.h>
9090848d 59#include <libc-diag.h>
8619129f 60
b35e58e4
UD
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. */
65#undef FCTNAME2
27822ce6 66#if _STRING_ARCH_unaligned || !defined DEFINE_UNALIGNED
b35e58e4 67/* We can handle unaligned memory access. */
a784e502
UD
68# define get16(addr) *((const uint16_t *) (addr))
69# define get32(addr) *((const uint32_t *) (addr))
b35e58e4
UD
70
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)
74
75# define FCTNAME2(name) name
76#else
77/* Distinguish between big endian and little endian. */
78# if __BYTE_ORDER == __LITTLE_ENDIAN
79# define get16(addr) \
a784e502
UD
80 (((const unsigned char *) (addr))[1] << 8 \
81 | ((const unsigned char *) (addr))[0])
b35e58e4 82# define get32(addr) \
a784e502
UD
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])
b35e58e4 87
cb2c5501 88# define put16(addr, val) \
b35e58e4 89 ({ uint16_t __val = (val); \
cb2c5501
UD
90 ((unsigned char *) (addr))[0] = __val; \
91 ((unsigned char *) (addr))[1] = __val >> 8; \
b35e58e4 92 (void) 0; })
cb2c5501
UD
93# define put32(addr, val) \
94 ({ uint32_t __val = (val); \
95 ((unsigned char *) (addr))[0] = __val; \
b35e58e4 96 __val >>= 8; \
cb2c5501 97 ((unsigned char *) (addr))[1] = __val; \
b35e58e4 98 __val >>= 8; \
cb2c5501 99 ((unsigned char *) (addr))[2] = __val; \
b35e58e4 100 __val >>= 8; \
cb2c5501 101 ((unsigned char *) (addr))[3] = __val; \
b35e58e4
UD
102 (void) 0; })
103# else
104# define get16(addr) \
a784e502
UD
105 (((const unsigned char *) (addr))[0] << 8 \
106 | ((const unsigned char *) (addr))[1])
b35e58e4 107# define get32(addr) \
a784e502
UD
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])
b35e58e4 112
cb2c5501 113# define put16(addr, val) \
b35e58e4 114 ({ uint16_t __val = (val); \
cb2c5501 115 ((unsigned char *) (addr))[1] = __val; \
f7ccf2fc 116 ((unsigned char *) (addr))[0] = __val >> 8; \
b35e58e4 117 (void) 0; })
cb2c5501
UD
118# define put32(addr, val) \
119 ({ uint32_t __val = (val); \
120 ((unsigned char *) (addr))[3] = __val; \
b35e58e4 121 __val >>= 8; \
cb2c5501 122 ((unsigned char *) (addr))[2] = __val; \
b35e58e4 123 __val >>= 8; \
cb2c5501 124 ((unsigned char *) (addr))[1] = __val; \
b35e58e4 125 __val >>= 8; \
cb2c5501 126 ((unsigned char *) (addr))[0] = __val; \
b35e58e4
UD
127 (void) 0; })
128# endif
129
130# define FCTNAME2(name) name##_unaligned
131#endif
132#define FCTNAME(name) FCTNAME2(name)
133
134
8619129f
UD
135/* We need at least one byte for the next round. */
136#ifndef MIN_NEEDED_INPUT
5aa8ff62 137# error "MIN_NEEDED_INPUT definition missing"
4a0de63b
UD
138#elif MIN_NEEDED_INPUT < 1
139# error "MIN_NEEDED_INPUT must be >= 1"
8619129f
UD
140#endif
141
142/* Let's see how many bytes we produce. */
143#ifndef MAX_NEEDED_INPUT
144# define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
145#endif
146
147/* We produce at least one byte in the next round. */
148#ifndef MIN_NEEDED_OUTPUT
5aa8ff62 149# error "MIN_NEEDED_OUTPUT definition missing"
c0a0f9a3
UD
150#elif MIN_NEEDED_OUTPUT < 1
151# error "MIN_NEEDED_OUTPUT must be >= 1"
8619129f
UD
152#endif
153
154/* Let's see how many bytes we produce. */
155#ifndef MAX_NEEDED_OUTPUT
156# define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
157#endif
158
159/* Default name for the function. */
160#ifndef LOOPFCT
161# define LOOPFCT loop
162#endif
163
164/* Make sure we have a loop body. */
165#ifndef BODY
166# error "Definition of BODY missing for function" LOOPFCT
167#endif
168
8619129f 169
28f1c862
UD
170/* If no arguments have to passed to the loop function define the macro
171 as empty. */
172#ifndef EXTRA_LOOP_DECLS
173# define EXTRA_LOOP_DECLS
174#endif
175
4b1b449d
UD
176/* Allow using UPDATE_PARAMS in macros where #ifdef UPDATE_PARAMS test
177 isn't possible. */
178#ifndef UPDATE_PARAMS
179# define UPDATE_PARAMS do { } while (0)
180#endif
181#ifndef REINIT_PARAMS
182# define REINIT_PARAMS do { } while (0)
183#endif
184
28f1c862 185
85830c4c
UD
186/* To make it easier for the writers of the modules, we define a macro
187 to test whether we have to ignore errors. */
b572c2da
UD
188#define ignore_errors_p() \
189 (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
85830c4c
UD
190
191
e438a468
UD
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) \
196 { \
197 result = __GCONV_ILLEGAL_INPUT; \
198 \
199 if (! ignore_errors_p ()) \
200 break; \
201 \
202 /* We ignore the invalid input byte sequence. */ \
203 inptr += (Incr); \
204 ++*irreversible; \
205 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
206 that "iconv -c" must give the same exitcode as "iconv". */ \
207 continue; \
208 }
209
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
213 points. */
214#define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
d6204268 215 { \
d6204268 216 result = __GCONV_ILLEGAL_INPUT; \
b572c2da
UD
217 \
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. */ \
221 break; \
222 \
4b1b449d
UD
223 /* If needed, flush any conversion state, so that __gconv_transliterate \
224 starts with current shift state. */ \
225 UPDATE_PARAMS; \
226 \
d6204268 227 /* First try the transliteration methods. */ \
ba7b4d29
FW
228 if ((step_data->__flags & __GCONV_TRANSLIT) != 0) \
229 result = __gconv_transliterate \
230 (step, step_data, *inptrp, \
231 &inptr, inend, &outptr, irreversible); \
4b1b449d
UD
232 \
233 REINIT_PARAMS; \
234 \
7888313d 235 /* If any of them recognized the input continue with the loop. */ \
d6204268 236 if (result != __GCONV_ILLEGAL_INPUT) \
f2a8406a 237 { \
a1ffb40e 238 if (__glibc_unlikely (result == __GCONV_FULL_OUTPUT)) \
f2a8406a
UD
239 break; \
240 \
241 continue; \
242 } \
d6204268
UD
243 \
244 /* Next see whether we have to ignore the error. If not, stop. */ \
245 if (! ignore_errors_p ()) \
246 break; \
b572c2da 247 \
d6204268
UD
248 /* When we come here it means we ignore the character. */ \
249 ++*irreversible; \
250 inptr += Incr; \
e438a468
UD
251 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
252 that "iconv -c" must give the same exitcode as "iconv". */ \
d6204268
UD
253 continue; \
254 }
255
256
6900d2ca
JM
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. */
265DIAG_PUSH_NEEDS_COMMENT;
266DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
9a1f6754
UD
267/* Handling of Unicode 3.1 TAG characters. Unicode recommends
268 "If language codes are not relevant to the particular processing
e438a468
UD
269 operation, then they should be ignored." This macro is usually
270 called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
9a1f6754
UD
271#define UNICODE_TAG_HANDLER(Character, Incr) \
272 { \
273 /* TAG characters are those in the range U+E0000..U+E007F. */ \
274 if (((Character) >> 7) == (0xe0000 >> 7)) \
275 { \
276 inptr += Incr; \
277 continue; \
278 } \
279 }
6900d2ca 280DIAG_POP_NEEDS_COMMENT;
9a1f6754
UD
281
282
8619129f
UD
283/* The function returns the status, as defined in gconv.h. */
284static inline int
dd9423a6 285__attribute ((always_inline))
55985355
UD
286FCTNAME (LOOPFCT) (struct __gconv_step *step,
287 struct __gconv_step_data *step_data,
288 const unsigned char **inptrp, const unsigned char *inend,
17427edd 289 unsigned char **outptrp, const unsigned char *outend,
38677ace 290 size_t *irreversible EXTRA_LOOP_DECLS)
8619129f 291{
55985355
UD
292#ifdef LOOP_NEED_STATE
293 mbstate_t *state = step_data->__statep;
294#endif
295#ifdef LOOP_NEED_FLAGS
296 int flags = step_data->__flags;
297#endif
298#ifdef LOOP_NEED_DATA
299 void *data = step->__data;
300#endif
301 int result = __GCONV_EMPTY_INPUT;
8619129f
UD
302 const unsigned char *inptr = *inptrp;
303 unsigned char *outptr = *outptrp;
8619129f 304
66175fa8
UD
305#ifdef INIT_PARAMS
306 INIT_PARAMS;
307#endif
308
55985355 309 while (inptr != inend)
8619129f 310 {
55985355 311 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
ca3c0135 312 compiler generating better code. They will be optimized away
55985355 313 since MIN_NEEDED_OUTPUT is always a constant. */
eb9dc2a2
UD
314 if (MIN_NEEDED_INPUT > 1
315 && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
316 {
317 /* We don't have enough input for another complete input
318 character. */
319 result = __GCONV_INCOMPLETE_INPUT;
320 break;
321 }
55985355
UD
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)))
326 {
327 /* Overflow in the output buffer. */
328 result = __GCONV_FULL_OUTPUT;
329 break;
330 }
55985355
UD
331
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). */
336 BODY
8619129f
UD
337 }
338
8619129f
UD
339 /* Update the pointers pointed to by the parameters. */
340 *inptrp = inptr;
341 *outptrp = outptr;
66175fa8 342 UPDATE_PARAMS;
8619129f
UD
343
344 return result;
345}
346
347
b02b4774
UD
348/* Include the file a second time to define the function to handle
349 unaligned access. */
27822ce6 350#if !defined DEFINE_UNALIGNED && !_STRING_ARCH_unaligned \
4a0de63b
UD
351 && MIN_NEEDED_INPUT != 1 && MAX_NEEDED_INPUT % MIN_NEEDED_INPUT == 0 \
352 && MIN_NEEDED_OUTPUT != 1 && MAX_NEEDED_OUTPUT % MIN_NEEDED_OUTPUT == 0
fd1b5c0f
UD
353# undef get16
354# undef get32
355# undef put16
356# undef put32
357# undef unaligned
358
b35e58e4
UD
359# define DEFINE_UNALIGNED
360# include "loop.c"
361# undef DEFINE_UNALIGNED
32bead5b
WN
362#else
363# if MAX_NEEDED_INPUT > 1
364# define SINGLE(fct) SINGLE2 (fct)
365# define SINGLE2(fct) fct##_single
fd1b5c0f 366static inline int
dd9423a6 367__attribute ((always_inline))
55985355
UD
368SINGLE(LOOPFCT) (struct __gconv_step *step,
369 struct __gconv_step_data *step_data,
370 const unsigned char **inptrp, const unsigned char *inend,
fd1b5c0f 371 unsigned char **outptrp, unsigned char *outend,
55985355 372 size_t *irreversible EXTRA_LOOP_DECLS)
fd1b5c0f 373{
55985355 374 mbstate_t *state = step_data->__statep;
32bead5b 375# ifdef LOOP_NEED_FLAGS
55985355 376 int flags = step_data->__flags;
32bead5b
WN
377# endif
378# ifdef LOOP_NEED_DATA
55985355 379 void *data = step->__data;
32bead5b 380# endif
fd1b5c0f
UD
381 int result = __GCONV_OK;
382 unsigned char bytebuf[MAX_NEEDED_INPUT];
383 const unsigned char *inptr = *inptrp;
384 unsigned char *outptr = *outptrp;
385 size_t inlen;
386
32bead5b 387# ifdef INIT_PARAMS
fd1b5c0f 388 INIT_PARAMS;
32bead5b 389# endif
fd1b5c0f 390
32bead5b 391# ifdef UNPACK_BYTES
fd1b5c0f 392 UNPACK_BYTES
32bead5b 393# else
fd1b5c0f 394 /* Add the bytes from the state to the input buffer. */
5e0d0300 395 assert ((state->__count & 7) <= sizeof (state->__value));
17427edd 396 for (inlen = 0; inlen < (size_t) (state->__count & 7); ++inlen)
fd1b5c0f 397 bytebuf[inlen] = state->__value.__wchb[inlen];
32bead5b 398# endif
fd1b5c0f
UD
399
400 /* Are there enough bytes in the input buffer? */
0656e90e
UD
401 if (MIN_NEEDED_INPUT > 1
402 && __builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
fd1b5c0f 403 {
fd1b5c0f 404 *inptrp = inend;
32bead5b 405# ifdef STORE_REST
5fe8e359
AK
406
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");
1af4e298
UD
411 while (inptr < inend)
412 bytebuf[inlen++] = *inptr++;
5fe8e359 413 DIAG_POP_NEEDS_COMMENT;
1af4e298 414
fd1b5c0f
UD
415 inptr = bytebuf;
416 inptrp = &inptr;
417 inend = &bytebuf[inlen];
418
419 STORE_REST
32bead5b 420# else
fd1b5c0f
UD
421 /* We don't have enough input for another complete input
422 character. */
423 while (inptr < inend)
424 state->__value.__wchb[inlen++] = *inptr++;
32bead5b 425# endif
fd1b5c0f
UD
426
427 return __GCONV_INCOMPLETE_INPUT;
428 }
429
430 /* Enough space in output buffer. */
431 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
432 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
433 /* Overflow in the output buffer. */
434 return __GCONV_FULL_OUTPUT;
435
436 /* Now add characters from the normal input buffer. */
437 do
438 bytebuf[inlen++] = *inptr++;
316518d6 439 while (inlen < MAX_NEEDED_INPUT && inptr < inend);
fd1b5c0f
UD
440
441 inptr = bytebuf;
316518d6 442 inend = &bytebuf[inlen];
55985355 443
fd1b5c0f
UD
444 do
445 {
446 BODY
447 }
448 while (0);
449
316518d6
UD
450 /* Now we either have produced an output character and consumed all the
451 bytes from the state and at least one more, or the character is still
452 incomplete, or we have some other error (like illegal input character,
453 no space in output buffer). */
a1ffb40e 454 if (__glibc_likely (inptr != bytebuf))
fd1b5c0f 455 {
316518d6 456 /* We found a new character. */
fd1b5c0f
UD
457 assert (inptr - bytebuf > (state->__count & 7));
458
459 *inptrp += inptr - bytebuf - (state->__count & 7);
460 *outptrp = outptr;
461
316518d6
UD
462 result = __GCONV_OK;
463
fd1b5c0f 464 /* Clear the state buffer. */
32bead5b 465# ifdef CLEAR_STATE
41f112ad 466 CLEAR_STATE;
32bead5b 467# else
fd1b5c0f 468 state->__count &= ~7;
32bead5b 469# endif
fd1b5c0f 470 }
316518d6
UD
471 else if (result == __GCONV_INCOMPLETE_INPUT)
472 {
473 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
474 available. */
475 assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
476
477 *inptrp += inend - bytebuf - (state->__count & 7);
32bead5b 478# ifdef STORE_REST
316518d6
UD
479 inptrp = &inptr;
480
481 STORE_REST
32bead5b 482# else
316518d6
UD
483 /* We don't have enough input for another complete input
484 character. */
5512d89b 485 assert (inend - inptr > (state->__count & ~7));
5e0d0300 486 assert (inend - inptr <= sizeof (state->__value));
5512d89b
UD
487 state->__count = (state->__count & ~7) | (inend - inptr);
488 inlen = 0;
316518d6
UD
489 while (inptr < inend)
490 state->__value.__wchb[inlen++] = *inptr++;
32bead5b 491# endif
316518d6 492 }
fd1b5c0f
UD
493
494 return result;
495}
32bead5b
WN
496# undef SINGLE
497# undef SINGLE2
498# endif
fd1b5c0f
UD
499
500
32bead5b 501# ifdef ONEBYTE_BODY
f9ad060c
UD
502/* Define the shortcut function for btowc. */
503static wint_t
504gconv_btowc (struct __gconv_step *step, unsigned char c)
505 ONEBYTE_BODY
32bead5b
WN
506# define FROM_ONEBYTE gconv_btowc
507# endif
f9ad060c 508
32bead5b 509#endif
f9ad060c 510
8619129f
UD
511/* We remove the macro definitions so that we can include this file again
512 for the definition of another function. */
513#undef MIN_NEEDED_INPUT
514#undef MAX_NEEDED_INPUT
515#undef MIN_NEEDED_OUTPUT
516#undef MAX_NEEDED_OUTPUT
517#undef LOOPFCT
8619129f
UD
518#undef BODY
519#undef LOOPFCT
28f1c862 520#undef EXTRA_LOOP_DECLS
66175fa8
UD
521#undef INIT_PARAMS
522#undef UPDATE_PARAMS
4b1b449d 523#undef REINIT_PARAMS
f9ad060c 524#undef ONEBYTE_BODY
55985355 525#undef UNPACK_BYTES
41f112ad 526#undef CLEAR_STATE
55985355
UD
527#undef LOOP_NEED_STATE
528#undef LOOP_NEED_FLAGS
529#undef LOOP_NEED_DATA
fd1b5c0f
UD
530#undef get16
531#undef get32
532#undef put16
533#undef put32
534#undef unaligned