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.
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.
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.
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.
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
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:
31 MIN_NEEDED_INPUT minimal number of input bytes needed for the next
33 MIN_NEEDED_OUTPUT minimal number of bytes produced by the next round
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.
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.
44 BODY this is supposed to expand to the body of the loop.
45 The user must provide this.
47 EXTRA_LOOP_DECLS extra arguments passed from converion loop call.
49 INIT_PARAMS code to define and initialize variables from params.
50 UPDATE_PARAMS code to store result in params.
59 #include <sys/param.h> /* For MIN. */
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. */
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))
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)
78 # define FCTNAME2(name) name
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])
91 # define put16(addr, val) \
92 ({ uint16_t __val = (val); \
93 ((unsigned char *) (addr))[0] = __val; \
94 ((unsigned char *) (addr))[1] = __val >> 8; \
96 # define put32(addr, val) \
97 ({ uint32_t __val = (val); \
98 ((unsigned char *) (addr))[0] = __val; \
100 ((unsigned char *) (addr))[1] = __val; \
102 ((unsigned char *) (addr))[2] = __val; \
104 ((unsigned char *) (addr))[3] = __val; \
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])
116 # define put16(addr, val) \
117 ({ uint16_t __val = (val); \
118 ((unsigned char *) (addr))[1] = __val; \
119 ((unsigned char *) (addr))[0] = __val >> 8; \
121 # define put32(addr, val) \
122 ({ uint32_t __val = (val); \
123 ((unsigned char *) (addr))[3] = __val; \
125 ((unsigned char *) (addr))[2] = __val; \
127 ((unsigned char *) (addr))[1] = __val; \
129 ((unsigned char *) (addr))[0] = __val; \
133 # define FCTNAME2(name) name##_unaligned
135 #define FCTNAME(name) FCTNAME2(name)
138 /* We need at least one byte for the next round. */
139 #ifndef MIN_NEEDED_INPUT
140 # error "MIN_NEEDED_INPUT definition missing"
143 /* Let's see how many bytes we produce. */
144 #ifndef MAX_NEEDED_INPUT
145 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
148 /* We produce at least one byte in the next round. */
149 #ifndef MIN_NEEDED_OUTPUT
150 # error "MIN_NEEDED_OUTPUT definition missing"
153 /* Let's see how many bytes we produce. */
154 #ifndef MAX_NEEDED_OUTPUT
155 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
158 /* Default name for the function. */
160 # define LOOPFCT loop
163 /* Make sure we have a loop body. */
165 # error "Definition of BODY missing for function" LOOPFCT
169 /* If no arguments have to passed to the loop function define the macro
171 #ifndef EXTRA_LOOP_DECLS
172 # define EXTRA_LOOP_DECLS
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))
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) \
187 struct __gconv_trans_data *trans; \
189 result = __GCONV_ILLEGAL_INPUT; \
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. */ \
196 /* First try the transliteration methods. */ \
197 for (trans = step_data->__trans; trans != NULL; trans = trans->__next) \
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) \
205 /* If any of them recognized the input continue with the loop. */ \
206 if (result != __GCONV_ILLEGAL_INPUT) \
209 /* Next see whether we have to ignore the error. If not, stop. */ \
210 if (! ignore_errors_p ()) \
213 /* When we come here it means we ignore the character. */ \
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) \
226 /* TAG characters are those in the range U+E0000..U+E007F. */ \
227 if (((Character) >> 7) == (0xe0000 >> 7)) \
235 /* The function returns the status, as defined in gconv.h. */
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)
243 #ifdef LOOP_NEED_STATE
244 mbstate_t *state = step_data->__statep;
246 #ifdef LOOP_NEED_FLAGS
247 int flags = step_data->__flags;
249 #ifdef LOOP_NEED_DATA
250 void *data = step->__data;
252 int result = __GCONV_EMPTY_INPUT;
253 const unsigned char *inptr = *inptrp;
254 unsigned char *outptr = *outptrp;
260 while (inptr != inend)
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)))
270 /* Overflow in the output buffer. */
271 result = __GCONV_FULL_OUTPUT;
274 if (MIN_NEEDED_INPUT > 1
275 && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
277 /* We don't have enough input for another complete input
279 result = __GCONV_INCOMPLETE_INPUT;
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). */
290 /* Update the pointers pointed to by the parameters. */
301 /* Include the file a second time to define the function to handle
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
312 # define DEFINE_UNALIGNED
314 # undef DEFINE_UNALIGNED
318 #if MAX_NEEDED_INPUT > 1
319 # define SINGLE(fct) SINGLE2 (fct)
320 # define SINGLE2(fct) fct##_single
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)
328 mbstate_t *state = step_data->__statep;
329 #ifdef LOOP_NEED_FLAGS
330 int flags = step_data->__flags;
332 #ifdef LOOP_NEED_DATA
333 void *data = step->__data;
335 int result = __GCONV_OK;
336 unsigned char bytebuf[MAX_NEEDED_INPUT];
337 const unsigned char *inptr = *inptrp;
338 unsigned char *outptr = *outptrp;
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];
353 /* Are there enough bytes in the input buffer? */
354 if (__builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
360 inend = &bytebuf[inlen];
364 /* We don't have enough input for another complete input
366 while (inptr < inend)
367 state->__value.__wchb[inlen++] = *inptr++;
370 return __GCONV_INCOMPLETE_INPUT;
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;
379 /* Now add characters from the normal input buffer. */
381 bytebuf[inlen++] = *inptr++;
382 while (inlen < MAX_NEEDED_INPUT && inptr < inend);
385 inend = &bytebuf[inlen];
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))
399 /* We found a new character. */
400 assert (inptr - bytebuf > (state->__count & 7));
402 *inptrp += inptr - bytebuf - (state->__count & 7);
407 /* Clear the state buffer. */
408 state->__count &= ~7;
410 else if (result == __GCONV_INCOMPLETE_INPUT)
412 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
414 assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
416 *inptrp += inend - bytebuf - (state->__count & 7);
422 /* We don't have enough input for another complete input
424 while (inptr < inend)
425 state->__value.__wchb[inlen++] = *inptr++;
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
445 #undef EXTRA_LOOP_DECLS
449 #undef LOOP_NEED_STATE
450 #undef LOOP_NEED_FLAGS
451 #undef LOOP_NEED_DATA