Update.
[glibc.git] / iconv / loop.c
blob9c5dbfca7787577323eaec82780835063b97c7d7
1 /* Conversion loop frame work.
2 Copyright (C) 1998, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 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) *((uint16_t *) (addr))
67 # define get32(addr) *((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))[2] = __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 #endif
138 /* Let's see how many bytes we produce. */
139 #ifndef MAX_NEEDED_INPUT
140 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
141 #endif
143 /* We produce at least one byte in the next round. */
144 #ifndef MIN_NEEDED_OUTPUT
145 # error "MIN_NEEDED_OUTPUT definition missing"
146 #endif
148 /* Let's see how many bytes we produce. */
149 #ifndef MAX_NEEDED_OUTPUT
150 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
151 #endif
153 /* Default name for the function. */
154 #ifndef LOOPFCT
155 # define LOOPFCT loop
156 #endif
158 /* Make sure we have a loop body. */
159 #ifndef BODY
160 # error "Definition of BODY missing for function" LOOPFCT
161 #endif
164 /* If no arguments have to passed to the loop function define the macro
165 as empty. */
166 #ifndef EXTRA_LOOP_DECLS
167 # define EXTRA_LOOP_DECLS
168 #endif
171 /* The function returns the status, as defined in gconv.h. */
172 static inline int
173 FCTNAME (LOOPFCT) (const unsigned char **inptrp, const unsigned char *inend,
174 unsigned char **outptrp, unsigned char *outend,
175 mbstate_t *state, void *data, size_t *converted
176 EXTRA_LOOP_DECLS)
178 int result = __GCONV_OK;
179 const unsigned char *inptr = *inptrp;
180 unsigned char *outptr = *outptrp;
182 /* We run one loop where we avoid checks for underflow/overflow of the
183 buffers to speed up the conversion a bit. */
184 size_t min_in_rounds = (inend - inptr) / MAX_NEEDED_INPUT;
185 size_t min_out_rounds = (outend - outptr) / MAX_NEEDED_OUTPUT;
186 size_t min_rounds = MIN (min_in_rounds, min_out_rounds);
188 #ifdef INIT_PARAMS
189 INIT_PARAMS;
190 #endif
192 #undef NEED_LENGTH_TEST
193 #define NEED_LENGTH_TEST 0
194 while (min_rounds-- > 0)
196 /* Here comes the body the user provides. It can stop with RESULT
197 set to GCONV_INCOMPLETE_INPUT (if the size of the input characters
198 vary in size), GCONV_ILLEGAL_INPUT, or GCONV_FULL_OUTPUT (if the
199 output characters vary in size. */
200 BODY
203 if (result == __GCONV_OK)
205 #if MIN_NEEDED_INPUT == MAX_NEEDED_INPUT \
206 && MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
207 /* We don't need to start another loop since we were able to determine
208 the maximal number of characters to copy in advance. What remains
209 to be determined is the status. */
210 if (inptr == inend)
211 /* No more input. */
212 result = __GCONV_EMPTY_INPUT;
213 else if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
214 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
215 /* Overflow in the output buffer. */
216 result = __GCONV_FULL_OUTPUT;
217 else
218 /* We have something left in the input buffer. */
219 result = __GCONV_INCOMPLETE_INPUT;
220 #else
221 result = __GCONV_EMPTY_INPUT;
223 # undef NEED_LENGTH_TEST
224 # define NEED_LENGTH_TEST 1
225 while (inptr != inend)
227 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
228 compiler generating better code. It will optimized away
229 since MIN_NEEDED_OUTPUT is always a constant. */
230 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
231 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
233 /* Overflow in the output buffer. */
234 result = __GCONV_FULL_OUTPUT;
235 break;
237 if (MIN_NEEDED_INPUT > 1 && inptr + MIN_NEEDED_INPUT > inend)
239 /* We don't have enough input for another complete input
240 character. */
241 result = __GCONV_INCOMPLETE_INPUT;
242 break;
245 /* Here comes the body the user provides. It can stop with
246 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
247 input characters vary in size), GCONV_ILLEGAL_INPUT, or
248 GCONV_FULL_OUTPUT (if the output characters vary in size). */
249 BODY
251 #endif /* Input and output charset are not both fixed width. */
254 /* Update the pointers pointed to by the parameters. */
255 *inptrp = inptr;
256 *outptrp = outptr;
257 #ifdef UPDATE_PARAMS
258 UPDATE_PARAMS;
259 #endif
261 return result;
265 /* Include the file a second time to define the function to define the
266 function to handle unaligned access. */
267 #if !defined DEFINE_UNALIGNED && !defined _STRING_ARCH_unaligned \
268 && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
269 && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
270 # undef get16
271 # undef get32
272 # undef put16
273 # undef put32
274 # undef unaligned
276 # define DEFINE_UNALIGNED
277 # include "loop.c"
278 # undef DEFINE_UNALIGNED
279 #endif
282 #if MAX_NEEDED_INPUT > 1
283 # define SINGLE(fct) SINGLE2 (fct)
284 # define SINGLE2(fct) fct##_single
285 static inline int
286 SINGLE(LOOPFCT) (const unsigned char **inptrp, const unsigned char *inend,
287 unsigned char **outptrp, unsigned char *outend,
288 mbstate_t *state, void *data, size_t *converted
289 EXTRA_LOOP_DECLS)
291 int result = __GCONV_OK;
292 unsigned char bytebuf[MAX_NEEDED_INPUT];
293 const unsigned char *inptr = *inptrp;
294 unsigned char *outptr = *outptrp;
295 size_t inlen;
297 #ifdef INIT_PARAMS
298 INIT_PARAMS;
299 #endif
301 #ifdef UNPACK_BYTES
302 UNPACK_BYTES
303 #else
304 /* Add the bytes from the state to the input buffer. */
305 for (inlen = 0; inlen < (state->__count & 7); ++ inlen)
306 bytebuf[inlen] = state->__value.__wchb[inlen];
307 #endif
309 /* Are there enough bytes in the input buffer? */
310 if (__builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
312 *inptrp = inend;
313 #ifdef STORE_REST
314 inptr = bytebuf;
315 inptrp = &inptr;
316 inend = &bytebuf[inlen];
318 STORE_REST
319 #else
320 /* We don't have enough input for another complete input
321 character. */
322 while (inptr < inend)
323 state->__value.__wchb[inlen++] = *inptr++;
324 #endif
326 return __GCONV_INCOMPLETE_INPUT;
329 /* Enough space in output buffer. */
330 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
331 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
332 /* Overflow in the output buffer. */
333 return __GCONV_FULL_OUTPUT;
335 /* Now add characters from the normal input buffer. */
337 bytebuf[inlen++] = *inptr++;
338 while (inlen < MAX_NEEDED_INPUT && inptr < inend);
340 inptr = bytebuf;
341 inend = &bytebuf[inlen];
342 #undef NEED_LENGTH_TEST
343 #define NEED_LENGTH_TEST 1
346 BODY
348 while (0);
350 /* Now we either have produced an output character and consumed all the
351 bytes from the state and at least one more, or the character is still
352 incomplete, or we have some other error (like illegal input character,
353 no space in output buffer). */
354 if (inptr != bytebuf)
356 /* We found a new character. */
357 assert (inptr - bytebuf > (state->__count & 7));
359 *inptrp += inptr - bytebuf - (state->__count & 7);
360 *outptrp = outptr;
362 result = __GCONV_OK;
364 /* Clear the state buffer. */
365 state->__count &= ~7;
367 else if (result == __GCONV_INCOMPLETE_INPUT)
369 /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
370 available. */
371 assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
373 *inptrp += inend - bytebuf - (state->__count & 7);
374 #ifdef STORE_REST
375 inptrp = &inptr;
377 STORE_REST
378 #else
379 /* We don't have enough input for another complete input
380 character. */
381 while (inptr < inend)
382 state->__value.__wchb[inlen++] = *inptr++;
383 #endif
386 return result;
388 # undef SINGLE
389 # undef SINGLE2
390 #endif
393 /* We remove the macro definitions so that we can include this file again
394 for the definition of another function. */
395 #undef MIN_NEEDED_INPUT
396 #undef MAX_NEEDED_INPUT
397 #undef MIN_NEEDED_OUTPUT
398 #undef MAX_NEEDED_OUTPUT
399 #undef LOOPFCT
400 #undef BODY
401 #undef LOOPFCT
402 #undef EXTRA_LOOP_DECLS
403 #undef INIT_PARAMS
404 #undef UPDATE_PARAMS
405 #undef get16
406 #undef get32
407 #undef put16
408 #undef put32
409 #undef unaligned
410 #undef UNPACK_BYTES