]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/loop.c
Update.
[thirdparty/glibc.git] / iconv / loop.c
CommitLineData
8619129f 1/* Conversion loop frame work.
41f112ad 2 Copyright (C) 1998-2002, 2003 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
AJ
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. */
8619129f
UD
20
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:
25
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.
30
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.
34
8619129f
UD
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.
38
8619129f
UD
39 BODY this is supposed to expand to the body of the loop.
40 The user must provide this.
28f1c862 41
66175fa8
UD
42 EXTRA_LOOP_DECLS extra arguments passed from converion loop call.
43
44 INIT_PARAMS code to define and initialize variables from params.
45 UPDATE_PARAMS code to store result in params.
f9ad060c
UD
46
47 ONEBYTE_BODY body of the specialized conversion function for a
48 single byte from the current character set to INTERNAL.
8619129f
UD
49*/
50
fd1b5c0f 51#include <assert.h>
b35e58e4 52#include <endian.h>
8619129f 53#include <gconv.h>
b35e58e4
UD
54#include <stdint.h>
55#include <string.h>
d64b6ad0 56#include <wchar.h>
8619129f
UD
57#include <sys/param.h> /* For MIN. */
58#define __need_size_t
59#include <stddef.h>
60
61
b35e58e4
UD
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. */
b91d5eda
UD
69# define get16(addr) *((__const uint16_t *) (addr))
70# define get32(addr) *((__const uint32_t *) (addr))
b35e58e4
UD
71
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)
75
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])
88
cb2c5501 89# define put16(addr, val) \
b35e58e4 90 ({ uint16_t __val = (val); \
cb2c5501
UD
91 ((unsigned char *) (addr))[0] = __val; \
92 ((unsigned char *) (addr))[1] = __val >> 8; \
b35e58e4 93 (void) 0; })
cb2c5501
UD
94# define put32(addr, val) \
95 ({ uint32_t __val = (val); \
96 ((unsigned char *) (addr))[0] = __val; \
b35e58e4 97 __val >>= 8; \
cb2c5501 98 ((unsigned char *) (addr))[1] = __val; \
b35e58e4 99 __val >>= 8; \
cb2c5501 100 ((unsigned char *) (addr))[2] = __val; \
b35e58e4 101 __val >>= 8; \
cb2c5501 102 ((unsigned char *) (addr))[3] = __val; \
b35e58e4
UD
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])
113
cb2c5501 114# define put16(addr, val) \
b35e58e4 115 ({ uint16_t __val = (val); \
cb2c5501 116 ((unsigned char *) (addr))[1] = __val; \
f7ccf2fc 117 ((unsigned char *) (addr))[0] = __val >> 8; \
b35e58e4 118 (void) 0; })
cb2c5501
UD
119# define put32(addr, val) \
120 ({ uint32_t __val = (val); \
121 ((unsigned char *) (addr))[3] = __val; \
b35e58e4 122 __val >>= 8; \
cb2c5501 123 ((unsigned char *) (addr))[2] = __val; \
b35e58e4 124 __val >>= 8; \
cb2c5501 125 ((unsigned char *) (addr))[1] = __val; \
b35e58e4 126 __val >>= 8; \
cb2c5501 127 ((unsigned char *) (addr))[0] = __val; \
b35e58e4
UD
128 (void) 0; })
129# endif
130
131# define FCTNAME2(name) name##_unaligned
132#endif
133#define FCTNAME(name) FCTNAME2(name)
134
135
8619129f
UD
136/* We need at least one byte for the next round. */
137#ifndef MIN_NEEDED_INPUT
5aa8ff62 138# error "MIN_NEEDED_INPUT definition missing"
4a0de63b
UD
139#elif MIN_NEEDED_INPUT < 1
140# error "MIN_NEEDED_INPUT must be >= 1"
8619129f
UD
141#endif
142
143/* Let's see how many bytes we produce. */
144#ifndef MAX_NEEDED_INPUT
145# define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
146#endif
147
148/* We produce at least one byte in the next round. */
149#ifndef MIN_NEEDED_OUTPUT
5aa8ff62 150# error "MIN_NEEDED_OUTPUT definition missing"
c0a0f9a3
UD
151#elif MIN_NEEDED_OUTPUT < 1
152# error "MIN_NEEDED_OUTPUT must be >= 1"
8619129f
UD
153#endif
154
155/* Let's see how many bytes we produce. */
156#ifndef MAX_NEEDED_OUTPUT
157# define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
158#endif
159
160/* Default name for the function. */
161#ifndef LOOPFCT
162# define LOOPFCT loop
163#endif
164
165/* Make sure we have a loop body. */
166#ifndef BODY
167# error "Definition of BODY missing for function" LOOPFCT
168#endif
169
8619129f 170
28f1c862
UD
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
176
177
85830c4c
UD
178/* To make it easier for the writers of the modules, we define a macro
179 to test whether we have to ignore errors. */
b572c2da
UD
180#define ignore_errors_p() \
181 (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
85830c4c
UD
182
183
e438a468
UD
184/* Error handling for the FROM_LOOP direction, with ignoring of errors.
185 Note that we cannot use the do while (0) trick since `break' and
186 `continue' must reach certain points. */
187#define STANDARD_FROM_LOOP_ERR_HANDLER(Incr) \
188 { \
189 result = __GCONV_ILLEGAL_INPUT; \
190 \
191 if (! ignore_errors_p ()) \
192 break; \
193 \
194 /* We ignore the invalid input byte sequence. */ \
195 inptr += (Incr); \
196 ++*irreversible; \
197 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
198 that "iconv -c" must give the same exitcode as "iconv". */ \
199 continue; \
200 }
201
202/* Error handling for the TO_LOOP direction, with use of transliteration/
203 transcription functions and ignoring of errors. Note that we cannot use
204 the do while (0) trick since `break' and `continue' must reach certain
205 points. */
206#define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
d6204268
UD
207 { \
208 struct __gconv_trans_data *trans; \
209 \
210 result = __GCONV_ILLEGAL_INPUT; \
b572c2da
UD
211 \
212 if (irreversible == NULL) \
213 /* This means we are in call from __gconv_transliterate. In this \
214 case we are not doing any error recovery outself. */ \
215 break; \
216 \
d6204268
UD
217 /* First try the transliteration methods. */ \
218 for (trans = step_data->__trans; trans != NULL; trans = trans->__next) \
219 { \
220 result = DL_CALL_FCT (trans->__trans_fct, \
221 (step, step_data, trans->__data, *inptrp, \
222 &inptr, inend, &outptr, irreversible)); \
223 if (result != __GCONV_ILLEGAL_INPUT) \
224 break; \
225 } \
7888313d 226 /* If any of them recognized the input continue with the loop. */ \
d6204268 227 if (result != __GCONV_ILLEGAL_INPUT) \
7888313d 228 continue; \
d6204268
UD
229 \
230 /* Next see whether we have to ignore the error. If not, stop. */ \
231 if (! ignore_errors_p ()) \
232 break; \
b572c2da 233 \
d6204268
UD
234 /* When we come here it means we ignore the character. */ \
235 ++*irreversible; \
236 inptr += Incr; \
e438a468
UD
237 /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
238 that "iconv -c" must give the same exitcode as "iconv". */ \
d6204268
UD
239 continue; \
240 }
241
242
9a1f6754
UD
243/* Handling of Unicode 3.1 TAG characters. Unicode recommends
244 "If language codes are not relevant to the particular processing
e438a468
UD
245 operation, then they should be ignored." This macro is usually
246 called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
9a1f6754
UD
247#define UNICODE_TAG_HANDLER(Character, Incr) \
248 { \
249 /* TAG characters are those in the range U+E0000..U+E007F. */ \
250 if (((Character) >> 7) == (0xe0000 >> 7)) \
251 { \
252 inptr += Incr; \
253 continue; \
254 } \
255 }
256
257
8619129f
UD
258/* The function returns the status, as defined in gconv.h. */
259static inline int
55985355
UD
260FCTNAME (LOOPFCT) (struct __gconv_step *step,
261 struct __gconv_step_data *step_data,
262 const unsigned char **inptrp, const unsigned char *inend,
17427edd 263 unsigned char **outptrp, const unsigned char *outend,
38677ace 264 size_t *irreversible EXTRA_LOOP_DECLS)
8619129f 265{
55985355
UD
266#ifdef LOOP_NEED_STATE
267 mbstate_t *state = step_data->__statep;
268#endif
269#ifdef LOOP_NEED_FLAGS
270 int flags = step_data->__flags;
271#endif
272#ifdef LOOP_NEED_DATA
273 void *data = step->__data;
274#endif
275 int result = __GCONV_EMPTY_INPUT;
8619129f
UD
276 const unsigned char *inptr = *inptrp;
277 unsigned char *outptr = *outptrp;
8619129f 278
66175fa8
UD
279#ifdef INIT_PARAMS
280 INIT_PARAMS;
281#endif
282
55985355 283 while (inptr != inend)
8619129f 284 {
55985355 285 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
ca3c0135 286 compiler generating better code. They will be optimized away
55985355 287 since MIN_NEEDED_OUTPUT is always a constant. */
eb9dc2a2
UD
288 if (MIN_NEEDED_INPUT > 1
289 && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
290 {
291 /* We don't have enough input for another complete input
292 character. */
293 result = __GCONV_INCOMPLETE_INPUT;
294 break;
295 }
55985355
UD
296 if ((MIN_NEEDED_OUTPUT != 1
297 && __builtin_expect (outptr + MIN_NEEDED_OUTPUT > outend, 0))
298 || (MIN_NEEDED_OUTPUT == 1
299 && __builtin_expect (outptr >= outend, 0)))
300 {
301 /* Overflow in the output buffer. */
302 result = __GCONV_FULL_OUTPUT;
303 break;
304 }
55985355
UD
305
306 /* Here comes the body the user provides. It can stop with
307 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
308 input characters vary in size), GCONV_ILLEGAL_INPUT, or
309 GCONV_FULL_OUTPUT (if the output characters vary in size). */
310 BODY
8619129f
UD
311 }
312
8619129f
UD
313 /* Update the pointers pointed to by the parameters. */
314 *inptrp = inptr;
315 *outptrp = outptr;
66175fa8
UD
316#ifdef UPDATE_PARAMS
317 UPDATE_PARAMS;
318#endif
8619129f
UD
319
320 return result;
321}
322
323
b02b4774
UD
324/* Include the file a second time to define the function to handle
325 unaligned access. */
fdf64555 326#if !defined DEFINE_UNALIGNED && !defined _STRING_ARCH_unaligned \
4a0de63b
UD
327 && MIN_NEEDED_INPUT != 1 && MAX_NEEDED_INPUT % MIN_NEEDED_INPUT == 0 \
328 && MIN_NEEDED_OUTPUT != 1 && MAX_NEEDED_OUTPUT % MIN_NEEDED_OUTPUT == 0
fd1b5c0f
UD
329# undef get16
330# undef get32
331# undef put16
332# undef put32
333# undef unaligned
334
b35e58e4
UD
335# define DEFINE_UNALIGNED
336# include "loop.c"
337# undef DEFINE_UNALIGNED
338#endif
339
340
fd1b5c0f
UD
341#if MAX_NEEDED_INPUT > 1
342# define SINGLE(fct) SINGLE2 (fct)
343# define SINGLE2(fct) fct##_single
344static inline int
55985355
UD
345SINGLE(LOOPFCT) (struct __gconv_step *step,
346 struct __gconv_step_data *step_data,
347 const unsigned char **inptrp, const unsigned char *inend,
fd1b5c0f 348 unsigned char **outptrp, unsigned char *outend,
55985355 349 size_t *irreversible EXTRA_LOOP_DECLS)
fd1b5c0f 350{
55985355
UD
351 mbstate_t *state = step_data->__statep;
352#ifdef LOOP_NEED_FLAGS
353 int flags = step_data->__flags;
354#endif
355#ifdef LOOP_NEED_DATA
356 void *data = step->__data;
357#endif
fd1b5c0f
UD
358 int result = __GCONV_OK;
359 unsigned char bytebuf[MAX_NEEDED_INPUT];
360 const unsigned char *inptr = *inptrp;
361 unsigned char *outptr = *outptrp;
362 size_t inlen;
363
364#ifdef INIT_PARAMS
365 INIT_PARAMS;
366#endif
367
368#ifdef UNPACK_BYTES
369 UNPACK_BYTES
370#else
371 /* Add the bytes from the state to the input buffer. */
17427edd 372 for (inlen = 0; inlen < (size_t) (state->__count & 7); ++inlen)
fd1b5c0f
UD
373 bytebuf[inlen] = state->__value.__wchb[inlen];
374#endif
375
376 /* Are there enough bytes in the input buffer? */
316518d6 377 if (__builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
fd1b5c0f 378 {
fd1b5c0f 379 *inptrp = inend;
316518d6 380#ifdef STORE_REST
fd1b5c0f
UD
381 inptr = bytebuf;
382 inptrp = &inptr;
383 inend = &bytebuf[inlen];
384
385 STORE_REST
386#else
387 /* We don't have enough input for another complete input
388 character. */
389 while (inptr < inend)
390 state->__value.__wchb[inlen++] = *inptr++;
391#endif
392
393 return __GCONV_INCOMPLETE_INPUT;
394 }
395
396 /* Enough space in output buffer. */
397 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
398 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
399 /* Overflow in the output buffer. */
400 return __GCONV_FULL_OUTPUT;
401
402 /* Now add characters from the normal input buffer. */
403 do
404 bytebuf[inlen++] = *inptr++;
316518d6 405 while (inlen < MAX_NEEDED_INPUT && inptr < inend);
fd1b5c0f
UD
406
407 inptr = bytebuf;
316518d6 408 inend = &bytebuf[inlen];
55985355 409
fd1b5c0f
UD
410 do
411 {
412 BODY
413 }
414 while (0);
415
316518d6
UD
416 /* Now we either have produced an output character and consumed all the
417 bytes from the state and at least one more, or the character is still
418 incomplete, or we have some other error (like illegal input character,
419 no space in output buffer). */
5ea1a82d 420 if (__builtin_expect (inptr != bytebuf, 1))
fd1b5c0f 421 {
316518d6 422 /* We found a new character. */
fd1b5c0f
UD
423 assert (inptr - bytebuf > (state->__count & 7));
424
425 *inptrp += inptr - bytebuf - (state->__count & 7);
426 *outptrp = outptr;
427
316518d6
UD
428 result = __GCONV_OK;
429
fd1b5c0f 430 /* Clear the state buffer. */
41f112ad
UD
431#ifdef CLEAR_STATE
432 CLEAR_STATE;
433#else
fd1b5c0f 434 state->__count &= ~7;
41f112ad 435#endif
fd1b5c0f 436 }
316518d6
UD
437 else if (result == __GCONV_INCOMPLETE_INPUT)
438 {
439 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
440 available. */
441 assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
442
443 *inptrp += inend - bytebuf - (state->__count & 7);
444#ifdef STORE_REST
445 inptrp = &inptr;
446
447 STORE_REST
448#else
449 /* We don't have enough input for another complete input
450 character. */
451 while (inptr < inend)
452 state->__value.__wchb[inlen++] = *inptr++;
453#endif
454 }
fd1b5c0f
UD
455
456 return result;
457}
458# undef SINGLE
459# undef SINGLE2
460#endif
461
462
f9ad060c
UD
463#ifdef ONEBYTE_BODY
464/* Define the shortcut function for btowc. */
465static wint_t
466gconv_btowc (struct __gconv_step *step, unsigned char c)
467 ONEBYTE_BODY
468# define FROM_ONEBYTE gconv_btowc
469#endif
470
471
8619129f
UD
472/* We remove the macro definitions so that we can include this file again
473 for the definition of another function. */
474#undef MIN_NEEDED_INPUT
475#undef MAX_NEEDED_INPUT
476#undef MIN_NEEDED_OUTPUT
477#undef MAX_NEEDED_OUTPUT
478#undef LOOPFCT
8619129f
UD
479#undef BODY
480#undef LOOPFCT
28f1c862 481#undef EXTRA_LOOP_DECLS
66175fa8
UD
482#undef INIT_PARAMS
483#undef UPDATE_PARAMS
f9ad060c 484#undef ONEBYTE_BODY
55985355 485#undef UNPACK_BYTES
41f112ad 486#undef CLEAR_STATE
55985355
UD
487#undef LOOP_NEED_STATE
488#undef LOOP_NEED_FLAGS
489#undef LOOP_NEED_DATA
fd1b5c0f
UD
490#undef get16
491#undef get32
492#undef put16
493#undef put32
494#undef unaligned