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