1 /* Conversion to and from TCVN5712-1.
2 Copyright (C) 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
6 The GNU C Library is free software; you can redistribute it and/or
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.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #define NELEMS(arr) (sizeof (arr) / sizeof (arr[0]))
27 /* Definitions used in the body of the `gconv' function. */
28 #define CHARSET_NAME "TCVN5712-1//"
29 #define FROM_LOOP from_tcvn5712_1
30 #define TO_LOOP to_tcvn5712_1
33 #define MIN_NEEDED_FROM 1
34 #define MIN_NEEDED_TO 4
35 #define PREPARE_LOOP \
37 int *statep = &data->__statep->__count;
38 #define EXTRA_LOOP_ARGS , statep
41 /* Since we might have to reset input pointer we must be able to save
42 and restore the state. */
43 #define SAVE_RESET_STATE(Save) \
45 saved_state = *statep; \
50 /* During TCVN5712-1 to UCS4 conversion, the COUNT element of the state
51 contains the last UCS4 character, shifted by 3 bits. */
54 /* Since this is a stateful encoding we have to provide code which resets
55 the output state to the initial state. This has to be done during the
57 #define EMIT_SHIFT_TO_INIT \
58 if (data->__statep->__count != 0) \
62 if (__builtin_expect (outbuf + 4 <= outend, 1)) \
64 /* Write out the last character. */ \
65 *((uint32_t *) outbuf)++ = data->__statep->__count >> 3; \
66 data->__statep->__count = 0; \
69 /* We don't have enough room in the output buffer. */ \
70 status = __GCONV_FULL_OUTPUT; \
73 /* We don't use shift states in the TO_DIRECTION. */ \
74 data->__statep->__count = 0; \
78 static const uint16_t map_from_tcvn_low
[0x18] =
80 0x0000, 0x00da, 0x1ee4, 0x0003, 0x1eea, 0x1eec, 0x1eee, 0x0007,
81 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
82 0x0010, 0x1ee8, 0x1ef0, 0x1ef2, 0x1ef6, 0x1ef8, 0x00dd, 0x1ef4
85 static const uint16_t map_from_tcvn_high
[0x80] =
87 0x00c0, 0x1ea2, 0x00c3, 0x00c1, 0x1ea0, 0x1eb6, 0x1eac, 0x00c8,
88 0x1eba, 0x1ebc, 0x00c9, 0x1eb8, 0x1ec6, 0x00cc, 0x1ec8, 0x0128,
89 0x00cd, 0x1eca, 0x00d2, 0x1ece, 0x00d5, 0x00d3, 0x1ecc, 0x1ed8,
90 0x1edc, 0x1ede, 0x1ee0, 0x1eda, 0x1ee2, 0x00d9, 0x1ee6, 0x0168,
91 0x00a0, 0x0102, 0x00c2, 0x00ca, 0x00d4, 0x01a0, 0x01af, 0x0110,
92 0x0103, 0x00e2, 0x00ea, 0x00f4, 0x01a1, 0x01b0, 0x0111, 0x1eb0,
93 0x0300, 0x0309, 0x0303, 0x0301, 0x0323, 0x00e0, 0x1ea3, 0x00e3,
94 0x00e1, 0x1ea1, 0x1eb2, 0x1eb1, 0x1eb3, 0x1eb5, 0x1eaf, 0x1eb4,
95 0x1eae, 0x1ea6, 0x1ea8, 0x1eaa, 0x1ea4, 0x1ec0, 0x1eb7, 0x1ea7,
96 0x1ea9, 0x1eab, 0x1ea5, 0x1ead, 0x00e8, 0x1ec2, 0x1ebb, 0x1ebd,
97 0x00e9, 0x1eb9, 0x1ec1, 0x1ec3, 0x1ec5, 0x1ebf, 0x1ec7, 0x00ec,
98 0x1ec9, 0x1ec4, 0x1ebe, 0x1ed2, 0x0129, 0x00ed, 0x1ecb, 0x00f2,
99 0x1ed4, 0x1ecf, 0x00f5, 0x00f3, 0x1ecd, 0x1ed3, 0x1ed5, 0x1ed7,
100 0x1ed1, 0x1ed9, 0x1edd, 0x1edf, 0x1ee1, 0x1edb, 0x1ee3, 0x00f9,
101 0x1ed6, 0x1ee7, 0x0169, 0x00fa, 0x1ee5, 0x1eeb, 0x1eed, 0x1eef,
102 0x1ee9, 0x1ef1, 0x1ef3, 0x1ef7, 0x1ef9, 0x00fd, 0x1ef5, 0x1ed0
106 /* TCVN5712-1 contains five combining characters:
107 0x0300, 0x0301, 0x0303, 0x0309, 0x0323. */
109 /* Composition tables for each of the relevant combining characters. */
114 } comp_table_data
[] =
116 #define COMP_TABLE_IDX_0300 0
117 #define COMP_TABLE_LEN_0300 28
134 /*{ 0x00A8, 0x1FED },*/
138 /*{ 0x00DC, 0x01DB },*/
142 /*{ 0x00FC, 0x01DC },*/
145 /*{ 0x0112, 0x1E14 },*/
146 /*{ 0x0113, 0x1E15 },*/
147 /*{ 0x014C, 0x1E50 },*/
148 /*{ 0x014D, 0x1E51 },*/
153 #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300)
154 #define COMP_TABLE_LEN_0301 51
190 /*{ 0x00A8, 0x1FEE },*/
192 /*{ 0x00C5, 0x01FA },*/
193 /*{ 0x00C6, 0x01FC },*/
194 /*{ 0x00C7, 0x1E08 },*/
196 /*{ 0x00CF, 0x1E2E },*/
199 /*{ 0x00D8, 0x01FE },*/
200 /*{ 0x00DC, 0x01D7 },*/
202 /*{ 0x00E5, 0x01FB },*/
203 /*{ 0x00E6, 0x01FD },*/
204 /*{ 0x00E7, 0x1E09 },*/
206 /*{ 0x00EF, 0x1E2F },*/
209 /*{ 0x00F8, 0x01FF },*/
210 /*{ 0x00FC, 0x01D8 },*/
213 /*{ 0x0112, 0x1E16 },*/
214 /*{ 0x0113, 0x1E17 },*/
215 /*{ 0x014C, 0x1E52 },*/
216 /*{ 0x014D, 0x1E53 },*/
223 #define COMP_TABLE_IDX_0303 (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301)
224 #define COMP_TABLE_LEN_0303 34
259 #define COMP_TABLE_IDX_0309 (COMP_TABLE_IDX_0303 + COMP_TABLE_LEN_0303)
260 #define COMP_TABLE_LEN_0309 24
285 #define COMP_TABLE_IDX_0323 (COMP_TABLE_IDX_0309 + COMP_TABLE_LEN_0309)
286 #define COMP_TABLE_LEN_0323 50
337 #define COMP_TABLE_IDX_END (COMP_TABLE_IDX_0323 + COMP_TABLE_LEN_0323)
339 /* Compile-time verification of table size. */
340 typedef int verify1
[(NELEMS (comp_table_data
) == COMP_TABLE_IDX_END
) - 1];
348 { COMP_TABLE_IDX_0300
, COMP_TABLE_LEN_0300
},
349 { COMP_TABLE_IDX_0301
, COMP_TABLE_LEN_0301
},
350 { COMP_TABLE_IDX_0303
, COMP_TABLE_LEN_0303
},
351 { COMP_TABLE_IDX_0309
, COMP_TABLE_LEN_0309
},
352 { COMP_TABLE_IDX_0323
, COMP_TABLE_LEN_0323
},
356 /* First define the conversion function from TCVN5712-1 to UCS4. */
357 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
358 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
359 #define MAX_NEEDED_OUTPUT 8
360 #define LOOPFCT FROM_LOOP
363 uint32_t ch = *inptr; \
365 int must_buffer_ch; \
368 ch = map_from_tcvn_low[ch]; \
369 else if (ch >= 0x80) \
370 ch = map_from_tcvn_high[ch - 0x80]; \
372 /* Determine whether there is a buffered character pending. */ \
373 last_ch = *statep >> 3; \
375 /* We have to buffer ch if it is a possible match in comp_table_data. */ \
376 must_buffer_ch = (ch >= 0x0041 && ch <= 0x01b0); \
380 if (ch >= 0x0300 && ch < 0x0340) \
382 /* See whether last_ch and ch can be combined. */ \
408 i1 = comp_table[i].idx; \
409 i2 = i1 + comp_table[i].len - 1; \
411 if (last_ch >= comp_table_data[i1].base \
412 && last_ch <= comp_table_data[i2].base) \
416 i = (i1 + i2) >> 1; \
417 if (last_ch == comp_table_data[i].base) \
419 if (last_ch < comp_table_data[i].base) \
422 goto not_combining; \
432 if (last_ch == comp_table_data[i].base) \
434 goto not_combining; \
438 last_ch = comp_table_data[i].composed; \
439 /* Output the combined character. */ \
440 put32 (outptr, last_ch); \
449 /* Output the buffered character. */ \
450 put32 (outptr, last_ch); \
454 /* If we don't have enough room to output ch as well, then deal \
455 with it in another round. */ \
456 if (!must_buffer_ch && __builtin_expect (outptr + 4 > outend, 0)) \
460 if (must_buffer_ch) \
464 put32 (outptr, ch); \
469 #define EXTRA_LOOP_DECLS , int *statep
470 #include <iconv/loop.c>
473 /* Next, define the conversion function from UCS4 to CP1258. */
475 static const unsigned char from_ucs4
[] =
477 #define FROM_IDX_00 0
478 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, /* 0x0001-0x0007 */
479 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 0x0008-0x000f */
480 0x10, /* 0x0010-0x0010 */
482 #define FROM_IDX_01 (FROM_IDX_00 + 16)
483 0x80, 0x83, 0xa2, 0x82, 0x00, 0x00, 0x00, 0x00, /* 0x00c0-0x00c7 */
484 0x87, 0x8a, 0xa3, 0x00, 0x8d, 0x90, 0x00, 0x00, /* 0x00c8-0x00cf */
485 0x00, 0x00, 0x92, 0x95, 0xa4, 0x94, 0x00, 0x00, /* 0x00d0-0x00d7 */
486 0x00, 0x9d, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00, /* 0x00d8-0x00df */
487 0xb5, 0xb8, 0xa9, 0xb7, 0x00, 0x00, 0x00, 0x00, /* 0x00e0-0x00e7 */
488 0xcc, 0xd0, 0xaa, 0x00, 0xd7, 0xdd, 0x00, 0x00, /* 0x00e8-0x00ef */
489 0x00, 0x00, 0xdf, 0xe3, 0xab, 0xe2, 0x00, 0x00, /* 0x00f0-0x00f7 */
490 0x00, 0xef, 0xf3, 0x00, 0x00, 0xfd, 0x00, 0x00, /* 0x00f8-0x00ff */
491 0x00, 0x00, 0xa1, 0xa8, 0x00, 0x00, 0x00, 0x00, /* 0x0100-0x0107 */
492 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0108-0x010f */
493 0xa7, 0xae, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0110-0x0117 */
494 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0118-0x011f */
495 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0120-0x0127 */
496 0x8f, 0xdc, /* 0x0128-0x0129 */
498 #define FROM_IDX_02 (FROM_IDX_01 + 106)
499 0x9f, 0xf2, /* 0x0168-0x0169 */
501 #define FROM_IDX_03 (FROM_IDX_02 + 2)
502 0xa5, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x01a0-0x01a7 */
503 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, /* 0x01a8-0x01af */
504 0xad, /* 0x01b0-0x01b0 */
506 #define FROM_IDX_04 (FROM_IDX_03 + 17)
507 0xb0, 0xb3, 0x00, 0xb2, 0x00, 0x00, 0x00, 0x00, /* 0x0300-0x0307 */
508 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0308-0x030f */
509 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0310-0x0317 */
510 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x0318-0x031f */
511 0x00, 0x00, 0x00, 0xb4, /* 0x0320-0x0323 */
513 #define FROM_IDX_05 (FROM_IDX_04 + 36)
514 0x84, 0xb9, 0x81, 0xb6, 0xc4, 0xca, 0xc1, 0xc7, /* 0x1ea0-0x1ea7 */
515 0xc2, 0xc8, 0xc3, 0xc9, 0x86, 0xcb, 0xc0, 0xbe, /* 0x1ea8-0x1eaf */
516 0xaf, 0xbb, 0xba, 0xbc, 0xbf, 0xbd, 0x85, 0xc6, /* 0x1eb0-0x1eb7 */
517 0x8b, 0xd1, 0x88, 0xce, 0x89, 0xcf, 0xda, 0xd5, /* 0x1eb8-0x1ebf */
518 0xc5, 0xd2, 0xcd, 0xd3, 0xd9, 0xd4, 0x8c, 0xd6, /* 0x1ec0-0x1ec7 */
519 0x8e, 0xd8, 0x91, 0xde, 0x96, 0xe4, 0x93, 0xe1, /* 0x1ec8-0x1ecf */
520 0xff, 0xe8, 0xdb, 0xe5, 0xe0, 0xe6, 0xf0, 0xe7, /* 0x1ed0-0x1ed7 */
521 0x97, 0xe9, 0x9b, 0xed, 0x98, 0xea, 0x99, 0xeb, /* 0x1ed8-0x1edf */
522 0x9a, 0xec, 0x9c, 0xee, 0x02, 0xf4, 0x9e, 0xf1, /* 0x1ee0-0x1ee7 */
523 0x11, 0xf8, 0x04, 0xf5, 0x05, 0xf6, 0x06, 0xf7, /* 0x1ee8-0x1eef */
524 0x12, 0xf9, 0x13, 0xfa, 0x17, 0xfe, 0x14, 0xfb, /* 0x1ef0-0x1ef7 */
525 0x15, 0xfc /* 0x1ef8-0x1ef9 */
526 #define FROM_IDX_END (FROM_IDX_05 + 90)
528 /* Compile-time verification of table size. */
529 typedef int verify2
[(NELEMS (from_ucs4
) == FROM_IDX_END
) - 1];
531 /* Decomposition table for the relevant Unicode characters. */
539 { 0x00d1, 0x4e, 0xb2 },
540 { 0x00f1, 0x6e, 0xb2 },
541 { 0x0106, 0x43, 0xb3 },
542 { 0x0107, 0x63, 0xb3 },
543 { 0x0139, 0x4c, 0xb3 },
544 { 0x013a, 0x6c, 0xb3 },
545 { 0x0143, 0x4e, 0xb3 },
546 { 0x0144, 0x6e, 0xb3 },
547 { 0x0154, 0x52, 0xb3 },
548 { 0x0155, 0x72, 0xb3 },
549 { 0x015a, 0x53, 0xb3 },
550 { 0x015b, 0x73, 0xb3 },
551 { 0x0179, 0x5a, 0xb3 },
552 { 0x017a, 0x7a, 0xb3 },
553 { 0x01f4, 0x47, 0xb3 },
554 { 0x01f5, 0x67, 0xb3 },
555 { 0x01f8, 0x4e, 0xb0 },
556 { 0x01f9, 0x6e, 0xb0 },
557 { 0x1e04, 0x42, 0xb4 },
558 { 0x1e05, 0x62, 0xb4 },
559 { 0x1e0c, 0x44, 0xb4 },
560 { 0x1e0d, 0x64, 0xb4 },
561 { 0x1e24, 0x48, 0xb4 },
562 { 0x1e25, 0x68, 0xb4 },
563 { 0x1e30, 0x4b, 0xb3 },
564 { 0x1e31, 0x6b, 0xb3 },
565 { 0x1e32, 0x4b, 0xb4 },
566 { 0x1e33, 0x6b, 0xb4 },
567 { 0x1e36, 0x4c, 0xb4 },
568 { 0x1e37, 0x6c, 0xb4 },
569 { 0x1e3e, 0x4d, 0xb3 },
570 { 0x1e3f, 0x6d, 0xb3 },
571 { 0x1e42, 0x4d, 0xb4 },
572 { 0x1e43, 0x6d, 0xb4 },
573 { 0x1e46, 0x4e, 0xb4 },
574 { 0x1e47, 0x6e, 0xb4 },
575 { 0x1e54, 0x50, 0xb3 },
576 { 0x1e55, 0x70, 0xb3 },
577 { 0x1e5a, 0x52, 0xb4 },
578 { 0x1e5b, 0x72, 0xb4 },
579 { 0x1e62, 0x53, 0xb4 },
580 { 0x1e63, 0x73, 0xb4 },
581 { 0x1e6c, 0x54, 0xb4 },
582 { 0x1e6d, 0x74, 0xb4 },
583 { 0x1e7c, 0x56, 0xb2 },
584 { 0x1e7d, 0x76, 0xb2 },
585 { 0x1e7e, 0x56, 0xb4 },
586 { 0x1e7f, 0x76, 0xb4 },
587 { 0x1e80, 0x57, 0xb0 },
588 { 0x1e81, 0x77, 0xb0 },
589 { 0x1e82, 0x57, 0xb3 },
590 { 0x1e83, 0x77, 0xb3 },
591 { 0x1e88, 0x57, 0xb4 },
592 { 0x1e89, 0x77, 0xb4 },
593 { 0x1e92, 0x5a, 0xb4 },
594 { 0x1e93, 0x7a, 0xb4 },
598 /* Next, define the other direction. */
599 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
600 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
601 #define MAX_NEEDED_OUTPUT 2
602 #define LOOPFCT TO_LOOP
605 uint32_t ch = get32 (inptr); \
607 if (ch == 0x00 || (ch >= 0x18 && ch < 0x80) || ch == 0xa0) \
617 res = from_ucs4[ch - 0x0001 + FROM_IDX_00]; \
618 else if (ch >= 0x00c0 && ch <= 0x0129) \
619 res = from_ucs4[ch - 0x00c0 + FROM_IDX_01]; \
620 else if (ch >= 0x0168 && ch <= 0x0169) \
621 res = from_ucs4[ch - 0x0168 + FROM_IDX_02]; \
622 else if (ch >= 0x01a0 && ch <= 0x01b0) \
623 res = from_ucs4[ch - 0x01a0 + FROM_IDX_03]; \
624 else if (ch >= 0x0300 && ch <= 0x0323) \
625 res = from_ucs4[ch - 0x0300 + FROM_IDX_04]; \
626 else if (ch >= 0x1ea0 && ch <= 0x1ef9) \
627 res = from_ucs4[ch - 0x1ea0 + FROM_IDX_05]; \
630 UNICODE_TAG_HANDLER (ch, 4); \
634 if (__builtin_expect (res != 0, 1)) \
641 /* Try canonical decomposition. */ \
646 i2 = sizeof (decomp_table) / sizeof (decomp_table[0]) - 1; \
647 if (ch >= decomp_table[i1].composed \
648 && ch <= decomp_table[i2].composed) \
654 i = (i1 + i2) >> 1; \
655 if (ch == decomp_table[i].composed) \
657 if (ch < decomp_table[i].composed) \
670 if (ch == decomp_table[i].composed) \
677 /* See whether we have room for two bytes. */ \
678 if (__builtin_expect (outptr + 1 >= outend, 0)) \
680 result = __GCONV_FULL_OUTPUT; \
684 /* Found a canonical decomposition. */ \
685 *outptr++ = decomp_table[i].base; \
686 *outptr++ = decomp_table[i].comb1; \
692 /* This is an illegal character. */ \
693 STANDARD_ERR_HANDLER (4); \
697 #define LOOP_NEED_FLAGS
698 #define EXTRA_LOOP_DECLS , int *statep
699 #include <iconv/loop.c>
702 /* Now define the toplevel functions. */
703 #include <iconv/skeleton.c>