1 /* Conversion from and to TSCII.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Bruno Haible <bruno@clisp.org>, 2002.
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, see
18 <http://www.gnu.org/licenses/>. */
25 /* TSCII is an 8-bit encoding consisting of:
27 0x80..0x90, 0x95..0x9F, 0xAB..0xFE:
28 Tamil letters and glyphs
29 0xA1..0xA5, 0xAA: Tamil combining letters (after the base character)
30 0xA6..0xA8: Tamil combining letters (before the base character)
31 0x91..0x94: Punctuation
35 /* Definitions used in the body of the `gconv' function. */
36 #define CHARSET_NAME "TSCII//"
37 #define FROM_LOOP from_tscii
38 #define TO_LOOP to_tscii
41 #define ONE_DIRECTION 0
42 #define FROM_LOOP_MIN_NEEDED_FROM 1
43 #define FROM_LOOP_MAX_NEEDED_FROM 2
44 #define FROM_LOOP_MIN_NEEDED_TO 4
45 #define FROM_LOOP_MAX_NEEDED_TO 16
46 #define TO_LOOP_MIN_NEEDED_FROM 4
47 #define TO_LOOP_MAX_NEEDED_FROM 4
48 #define TO_LOOP_MIN_NEEDED_TO 1
49 #define TO_LOOP_MAX_NEEDED_TO 3
50 #define PREPARE_LOOP \
52 int *statep = &data->__statep->__count;
53 #define EXTRA_LOOP_ARGS , statep
56 /* Since we might have to reset input pointer we must be able to save
57 and restore the state. */
58 #define SAVE_RESET_STATE(Save) \
60 saved_state = *statep; \
65 /* During TSCII to UCS-4 conversion, the COUNT element of the state contains
66 the last UCS-4 character to be output, shifted by 8 bits, and an encoded
67 representation of additional UCS-4 characters to be output (if any),
68 shifted by 4 bits. This character can be:
69 0x0000 Nothing pending.
70 0x0BCD Pending VIRAMA sign. If bit 3 is set, it may be
71 omitted if followed by a vowel sign U or UU.
72 0x0BC6, 0x0BC7, 0x0BC8 Pending vowel sign. Bit 3 is set after the
74 Other Bit 3 always cleared. */
76 /* During UCS-4 to TSCII conversion, the COUNT element of the state contains
77 the last byte (or sometimes the last two bytes) to be output, shifted by
80 0xB8..0xC9, 0x83..0x86 A consonant.
81 0xEC, 0x8A A consonant with VIRAMA sign (final or joining).
82 0x87, 0xC38A Two consonants combined through a VIRAMA sign. */
84 /* Since this is a stateful encoding we have to provide code which resets
85 the output state to the initial state. This has to be done during the
87 #define EMIT_SHIFT_TO_INIT \
88 if (data->__statep->__count != 0) \
94 if (__glibc_unlikely (outbuf + 4 > outend)) \
96 /* We don't have enough room in the output buffer. */ \
97 status = __GCONV_FULL_OUTPUT; \
100 /* Write out the pending character. */ \
101 *((uint32_t *) outbuf) = data->__statep->__count >> 8; \
102 outbuf += sizeof (uint32_t); \
103 /* Retrieve the successor state. */ \
104 data->__statep->__count = \
105 tscii_next_state[(data->__statep->__count >> 4) & 0x0f]; \
107 while (data->__statep->__count != 0); \
111 uint32_t last = data->__statep->__count >> 3; \
112 if (__glibc_unlikely (last >> 8)) \
114 /* Write out the last character, two bytes. */ \
115 if (__glibc_likely (outbuf + 2 <= outend)) \
117 *outbuf++ = last & 0xff; \
118 *outbuf++ = (last >> 8) & 0xff; \
119 data->__statep->__count = 0; \
122 /* We don't have enough room in the output buffer. */ \
123 status = __GCONV_FULL_OUTPUT; \
127 /* Write out the last character, a single byte. */ \
128 if (__glibc_likely (outbuf < outend)) \
130 *outbuf++ = last & 0xff; \
131 data->__statep->__count = 0; \
134 /* We don't have enough room in the output buffer. */ \
135 status = __GCONV_FULL_OUTPUT; \
141 /* First define the conversion function from TSCII to UCS-4. */
143 static const uint16_t tscii_to_ucs4
[128][2] =
147 { 0, 0 }, /* 0x82 - maps to <U0BB8><U0BCD><U0BB0><U0BC0> */
152 { 0, 0 }, /* 0x87 - maps to <U0B95><U0BCD><U0BB7> */
155 { 0, 0 }, /* 0x8a - maps to <U0BB8> and buffers <U0BCD> */
156 { 0, 0 }, /* 0x8b - maps to <U0BB9> and buffers <U0BCD> */
157 { 0, 0 }, /* 0x8c - maps to <U0B95><U0BCD><U0BB7><U0BCD> */
177 { 0, 0 }, /* 0xa0 - unmapped */
183 { 0, 0 }, /* 0xa6 - buffers <U0BC6> */
184 { 0, 0 }, /* 0xa7 - buffers <U0BC7> */
185 { 0, 0 }, /* 0xa8 - buffers <U0BC8> */
272 { 0, 0 } /* 0xff - unmapped */
275 static const uint32_t tscii_next_state
[6] =
277 /* 0 means no more pending Unicode characters. */
279 /* 1 means <U0BB7>. */
281 /* 2 means <U0BC0>. */
283 /* 3 means <U0BCD>. */
285 /* 4 means <U0BB0><U0BC0>. */
286 (0x0BB0 << 8) + (2 << 4),
287 /* 5 means <U0BB7><U0BCD>. */
288 (0x0BB7 << 8) + (3 << 4)
291 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
292 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
293 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
294 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
295 #define LOOPFCT FROM_LOOP
298 uint32_t ch = *inptr; \
300 if ((*statep >> 8) != 0) \
302 /* Attempt to combine the last character with this one. */ \
303 uint32_t last = *statep >> 8; \
305 if (last == 0x0BCD && (*statep & (1 << 3))) \
307 if (ch == 0xa4 || ch == 0xa5) \
310 /* Now ch = 0x0BC1 or ch = 0x0BC2. */ \
311 put32 (outptr, ch); \
318 else if (last >= 0x0BC6 && last <= 0x0BC8) \
320 if ((last == 0x0BC6 && ch == 0xa1) \
321 || (last == 0x0BC7 && (ch == 0xa1 || ch == 0xaa))) \
323 ch = last + 4 + (ch != 0xa1); \
324 /* Now ch = 0x0BCA or ch = 0x0BCB or ch = 0x0BCC. */ \
325 put32 (outptr, ch); \
331 if ((ch >= 0xb8 && ch <= 0xc9) && (*statep & (1 << 3)) == 0) \
333 ch = tscii_to_ucs4[ch - 0x80][0]; \
334 put32 (outptr, ch); \
344 /* Output the buffered character. */ \
345 put32 (outptr, last); \
347 /* Retrieve the successor state. */ \
348 *statep = tscii_next_state[(*statep >> 4) & 0x0f]; \
350 while (*statep != 0 && __builtin_expect (outptr + 4 <= outend, 1)); \
354 /* We don't have enough room in the output buffer. \
355 Tell the caller why we terminate the loop. */ \
356 result = __GCONV_FULL_OUTPUT; \
365 /* Plain ASCII character. */ \
366 put32 (outptr, ch); \
371 /* Tamil character. */ \
372 uint32_t u1 = tscii_to_ucs4[ch - 0x80][0]; \
376 uint32_t u2 = tscii_to_ucs4[ch - 0x80][1]; \
380 put32 (outptr, u1); \
385 /* See whether we have room for two characters. Otherwise \
386 store only the first character now, and put the second \
387 one into the queue. */ \
388 if (__glibc_unlikely (outptr + 4 > outend)) \
391 result = __GCONV_FULL_OUTPUT; \
394 put32 (outptr, u2); \
399 /* Special handling of a few Tamil characters. */ \
400 else if (ch == 0xa6 || ch == 0xa7 || ch == 0xa8) \
403 /* Now ch = 0x0BC6 or ch = 0x0BC7 or ch = 0x0BC8. */ \
408 else if (ch == 0x8a || ch == 0x8b) \
411 /* Now ch = 0x0BB8 or ch = 0x0BB9. */ \
412 put32 (outptr, ch); \
414 *statep = (0x0BCD << 8) + (1 << 3); \
418 else if (ch == 0x82) \
420 /* Output <U0BB8><U0BCD><U0BB0><U0BC0>, if we have room for \
421 four characters. */ \
423 put32 (outptr, 0x0BB8); \
425 if (__glibc_unlikely (outptr + 4 > outend)) \
427 *statep = (0x0BCD << 8) + (4 << 4); \
428 result = __GCONV_FULL_OUTPUT; \
431 put32 (outptr, 0x0BCD); \
433 if (__glibc_unlikely (outptr + 4 > outend)) \
435 *statep = (0x0BB0 << 8) + (2 << 4); \
436 result = __GCONV_FULL_OUTPUT; \
439 put32 (outptr, 0x0BB0); \
441 if (__glibc_unlikely (outptr + 4 > outend)) \
443 *statep = (0x0BC0 << 8); \
444 result = __GCONV_FULL_OUTPUT; \
447 put32 (outptr, 0x0BC0); \
451 else if (ch == 0x87) \
453 /* Output <U0B95><U0BCD><U0BB7>, if we have room for \
454 three characters. */ \
456 put32 (outptr, 0x0B95); \
458 if (__glibc_unlikely (outptr + 4 > outend)) \
460 *statep = (0x0BCD << 8) + (1 << 4); \
461 result = __GCONV_FULL_OUTPUT; \
464 put32 (outptr, 0x0BCD); \
466 if (__glibc_unlikely (outptr + 4 > outend)) \
468 *statep = (0x0BB7 << 8); \
469 result = __GCONV_FULL_OUTPUT; \
472 put32 (outptr, 0x0BB7); \
476 else if (ch == 0x8c) \
478 /* Output <U0B95><U0BCD><U0BB7><U0BCD>, if we have room for \
479 four characters. */ \
481 put32 (outptr, 0x0B95); \
483 if (__glibc_unlikely (outptr + 4 > outend)) \
485 *statep = (0x0BCD << 8) + (5 << 4); \
486 result = __GCONV_FULL_OUTPUT; \
489 put32 (outptr, 0x0BCD); \
491 if (__glibc_unlikely (outptr + 4 > outend)) \
493 *statep = (0x0BB7 << 8) + (3 << 4); \
494 result = __GCONV_FULL_OUTPUT; \
497 put32 (outptr, 0x0BB7); \
499 if (__glibc_unlikely (outptr + 4 > outend)) \
501 *statep = (0x0BCD << 8); \
502 result = __GCONV_FULL_OUTPUT; \
505 put32 (outptr, 0x0BCD); \
511 /* This is illegal. */ \
512 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
516 /* Now that we wrote the output increment the input pointer. */ \
519 #define LOOP_NEED_FLAGS
520 #define EXTRA_LOOP_DECLS , int *statep
521 #include <iconv/loop.c>
524 /* Next, define the other direction, from UCS-4 to TSCII. */
526 static const uint8_t ucs4_to_tscii
[128] =
528 0, 0, 0, 0xb7, 0, 0xab, 0xac, 0xfe, /* 0x0B80..0x0B87 */
529 0xae, 0xaf, 0xb0, 0, 0, 0, 0xb1, 0xb2, /* 0x0B88..0x0B8F */
530 0xb3, 0, 0xb4, 0xb5, 0xb6, 0xb8, 0, 0, /* 0x0B90..0x0B97 */
531 0, 0xb9, 0xba, 0, 0x83, 0, 0xbb, 0xbc, /* 0x0B98..0x0B9F */
532 0, 0, 0, 0xbd, 0xbe, 0, 0, 0, /* 0x0BA0..0x0BA7 */
533 0xbf, 0xc9, 0xc0, 0, 0, 0, 0xc1, 0xc2, /* 0x0BA8..0x0BAF */
534 0xc3, 0xc8, 0xc4, 0xc7, 0xc6, 0xc5, 0, 0x84, /* 0x0BB0..0x0BB7 */
535 0x85, 0x86, 0, 0, 0, 0, 0xa1, 0xa2, /* 0x0BB8..0x0BBF */
536 0xa3, 0xa4, 0xa5, 0, 0, 0, 0xa6, 0xa7, /* 0x0BC0..0x0BC7 */
537 0xa8, 0, 0, 0, 0, 0, 0, 0, /* 0x0BC8..0x0BCF */
538 0, 0, 0, 0, 0, 0, 0, 0xaa, /* 0x0BD0..0x0BD7 */
539 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0BD8..0x0BDF */
540 0, 0, 0, 0, 0, 0, 0x80, 0x81, /* 0x0BE0..0x0BE7 */
541 0x8d, 0x8e, 0x8f, 0x90, 0x95, 0x96, 0x97, 0x98, /* 0x0BE8..0x0BEF */
542 0x9d, 0x9e, 0x9f, 0, 0, 0, 0, 0, /* 0x0BF0..0x0BF7 */
543 0, 0, 0, 0, 0, 0, 0, 0 /* 0x0BF8..0x0BFF */
546 static const uint8_t consonant_with_u
[18] =
548 0xcc, 0x99, 0xcd, 0x9a, 0xce, 0xcf, 0xd0, 0xd1, 0xd2,
549 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb
552 static const uint8_t consonant_with_uu
[18] =
554 0xdc, 0x9b, 0xdd, 0x9c, 0xde, 0xdf, 0xe0, 0xe1, 0xe2,
555 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb
558 static const uint8_t consonant_with_virama
[18] =
560 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4,
561 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
564 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
565 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
566 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
567 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
568 #define LOOPFCT TO_LOOP
571 uint32_t ch = get32 (inptr); \
573 if ((*statep >> 3) != 0) \
575 /* Attempt to combine the last character with this one. */ \
576 uint32_t last = *statep >> 3; \
578 if (last >= 0xb8 && last <= 0xc9) \
582 *outptr++ = consonant_with_u[last - 0xb8]; \
589 *outptr++ = consonant_with_uu[last - 0xb8]; \
596 if (__glibc_likely (outptr + 2 <= outend)) \
606 result = __GCONV_FULL_OUTPUT; \
612 if (__glibc_likely (outptr + 2 <= outend)) \
622 result = __GCONV_FULL_OUTPUT; \
628 if (__glibc_likely (outptr + 2 <= outend)) \
638 result = __GCONV_FULL_OUTPUT; \
644 if (__glibc_likely (outptr + 3 <= outend)) \
655 result = __GCONV_FULL_OUTPUT; \
661 if (__glibc_likely (outptr + 3 <= outend)) \
672 result = __GCONV_FULL_OUTPUT; \
678 if (__glibc_likely (outptr + 3 <= outend)) \
689 result = __GCONV_FULL_OUTPUT; \
697 *outptr++ = consonant_with_virama[last - 0xb8]; \
701 *statep = 0xec << 3; \
705 if (last == 0xbc && (ch == 0x0BBF || ch == 0x0BC0)) \
707 *outptr++ = ch - 0x0af5; \
713 else if (last >= 0x83 && last <= 0x86) \
715 if (last >= 0x85 && (ch == 0x0BC1 || ch == 0x0BC2)) \
717 *outptr++ = last + 5; \
725 *outptr++ = last + 5; \
729 *statep = 0x8a << 3; \
734 else if (last == 0xec) \
738 *statep = 0x87 << 3; \
743 else if (last == 0x8a) \
747 *statep = 0xc38a << 3; \
752 else if (last == 0x87) \
764 assert (last == 0xc38a); \
774 /* Output the buffered character. */ \
775 if (__glibc_unlikely (last >> 8)) \
777 if (__glibc_likely (outptr + 2 <= outend)) \
779 *outptr++ = last & 0xff; \
780 *outptr++ = (last >> 8) & 0xff; \
784 result = __GCONV_FULL_OUTPUT; \
789 *outptr++ = last & 0xff; \
795 /* Plain ASCII character. */ \
797 else if (ch >= 0x0B80 && ch <= 0x0BFF) \
799 /* Tamil character. */ \
800 uint8_t t = ucs4_to_tscii[ch - 0x0B80]; \
804 if ((t >= 0xb8 && t <= 0xc9) || (t >= 0x83 && t <= 0x86)) \
805 *statep = (uint32_t) t << 3; \
809 else if (ch >= 0x0BCA && ch <= 0x0BCC) \
811 /* See whether we have room for two bytes. */ \
812 if (__glibc_likely (outptr + 2 <= outend)) \
814 *outptr++ = (ch == 0x0BCA ? 0xa6 : 0xa7); \
815 *outptr++ = (ch != 0x0BCC ? 0xa1 : 0xaa); \
819 result = __GCONV_FULL_OUTPUT; \
825 /* Illegal character. */ \
826 STANDARD_TO_LOOP_ERR_HANDLER (4); \
829 else if (ch == 0x00A9) \
831 else if (ch == 0x2018 || ch == 0x2019) \
832 *outptr++ = ch - 0x1f87; \
833 else if (ch == 0x201C || ch == 0x201D) \
834 *outptr++ = ch - 0x1f89; \
837 UNICODE_TAG_HANDLER (ch, 4); \
839 /* Illegal character. */ \
840 STANDARD_TO_LOOP_ERR_HANDLER (4); \
843 /* Now that we wrote the output increment the input pointer. */ \
846 #define LOOP_NEED_FLAGS
847 #define EXTRA_LOOP_DECLS , int *statep
848 #include <iconv/loop.c>
851 /* Now define the toplevel functions. */
852 #include <iconv/skeleton.c>