2 * Character Set Conversion Library 1.2
4 * (c) 1998--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/unicode.h"
13 #include "lib/unaligned.h"
14 #include "charset/charconv.h"
15 #include "charset/chartable.h"
18 conv_init(struct conv_context *c)
20 c->source = c->source_end = NULL;
21 c->dest = c->dest_start = c->dest_end = NULL;
25 conv_none(struct conv_context *c)
27 c->dest_start = (char *) c->source;
28 c->dest = (char *) c->source_end;
29 return CONV_SOURCE_END | CONV_DEST_END | CONV_SKIP;
52 conv_slow(struct conv_context *c)
54 const unsigned char *s = c->source;
55 const unsigned char *se = c->source_end;
56 unsigned char *d = c->dest;
57 unsigned char *de = c->dest_end;
72 *d++ = *c->string_at++;
82 if ((*s & 0xc0) != 0x80)
87 c->code = (c->code << 6) | (*s++ & 0x3f);
90 if (c->code >= 0x10000)
96 /* Writing of UTF-8 */
97 case UTF8_WRITE_START:
105 else if (c->code < 0x800)
107 *d++ = 0xc0 | (c->code >> 6);
113 *d++ = 0xe0 | (c->code >> 12);
118 c->state = UTF8_WRITE_CONT;
120 case UTF8_WRITE_CONT:
125 *d++ = 0x80 | (c->code >> 10);
131 /* Writing of UTF-16BE */
136 if (c->code < 0xd800 || c->code - 0xe000 < 0x2000)
138 else if ((c->code -= 0x10000) < 0x100000)
140 put_u16_be(p, 0xd800 | (c->code >> 10));
141 put_u16_be(p + 2, 0xdc00 | (c->code & 0x3ff));
146 c->code = UNI_REPLACEMENT;
147 put_u16_be(p, c->code);
152 /* Writing of UTF-16LE */
157 if (c->code < 0xd800 || c->code - 0xe000 < 0x2000)
159 else if ((c->code -= 0x10000) < 0x100000)
161 put_u16_le(p, 0xd800 | (c->code >> 10));
162 put_u16_le(p + 2, 0xdc00 | (c->code & 0x3ff));
166 c->code = UNI_REPLACEMENT;
167 put_u16_le(p, c->code);
172 /* Reading of UTF16-BE */
177 c->state = UTF16_BE_READ_1;
179 case UTF16_BE_READ_1:
182 c->code = (c->code << 8) | *s++;
183 if (c->code - 0xd800 >= 0x800)
185 c->code = (c->code - 0xd800) << 10;
186 c->state = UTF16_BE_READ_2;
188 case UTF16_BE_READ_2:
194 c->code |= (*s - 0xdc) << 8;
196 c->state = UTF16_BE_READ_3;
198 case UTF16_BE_READ_3:
201 if ((int)c->code >= 0)
202 c->code += 0x10000 + *s;
204 c->code = UNI_REPLACEMENT;
208 /* Reading of UTF16-LE */
213 c->state = UTF16_LE_READ_1;
215 case UTF16_LE_READ_1:
218 c->code |= *s++ << 8;
219 if (c->code - 0xd800 >= 0x800)
221 c->code = (c->code - 0xd800) << 10;
222 c->state = UTF16_LE_READ_2;
224 case UTF16_LE_READ_2:
228 c->state = UTF16_LE_READ_3;
230 case UTF16_LE_READ_3:
234 c->code += 0x10000 + ((*s - 0xdc) << 8);
236 c->code = UNI_REPLACEMENT;
250 return CONV_SOURCE_END;
254 return CONV_DEST_END;
257 /* Generate inlined routines */
260 conv_std_to_utf8(struct conv_context *c)
262 #define CONV_READ_STD
263 #define CONV_WRITE_UTF8
264 #include "charset/charconv-gen.h"
268 conv_utf8_to_std(struct conv_context *c)
270 #define CONV_READ_UTF8
271 #define CONV_WRITE_STD
272 #include "charset/charconv-gen.h"
276 conv_std_to_utf16_be(struct conv_context *c)
278 #define CONV_READ_STD
279 #define CONV_WRITE_UTF16_BE
280 #include "charset/charconv-gen.h"
284 conv_utf16_be_to_std(struct conv_context *c)
286 #define CONV_READ_UTF16_BE
287 #define CONV_WRITE_STD
288 #include "charset/charconv-gen.h"
292 conv_std_to_utf16_le(struct conv_context *c)
294 #define CONV_READ_STD
295 #define CONV_WRITE_UTF16_LE
296 #include "charset/charconv-gen.h"
300 conv_utf16_le_to_std(struct conv_context *c)
302 #define CONV_READ_UTF16_LE
303 #define CONV_WRITE_STD
304 #include "charset/charconv-gen.h"
308 conv_utf8_to_utf16_be(struct conv_context *c)
310 #define CONV_READ_UTF8
311 #define CONV_WRITE_UTF16_BE
312 #include "charset/charconv-gen.h"
316 conv_utf16_be_to_utf8(struct conv_context *c)
318 #define CONV_READ_UTF16_BE
319 #define CONV_WRITE_UTF8
320 #include "charset/charconv-gen.h"
324 conv_utf8_to_utf16_le(struct conv_context *c)
326 #define CONV_READ_UTF8
327 #define CONV_WRITE_UTF16_LE
328 #include "charset/charconv-gen.h"
332 conv_utf16_le_to_utf8(struct conv_context *c)
334 #define CONV_READ_UTF16_LE
335 #define CONV_WRITE_UTF8
336 #include "charset/charconv-gen.h"
340 conv_utf16_be_to_utf16_le(struct conv_context *c)
342 #define CONV_READ_UTF16_BE
343 #define CONV_WRITE_UTF16_LE
344 #include "charset/charconv-gen.h"
348 conv_standard(struct conv_context *c)
350 unsigned short *in_to_x = c->in_to_x;
351 unsigned short *x_to_out = c->x_to_out;
352 const unsigned char *s, *se;
353 unsigned char *d, *de, *k;
356 if (unlikely(c->state))
366 unsigned int code = x_to_out[in_to_x[*s]];
369 if (unlikely(d >= de))
375 k = string_table + code - 0x100;
377 if (unlikely(d + len > de))
386 return CONV_SOURCE_END;
391 return CONV_DEST_END;
396 c->state = SEQ_WRITE;
407 conv_set_charset(struct conv_context *c, int src, int dest)
409 c->source_charset = src;
410 c->dest_charset = dest;
412 c->convert = conv_none;
415 static uns lookup[] = {
416 [CONV_CHARSET_UTF8] = 1,
417 [CONV_CHARSET_UTF16_BE] = 2,
418 [CONV_CHARSET_UTF16_LE] = 3,
420 static int (*tab[4][4])(struct conv_context *c) = {
421 { conv_standard, conv_std_to_utf8, conv_std_to_utf16_be, conv_std_to_utf16_le },
422 { conv_utf8_to_std, conv_none, conv_utf8_to_utf16_be, conv_utf8_to_utf16_le },
423 { conv_utf16_be_to_std, conv_utf16_be_to_utf8, conv_none, conv_utf16_be_to_utf16_le },
424 { conv_utf16_le_to_std, conv_utf16_le_to_utf8, conv_utf16_be_to_utf16_le, conv_none },
426 uns src_idx = ((uns)src < ARRAY_SIZE(lookup)) ? lookup[src] : 0;
427 uns dest_idx = ((uns)dest < ARRAY_SIZE(lookup)) ? lookup[dest] : 0;
428 c->convert = tab[src_idx][dest_idx];
430 c->in_to_x = input_to_x[src];
432 c->x_to_out = x_to_output[dest];
438 conv_x_to_ucs(unsigned int x)
444 conv_ucs_to_x(unsigned int ucs)
446 return uni_to_x[ucs >> 8U][ucs & 0xff];
452 return sizeof(x_to_uni) / sizeof(x_to_uni[0]);
456 conv_in_to_ucs(struct conv_context *c, unsigned int y)
458 return x_to_uni[c->in_to_x[y]];
461 int conv_ucs_to_out(struct conv_context *c, unsigned int ucs)
463 uns x = uni_to_x[ucs >> 8U][ucs & 0xff];
464 if (x == 256 || c->x_to_out[x] >= 256)
467 return c->x_to_out[x];