Some additional configuration.
[gedcom-parse.git] / iconv / glibc / loop.c
1 /* This file is taken from the glibc sources (release 2.2.4).
2    Only this initial comment has been added.  The next comment
3    gives the original copyright notice.
4 */
5
6 /* Conversion loop frame work.
7    Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
8    This file is part of the GNU C Library.
9    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
10
11    The GNU C Library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2.1 of the License, or (at your option) any later version.
15
16    The GNU C Library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with the GNU C Library; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24    02111-1307 USA.  */
25
26 /* This file provides a frame for the reader loop in all conversion modules.
27    The actual code must (of course) be provided in the actual module source
28    code but certain actions can be written down generically, with some
29    customization options which are these:
30
31      MIN_NEEDED_INPUT   minimal number of input bytes needed for the next
32                         conversion.
33      MIN_NEEDED_OUTPUT  minimal number of bytes produced by the next round
34                         of conversion.
35
36      MAX_NEEDED_INPUT   you guess it, this is the maximal number of input
37                         bytes needed.  It defaults to MIN_NEEDED_INPUT
38      MAX_NEEDED_OUTPUT  likewise for output bytes.
39
40      LOOPFCT            name of the function created.  If not specified
41                         the name is `loop' but this prevents the use
42                         of multiple functions in the same file.
43
44      BODY               this is supposed to expand to the body of the loop.
45                         The user must provide this.
46
47      EXTRA_LOOP_DECLS   extra arguments passed from converion loop call.
48
49      INIT_PARAMS        code to define and initialize variables from params.
50      UPDATE_PARAMS      code to store result in params.
51 */
52
53 #include <assert.h>
54 #include <endian.h>
55 #include <gconv.h>
56 #include <stdint.h>
57 #include <string.h>
58 #include <wchar.h>
59 #include <sys/param.h>          /* For MIN.  */
60 #define __need_size_t
61 #include <stddef.h>
62
63
64 /* We have to provide support for machines which are not able to handled
65    unaligned memory accesses.  Some of the character encodings have
66    representations with a fixed width of 2 or 4 bytes.  But if we cannot
67    access unaligned memory we still have to read byte-wise.  */
68 #undef FCTNAME2
69 #if defined _STRING_ARCH_unaligned || !defined DEFINE_UNALIGNED
70 /* We can handle unaligned memory access.  */
71 # define get16(addr) *((__const uint16_t *) (addr))
72 # define get32(addr) *((__const uint32_t *) (addr))
73
74 /* We need no special support for writing values either.  */
75 # define put16(addr, val) *((uint16_t *) (addr)) = (val)
76 # define put32(addr, val) *((uint32_t *) (addr)) = (val)
77
78 # define FCTNAME2(name) name
79 #else
80 /* Distinguish between big endian and little endian.  */
81 # if __BYTE_ORDER == __LITTLE_ENDIAN
82 #  define get16(addr) \
83      (((__const unsigned char *) (addr))[1] << 8                              \
84       | ((__const unsigned char *) (addr))[0])
85 #  define get32(addr) \
86      (((((__const unsigned char *) (addr))[3] << 8                            \
87         | ((__const unsigned char *) (addr))[2]) << 8                         \
88        | ((__const unsigned char *) (addr))[1]) << 8                          \
89       | ((__const unsigned char *) (addr))[0])
90
91 #  define put16(addr, val) \
92      ({ uint16_t __val = (val);                                               \
93         ((unsigned char *) (addr))[0] = __val;                                \
94         ((unsigned char *) (addr))[1] = __val >> 8;                           \
95         (void) 0; })
96 #  define put32(addr, val) \
97      ({ uint32_t __val = (val);                                               \
98         ((unsigned char *) (addr))[0] = __val;                                \
99         __val >>= 8;                                                          \
100         ((unsigned char *) (addr))[1] = __val;                                \
101         __val >>= 8;                                                          \
102         ((unsigned char *) (addr))[2] = __val;                                \
103         __val >>= 8;                                                          \
104         ((unsigned char *) (addr))[3] = __val;                                \
105         (void) 0; })
106 # else
107 #  define get16(addr) \
108      (((__const unsigned char *) (addr))[0] << 8                              \
109       | ((__const unsigned char *) (addr))[1])
110 #  define get32(addr) \
111      (((((__const unsigned char *) (addr))[0] << 8                            \
112         | ((__const unsigned char *) (addr))[1]) << 8                         \
113        | ((__const unsigned char *) (addr))[2]) << 8                          \
114       | ((__const unsigned char *) (addr))[3])
115
116 #  define put16(addr, val) \
117      ({ uint16_t __val = (val);                                               \
118         ((unsigned char *) (addr))[1] = __val;                                \
119         ((unsigned char *) (addr))[0] = __val >> 8;                           \
120         (void) 0; })
121 #  define put32(addr, val) \
122      ({ uint32_t __val = (val);                                               \
123         ((unsigned char *) (addr))[3] = __val;                                \
124         __val >>= 8;                                                          \
125         ((unsigned char *) (addr))[2] = __val;                                \
126         __val >>= 8;                                                          \
127         ((unsigned char *) (addr))[1] = __val;                                \
128         __val >>= 8;                                                          \
129         ((unsigned char *) (addr))[0] = __val;                                \
130         (void) 0; })
131 # endif
132
133 # define FCTNAME2(name) name##_unaligned
134 #endif
135 #define FCTNAME(name) FCTNAME2(name)
136
137
138 /* We need at least one byte for the next round.  */
139 #ifndef MIN_NEEDED_INPUT
140 # error "MIN_NEEDED_INPUT definition missing"
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
150 # error "MIN_NEEDED_OUTPUT definition missing"
151 #endif
152
153 /* Let's see how many bytes we produce.  */
154 #ifndef MAX_NEEDED_OUTPUT
155 # define MAX_NEEDED_OUTPUT      MIN_NEEDED_OUTPUT
156 #endif
157
158 /* Default name for the function.  */
159 #ifndef LOOPFCT
160 # define LOOPFCT                loop
161 #endif
162
163 /* Make sure we have a loop body.  */
164 #ifndef BODY
165 # error "Definition of BODY missing for function" LOOPFCT
166 #endif
167
168
169 /* If no arguments have to passed to the loop function define the macro
170    as empty.  */
171 #ifndef EXTRA_LOOP_DECLS
172 # define EXTRA_LOOP_DECLS
173 #endif
174
175
176 /* To make it easier for the writers of the modules, we define a macro
177    to test whether we have to ignore errors.  */
178 #define ignore_errors_p() \
179   (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
180
181
182 /* Error handling with transliteration/transcription function use and
183    ignoring of errors.  Note that we cannot use the do while (0) trick
184    since `break' and `continue' must reach certain points.  */
185 #define STANDARD_ERR_HANDLER(Incr) \
186   {                                                                           \
187     struct __gconv_trans_data *trans;                                         \
188                                                                               \
189     result = __GCONV_ILLEGAL_INPUT;                                           \
190                                                                               \
191     if (irreversible == NULL)                                                 \
192       /* This means we are in call from __gconv_transliterate.  In this       \
193          case we are not doing any error recovery outself.  */                \
194       break;                                                                  \
195                                                                               \
196     /* First try the transliteration methods.  */                             \
197     for (trans = step_data->__trans; trans != NULL; trans = trans->__next)    \
198       {                                                                       \
199         result = DL_CALL_FCT (trans->__trans_fct,                             \
200                               (step, step_data, trans->__data, *inptrp,       \
201                                &inptr, inend, &outptr, irreversible));        \
202         if (result != __GCONV_ILLEGAL_INPUT)                                  \
203           break;                                                              \
204       }                                                                       \
205     /* If any of them recognized the input continue with the loop.  */        \
206     if (result != __GCONV_ILLEGAL_INPUT)                                      \
207       continue;                                                               \
208                                                                               \
209     /* Next see whether we have to ignore the error.  If not, stop.  */       \
210     if (! ignore_errors_p ())                                                 \
211       break;                                                                  \
212                                                                               \
213     /* When we come here it means we ignore the character.  */                \
214     ++*irreversible;                                                          \
215     inptr += Incr;                                                            \
216     continue;                                                                 \
217   }
218
219
220 /* Handling of Unicode 3.1 TAG characters.  Unicode recommends
221    "If language codes are not relevant to the particular processing
222     operation, then they should be ignored."
223    This macro is usually called right before STANDARD_ERR_HANDLER (Incr).  */
224 #define UNICODE_TAG_HANDLER(Character, Incr) \
225   {                                                                           \
226     /* TAG characters are those in the range U+E0000..U+E007F.  */            \
227     if (((Character) >> 7) == (0xe0000 >> 7))                                 \
228       {                                                                       \
229         inptr += Incr;                                                        \
230         continue;                                                             \
231       }                                                                       \
232   }
233
234
235 /* The function returns the status, as defined in gconv.h.  */
236 static inline int
237 FCTNAME (LOOPFCT) (struct __gconv_step *step,
238                    struct __gconv_step_data *step_data,
239                    const unsigned char **inptrp, const unsigned char *inend,
240                    unsigned char **outptrp, const unsigned char *outend,
241                    size_t *irreversible EXTRA_LOOP_DECLS)
242 {
243 #ifdef LOOP_NEED_STATE
244   mbstate_t *state = step_data->__statep;
245 #endif
246 #ifdef LOOP_NEED_FLAGS
247   int flags = step_data->__flags;
248 #endif
249 #ifdef LOOP_NEED_DATA
250   void *data = step->__data;
251 #endif
252   int result = __GCONV_EMPTY_INPUT;
253   const unsigned char *inptr = *inptrp;
254   unsigned char *outptr = *outptrp;
255
256 #ifdef INIT_PARAMS
257   INIT_PARAMS;
258 #endif
259
260   while (inptr != inend)
261     {
262       /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
263          compiler generating better code.  They will be optimized away
264          since MIN_NEEDED_OUTPUT is always a constant.  */
265       if ((MIN_NEEDED_OUTPUT != 1
266            && __builtin_expect (outptr + MIN_NEEDED_OUTPUT > outend, 0))
267           || (MIN_NEEDED_OUTPUT == 1
268               && __builtin_expect (outptr >= outend, 0)))
269         {
270           /* Overflow in the output buffer.  */
271           result = __GCONV_FULL_OUTPUT;
272           break;
273         }
274       if (MIN_NEEDED_INPUT > 1
275           && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
276         {
277           /* We don't have enough input for another complete input
278              character.  */
279           result = __GCONV_INCOMPLETE_INPUT;
280           break;
281         }
282
283       /* Here comes the body the user provides.  It can stop with
284          RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
285          input characters vary in size), GCONV_ILLEGAL_INPUT, or
286          GCONV_FULL_OUTPUT (if the output characters vary in size).  */
287       BODY
288     }
289
290   /* Update the pointers pointed to by the parameters.  */
291   *inptrp = inptr;
292   *outptrp = outptr;
293 #ifdef UPDATE_PARAMS
294   UPDATE_PARAMS;
295 #endif
296
297   return result;
298 }
299
300
301 /* Include the file a second time to define the function to handle
302    unaligned access.  */
303 #if !defined DEFINE_UNALIGNED && !defined _STRING_ARCH_unaligned \
304     && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
305     && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
306 # undef get16
307 # undef get32
308 # undef put16
309 # undef put32
310 # undef unaligned
311
312 # define DEFINE_UNALIGNED
313 # include "loop.c"
314 # undef DEFINE_UNALIGNED
315 #endif
316
317
318 #if MAX_NEEDED_INPUT > 1
319 # define SINGLE(fct) SINGLE2 (fct)
320 # define SINGLE2(fct) fct##_single
321 static inline int
322 SINGLE(LOOPFCT) (struct __gconv_step *step,
323                  struct __gconv_step_data *step_data,
324                  const unsigned char **inptrp, const unsigned char *inend,
325                  unsigned char **outptrp, unsigned char *outend,
326                  size_t *irreversible EXTRA_LOOP_DECLS)
327 {
328   mbstate_t *state = step_data->__statep;
329 #ifdef LOOP_NEED_FLAGS
330   int flags = step_data->__flags;
331 #endif
332 #ifdef LOOP_NEED_DATA
333   void *data = step->__data;
334 #endif
335   int result = __GCONV_OK;
336   unsigned char bytebuf[MAX_NEEDED_INPUT];
337   const unsigned char *inptr = *inptrp;
338   unsigned char *outptr = *outptrp;
339   size_t inlen;
340
341 #ifdef INIT_PARAMS
342   INIT_PARAMS;
343 #endif
344
345 #ifdef UNPACK_BYTES
346   UNPACK_BYTES
347 #else
348   /* Add the bytes from the state to the input buffer.  */
349   for (inlen = 0; inlen < (size_t) (state->__count & 7); ++inlen)
350     bytebuf[inlen] = state->__value.__wchb[inlen];
351 #endif
352
353   /* Are there enough bytes in the input buffer?  */
354   if (__builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
355     {
356       *inptrp = inend;
357 #ifdef STORE_REST
358       inptr = bytebuf;
359       inptrp = &inptr;
360       inend = &bytebuf[inlen];
361
362       STORE_REST
363 #else
364       /* We don't have enough input for another complete input
365          character.  */
366       while (inptr < inend)
367         state->__value.__wchb[inlen++] = *inptr++;
368 #endif
369
370       return __GCONV_INCOMPLETE_INPUT;
371     }
372
373   /* Enough space in output buffer.  */
374   if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
375       || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
376     /* Overflow in the output buffer.  */
377     return __GCONV_FULL_OUTPUT;
378
379   /*  Now add characters from the normal input buffer.  */
380   do
381     bytebuf[inlen++] = *inptr++;
382   while (inlen < MAX_NEEDED_INPUT && inptr < inend);
383
384   inptr = bytebuf;
385   inend = &bytebuf[inlen];
386
387   do
388     {
389       BODY
390     }
391   while (0);
392
393   /* Now we either have produced an output character and consumed all the
394      bytes from the state and at least one more, or the character is still
395      incomplete, or we have some other error (like illegal input character,
396      no space in output buffer).  */
397   if (__builtin_expect (inptr != bytebuf, 1))
398     {
399       /* We found a new character.  */
400       assert (inptr - bytebuf > (state->__count & 7));
401
402       *inptrp += inptr - bytebuf - (state->__count & 7);
403       *outptrp = outptr;
404
405       result = __GCONV_OK;
406
407       /* Clear the state buffer.  */
408       state->__count &= ~7;
409     }
410   else if (result == __GCONV_INCOMPLETE_INPUT)
411     {
412       /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
413          available.  */
414       assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
415
416       *inptrp += inend - bytebuf - (state->__count & 7);
417 #ifdef STORE_REST
418       inptrp = &inptr;
419
420       STORE_REST
421 #else
422       /* We don't have enough input for another complete input
423          character.  */
424       while (inptr < inend)
425         state->__value.__wchb[inlen++] = *inptr++;
426 #endif
427     }
428
429   return result;
430 }
431 # undef SINGLE
432 # undef SINGLE2
433 #endif
434
435
436 /* We remove the macro definitions so that we can include this file again
437    for the definition of another function.  */
438 #undef MIN_NEEDED_INPUT
439 #undef MAX_NEEDED_INPUT
440 #undef MIN_NEEDED_OUTPUT
441 #undef MAX_NEEDED_OUTPUT
442 #undef LOOPFCT
443 #undef BODY
444 #undef LOOPFCT
445 #undef EXTRA_LOOP_DECLS
446 #undef INIT_PARAMS
447 #undef UPDATE_PARAMS
448 #undef UNPACK_BYTES
449 #undef LOOP_NEED_STATE
450 #undef LOOP_NEED_FLAGS
451 #undef LOOP_NEED_DATA
452 #undef get16
453 #undef get32
454 #undef put16
455 #undef put32
456 #undef unaligned