1999-11-29 Andreas Schwab <schwab@suse.de>
[glibc/pb-stable.git] / iconv / loop.c
blobada4f0a755e079e26e883aa9db16fd773ab96f28
1 /* Conversion loop frame work.
2 Copyright (C) 1998, 1999 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 COUNT_CONVERTED optional macro which is used to count the actual
40 number of characters converted. For some conversion
41 it is easy to compute the value afterwards, but for
42 others explicit counting is cheaper.
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.
53 #include <gconv.h>
54 #include <wchar.h>
55 #include <sys/param.h> /* For MIN. */
56 #define __need_size_t
57 #include <stddef.h>
60 /* We need at least one byte for the next round. */
61 #ifndef MIN_NEEDED_INPUT
62 # error "MIN_NEEDED_INPUT definition missing"
63 #endif
65 /* Let's see how many bytes we produce. */
66 #ifndef MAX_NEEDED_INPUT
67 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
68 #endif
70 /* We produce at least one byte in the next round. */
71 #ifndef MIN_NEEDED_OUTPUT
72 # error "MIN_NEEDED_OUTPUT definition missing"
73 #endif
75 /* Let's see how many bytes we produce. */
76 #ifndef MAX_NEEDED_OUTPUT
77 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
78 #endif
80 /* Default name for the function. */
81 #ifndef LOOPFCT
82 # define LOOPFCT loop
83 #endif
85 /* Make sure we have a loop body. */
86 #ifndef BODY
87 # error "Definition of BODY missing for function" LOOPFCT
88 #endif
90 /* We can calculate the number of converted characters easily if one
91 of the character sets has a fixed width. */
92 #ifndef COUNT_CONVERTED
93 # if MIN_NEEDED_INPUT == MAX_NEEDED_INPUT
94 # if MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
95 /* Decide whether one of the charsets has size 1. */
96 # if MIN_NEEDED_INPUT == 1
97 # define COUNT_CONVERTED (inptr - *inptrp)
98 # elif MIN_NEEDED_OUTPUT == 1
99 # define COUNT_CONVERTED (outptr - *outptrp)
100 # else
101 /* Else we should see whether one of the two numbers is a power of 2. */
102 # define COUNT_CONVERTED \
103 ((MIN_NEEDED_INPUT & (-MIN_NEEDED_INPUT)) == MIN_NEEDED_INPUT \
104 ? (inptr - *inptrp) : (outptr - *outptrp))
105 # endif
106 # else
107 # define COUNT_CONVERTED ((inptr - *inptrp) / MIN_NEEDED_INPUT)
108 # endif
109 # elif MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
110 # define COUNT_CONVERTED ((outptr - *outptrp) / MIN_NEEDED_OUTPUT)
111 # endif
112 #endif
115 /* If no arguments have to passed to the loop function define the macro
116 as empty. */
117 #ifndef EXTRA_LOOP_DECLS
118 # define EXTRA_LOOP_DECLS
119 #endif
122 /* The function returns the status, as defined in gconv.h. */
123 static inline int
124 LOOPFCT (const unsigned char **inptrp, const unsigned char *inend,
125 unsigned char **outptrp, unsigned char *outend, mbstate_t *state,
126 void *data, size_t *converted EXTRA_LOOP_DECLS)
128 int result = __GCONV_OK;
129 const unsigned char *inptr = *inptrp;
130 unsigned char *outptr = *outptrp;
131 #ifndef COUNT_CONVERTED
132 size_t done = 0;
133 #endif
135 /* We run one loop where we avoid checks for underflow/overflow of the
136 buffers to speed up the conversion a bit. */
137 size_t min_in_rounds = (inend - inptr) / MAX_NEEDED_INPUT;
138 size_t min_out_rounds = (outend - outptr) / MAX_NEEDED_OUTPUT;
139 size_t min_rounds = MIN (min_in_rounds, min_out_rounds);
141 #ifdef INIT_PARAMS
142 INIT_PARAMS;
143 #endif
145 #undef NEED_LENGTH_TEST
146 #define NEED_LENGTH_TEST 0
147 while (min_rounds-- > 0)
149 /* Here comes the body the user provides. It can stop with RESULT
150 set to GCONV_INCOMPLETE_INPUT (if the size of the input characters
151 vary in size), GCONV_ILLEGAL_INPUT, or GCONV_FULL_OUTPUT (if the
152 output characters vary in size. */
153 BODY
155 /* If necessary count the successful conversion. */
156 #ifndef COUNT_CONVERTED
157 ++done;
158 #endif
161 if (result == __GCONV_OK)
163 #if MIN_NEEDED_INPUT == MAX_NEEDED_INPUT \
164 && MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
165 /* We don't need to start another loop since we were able to determine
166 the maximal number of characters to copy in advance. What remains
167 to be determined is the status. */
168 if (inptr == inend)
169 /* No more input. */
170 result = __GCONV_EMPTY_INPUT;
171 else if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
172 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
173 /* Overflow in the output buffer. */
174 result = __GCONV_FULL_OUTPUT;
175 else
176 /* We have something left in the input buffer. */
177 result = __GCONV_INCOMPLETE_INPUT;
178 #else
179 result = __GCONV_EMPTY_INPUT;
181 # undef NEED_LENGTH_TEST
182 # define NEED_LENGTH_TEST 1
183 while (inptr != inend)
185 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
186 compiler generating better code. It will optimized away
187 since MIN_NEEDED_OUTPUT is always a constant. */
188 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
189 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
191 /* Overflow in the output buffer. */
192 result = __GCONV_FULL_OUTPUT;
193 break;
195 if (MIN_NEEDED_INPUT > 1 && inptr + MIN_NEEDED_INPUT > inend)
197 /* We don't have enough input for another complete input
198 character. */
199 result = __GCONV_INCOMPLETE_INPUT;
200 break;
203 /* Here comes the body the user provides. It can stop with
204 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
205 input characters vary in size), GCONV_ILLEGAL_INPUT, or
206 GCONV_FULL_OUTPUT (if the output characters vary in size). */
207 BODY
209 /* If necessary count the successful conversion. */
210 # ifndef COUNT_CONVERTED
211 ++done;
212 # endif
214 #endif /* Input and output charset are not both fixed width. */
217 /* Add the number of characters we actually converted. */
218 #ifdef COUNT_CONVERTED
219 *converted += COUNT_CONVERTED;
220 #else
221 *converted += done;
222 #endif
224 /* Update the pointers pointed to by the parameters. */
225 *inptrp = inptr;
226 *outptrp = outptr;
227 #ifdef UPDATE_PARAMS
228 UPDATE_PARAMS;
229 #endif
231 return result;
235 /* We remove the macro definitions so that we can include this file again
236 for the definition of another function. */
237 #undef MIN_NEEDED_INPUT
238 #undef MAX_NEEDED_INPUT
239 #undef MIN_NEEDED_OUTPUT
240 #undef MAX_NEEDED_OUTPUT
241 #undef LOOPFCT
242 #undef COUNT_CONVERTED
243 #undef BODY
244 #undef LOOPFCT
245 #undef EXTRA_LOOP_DECLS
246 #undef INIT_PARAMS
247 #undef UPDATE_PARAMS