]>
Commit | Line | Data |
---|---|---|
09376451 | 1 | /* Skeleton for a conversion module. |
2b778ceb | 2 | Copyright (C) 1998-2021 Free Software Foundation, Inc. |
8619129f UD |
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 | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
8619129f UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
8619129f | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
8619129f UD |
19 | |
20 | /* This file can be included to provide definitions of several things | |
21 | many modules have in common. It can be customized using the following | |
22 | macros: | |
23 | ||
24 | DEFINE_INIT define the default initializer. This requires the | |
25 | following symbol to be defined. | |
26 | ||
27 | CHARSET_NAME string with official name of the coded character | |
28 | set (in all-caps) | |
29 | ||
30 | DEFINE_FINI define the default destructor function. | |
31 | ||
32 | MIN_NEEDED_FROM minimal number of bytes needed for the from-charset. | |
33 | MIN_NEEDED_TO likewise for the to-charset. | |
34 | ||
35 | MAX_NEEDED_FROM maximal number of bytes needed for the from-charset. | |
36 | This macro is optional, it defaults to MIN_NEEDED_FROM. | |
37 | MAX_NEEDED_TO likewise for the to-charset. | |
38 | ||
faaa6f62 UD |
39 | FROM_LOOP_MIN_NEEDED_FROM |
40 | FROM_LOOP_MAX_NEEDED_FROM | |
a784e502 UD |
41 | minimal/maximal number of bytes needed on input |
42 | of one round through the FROM_LOOP. Defaults | |
43 | to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively. | |
faaa6f62 UD |
44 | FROM_LOOP_MIN_NEEDED_TO |
45 | FROM_LOOP_MAX_NEEDED_TO | |
a784e502 UD |
46 | minimal/maximal number of bytes needed on output |
47 | of one round through the FROM_LOOP. Defaults | |
48 | to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively. | |
faaa6f62 UD |
49 | TO_LOOP_MIN_NEEDED_FROM |
50 | TO_LOOP_MAX_NEEDED_FROM | |
a784e502 UD |
51 | minimal/maximal number of bytes needed on input |
52 | of one round through the TO_LOOP. Defaults | |
53 | to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively. | |
faaa6f62 UD |
54 | TO_LOOP_MIN_NEEDED_TO |
55 | TO_LOOP_MAX_NEEDED_TO | |
a784e502 UD |
56 | minimal/maximal number of bytes needed on output |
57 | of one round through the TO_LOOP. Defaults | |
58 | to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively. | |
faaa6f62 | 59 | |
8619129f UD |
60 | FROM_DIRECTION this macro is supposed to return a value != 0 |
61 | if we convert from the current character set, | |
62 | otherwise it return 0. | |
63 | ||
64 | EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it | |
65 | defines some code which writes out a sequence | |
faaa6f62 | 66 | of bytes which bring the current state into |
8619129f UD |
67 | the initial state. |
68 | ||
69 | FROM_LOOP name of the function implementing the conversion | |
faaa6f62 | 70 | from the current character set. |
8619129f UD |
71 | TO_LOOP likewise for the other direction |
72 | ||
f6ad4726 UD |
73 | ONE_DIRECTION optional. If defined to 1, only one conversion |
74 | direction is defined instead of two. In this | |
75 | case, FROM_DIRECTION should be defined to 1, and | |
76 | FROM_LOOP and TO_LOOP should have the same value. | |
77 | ||
78 | SAVE_RESET_STATE in case of an error we must reset the state for | |
8619129f UD |
79 | the rerun so this macro must be defined for |
80 | stateful encodings. It takes an argument which | |
81 | is nonzero when saving. | |
82 | ||
83 | RESET_INPUT_BUFFER If the input character sets allow this the macro | |
84 | can be defined to reset the input buffer pointers | |
85 | to cover only those characters up to the error. | |
4802be92 AS |
86 | Note that if the conversion has skipped over |
87 | irreversible characters (due to | |
88 | __GCONV_IGNORE_ERRORS) there is no longer a direct | |
89 | correspondence between input and output pointers, | |
90 | and this macro is not called. | |
8619129f UD |
91 | |
92 | FUNCTION_NAME if not set the conversion function is named `gconv'. | |
28f1c862 UD |
93 | |
94 | PREPARE_LOOP optional code preparing the conversion loop. Can | |
95 | contain variable definitions. | |
918b9d72 | 96 | END_LOOP also optional, may be used to store information |
28f1c862 UD |
97 | |
98 | EXTRA_LOOP_ARGS optional macro specifying extra arguments passed | |
99 | to loop function. | |
faaa6f62 | 100 | |
f9ad060c UD |
101 | STORE_REST optional, needed only when MAX_NEEDED_FROM > 4. |
102 | This macro stores the seen but unconverted input bytes | |
103 | in the state. | |
104 | ||
105 | FROM_ONEBYTE optional. If defined, should be the name of a | |
106 | specialized conversion function for a single byte | |
107 | from the current character set to INTERNAL. This | |
108 | function has prototype | |
109 | wint_t | |
110 | FROM_ONEBYTE (struct __gconv_step *, unsigned char); | |
111 | and does a special conversion: | |
112 | - The input is a single byte. | |
113 | - The output is a single uint32_t. | |
114 | - The state before the conversion is the initial state; | |
115 | the state after the conversion is irrelevant. | |
116 | - No transliteration. | |
117 | - __invocation_counter = 0. | |
118 | - __internal_use = 1. | |
119 | - do_flush = 0. | |
120 | ||
faaa6f62 UD |
121 | Modules can use mbstate_t to store conversion state as follows: |
122 | ||
123 | * Bits 2..0 of '__count' contain the number of lookahead input bytes | |
124 | stored in __value.__wchb. Always zero if the converter never | |
125 | returns __GCONV_INCOMPLETE_INPUT. | |
126 | ||
127 | * Bits 31..3 of '__count' are module dependent shift state. | |
128 | ||
129 | * __value: When STORE_REST/UNPACK_BYTES aren't defined and when the | |
130 | converter has returned __GCONV_INCOMPLETE_INPUT, this contains | |
131 | at most 4 lookahead bytes. Converters with an mb_cur_max > 4 | |
132 | (currently only UTF-8) must find a way to store their state | |
133 | in __value.__wch and define STORE_REST/UNPACK_BYTES appropriately. | |
134 | ||
135 | When __value contains lookahead, __count must not be zero, because | |
136 | the converter is not in the initial state then, and mbsinit() -- | |
137 | defined as a (__count == 0) test -- must reflect this. | |
8619129f UD |
138 | */ |
139 | ||
140 | #include <assert.h> | |
7ac6fad9 | 141 | #include <iconv/gconv_int.h> |
8619129f UD |
142 | #include <string.h> |
143 | #define __need_size_t | |
144 | #define __need_NULL | |
145 | #include <stddef.h> | |
c66dbe00 UD |
146 | |
147 | #ifndef STATIC_GCONV | |
148 | # include <dlfcn.h> | |
c66dbe00 | 149 | #endif |
8619129f | 150 | |
915a6c51 | 151 | #include <sysdep.h> |
e054f494 | 152 | #include <stdint.h> |
915a6c51 | 153 | |
a808d541 UD |
154 | #ifndef DL_CALL_FCT |
155 | # define DL_CALL_FCT(fct, args) fct args | |
156 | #endif | |
157 | ||
8619129f | 158 | /* The direction objects. */ |
45541bdf | 159 | #if DEFINE_INIT |
8619129f | 160 | # ifndef FROM_DIRECTION |
45541bdf UD |
161 | # define FROM_DIRECTION_VAL NULL |
162 | # define TO_DIRECTION_VAL ((void *) ~((uintptr_t) 0)) | |
163 | # define FROM_DIRECTION (step->__data == FROM_DIRECTION_VAL) | |
8619129f UD |
164 | # endif |
165 | #else | |
166 | # ifndef FROM_DIRECTION | |
dbc92dc6 | 167 | # error "FROM_DIRECTION must be provided if non-default init is used" |
8619129f UD |
168 | # endif |
169 | #endif | |
170 | ||
8619129f UD |
171 | /* How many bytes are needed at most for the from-charset. */ |
172 | #ifndef MAX_NEEDED_FROM | |
173 | # define MAX_NEEDED_FROM MIN_NEEDED_FROM | |
174 | #endif | |
175 | ||
176 | /* Same for the to-charset. */ | |
177 | #ifndef MAX_NEEDED_TO | |
178 | # define MAX_NEEDED_TO MIN_NEEDED_TO | |
179 | #endif | |
180 | ||
faaa6f62 UD |
181 | /* Defaults for the per-direction min/max constants. */ |
182 | #ifndef FROM_LOOP_MIN_NEEDED_FROM | |
183 | # define FROM_LOOP_MIN_NEEDED_FROM MIN_NEEDED_FROM | |
184 | #endif | |
185 | #ifndef FROM_LOOP_MAX_NEEDED_FROM | |
186 | # define FROM_LOOP_MAX_NEEDED_FROM MAX_NEEDED_FROM | |
187 | #endif | |
188 | #ifndef FROM_LOOP_MIN_NEEDED_TO | |
189 | # define FROM_LOOP_MIN_NEEDED_TO MIN_NEEDED_TO | |
190 | #endif | |
191 | #ifndef FROM_LOOP_MAX_NEEDED_TO | |
192 | # define FROM_LOOP_MAX_NEEDED_TO MAX_NEEDED_TO | |
193 | #endif | |
194 | #ifndef TO_LOOP_MIN_NEEDED_FROM | |
195 | # define TO_LOOP_MIN_NEEDED_FROM MIN_NEEDED_TO | |
196 | #endif | |
197 | #ifndef TO_LOOP_MAX_NEEDED_FROM | |
198 | # define TO_LOOP_MAX_NEEDED_FROM MAX_NEEDED_TO | |
199 | #endif | |
200 | #ifndef TO_LOOP_MIN_NEEDED_TO | |
201 | # define TO_LOOP_MIN_NEEDED_TO MIN_NEEDED_FROM | |
202 | #endif | |
203 | #ifndef TO_LOOP_MAX_NEEDED_TO | |
204 | # define TO_LOOP_MAX_NEEDED_TO MAX_NEEDED_FROM | |
205 | #endif | |
206 | ||
8619129f | 207 | |
77e1d15a UD |
208 | /* Define macros which can access unaligned buffers. These macros are |
209 | supposed to be used only in code outside the inner loops. For the inner | |
210 | loops we have other definitions which allow optimized access. */ | |
27822ce6 | 211 | #if _STRING_ARCH_unaligned |
77e1d15a | 212 | /* We can handle unaligned memory access. */ |
a784e502 UD |
213 | # define get16u(addr) *((const uint16_t *) (addr)) |
214 | # define get32u(addr) *((const uint32_t *) (addr)) | |
77e1d15a UD |
215 | |
216 | /* We need no special support for writing values either. */ | |
c1db8b0d UD |
217 | # define put16u(addr, val) *((uint16_t *) (addr)) = (val) |
218 | # define put32u(addr, val) *((uint32_t *) (addr)) = (val) | |
77e1d15a UD |
219 | #else |
220 | /* Distinguish between big endian and little endian. */ | |
221 | # if __BYTE_ORDER == __LITTLE_ENDIAN | |
c1db8b0d | 222 | # define get16u(addr) \ |
a784e502 UD |
223 | (((const unsigned char *) (addr))[1] << 8 \ |
224 | | ((const unsigned char *) (addr))[0]) | |
c1db8b0d | 225 | # define get32u(addr) \ |
a784e502 UD |
226 | (((((const unsigned char *) (addr))[3] << 8 \ |
227 | | ((const unsigned char *) (addr))[2]) << 8 \ | |
228 | | ((const unsigned char *) (addr))[1]) << 8 \ | |
229 | | ((const unsigned char *) (addr))[0]) | |
77e1d15a | 230 | |
c1db8b0d | 231 | # define put16u(addr, val) \ |
77e1d15a | 232 | ({ uint16_t __val = (val); \ |
cb2c5501 UD |
233 | ((unsigned char *) (addr))[0] = __val; \ |
234 | ((unsigned char *) (addr))[1] = __val >> 8; \ | |
77e1d15a | 235 | (void) 0; }) |
c1db8b0d | 236 | # define put32u(addr, val) \ |
cb2c5501 UD |
237 | ({ uint32_t __val = (val); \ |
238 | ((unsigned char *) (addr))[0] = __val; \ | |
77e1d15a | 239 | __val >>= 8; \ |
cb2c5501 | 240 | ((unsigned char *) (addr))[1] = __val; \ |
77e1d15a | 241 | __val >>= 8; \ |
cb2c5501 | 242 | ((unsigned char *) (addr))[2] = __val; \ |
77e1d15a | 243 | __val >>= 8; \ |
cb2c5501 | 244 | ((unsigned char *) (addr))[3] = __val; \ |
77e1d15a UD |
245 | (void) 0; }) |
246 | # else | |
c1db8b0d | 247 | # define get16u(addr) \ |
a784e502 UD |
248 | (((const unsigned char *) (addr))[0] << 8 \ |
249 | | ((const unsigned char *) (addr))[1]) | |
c1db8b0d | 250 | # define get32u(addr) \ |
a784e502 UD |
251 | (((((const unsigned char *) (addr))[0] << 8 \ |
252 | | ((const unsigned char *) (addr))[1]) << 8 \ | |
253 | | ((const unsigned char *) (addr))[2]) << 8 \ | |
254 | | ((const unsigned char *) (addr))[3]) | |
77e1d15a | 255 | |
c1db8b0d | 256 | # define put16u(addr, val) \ |
77e1d15a | 257 | ({ uint16_t __val = (val); \ |
cb2c5501 | 258 | ((unsigned char *) (addr))[1] = __val; \ |
a1303dc8 | 259 | ((unsigned char *) (addr))[0] = __val >> 8; \ |
77e1d15a | 260 | (void) 0; }) |
c1db8b0d | 261 | # define put32u(addr, val) \ |
cb2c5501 UD |
262 | ({ uint32_t __val = (val); \ |
263 | ((unsigned char *) (addr))[3] = __val; \ | |
77e1d15a | 264 | __val >>= 8; \ |
cb2c5501 | 265 | ((unsigned char *) (addr))[2] = __val; \ |
77e1d15a | 266 | __val >>= 8; \ |
cb2c5501 | 267 | ((unsigned char *) (addr))[1] = __val; \ |
77e1d15a | 268 | __val >>= 8; \ |
cb2c5501 | 269 | ((unsigned char *) (addr))[0] = __val; \ |
77e1d15a UD |
270 | (void) 0; }) |
271 | # endif | |
272 | #endif | |
273 | ||
274 | ||
f6ad4726 UD |
275 | /* For conversions from a fixed width character set to another fixed width |
276 | character set we can define RESET_INPUT_BUFFER in a very fast way. */ | |
8619129f | 277 | #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE |
faaa6f62 UD |
278 | # if FROM_LOOP_MIN_NEEDED_FROM == FROM_LOOP_MAX_NEEDED_FROM \ |
279 | && FROM_LOOP_MIN_NEEDED_TO == FROM_LOOP_MAX_NEEDED_TO \ | |
280 | && TO_LOOP_MIN_NEEDED_FROM == TO_LOOP_MAX_NEEDED_FROM \ | |
281 | && TO_LOOP_MIN_NEEDED_TO == TO_LOOP_MAX_NEEDED_TO | |
282 | /* We have to use these `if's here since the compiler cannot know that | |
283 | (outbuf - outerr) is always divisible by FROM/TO_LOOP_MIN_NEEDED_TO. | |
284 | The ?:1 avoids division by zero warnings that gcc 3.2 emits even for | |
285 | obviously unreachable code. */ | |
286 | # define RESET_INPUT_BUFFER \ | |
287 | if (FROM_DIRECTION) \ | |
288 | { \ | |
289 | if (FROM_LOOP_MIN_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_TO == 0) \ | |
290 | *inptrp -= (outbuf - outerr) \ | |
291 | * (FROM_LOOP_MIN_NEEDED_FROM / FROM_LOOP_MIN_NEEDED_TO); \ | |
292 | else if (FROM_LOOP_MIN_NEEDED_TO % FROM_LOOP_MIN_NEEDED_FROM == 0) \ | |
293 | *inptrp -= (outbuf - outerr) \ | |
294 | / (FROM_LOOP_MIN_NEEDED_TO / FROM_LOOP_MIN_NEEDED_FROM \ | |
295 | ? : 1); \ | |
296 | else \ | |
297 | *inptrp -= ((outbuf - outerr) / FROM_LOOP_MIN_NEEDED_TO) \ | |
298 | * FROM_LOOP_MIN_NEEDED_FROM; \ | |
299 | } \ | |
300 | else \ | |
301 | { \ | |
302 | if (TO_LOOP_MIN_NEEDED_FROM % TO_LOOP_MIN_NEEDED_TO == 0) \ | |
303 | *inptrp -= (outbuf - outerr) \ | |
304 | * (TO_LOOP_MIN_NEEDED_FROM / TO_LOOP_MIN_NEEDED_TO); \ | |
305 | else if (TO_LOOP_MIN_NEEDED_TO % TO_LOOP_MIN_NEEDED_FROM == 0) \ | |
306 | *inptrp -= (outbuf - outerr) \ | |
307 | / (TO_LOOP_MIN_NEEDED_TO / TO_LOOP_MIN_NEEDED_FROM ? : 1); \ | |
308 | else \ | |
309 | *inptrp -= ((outbuf - outerr) / TO_LOOP_MIN_NEEDED_TO) \ | |
310 | * TO_LOOP_MIN_NEEDED_FROM; \ | |
311 | } | |
8619129f UD |
312 | # endif |
313 | #endif | |
314 | ||
315 | ||
316 | /* The default init function. It simply matches the name and initializes | |
317 | the step data to point to one of the objects above. */ | |
318 | #if DEFINE_INIT | |
319 | # ifndef CHARSET_NAME | |
320 | # error "CHARSET_NAME not defined" | |
321 | # endif | |
322 | ||
a1620a4c | 323 | extern int gconv_init (struct __gconv_step *step); |
8619129f | 324 | int |
d64b6ad0 | 325 | gconv_init (struct __gconv_step *step) |
8619129f UD |
326 | { |
327 | /* Determine which direction. */ | |
d64b6ad0 | 328 | if (strcmp (step->__from_name, CHARSET_NAME) == 0) |
918b9d72 | 329 | { |
45541bdf | 330 | step->__data = FROM_DIRECTION_VAL; |
390955cb | 331 | |
faaa6f62 UD |
332 | step->__min_needed_from = FROM_LOOP_MIN_NEEDED_FROM; |
333 | step->__max_needed_from = FROM_LOOP_MAX_NEEDED_FROM; | |
334 | step->__min_needed_to = FROM_LOOP_MIN_NEEDED_TO; | |
335 | step->__max_needed_to = FROM_LOOP_MAX_NEEDED_TO; | |
f9ad060c UD |
336 | |
337 | #ifdef FROM_ONEBYTE | |
338 | step->__btowc_fct = FROM_ONEBYTE; | |
339 | #endif | |
918b9d72 | 340 | } |
365afefc | 341 | else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0) |
918b9d72 | 342 | { |
45541bdf | 343 | step->__data = TO_DIRECTION_VAL; |
390955cb | 344 | |
faaa6f62 UD |
345 | step->__min_needed_from = TO_LOOP_MIN_NEEDED_FROM; |
346 | step->__max_needed_from = TO_LOOP_MAX_NEEDED_FROM; | |
347 | step->__min_needed_to = TO_LOOP_MIN_NEEDED_TO; | |
348 | step->__max_needed_to = TO_LOOP_MAX_NEEDED_TO; | |
918b9d72 | 349 | } |
390955cb | 350 | else |
d64b6ad0 | 351 | return __GCONV_NOCONV; |
8619129f | 352 | |
f6ad4726 | 353 | #ifdef SAVE_RESET_STATE |
d64b6ad0 | 354 | step->__stateful = 1; |
9ce5071a | 355 | #else |
d64b6ad0 | 356 | step->__stateful = 0; |
9ce5071a UD |
357 | #endif |
358 | ||
d64b6ad0 | 359 | return __GCONV_OK; |
8619129f UD |
360 | } |
361 | #endif | |
362 | ||
363 | ||
364 | /* The default destructor function does nothing in the moment and so | |
f6ad4726 UD |
365 | we don't define it at all. But we still provide the macro just in |
366 | case we need it some day. */ | |
8619129f UD |
367 | #if DEFINE_FINI |
368 | #endif | |
369 | ||
370 | ||
28f1c862 UD |
371 | /* If no arguments have to passed to the loop function define the macro |
372 | as empty. */ | |
373 | #ifndef EXTRA_LOOP_ARGS | |
374 | # define EXTRA_LOOP_ARGS | |
375 | #endif | |
376 | ||
377 | ||
8619129f UD |
378 | /* This is the actual conversion function. */ |
379 | #ifndef FUNCTION_NAME | |
380 | # define FUNCTION_NAME gconv | |
381 | #endif | |
382 | ||
fd1b5c0f UD |
383 | /* The macros are used to access the function to convert single characters. */ |
384 | #define SINGLE(fct) SINGLE2 (fct) | |
385 | #define SINGLE2(fct) fct##_single | |
386 | ||
387 | ||
a1620a4c UD |
388 | extern int FUNCTION_NAME (struct __gconv_step *step, |
389 | struct __gconv_step_data *data, | |
390 | const unsigned char **inptrp, | |
391 | const unsigned char *inend, | |
392 | unsigned char **outbufstart, size_t *irreversible, | |
393 | int do_flush, int consume_incomplete); | |
8619129f | 394 | int |
d64b6ad0 | 395 | FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data, |
fd1b5c0f | 396 | const unsigned char **inptrp, const unsigned char *inend, |
f1d5c60d | 397 | unsigned char **outbufstart, size_t *irreversible, int do_flush, |
55985355 | 398 | int consume_incomplete) |
8619129f | 399 | { |
d64b6ad0 UD |
400 | struct __gconv_step *next_step = step + 1; |
401 | struct __gconv_step_data *next_data = data + 1; | |
915a6c51 | 402 | __gconv_fct fct = NULL; |
8619129f UD |
403 | int status; |
404 | ||
915a6c51 UD |
405 | if ((data->__flags & __GCONV_IS_LAST) == 0) |
406 | { | |
407 | fct = next_step->__fct; | |
408 | #ifdef PTR_DEMANGLE | |
409 | if (next_step->__shlib_handle != NULL) | |
410 | PTR_DEMANGLE (fct); | |
411 | #endif | |
412 | } | |
85830c4c | 413 | |
8619129f UD |
414 | /* If the function is called with no input this means we have to reset |
415 | to the initial state. The possibly partly converted input is | |
416 | dropped. */ | |
a1ffb40e | 417 | if (__glibc_unlikely (do_flush)) |
8619129f | 418 | { |
f1d5c60d UD |
419 | /* This should never happen during error handling. */ |
420 | assert (outbufstart == NULL); | |
421 | ||
2373b30e UD |
422 | status = __GCONV_OK; |
423 | ||
390955cb | 424 | #ifdef EMIT_SHIFT_TO_INIT |
2373b30e UD |
425 | if (do_flush == 1) |
426 | { | |
427 | /* We preserve the initial values of the pointer variables. */ | |
428 | unsigned char *outbuf = data->__outbuf; | |
429 | unsigned char *outstart = outbuf; | |
430 | unsigned char *outend = data->__outbufend; | |
431 | ||
432 | # ifdef PREPARE_LOOP | |
433 | PREPARE_LOOP | |
434 | # endif | |
435 | ||
436 | # ifdef SAVE_RESET_STATE | |
437 | SAVE_RESET_STATE (1); | |
438 | # endif | |
439 | ||
440 | /* Emit the escape sequence to reset the state. */ | |
441 | EMIT_SHIFT_TO_INIT; | |
442 | ||
443 | /* Call the steps down the chain if there are any but only if we | |
444 | successfully emitted the escape sequence. This should only | |
445 | fail if the output buffer is full. If the input is invalid | |
446 | it should be discarded since the user wants to start from a | |
447 | clean state. */ | |
448 | if (status == __GCONV_OK) | |
449 | { | |
450 | if (data->__flags & __GCONV_IS_LAST) | |
451 | /* Store information about how many bytes are available. */ | |
452 | data->__outbuf = outbuf; | |
453 | else | |
454 | { | |
455 | /* Write out all output which was produced. */ | |
456 | if (outbuf > outstart) | |
457 | { | |
458 | const unsigned char *outerr = outstart; | |
459 | int result; | |
460 | ||
461 | result = DL_CALL_FCT (fct, (next_step, next_data, | |
462 | &outerr, outbuf, NULL, | |
463 | irreversible, 0, | |
464 | consume_incomplete)); | |
465 | ||
466 | if (result != __GCONV_EMPTY_INPUT) | |
467 | { | |
a1ffb40e | 468 | if (__glibc_unlikely (outerr != outbuf)) |
2373b30e UD |
469 | { |
470 | /* We have a problem. Undo the conversion. */ | |
471 | outbuf = outstart; | |
472 | ||
473 | /* Restore the state. */ | |
474 | # ifdef SAVE_RESET_STATE | |
475 | SAVE_RESET_STATE (0); | |
476 | # endif | |
477 | } | |
478 | ||
479 | /* Change the status. */ | |
480 | status = result; | |
481 | } | |
482 | } | |
483 | ||
484 | if (status == __GCONV_OK) | |
485 | /* Now flush the remaining steps. */ | |
486 | status = DL_CALL_FCT (fct, (next_step, next_data, NULL, | |
487 | NULL, NULL, irreversible, 1, | |
488 | consume_incomplete)); | |
489 | } | |
490 | } | |
491 | } | |
492 | else | |
8619129f | 493 | #endif |
2373b30e UD |
494 | { |
495 | /* Clear the state object. There might be bytes in there from | |
496 | previous calls with CONSUME_INCOMPLETE == 1. But don't emit | |
497 | escape sequences. */ | |
498 | memset (data->__statep, '\0', sizeof (*data->__statep)); | |
499 | ||
500 | if (! (data->__flags & __GCONV_IS_LAST)) | |
501 | /* Now flush the remaining steps. */ | |
502 | status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL, | |
503 | NULL, irreversible, do_flush, | |
504 | consume_incomplete)); | |
505 | } | |
8619129f UD |
506 | } |
507 | else | |
508 | { | |
ba7b4d29 FW |
509 | /* We preserve the initial values of the pointer variables, |
510 | but only some conversion modules need it. */ | |
511 | const unsigned char *inptr __attribute__ ((__unused__)) = *inptrp; | |
f1d5c60d UD |
512 | unsigned char *outbuf = (__builtin_expect (outbufstart == NULL, 1) |
513 | ? data->__outbuf : *outbufstart); | |
d64b6ad0 | 514 | unsigned char *outend = data->__outbufend; |
b117f744 | 515 | unsigned char *outstart; |
3aaad0b6 UD |
516 | /* This variable is used to count the number of characters we |
517 | actually converted. */ | |
38677ace | 518 | size_t lirreversible = 0; |
b572c2da | 519 | size_t *lirreversiblep = irreversible ? &lirreversible : NULL; |
faaa6f62 UD |
520 | |
521 | /* The following assumes that encodings, which have a variable length | |
c0c3f78a | 522 | what might unalign a buffer even though it is an aligned in the |
faaa6f62 UD |
523 | beginning, either don't have the minimal number of bytes as a divisor |
524 | of the maximum length or have a minimum length of 1. This is true | |
525 | for all known and supported encodings. | |
526 | We use && instead of || to combine the subexpression for the FROM | |
527 | encoding and for the TO encoding, because usually one of them is | |
528 | INTERNAL, for which the subexpression evaluates to 1, but INTERNAL | |
529 | buffers are always aligned correctly. */ | |
530 | #define POSSIBLY_UNALIGNED \ | |
27822ce6 | 531 | (!_STRING_ARCH_unaligned \ |
faaa6f62 UD |
532 | && (((FROM_LOOP_MIN_NEEDED_FROM != 1 \ |
533 | && FROM_LOOP_MAX_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_FROM == 0) \ | |
534 | && (FROM_LOOP_MIN_NEEDED_TO != 1 \ | |
535 | && FROM_LOOP_MAX_NEEDED_TO % FROM_LOOP_MIN_NEEDED_TO == 0)) \ | |
536 | || ((TO_LOOP_MIN_NEEDED_FROM != 1 \ | |
537 | && TO_LOOP_MAX_NEEDED_FROM % TO_LOOP_MIN_NEEDED_FROM == 0) \ | |
538 | && (TO_LOOP_MIN_NEEDED_TO != 1 \ | |
539 | && TO_LOOP_MAX_NEEDED_TO % TO_LOOP_MIN_NEEDED_TO == 0)))) | |
540 | #if POSSIBLY_UNALIGNED | |
3aaad0b6 | 541 | int unaligned; |
77e1d15a UD |
542 | # define GEN_unaligned(name) GEN_unaligned2 (name) |
543 | # define GEN_unaligned2(name) name##_unaligned | |
faaa6f62 UD |
544 | #else |
545 | # define unaligned 0 | |
77e1d15a | 546 | #endif |
8619129f | 547 | |
28f1c862 UD |
548 | #ifdef PREPARE_LOOP |
549 | PREPARE_LOOP | |
550 | #endif | |
551 | ||
faaa6f62 | 552 | #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1 |
fd1b5c0f UD |
553 | /* If the function is used to implement the mb*towc*() or wc*tomb*() |
554 | functions we must test whether any bytes from the last call are | |
555 | stored in the `state' object. */ | |
faaa6f62 UD |
556 | if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1) |
557 | || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION) | |
558 | || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION)) | |
fd1b5c0f UD |
559 | && consume_incomplete && (data->__statep->__count & 7) != 0) |
560 | { | |
f1d5c60d | 561 | /* Yep, we have some bytes left over. Process them now. |
a784e502 UD |
562 | But this must not happen while we are called from an |
563 | error handler. */ | |
f1d5c60d | 564 | assert (outbufstart == NULL); |
fd1b5c0f | 565 | |
faaa6f62 UD |
566 | # if FROM_LOOP_MAX_NEEDED_FROM > 1 |
567 | if (TO_LOOP_MAX_NEEDED_FROM == 1 || FROM_DIRECTION) | |
55985355 | 568 | status = SINGLE(FROM_LOOP) (step, data, inptrp, inend, &outbuf, |
b572c2da | 569 | outend, lirreversiblep |
85830c4c | 570 | EXTRA_LOOP_ARGS); |
fd1b5c0f | 571 | # endif |
faaa6f62 UD |
572 | # if !ONE_DIRECTION |
573 | # if FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1 | |
fd1b5c0f | 574 | else |
faaa6f62 UD |
575 | # endif |
576 | # if TO_LOOP_MAX_NEEDED_FROM > 1 | |
55985355 | 577 | status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf, |
b572c2da | 578 | outend, lirreversiblep EXTRA_LOOP_ARGS); |
faaa6f62 | 579 | # endif |
fd1b5c0f UD |
580 | # endif |
581 | ||
365afefc | 582 | if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK) |
fd1b5c0f UD |
583 | return status; |
584 | } | |
585 | #endif | |
586 | ||
faaa6f62 UD |
587 | #if POSSIBLY_UNALIGNED |
588 | unaligned = | |
589 | ((FROM_DIRECTION | |
590 | && ((uintptr_t) inptr % FROM_LOOP_MIN_NEEDED_FROM != 0 | |
591 | || ((data->__flags & __GCONV_IS_LAST) | |
592 | && (uintptr_t) outbuf % FROM_LOOP_MIN_NEEDED_TO != 0))) | |
593 | || (!FROM_DIRECTION | |
594 | && (((data->__flags & __GCONV_IS_LAST) | |
595 | && (uintptr_t) outbuf % TO_LOOP_MIN_NEEDED_TO != 0) | |
596 | || (uintptr_t) inptr % TO_LOOP_MIN_NEEDED_FROM != 0))); | |
3aaad0b6 UD |
597 | #endif |
598 | ||
f1813b56 | 599 | while (1) |
8619129f UD |
600 | { |
601 | /* Remember the start value for this round. */ | |
fd1b5c0f | 602 | inptr = *inptrp; |
8619129f | 603 | /* The outbuf buffer is empty. */ |
b117f744 | 604 | outstart = outbuf; |
4802be92 AS |
605 | #ifdef RESET_INPUT_BUFFER |
606 | /* Remember how many irreversible characters were skipped before | |
607 | this round. */ | |
608 | size_t loop_irreversible | |
609 | = lirreversible + (irreversible ? *irreversible : 0); | |
610 | #endif | |
8619129f | 611 | |
8619129f UD |
612 | #ifdef SAVE_RESET_STATE |
613 | SAVE_RESET_STATE (1); | |
614 | #endif | |
615 | ||
a1ffb40e | 616 | if (__glibc_likely (!unaligned)) |
77e1d15a UD |
617 | { |
618 | if (FROM_DIRECTION) | |
619 | /* Run the conversion loop. */ | |
55985355 | 620 | status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend, |
b572c2da | 621 | lirreversiblep EXTRA_LOOP_ARGS); |
77e1d15a UD |
622 | else |
623 | /* Run the conversion loop. */ | |
55985355 | 624 | status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend, |
b572c2da | 625 | lirreversiblep EXTRA_LOOP_ARGS); |
77e1d15a | 626 | } |
faaa6f62 | 627 | #if POSSIBLY_UNALIGNED |
8619129f | 628 | else |
77e1d15a UD |
629 | { |
630 | if (FROM_DIRECTION) | |
631 | /* Run the conversion loop. */ | |
55985355 UD |
632 | status = GEN_unaligned (FROM_LOOP) (step, data, inptrp, inend, |
633 | &outbuf, outend, | |
b572c2da | 634 | lirreversiblep |
77e1d15a UD |
635 | EXTRA_LOOP_ARGS); |
636 | else | |
637 | /* Run the conversion loop. */ | |
55985355 UD |
638 | status = GEN_unaligned (TO_LOOP) (step, data, inptrp, inend, |
639 | &outbuf, outend, | |
b572c2da | 640 | lirreversiblep |
77e1d15a UD |
641 | EXTRA_LOOP_ARGS); |
642 | } | |
643 | #endif | |
8619129f | 644 | |
f1d5c60d UD |
645 | /* If we were called as part of an error handling module we |
646 | don't do anything else here. */ | |
a1ffb40e | 647 | if (__glibc_unlikely (outbufstart != NULL)) |
f1d5c60d UD |
648 | { |
649 | *outbufstart = outbuf; | |
650 | return status; | |
651 | } | |
652 | ||
0aece08d UD |
653 | /* We finished one use of the loops. */ |
654 | ++data->__invocation_counter; | |
655 | ||
390955cb | 656 | /* If this is the last step leave the loop, there is nothing |
a784e502 | 657 | we can do. */ |
a1ffb40e | 658 | if (__glibc_unlikely (data->__flags & __GCONV_IS_LAST)) |
8619129f UD |
659 | { |
660 | /* Store information about how many bytes are available. */ | |
d64b6ad0 | 661 | data->__outbuf = outbuf; |
bf979eef | 662 | |
38677ace | 663 | /* Remember how many non-identical characters we |
c0c3f78a | 664 | converted in an irreversible way. */ |
38677ace | 665 | *irreversible += lirreversible; |
bf979eef | 666 | |
8619129f UD |
667 | break; |
668 | } | |
669 | ||
670 | /* Write out all output which was produced. */ | |
a1ffb40e | 671 | if (__glibc_likely (outbuf > outstart)) |
8619129f | 672 | { |
d64b6ad0 | 673 | const unsigned char *outerr = data->__outbuf; |
8619129f UD |
674 | int result; |
675 | ||
4bae5567 | 676 | result = DL_CALL_FCT (fct, (next_step, next_data, &outerr, |
f1d5c60d | 677 | outbuf, NULL, irreversible, 0, |
fd1b5c0f | 678 | consume_incomplete)); |
8619129f | 679 | |
d64b6ad0 | 680 | if (result != __GCONV_EMPTY_INPUT) |
8619129f | 681 | { |
a1ffb40e | 682 | if (__glibc_unlikely (outerr != outbuf)) |
8619129f UD |
683 | { |
684 | #ifdef RESET_INPUT_BUFFER | |
4802be92 AS |
685 | /* RESET_INPUT_BUFFER can only work when there were |
686 | no new irreversible characters skipped during | |
687 | this round. */ | |
688 | if (loop_irreversible | |
689 | == lirreversible + (irreversible ? *irreversible : 0)) | |
690 | { | |
691 | RESET_INPUT_BUFFER; | |
692 | goto done_reset; | |
693 | } | |
694 | #endif | |
dbc92dc6 UD |
695 | /* We have a problem in one of the functions below. |
696 | Undo the conversion upto the error point. */ | |
ad37480c | 697 | size_t nstatus __attribute__ ((unused)); |
8619129f UD |
698 | |
699 | /* Reload the pointers. */ | |
fd1b5c0f | 700 | *inptrp = inptr; |
b117f744 | 701 | outbuf = outstart; |
8619129f | 702 | |
2373b30e | 703 | /* Restore the state. */ |
4802be92 | 704 | #ifdef SAVE_RESET_STATE |
8619129f | 705 | SAVE_RESET_STATE (0); |
4802be92 | 706 | #endif |
8619129f | 707 | |
a1ffb40e | 708 | if (__glibc_likely (!unaligned)) |
f6ad4726 UD |
709 | { |
710 | if (FROM_DIRECTION) | |
711 | /* Run the conversion loop. */ | |
712 | nstatus = FROM_LOOP (step, data, inptrp, inend, | |
17427edd | 713 | &outbuf, outerr, |
f6ad4726 UD |
714 | lirreversiblep |
715 | EXTRA_LOOP_ARGS); | |
716 | else | |
717 | /* Run the conversion loop. */ | |
718 | nstatus = TO_LOOP (step, data, inptrp, inend, | |
17427edd | 719 | &outbuf, outerr, |
f6ad4726 UD |
720 | lirreversiblep |
721 | EXTRA_LOOP_ARGS); | |
722 | } | |
4802be92 | 723 | #if POSSIBLY_UNALIGNED |
8619129f | 724 | else |
f6ad4726 UD |
725 | { |
726 | if (FROM_DIRECTION) | |
727 | /* Run the conversion loop. */ | |
728 | nstatus = GEN_unaligned (FROM_LOOP) (step, data, | |
729 | inptrp, inend, | |
d1700665 | 730 | &outbuf, |
17427edd | 731 | outerr, |
f6ad4726 UD |
732 | lirreversiblep |
733 | EXTRA_LOOP_ARGS); | |
734 | else | |
735 | /* Run the conversion loop. */ | |
736 | nstatus = GEN_unaligned (TO_LOOP) (step, data, | |
737 | inptrp, inend, | |
17427edd | 738 | &outbuf, outerr, |
f6ad4726 UD |
739 | lirreversiblep |
740 | EXTRA_LOOP_ARGS); | |
741 | } | |
4802be92 | 742 | #endif |
8619129f UD |
743 | |
744 | /* We must run out of output buffer space in this | |
745 | rerun. */ | |
5aa8ff62 | 746 | assert (outbuf == outerr); |
d64b6ad0 | 747 | assert (nstatus == __GCONV_FULL_OUTPUT); |
0aece08d UD |
748 | |
749 | /* If we haven't consumed a single byte decrement | |
750 | the invocation counter. */ | |
a1ffb40e | 751 | if (__glibc_unlikely (outbuf == outstart)) |
0aece08d | 752 | --data->__invocation_counter; |
8619129f UD |
753 | } |
754 | ||
4802be92 AS |
755 | #ifdef RESET_INPUT_BUFFER |
756 | done_reset: | |
757 | #endif | |
8619129f UD |
758 | /* Change the status. */ |
759 | status = result; | |
760 | } | |
761 | else | |
762 | /* All the output is consumed, we can make another run | |
763 | if everything was ok. */ | |
d64b6ad0 | 764 | if (status == __GCONV_FULL_OUTPUT) |
f1813b56 UD |
765 | { |
766 | status = __GCONV_OK; | |
767 | outbuf = data->__outbuf; | |
768 | } | |
8619129f | 769 | } |
f1813b56 UD |
770 | |
771 | if (status != __GCONV_OK) | |
772 | break; | |
773 | ||
774 | /* Reset the output buffer pointer for the next round. */ | |
775 | outbuf = data->__outbuf; | |
8619129f | 776 | } |
8619129f | 777 | |
918b9d72 UD |
778 | #ifdef END_LOOP |
779 | END_LOOP | |
780 | #endif | |
fd1b5c0f UD |
781 | |
782 | /* If we are supposed to consume all character store now all of the | |
783 | remaining characters in the `state' object. */ | |
faaa6f62 UD |
784 | #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1 |
785 | if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1) | |
786 | || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION) | |
787 | || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION)) | |
365afefc UD |
788 | && __builtin_expect (consume_incomplete, 0) |
789 | && status == __GCONV_INCOMPLETE_INPUT) | |
fd1b5c0f UD |
790 | { |
791 | # ifdef STORE_REST | |
792 | mbstate_t *state = data->__statep; | |
793 | ||
794 | STORE_REST | |
795 | # else | |
fd1b5c0f | 796 | /* Make sure the remaining bytes fit into the state objects |
a784e502 | 797 | buffer. */ |
08538f36 SL |
798 | size_t cnt_after = inend - *inptrp; |
799 | assert (cnt_after <= sizeof (data->__statep->__value.__wchb)); | |
fd1b5c0f | 800 | |
dbc92dc6 | 801 | size_t cnt; |
08538f36 SL |
802 | for (cnt = 0; cnt < cnt_after; ++cnt) |
803 | data->__statep->__value.__wchb[cnt] = (*inptrp)[cnt]; | |
804 | *inptrp = inend; | |
fd1b5c0f UD |
805 | data->__statep->__count &= ~7; |
806 | data->__statep->__count |= cnt; | |
807 | # endif | |
808 | } | |
809 | #endif | |
faaa6f62 UD |
810 | #undef unaligned |
811 | #undef POSSIBLY_UNALIGNED | |
8619129f UD |
812 | } |
813 | ||
814 | return status; | |
815 | } | |
816 | ||
817 | #undef DEFINE_INIT | |
818 | #undef CHARSET_NAME | |
819 | #undef DEFINE_FINI | |
820 | #undef MIN_NEEDED_FROM | |
821 | #undef MIN_NEEDED_TO | |
822 | #undef MAX_NEEDED_FROM | |
823 | #undef MAX_NEEDED_TO | |
faaa6f62 UD |
824 | #undef FROM_LOOP_MIN_NEEDED_FROM |
825 | #undef FROM_LOOP_MAX_NEEDED_FROM | |
826 | #undef FROM_LOOP_MIN_NEEDED_TO | |
827 | #undef FROM_LOOP_MAX_NEEDED_TO | |
828 | #undef TO_LOOP_MIN_NEEDED_FROM | |
829 | #undef TO_LOOP_MAX_NEEDED_FROM | |
830 | #undef TO_LOOP_MIN_NEEDED_TO | |
831 | #undef TO_LOOP_MAX_NEEDED_TO | |
8619129f UD |
832 | #undef FROM_DIRECTION |
833 | #undef EMIT_SHIFT_TO_INIT | |
834 | #undef FROM_LOOP | |
835 | #undef TO_LOOP | |
f9ad060c | 836 | #undef ONE_DIRECTION |
f6ad4726 | 837 | #undef SAVE_RESET_STATE |
8619129f UD |
838 | #undef RESET_INPUT_BUFFER |
839 | #undef FUNCTION_NAME | |
918b9d72 UD |
840 | #undef PREPARE_LOOP |
841 | #undef END_LOOP | |
f9ad060c | 842 | #undef EXTRA_LOOP_ARGS |
fd1b5c0f | 843 | #undef STORE_REST |
f9ad060c | 844 | #undef FROM_ONEBYTE |