Add sigstack handling to Linux ____longjmp_chk on powerpc.
[glibc.git] / iconv / loop.c
blobda11bc209d3755f0d1100d497545d1341603e676
1 /* Conversion loop frame work.
2 Copyright (C) 1998-2002, 2003, 2005, 2008 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* This file provides a frame for the reader loop in all conversion modules.
22 The actual code must (of course) be provided in the actual module source
23 code but certain actions can be written down generically, with some
24 customization options which are these:
26 MIN_NEEDED_INPUT minimal number of input bytes needed for the next
27 conversion.
28 MIN_NEEDED_OUTPUT minimal number of bytes produced by the next round
29 of conversion.
31 MAX_NEEDED_INPUT you guess it, this is the maximal number of input
32 bytes needed. It defaults to MIN_NEEDED_INPUT
33 MAX_NEEDED_OUTPUT likewise for output bytes.
35 LOOPFCT name of the function created. If not specified
36 the name is `loop' but this prevents the use
37 of multiple functions in the same file.
39 BODY this is supposed to expand to the body of the loop.
40 The user must provide this.
42 EXTRA_LOOP_DECLS extra arguments passed from converion loop call.
44 INIT_PARAMS code to define and initialize variables from params.
45 UPDATE_PARAMS code to store result in params.
47 ONEBYTE_BODY body of the specialized conversion function for a
48 single byte from the current character set to INTERNAL.
51 #include <assert.h>
52 #include <endian.h>
53 #include <gconv.h>
54 #include <stdint.h>
55 #include <string.h>
56 #include <wchar.h>
57 #include <sys/param.h> /* For MIN. */
58 #define __need_size_t
59 #include <stddef.h>
62 /* We have to provide support for machines which are not able to handled
63 unaligned memory accesses. Some of the character encodings have
64 representations with a fixed width of 2 or 4 bytes. But if we cannot
65 access unaligned memory we still have to read byte-wise. */
66 #undef FCTNAME2
67 #if defined _STRING_ARCH_unaligned || !defined DEFINE_UNALIGNED
68 /* We can handle unaligned memory access. */
69 # define get16(addr) *((__const uint16_t *) (addr))
70 # define get32(addr) *((__const uint32_t *) (addr))
72 /* We need no special support for writing values either. */
73 # define put16(addr, val) *((uint16_t *) (addr)) = (val)
74 # define put32(addr, val) *((uint32_t *) (addr)) = (val)
76 # define FCTNAME2(name) name
77 #else
78 /* Distinguish between big endian and little endian. */
79 # if __BYTE_ORDER == __LITTLE_ENDIAN
80 # define get16(addr) \
81 (((__const unsigned char *) (addr))[1] << 8 \
82 | ((__const unsigned char *) (addr))[0])
83 # define get32(addr) \
84 (((((__const unsigned char *) (addr))[3] << 8 \
85 | ((__const unsigned char *) (addr))[2]) << 8 \
86 | ((__const unsigned char *) (addr))[1]) << 8 \
87 | ((__const unsigned char *) (addr))[0])
89 # define put16(addr, val) \
90 ({ uint16_t __val = (val); \
91 ((unsigned char *) (addr))[0] = __val; \
92 ((unsigned char *) (addr))[1] = __val >> 8; \
93 (void) 0; })
94 # define put32(addr, val) \
95 ({ uint32_t __val = (val); \
96 ((unsigned char *) (addr))[0] = __val; \
97 __val >>= 8; \
98 ((unsigned char *) (addr))[1] = __val; \
99 __val >>= 8; \
100 ((unsigned char *) (addr))[2] = __val; \
101 __val >>= 8; \
102 ((unsigned char *) (addr))[3] = __val; \
103 (void) 0; })
104 # else
105 # define get16(addr) \
106 (((__const unsigned char *) (addr))[0] << 8 \
107 | ((__const unsigned char *) (addr))[1])
108 # define get32(addr) \
109 (((((__const unsigned char *) (addr))[0] << 8 \
110 | ((__const unsigned char *) (addr))[1]) << 8 \
111 | ((__const unsigned char *) (addr))[2]) << 8 \
112 | ((__const unsigned char *) (addr))[3])
114 # define put16(addr, val) \
115 ({ uint16_t __val = (val); \
116 ((unsigned char *) (addr))[1] = __val; \
117 ((unsigned char *) (addr))[0] = __val >> 8; \
118 (void) 0; })
119 # define put32(addr, val) \
120 ({ uint32_t __val = (val); \
121 ((unsigned char *) (addr))[3] = __val; \
122 __val >>= 8; \
123 ((unsigned char *) (addr))[2] = __val; \
124 __val >>= 8; \
125 ((unsigned char *) (addr))[1] = __val; \
126 __val >>= 8; \
127 ((unsigned char *) (addr))[0] = __val; \
128 (void) 0; })
129 # endif
131 # define FCTNAME2(name) name##_unaligned
132 #endif
133 #define FCTNAME(name) FCTNAME2(name)
136 /* We need at least one byte for the next round. */
137 #ifndef MIN_NEEDED_INPUT
138 # error "MIN_NEEDED_INPUT definition missing"
139 #elif MIN_NEEDED_INPUT < 1
140 # error "MIN_NEEDED_INPUT must be >= 1"
141 #endif
143 /* Let's see how many bytes we produce. */
144 #ifndef MAX_NEEDED_INPUT
145 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
146 #endif
148 /* We produce at least one byte in the next round. */
149 #ifndef MIN_NEEDED_OUTPUT
150 # error "MIN_NEEDED_OUTPUT definition missing"
151 #elif MIN_NEEDED_OUTPUT < 1
152 # error "MIN_NEEDED_OUTPUT must be >= 1"
153 #endif
155 /* Let's see how many bytes we produce. */
156 #ifndef MAX_NEEDED_OUTPUT
157 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
158 #endif
160 /* Default name for the function. */
161 #ifndef LOOPFCT
162 # define LOOPFCT loop
163 #endif
165 /* Make sure we have a loop body. */
166 #ifndef BODY
167 # error "Definition of BODY missing for function" LOOPFCT
168 #endif
171 /* If no arguments have to passed to the loop function define the macro
172 as empty. */
173 #ifndef EXTRA_LOOP_DECLS
174 # define EXTRA_LOOP_DECLS
175 #endif
177 /* Allow using UPDATE_PARAMS in macros where #ifdef UPDATE_PARAMS test
178 isn't possible. */
179 #ifndef UPDATE_PARAMS
180 # define UPDATE_PARAMS do { } while (0)
181 #endif
182 #ifndef REINIT_PARAMS
183 # define REINIT_PARAMS do { } while (0)
184 #endif
187 /* To make it easier for the writers of the modules, we define a macro
188 to test whether we have to ignore errors. */
189 #define ignore_errors_p() \
190 (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
193 /* Error handling for the FROM_LOOP direction, with ignoring of errors.
194 Note that we cannot use the do while (0) trick since `break' and
195 `continue' must reach certain points. */
196 #define STANDARD_FROM_LOOP_ERR_HANDLER(Incr) \
198 result = __GCONV_ILLEGAL_INPUT; \
200 if (! ignore_errors_p ()) \
201 break; \
203 /* We ignore the invalid input byte sequence. */ \
204 inptr += (Incr); \
205 ++*irreversible; \
206 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
207 that "iconv -c" must give the same exitcode as "iconv". */ \
208 continue; \
211 /* Error handling for the TO_LOOP direction, with use of transliteration/
212 transcription functions and ignoring of errors. Note that we cannot use
213 the do while (0) trick since `break' and `continue' must reach certain
214 points. */
215 #define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
217 struct __gconv_trans_data *trans; \
219 result = __GCONV_ILLEGAL_INPUT; \
221 if (irreversible == NULL) \
222 /* This means we are in call from __gconv_transliterate. In this \
223 case we are not doing any error recovery outself. */ \
224 break; \
226 /* If needed, flush any conversion state, so that __gconv_transliterate \
227 starts with current shift state. */ \
228 UPDATE_PARAMS; \
230 /* First try the transliteration methods. */ \
231 for (trans = step_data->__trans; trans != NULL; trans = trans->__next) \
233 result = DL_CALL_FCT (trans->__trans_fct, \
234 (step, step_data, trans->__data, *inptrp, \
235 &inptr, inend, &outptr, irreversible)); \
236 if (result != __GCONV_ILLEGAL_INPUT) \
237 break; \
240 REINIT_PARAMS; \
242 /* If any of them recognized the input continue with the loop. */ \
243 if (result != __GCONV_ILLEGAL_INPUT) \
245 if (__builtin_expect (result == __GCONV_FULL_OUTPUT, 0)) \
246 break; \
248 continue; \
251 /* Next see whether we have to ignore the error. If not, stop. */ \
252 if (! ignore_errors_p ()) \
253 break; \
255 /* When we come here it means we ignore the character. */ \
256 ++*irreversible; \
257 inptr += Incr; \
258 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
259 that "iconv -c" must give the same exitcode as "iconv". */ \
260 continue; \
264 /* Handling of Unicode 3.1 TAG characters. Unicode recommends
265 "If language codes are not relevant to the particular processing
266 operation, then they should be ignored." This macro is usually
267 called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
268 #define UNICODE_TAG_HANDLER(Character, Incr) \
270 /* TAG characters are those in the range U+E0000..U+E007F. */ \
271 if (((Character) >> 7) == (0xe0000 >> 7)) \
273 inptr += Incr; \
274 continue; \
279 /* The function returns the status, as defined in gconv.h. */
280 static inline int
281 __attribute ((always_inline))
282 FCTNAME (LOOPFCT) (struct __gconv_step *step,
283 struct __gconv_step_data *step_data,
284 const unsigned char **inptrp, const unsigned char *inend,
285 unsigned char **outptrp, const unsigned char *outend,
286 size_t *irreversible EXTRA_LOOP_DECLS)
288 #ifdef LOOP_NEED_STATE
289 mbstate_t *state = step_data->__statep;
290 #endif
291 #ifdef LOOP_NEED_FLAGS
292 int flags = step_data->__flags;
293 #endif
294 #ifdef LOOP_NEED_DATA
295 void *data = step->__data;
296 #endif
297 int result = __GCONV_EMPTY_INPUT;
298 const unsigned char *inptr = *inptrp;
299 unsigned char *outptr = *outptrp;
301 #ifdef INIT_PARAMS
302 INIT_PARAMS;
303 #endif
305 while (inptr != inend)
307 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
308 compiler generating better code. They will be optimized away
309 since MIN_NEEDED_OUTPUT is always a constant. */
310 if (MIN_NEEDED_INPUT > 1
311 && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
313 /* We don't have enough input for another complete input
314 character. */
315 result = __GCONV_INCOMPLETE_INPUT;
316 break;
318 if ((MIN_NEEDED_OUTPUT != 1
319 && __builtin_expect (outptr + MIN_NEEDED_OUTPUT > outend, 0))
320 || (MIN_NEEDED_OUTPUT == 1
321 && __builtin_expect (outptr >= outend, 0)))
323 /* Overflow in the output buffer. */
324 result = __GCONV_FULL_OUTPUT;
325 break;
328 /* Here comes the body the user provides. It can stop with
329 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
330 input characters vary in size), GCONV_ILLEGAL_INPUT, or
331 GCONV_FULL_OUTPUT (if the output characters vary in size). */
332 BODY
335 /* Update the pointers pointed to by the parameters. */
336 *inptrp = inptr;
337 *outptrp = outptr;
338 UPDATE_PARAMS;
340 return result;
344 /* Include the file a second time to define the function to handle
345 unaligned access. */
346 #if !defined DEFINE_UNALIGNED && !defined _STRING_ARCH_unaligned \
347 && MIN_NEEDED_INPUT != 1 && MAX_NEEDED_INPUT % MIN_NEEDED_INPUT == 0 \
348 && MIN_NEEDED_OUTPUT != 1 && MAX_NEEDED_OUTPUT % MIN_NEEDED_OUTPUT == 0
349 # undef get16
350 # undef get32
351 # undef put16
352 # undef put32
353 # undef unaligned
355 # define DEFINE_UNALIGNED
356 # include "loop.c"
357 # undef DEFINE_UNALIGNED
358 #endif
361 #if MAX_NEEDED_INPUT > 1
362 # define SINGLE(fct) SINGLE2 (fct)
363 # define SINGLE2(fct) fct##_single
364 static inline int
365 __attribute ((always_inline))
366 SINGLE(LOOPFCT) (struct __gconv_step *step,
367 struct __gconv_step_data *step_data,
368 const unsigned char **inptrp, const unsigned char *inend,
369 unsigned char **outptrp, unsigned char *outend,
370 size_t *irreversible EXTRA_LOOP_DECLS)
372 mbstate_t *state = step_data->__statep;
373 #ifdef LOOP_NEED_FLAGS
374 int flags = step_data->__flags;
375 #endif
376 #ifdef LOOP_NEED_DATA
377 void *data = step->__data;
378 #endif
379 int result = __GCONV_OK;
380 unsigned char bytebuf[MAX_NEEDED_INPUT];
381 const unsigned char *inptr = *inptrp;
382 unsigned char *outptr = *outptrp;
383 size_t inlen;
385 #ifdef INIT_PARAMS
386 INIT_PARAMS;
387 #endif
389 #ifdef UNPACK_BYTES
390 UNPACK_BYTES
391 #else
392 /* Add the bytes from the state to the input buffer. */
393 for (inlen = 0; inlen < (size_t) (state->__count & 7); ++inlen)
394 bytebuf[inlen] = state->__value.__wchb[inlen];
395 #endif
397 /* Are there enough bytes in the input buffer? */
398 if (__builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
400 *inptrp = inend;
401 #ifdef STORE_REST
402 inptr = bytebuf;
403 inptrp = &inptr;
404 inend = &bytebuf[inlen];
406 STORE_REST
407 #else
408 /* We don't have enough input for another complete input
409 character. */
410 while (inptr < inend)
411 state->__value.__wchb[inlen++] = *inptr++;
412 #endif
414 return __GCONV_INCOMPLETE_INPUT;
417 /* Enough space in output buffer. */
418 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
419 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
420 /* Overflow in the output buffer. */
421 return __GCONV_FULL_OUTPUT;
423 /* Now add characters from the normal input buffer. */
425 bytebuf[inlen++] = *inptr++;
426 while (inlen < MAX_NEEDED_INPUT && inptr < inend);
428 inptr = bytebuf;
429 inend = &bytebuf[inlen];
433 BODY
435 while (0);
437 /* Now we either have produced an output character and consumed all the
438 bytes from the state and at least one more, or the character is still
439 incomplete, or we have some other error (like illegal input character,
440 no space in output buffer). */
441 if (__builtin_expect (inptr != bytebuf, 1))
443 /* We found a new character. */
444 assert (inptr - bytebuf > (state->__count & 7));
446 *inptrp += inptr - bytebuf - (state->__count & 7);
447 *outptrp = outptr;
449 result = __GCONV_OK;
451 /* Clear the state buffer. */
452 #ifdef CLEAR_STATE
453 CLEAR_STATE;
454 #else
455 state->__count &= ~7;
456 #endif
458 else if (result == __GCONV_INCOMPLETE_INPUT)
460 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
461 available. */
462 assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
464 *inptrp += inend - bytebuf - (state->__count & 7);
465 #ifdef STORE_REST
466 inptrp = &inptr;
468 STORE_REST
469 #else
470 /* We don't have enough input for another complete input
471 character. */
472 assert (inend - inptr > (state->__count & ~7));
473 assert (inend - inptr <= 7);
474 state->__count = (state->__count & ~7) | (inend - inptr);
475 inlen = 0;
476 while (inptr < inend)
477 state->__value.__wchb[inlen++] = *inptr++;
478 #endif
481 return result;
483 # undef SINGLE
484 # undef SINGLE2
485 #endif
488 #ifdef ONEBYTE_BODY
489 /* Define the shortcut function for btowc. */
490 static wint_t
491 gconv_btowc (struct __gconv_step *step, unsigned char c)
492 ONEBYTE_BODY
493 # define FROM_ONEBYTE gconv_btowc
494 #endif
497 /* We remove the macro definitions so that we can include this file again
498 for the definition of another function. */
499 #undef MIN_NEEDED_INPUT
500 #undef MAX_NEEDED_INPUT
501 #undef MIN_NEEDED_OUTPUT
502 #undef MAX_NEEDED_OUTPUT
503 #undef LOOPFCT
504 #undef BODY
505 #undef LOOPFCT
506 #undef EXTRA_LOOP_DECLS
507 #undef INIT_PARAMS
508 #undef UPDATE_PARAMS
509 #undef REINIT_PARAMS
510 #undef ONEBYTE_BODY
511 #undef UNPACK_BYTES
512 #undef CLEAR_STATE
513 #undef LOOP_NEED_STATE
514 #undef LOOP_NEED_FLAGS
515 #undef LOOP_NEED_DATA
516 #undef get16
517 #undef get32
518 #undef put16
519 #undef put32
520 #undef unaligned