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