(main): Improve error message for unexpected signal case.
[glibc.git] / iconv / loop.c
blob2fb73da7ea43b8833a10bdd8b2e41ea59dd1eff7
1 /* Conversion loop frame work.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 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.
48 #include <assert.h>
49 #include <endian.h>
50 #include <gconv.h>
51 #include <stdint.h>
52 #include <string.h>
53 #include <wchar.h>
54 #include <sys/param.h> /* For MIN. */
55 #define __need_size_t
56 #include <stddef.h>
59 /* We have to provide support for machines which are not able to handled
60 unaligned memory accesses. Some of the character encodings have
61 representations with a fixed width of 2 or 4 bytes. But if we cannot
62 access unaligned memory we still have to read byte-wise. */
63 #undef FCTNAME2
64 #if defined _STRING_ARCH_unaligned || !defined DEFINE_UNALIGNED
65 /* We can handle unaligned memory access. */
66 # define get16(addr) *((__const uint16_t *) (addr))
67 # define get32(addr) *((__const uint32_t *) (addr))
69 /* We need no special support for writing values either. */
70 # define put16(addr, val) *((uint16_t *) (addr)) = (val)
71 # define put32(addr, val) *((uint32_t *) (addr)) = (val)
73 # define FCTNAME2(name) name
74 #else
75 /* Distinguish between big endian and little endian. */
76 # if __BYTE_ORDER == __LITTLE_ENDIAN
77 # define get16(addr) \
78 (((__const unsigned char *) (addr))[1] << 8 \
79 | ((__const unsigned char *) (addr))[0])
80 # define get32(addr) \
81 (((((__const unsigned char *) (addr))[3] << 8 \
82 | ((__const unsigned char *) (addr))[2]) << 8 \
83 | ((__const unsigned char *) (addr))[1]) << 8 \
84 | ((__const unsigned char *) (addr))[0])
86 # define put16(addr, val) \
87 ({ uint16_t __val = (val); \
88 ((unsigned char *) (addr))[0] = __val; \
89 ((unsigned char *) (addr))[1] = __val >> 8; \
90 (void) 0; })
91 # define put32(addr, val) \
92 ({ uint32_t __val = (val); \
93 ((unsigned char *) (addr))[0] = __val; \
94 __val >>= 8; \
95 ((unsigned char *) (addr))[1] = __val; \
96 __val >>= 8; \
97 ((unsigned char *) (addr))[2] = __val; \
98 __val >>= 8; \
99 ((unsigned char *) (addr))[3] = __val; \
100 (void) 0; })
101 # else
102 # define get16(addr) \
103 (((__const unsigned char *) (addr))[0] << 8 \
104 | ((__const unsigned char *) (addr))[1])
105 # define get32(addr) \
106 (((((__const unsigned char *) (addr))[0] << 8 \
107 | ((__const unsigned char *) (addr))[1]) << 8 \
108 | ((__const unsigned char *) (addr))[2]) << 8 \
109 | ((__const unsigned char *) (addr))[3])
111 # define put16(addr, val) \
112 ({ uint16_t __val = (val); \
113 ((unsigned char *) (addr))[1] = __val; \
114 ((unsigned char *) (addr))[0] = __val >> 8; \
115 (void) 0; })
116 # define put32(addr, val) \
117 ({ uint32_t __val = (val); \
118 ((unsigned char *) (addr))[3] = __val; \
119 __val >>= 8; \
120 ((unsigned char *) (addr))[2] = __val; \
121 __val >>= 8; \
122 ((unsigned char *) (addr))[1] = __val; \
123 __val >>= 8; \
124 ((unsigned char *) (addr))[0] = __val; \
125 (void) 0; })
126 # endif
128 # define FCTNAME2(name) name##_unaligned
129 #endif
130 #define FCTNAME(name) FCTNAME2(name)
133 /* We need at least one byte for the next round. */
134 #ifndef MIN_NEEDED_INPUT
135 # error "MIN_NEEDED_INPUT definition missing"
136 #elif MIN_NEEDED_INPUT < 1
137 # error "MIN_NEEDED_INPUT must be >= 1"
138 #endif
140 /* Let's see how many bytes we produce. */
141 #ifndef MAX_NEEDED_INPUT
142 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
143 #endif
145 /* We produce at least one byte in the next round. */
146 #ifndef MIN_NEEDED_OUTPUT
147 # error "MIN_NEEDED_OUTPUT definition missing"
148 #elif MIN_NEEDED_OUTPUT < 1
149 # error "MIN_NEEDED_OUTPUT must be >= 1"
150 #endif
152 /* Let's see how many bytes we produce. */
153 #ifndef MAX_NEEDED_OUTPUT
154 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
155 #endif
157 /* Default name for the function. */
158 #ifndef LOOPFCT
159 # define LOOPFCT loop
160 #endif
162 /* Make sure we have a loop body. */
163 #ifndef BODY
164 # error "Definition of BODY missing for function" LOOPFCT
165 #endif
168 /* If no arguments have to passed to the loop function define the macro
169 as empty. */
170 #ifndef EXTRA_LOOP_DECLS
171 # define EXTRA_LOOP_DECLS
172 #endif
175 /* To make it easier for the writers of the modules, we define a macro
176 to test whether we have to ignore errors. */
177 #define ignore_errors_p() \
178 (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
181 /* Error handling for the FROM_LOOP direction, with ignoring of errors.
182 Note that we cannot use the do while (0) trick since `break' and
183 `continue' must reach certain points. */
184 #define STANDARD_FROM_LOOP_ERR_HANDLER(Incr) \
186 result = __GCONV_ILLEGAL_INPUT; \
188 if (! ignore_errors_p ()) \
189 break; \
191 /* We ignore the invalid input byte sequence. */ \
192 inptr += (Incr); \
193 ++*irreversible; \
194 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
195 that "iconv -c" must give the same exitcode as "iconv". */ \
196 continue; \
199 /* Error handling for the TO_LOOP direction, with use of transliteration/
200 transcription functions and ignoring of errors. Note that we cannot use
201 the do while (0) trick since `break' and `continue' must reach certain
202 points. */
203 #define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
205 struct __gconv_trans_data *trans; \
207 result = __GCONV_ILLEGAL_INPUT; \
209 if (irreversible == NULL) \
210 /* This means we are in call from __gconv_transliterate. In this \
211 case we are not doing any error recovery outself. */ \
212 break; \
214 /* First try the transliteration methods. */ \
215 for (trans = step_data->__trans; trans != NULL; trans = trans->__next) \
217 result = DL_CALL_FCT (trans->__trans_fct, \
218 (step, step_data, trans->__data, *inptrp, \
219 &inptr, inend, &outptr, irreversible)); \
220 if (result != __GCONV_ILLEGAL_INPUT) \
221 break; \
223 /* If any of them recognized the input continue with the loop. */ \
224 if (result != __GCONV_ILLEGAL_INPUT) \
225 continue; \
227 /* Next see whether we have to ignore the error. If not, stop. */ \
228 if (! ignore_errors_p ()) \
229 break; \
231 /* When we come here it means we ignore the character. */ \
232 ++*irreversible; \
233 inptr += Incr; \
234 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
235 that "iconv -c" must give the same exitcode as "iconv". */ \
236 continue; \
240 /* Handling of Unicode 3.1 TAG characters. Unicode recommends
241 "If language codes are not relevant to the particular processing
242 operation, then they should be ignored." This macro is usually
243 called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
244 #define UNICODE_TAG_HANDLER(Character, Incr) \
246 /* TAG characters are those in the range U+E0000..U+E007F. */ \
247 if (((Character) >> 7) == (0xe0000 >> 7)) \
249 inptr += Incr; \
250 continue; \
255 /* The function returns the status, as defined in gconv.h. */
256 static inline int
257 FCTNAME (LOOPFCT) (struct __gconv_step *step,
258 struct __gconv_step_data *step_data,
259 const unsigned char **inptrp, const unsigned char *inend,
260 unsigned char **outptrp, const unsigned char *outend,
261 size_t *irreversible EXTRA_LOOP_DECLS)
263 #ifdef LOOP_NEED_STATE
264 mbstate_t *state = step_data->__statep;
265 #endif
266 #ifdef LOOP_NEED_FLAGS
267 int flags = step_data->__flags;
268 #endif
269 #ifdef LOOP_NEED_DATA
270 void *data = step->__data;
271 #endif
272 int result = __GCONV_EMPTY_INPUT;
273 const unsigned char *inptr = *inptrp;
274 unsigned char *outptr = *outptrp;
276 #ifdef INIT_PARAMS
277 INIT_PARAMS;
278 #endif
280 while (inptr != inend)
282 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
283 compiler generating better code. They will be optimized away
284 since MIN_NEEDED_OUTPUT is always a constant. */
285 if ((MIN_NEEDED_OUTPUT != 1
286 && __builtin_expect (outptr + MIN_NEEDED_OUTPUT > outend, 0))
287 || (MIN_NEEDED_OUTPUT == 1
288 && __builtin_expect (outptr >= outend, 0)))
290 /* Overflow in the output buffer. */
291 result = __GCONV_FULL_OUTPUT;
292 break;
294 if (MIN_NEEDED_INPUT > 1
295 && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
297 /* We don't have enough input for another complete input
298 character. */
299 result = __GCONV_INCOMPLETE_INPUT;
300 break;
303 /* Here comes the body the user provides. It can stop with
304 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
305 input characters vary in size), GCONV_ILLEGAL_INPUT, or
306 GCONV_FULL_OUTPUT (if the output characters vary in size). */
307 BODY
310 /* Update the pointers pointed to by the parameters. */
311 *inptrp = inptr;
312 *outptrp = outptr;
313 #ifdef UPDATE_PARAMS
314 UPDATE_PARAMS;
315 #endif
317 return result;
321 /* Include the file a second time to define the function to handle
322 unaligned access. */
323 #if !defined DEFINE_UNALIGNED && !defined _STRING_ARCH_unaligned \
324 && MIN_NEEDED_INPUT != 1 && MAX_NEEDED_INPUT % MIN_NEEDED_INPUT == 0 \
325 && MIN_NEEDED_OUTPUT != 1 && MAX_NEEDED_OUTPUT % MIN_NEEDED_OUTPUT == 0
326 # undef get16
327 # undef get32
328 # undef put16
329 # undef put32
330 # undef unaligned
332 # define DEFINE_UNALIGNED
333 # include "loop.c"
334 # undef DEFINE_UNALIGNED
335 #endif
338 #if MAX_NEEDED_INPUT > 1
339 # define SINGLE(fct) SINGLE2 (fct)
340 # define SINGLE2(fct) fct##_single
341 static inline int
342 SINGLE(LOOPFCT) (struct __gconv_step *step,
343 struct __gconv_step_data *step_data,
344 const unsigned char **inptrp, const unsigned char *inend,
345 unsigned char **outptrp, unsigned char *outend,
346 size_t *irreversible EXTRA_LOOP_DECLS)
348 mbstate_t *state = step_data->__statep;
349 #ifdef LOOP_NEED_FLAGS
350 int flags = step_data->__flags;
351 #endif
352 #ifdef LOOP_NEED_DATA
353 void *data = step->__data;
354 #endif
355 int result = __GCONV_OK;
356 unsigned char bytebuf[MAX_NEEDED_INPUT];
357 const unsigned char *inptr = *inptrp;
358 unsigned char *outptr = *outptrp;
359 size_t inlen;
361 #ifdef INIT_PARAMS
362 INIT_PARAMS;
363 #endif
365 #ifdef UNPACK_BYTES
366 UNPACK_BYTES
367 #else
368 /* Add the bytes from the state to the input buffer. */
369 for (inlen = 0; inlen < (size_t) (state->__count & 7); ++inlen)
370 bytebuf[inlen] = state->__value.__wchb[inlen];
371 #endif
373 /* Are there enough bytes in the input buffer? */
374 if (__builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
376 *inptrp = inend;
377 #ifdef STORE_REST
378 inptr = bytebuf;
379 inptrp = &inptr;
380 inend = &bytebuf[inlen];
382 STORE_REST
383 #else
384 /* We don't have enough input for another complete input
385 character. */
386 while (inptr < inend)
387 state->__value.__wchb[inlen++] = *inptr++;
388 #endif
390 return __GCONV_INCOMPLETE_INPUT;
393 /* Enough space in output buffer. */
394 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
395 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
396 /* Overflow in the output buffer. */
397 return __GCONV_FULL_OUTPUT;
399 /* Now add characters from the normal input buffer. */
401 bytebuf[inlen++] = *inptr++;
402 while (inlen < MAX_NEEDED_INPUT && inptr < inend);
404 inptr = bytebuf;
405 inend = &bytebuf[inlen];
409 BODY
411 while (0);
413 /* Now we either have produced an output character and consumed all the
414 bytes from the state and at least one more, or the character is still
415 incomplete, or we have some other error (like illegal input character,
416 no space in output buffer). */
417 if (__builtin_expect (inptr != bytebuf, 1))
419 /* We found a new character. */
420 assert (inptr - bytebuf > (state->__count & 7));
422 *inptrp += inptr - bytebuf - (state->__count & 7);
423 *outptrp = outptr;
425 result = __GCONV_OK;
427 /* Clear the state buffer. */
428 state->__count &= ~7;
430 else if (result == __GCONV_INCOMPLETE_INPUT)
432 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
433 available. */
434 assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
436 *inptrp += inend - bytebuf - (state->__count & 7);
437 #ifdef STORE_REST
438 inptrp = &inptr;
440 STORE_REST
441 #else
442 /* We don't have enough input for another complete input
443 character. */
444 while (inptr < inend)
445 state->__value.__wchb[inlen++] = *inptr++;
446 #endif
449 return result;
451 # undef SINGLE
452 # undef SINGLE2
453 #endif
456 /* We remove the macro definitions so that we can include this file again
457 for the definition of another function. */
458 #undef MIN_NEEDED_INPUT
459 #undef MAX_NEEDED_INPUT
460 #undef MIN_NEEDED_OUTPUT
461 #undef MAX_NEEDED_OUTPUT
462 #undef LOOPFCT
463 #undef BODY
464 #undef LOOPFCT
465 #undef EXTRA_LOOP_DECLS
466 #undef INIT_PARAMS
467 #undef UPDATE_PARAMS
468 #undef UNPACK_BYTES
469 #undef LOOP_NEED_STATE
470 #undef LOOP_NEED_FLAGS
471 #undef LOOP_NEED_DATA
472 #undef get16
473 #undef get32
474 #undef put16
475 #undef put32
476 #undef unaligned