]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/loop.c
b8657d574cf16dd305ba5f425dd79133ff016b2b
[thirdparty/glibc.git] / iconv / loop.c
1 /* Conversion loop frame work.
2 Copyright (C) 1998 Free Software Foundation, Inc.
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
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.
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
14 Library General Public License for more details.
15
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. */
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
35 Both values have a default of 1.
36
37 LOOPFCT name of the function created. If not specified
38 the name is `loop' but this prevents the use
39 of multiple functions in the same file.
40
41 COUNT_CONVERTED optional macro which is used to count the actual
42 number of characters converted. For some conversion
43 it is easy to compute the value afterwards, but for
44 others explicit counting is cheaper.
45
46 BODY this is supposed to expand to the body of the loop.
47 The user must provide this.
48 */
49
50 #include <gconv.h>
51 #include <sys/param.h> /* For MIN. */
52 #define __need_size_t
53 #include <stddef.h>
54
55
56 /* We need at least one byte for the next round. */
57 #ifndef MIN_NEEDED_INPUT
58 # define MIN_NEEDED_INPUT 1
59 #endif
60
61 /* Let's see how many bytes we produce. */
62 #ifndef MAX_NEEDED_INPUT
63 # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
64 #endif
65
66 /* We produce at least one byte in the next round. */
67 #ifndef MIN_NEEDED_OUTPUT
68 # define MIN_NEEDED_OUTPUT 1
69 #endif
70
71 /* Let's see how many bytes we produce. */
72 #ifndef MAX_NEEDED_OUTPUT
73 # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
74 #endif
75
76 /* Default name for the function. */
77 #ifndef LOOPFCT
78 # define LOOPFCT loop
79 #endif
80
81 /* Make sure we have a loop body. */
82 #ifndef BODY
83 # error "Definition of BODY missing for function" LOOPFCT
84 #endif
85
86 /* We can calculate the number of converted characters easily if one
87 of the character sets has a fixed width. */
88 #ifndef COUNT_CONVERTED
89 # if MIN_NEEDED_INPUT == MAX_NEEDED_INPUT
90 # if MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
91 /* Decide whether one of the charsets has size 1. */
92 # if MIN_NEEDED_INPUT == 1
93 # define COUNT_CONVERTED (inptr - *inptrp)
94 # elif MIN_NEEDED_OUTPUT == 1
95 # define COUNT_CONVERTED (outptr - *outptrp)
96 # else
97 /* Else we should see whether one of the two numbers is a power of 2. */
98 # define COUNT_CONVERTED \
99 ((MIN_NEEDED_INPUT & (-MIN_NEEDED_INPUT)) == MIN_NEEDED_INPUT \
100 ? (inptr - *inptrp) : (outptr - *outptrp))
101 # endif
102 # else
103 # define COUNT_CONVERTED (inptr - *inptrp)
104 # endif
105 # elif MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
106 # define COUNT_CONVERTED (outptr - *outptrp)
107 # endif
108 #endif
109
110
111 /* The function returns the status, as defined in gconv.h. */
112 static inline int
113 LOOPFCT (const unsigned char **inptrp, const unsigned char *inend,
114 unsigned char **outptrp, unsigned char *outend, mbstate_t *state,
115 void *data, size_t *converted)
116 {
117 int result = GCONV_OK;
118 const unsigned char *inptr = *inptrp;
119 unsigned char *outptr = *outptrp;
120 #ifndef COUNT_CONVERTED
121 size_t done = 0;
122 #endif
123
124 /* We run one loop where we avoid checks for underflow/overflow of the
125 buffers to speed up the conversion a bit. */
126 size_t min_in_rounds = (inend - inptr) / MAX_NEEDED_INPUT;
127 size_t min_out_rounds = (outend - outptr) / MAX_NEEDED_OUTPUT;
128 size_t min_rounds = MIN (min_in_rounds, min_out_rounds);
129
130 #undef NEED_LENGTH_TEST
131 #define NEED_LENGTH_TEST 0
132 while (min_rounds-- > 0)
133 {
134 /* Here comes the body the user provides. It can stop with RESULT
135 set to GCONV_INCOMPLETE_INPUT (if the size of the input characters
136 vary in size), GCONV_ILLEGAL_INPUT, or GCONV_FULL_OUTPUT (if the
137 output characters vary in size. */
138 BODY
139
140 /* If necessary count the successful conversion. */
141 #ifndef COUNT_CONVERTED
142 ++done;
143 #endif
144 }
145
146 if (result == GCONV_OK)
147 {
148 #if MIN_NEEDED_INPUT == MAX_NEEDED_INPUT \
149 && MIN_NEEDED_OUTPUT == MAX_NEEDED_OUTPUT
150 /* We don't need to start another loop since we were able to determine
151 the maximal number of characters to copy in advance. What remains
152 to be determined is the status. */
153 if (inptr == inend)
154 /* No more input. */
155 result = GCONV_EMPTY_INPUT;
156 else if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
157 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
158 /* Overflow in the output buffer. */
159 result = GCONV_FULL_OUTPUT;
160 else
161 /* We have something left in the input buffer. */
162 result = GCONV_INCOMPLETE_INPUT;
163 #else
164 result = GCONV_EMPTY_INPUT;
165
166 # undef NEED_LENGTH_TEST
167 # define NEED_LENGTH_TEST 1
168 while (inptr != inend)
169 {
170 /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
171 compiler generating better code. It will optimized away
172 since MIN_NEEDED_OUTPUT is always a constant. */
173 if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
174 || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
175 {
176 /* Overflow in the output buffer. */
177 result = GCONV_FULL_OUTPUT;
178 break;
179 }
180 if (MIN_NEEDED_INPUT > 1 && inptr + MIN_NEEDED_INPUT > inend)
181 {
182 /* We don't have enough input for another complete input
183 character. */
184 result = GCONV_INCOMPLETE_INPUT;
185 break;
186 }
187
188 /* Here comes the body the user provides. It can stop with
189 RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
190 input characters vary in size), GCONV_ILLEGAL_INPUT, or
191 GCONV_FULL_OUTPUT (if the output characters vary in size. */
192 BODY
193
194 /* If necessary count the successful conversion. */
195 # ifndef COUNT_CONVERTED
196 ++done;
197 # endif
198 }
199 #endif /* Input and output charset are not both fixed width. */
200 }
201
202 /* Add the number of characters we actually converted. */
203 #ifdef COUNT_CONVERTED
204 *converted += COUNT_CONVERTED;
205 #else
206 *converted += done;
207 #endif
208
209 /* Update the pointers pointed to by the parameters. */
210 *inptrp = inptr;
211 *outptrp = outptr;
212
213 return result;
214 }
215
216
217 /* We remove the macro definitions so that we can include this file again
218 for the definition of another function. */
219 #undef MIN_NEEDED_INPUT
220 #undef MAX_NEEDED_INPUT
221 #undef MIN_NEEDED_OUTPUT
222 #undef MAX_NEEDED_OUTPUT
223 #undef LOOPFCT
224 #undef COUNT_CONVERTED
225 #undef BODY
226 #undef LOOPFCT