1 /* Conversion from and to TSCII.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 /* TSCII is an 8-bit encoding consisting of:
26 0x80..0x90, 0x95..0x9F, 0xAB..0xFE:
27 Tamil letters and glyphs
28 0xA1..0xA5, 0xAA: Tamil combining letters (after the base character)
29 0xA6..0xA8: Tamil combining letters (before the base character)
30 0x91..0x94: Punctuation
34 /* Definitions used in the body of the `gconv' function. */
35 #define CHARSET_NAME "TSCII//"
36 #define FROM_LOOP from_tscii
37 #define TO_LOOP to_tscii
40 #define ONE_DIRECTION 0
41 #define FROM_LOOP_MIN_NEEDED_FROM 1
42 #define FROM_LOOP_MAX_NEEDED_FROM 2
43 #define FROM_LOOP_MIN_NEEDED_TO 4
44 #define FROM_LOOP_MAX_NEEDED_TO 16
45 #define TO_LOOP_MIN_NEEDED_FROM 4
46 #define TO_LOOP_MAX_NEEDED_FROM 4
47 #define TO_LOOP_MIN_NEEDED_TO 1
48 #define TO_LOOP_MAX_NEEDED_TO 3
49 #define PREPARE_LOOP \
51 int *statep = &data->__statep->__count;
52 #define EXTRA_LOOP_ARGS , statep
55 /* Since we might have to reset input pointer we must be able to save
56 and restore the state. */
57 #define SAVE_RESET_STATE(Save) \
59 saved_state = *statep; \
64 /* During TSCII to UCS-4 conversion, the COUNT element of the state contains
65 the last UCS-4 character to be output, shifted by 8 bits, and an encoded
66 representation of additional UCS-4 characters to be output (if any),
67 shifted by 4 bits. This character can be:
68 0x0000 Nothing pending.
69 0x0BCD Pending VIRAMA sign. If bit 3 is set, it may be
70 omitted if followed by a vowel sign U or UU.
71 0x0BC6, 0x0BC7, 0x0BC8 Pending vowel sign. Bit 3 is set after the
73 Other Bit 3 always cleared. */
75 /* During UCS-4 to TSCII conversion, the COUNT element of the state contains
76 the last byte (or sometimes the last two bytes) to be output, shifted by
79 0xB8..0xC9, 0x83..0x86 A consonant.
80 0xEC, 0x8A A consonant with VIRAMA sign (final or joining).
81 0x87, 0xC38A Two consonants combined through a VIRAMA sign. */
83 /* Since this is a stateful encoding we have to provide code which resets
84 the output state to the initial state. This has to be done during the
86 #define EMIT_SHIFT_TO_INIT \
87 if (data->__statep->__count != 0) \
93 if (__glibc_unlikely (outbuf + 4 > outend)) \
95 /* We don't have enough room in the output buffer. */ \
96 status = __GCONV_FULL_OUTPUT; \
99 /* Write out the pending character. */ \
100 *((uint32_t *) outbuf) = data->__statep->__count >> 8; \
101 outbuf += sizeof (uint32_t); \
102 /* Retrieve the successor state. */ \
103 data->__statep->__count = \
104 tscii_next_state[(data->__statep->__count >> 4) & 0x0f]; \
106 while (data->__statep->__count != 0); \
110 uint32_t last = data->__statep->__count >> 3; \
111 if (__glibc_unlikely (last >> 8)) \
113 /* Write out the last character, two bytes. */ \
114 if (__glibc_likely (outbuf + 2 <= outend)) \
116 *outbuf++ = last & 0xff; \
117 *outbuf++ = (last >> 8) & 0xff; \
118 data->__statep->__count = 0; \
121 /* We don't have enough room in the output buffer. */ \
122 status = __GCONV_FULL_OUTPUT; \
126 /* Write out the last character, a single byte. */ \
127 if (__glibc_likely (outbuf < outend)) \
129 *outbuf++ = last & 0xff; \
130 data->__statep->__count = 0; \
133 /* We don't have enough room in the output buffer. */ \
134 status = __GCONV_FULL_OUTPUT; \
140 /* First define the conversion function from TSCII to UCS-4. */
142 static const uint16_t tscii_to_ucs4
[128][2] =
146 { 0, 0 }, /* 0x82 - maps to <U0BB8><U0BCD><U0BB0><U0BC0> */
151 { 0, 0 }, /* 0x87 - maps to <U0B95><U0BCD><U0BB7> */
154 { 0, 0 }, /* 0x8a - maps to <U0BB8> and buffers <U0BCD> */
155 { 0, 0 }, /* 0x8b - maps to <U0BB9> and buffers <U0BCD> */
156 { 0, 0 }, /* 0x8c - maps to <U0B95><U0BCD><U0BB7><U0BCD> */
176 { 0, 0 }, /* 0xa0 - unmapped */
182 { 0, 0 }, /* 0xa6 - buffers <U0BC6> */
183 { 0, 0 }, /* 0xa7 - buffers <U0BC7> */
184 { 0, 0 }, /* 0xa8 - buffers <U0BC8> */
271 { 0, 0 } /* 0xff - unmapped */
274 static const uint32_t tscii_next_state
[6] =
276 /* 0 means no more pending Unicode characters. */
278 /* 1 means <U0BB7>. */
280 /* 2 means <U0BC0>. */
282 /* 3 means <U0BCD>. */
284 /* 4 means <U0BB0><U0BC0>. */
285 (0x0BB0 << 8) + (2 << 4),
286 /* 5 means <U0BB7><U0BCD>. */
287 (0x0BB7 << 8) + (3 << 4)
290 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
291 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
292 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
293 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
294 #define LOOPFCT FROM_LOOP
297 uint32_t ch = *inptr; \
299 if ((*statep >> 8) != 0) \
301 /* Attempt to combine the last character with this one. */ \
302 uint32_t last = *statep >> 8; \
304 if (last == 0x0BCD && (*statep & (1 << 3))) \
306 if (ch == 0xa4 || ch == 0xa5) \
309 /* Now ch = 0x0BC1 or ch = 0x0BC2. */ \
310 put32 (outptr, ch); \
317 else if (last >= 0x0BC6 && last <= 0x0BC8) \
319 if ((last == 0x0BC6 && ch == 0xa1) \
320 || (last == 0x0BC7 && (ch == 0xa1 || ch == 0xaa))) \
322 ch = last + 4 + (ch != 0xa1); \
323 /* Now ch = 0x0BCA or ch = 0x0BCB or ch = 0x0BCC. */ \
324 put32 (outptr, ch); \
330 if ((ch >= 0xb8 && ch <= 0xc9) && (*statep & (1 << 3)) == 0) \
332 ch = tscii_to_ucs4[ch - 0x80][0]; \
333 put32 (outptr, ch); \
343 /* Output the buffered character. */ \
344 put32 (outptr, last); \
346 /* Retrieve the successor state. */ \
347 *statep = tscii_next_state[(*statep >> 4) & 0x0f]; \
349 while (*statep != 0 && __builtin_expect (outptr + 4 <= outend, 1)); \
353 /* We don't have enough room in the output buffer. \
354 Tell the caller why we terminate the loop. */ \
355 result = __GCONV_FULL_OUTPUT; \
364 /* Plain ASCII character. */ \
365 put32 (outptr, ch); \
370 /* Tamil character. */ \
371 uint32_t u1 = tscii_to_ucs4[ch - 0x80][0]; \
375 uint32_t u2 = tscii_to_ucs4[ch - 0x80][1]; \
379 put32 (outptr, u1); \
384 /* See whether we have room for two characters. Otherwise \
385 store only the first character now, and put the second \
386 one into the queue. */ \
387 if (__glibc_unlikely (outptr + 4 > outend)) \
390 result = __GCONV_FULL_OUTPUT; \
393 put32 (outptr, u2); \
398 /* Special handling of a few Tamil characters. */ \
399 else if (ch == 0xa6 || ch == 0xa7 || ch == 0xa8) \
402 /* Now ch = 0x0BC6 or ch = 0x0BC7 or ch = 0x0BC8. */ \
407 else if (ch == 0x8a || ch == 0x8b) \
410 /* Now ch = 0x0BB8 or ch = 0x0BB9. */ \
411 put32 (outptr, ch); \
413 *statep = (0x0BCD << 8) + (1 << 3); \
417 else if (ch == 0x82) \
419 /* Output <U0BB8><U0BCD><U0BB0><U0BC0>, if we have room for \
420 four characters. */ \
422 put32 (outptr, 0x0BB8); \
424 if (__glibc_unlikely (outptr + 4 > outend)) \
426 *statep = (0x0BCD << 8) + (4 << 4); \
427 result = __GCONV_FULL_OUTPUT; \
430 put32 (outptr, 0x0BCD); \
432 if (__glibc_unlikely (outptr + 4 > outend)) \
434 *statep = (0x0BB0 << 8) + (2 << 4); \
435 result = __GCONV_FULL_OUTPUT; \
438 put32 (outptr, 0x0BB0); \
440 if (__glibc_unlikely (outptr + 4 > outend)) \
442 *statep = (0x0BC0 << 8); \
443 result = __GCONV_FULL_OUTPUT; \
446 put32 (outptr, 0x0BC0); \
450 else if (ch == 0x87) \
452 /* Output <U0B95><U0BCD><U0BB7>, if we have room for \
453 three characters. */ \
455 put32 (outptr, 0x0B95); \
457 if (__glibc_unlikely (outptr + 4 > outend)) \
459 *statep = (0x0BCD << 8) + (1 << 4); \
460 result = __GCONV_FULL_OUTPUT; \
463 put32 (outptr, 0x0BCD); \
465 if (__glibc_unlikely (outptr + 4 > outend)) \
467 *statep = (0x0BB7 << 8); \
468 result = __GCONV_FULL_OUTPUT; \
471 put32 (outptr, 0x0BB7); \
475 else if (ch == 0x8c) \
477 /* Output <U0B95><U0BCD><U0BB7><U0BCD>, if we have room for \
478 four characters. */ \
480 put32 (outptr, 0x0B95); \
482 if (__glibc_unlikely (outptr + 4 > outend)) \
484 *statep = (0x0BCD << 8) + (5 << 4); \
485 result = __GCONV_FULL_OUTPUT; \
488 put32 (outptr, 0x0BCD); \
490 if (__glibc_unlikely (outptr + 4 > outend)) \
492 *statep = (0x0BB7 << 8) + (3 << 4); \
493 result = __GCONV_FULL_OUTPUT; \
496 put32 (outptr, 0x0BB7); \
498 if (__glibc_unlikely (outptr + 4 > outend)) \
500 *statep = (0x0BCD << 8); \
501 result = __GCONV_FULL_OUTPUT; \
504 put32 (outptr, 0x0BCD); \
510 /* This is illegal. */ \
511 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
515 /* Now that we wrote the output increment the input pointer. */ \
518 #define LOOP_NEED_FLAGS
519 #define EXTRA_LOOP_DECLS , int *statep
520 #include <iconv/loop.c>
523 /* Next, define the other direction, from UCS-4 to TSCII. */
525 static const uint8_t ucs4_to_tscii
[128] =
527 0, 0, 0, 0xb7, 0, 0xab, 0xac, 0xfe, /* 0x0B80..0x0B87 */
528 0xae, 0xaf, 0xb0, 0, 0, 0, 0xb1, 0xb2, /* 0x0B88..0x0B8F */
529 0xb3, 0, 0xb4, 0xb5, 0xb6, 0xb8, 0, 0, /* 0x0B90..0x0B97 */
530 0, 0xb9, 0xba, 0, 0x83, 0, 0xbb, 0xbc, /* 0x0B98..0x0B9F */
531 0, 0, 0, 0xbd, 0xbe, 0, 0, 0, /* 0x0BA0..0x0BA7 */
532 0xbf, 0xc9, 0xc0, 0, 0, 0, 0xc1, 0xc2, /* 0x0BA8..0x0BAF */
533 0xc3, 0xc8, 0xc4, 0xc7, 0xc6, 0xc5, 0, 0x84, /* 0x0BB0..0x0BB7 */
534 0x85, 0x86, 0, 0, 0, 0, 0xa1, 0xa2, /* 0x0BB8..0x0BBF */
535 0xa3, 0xa4, 0xa5, 0, 0, 0, 0xa6, 0xa7, /* 0x0BC0..0x0BC7 */
536 0xa8, 0, 0, 0, 0, 0, 0, 0, /* 0x0BC8..0x0BCF */
537 0, 0, 0, 0, 0, 0, 0, 0xaa, /* 0x0BD0..0x0BD7 */
538 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0BD8..0x0BDF */
539 0, 0, 0, 0, 0, 0, 0x80, 0x81, /* 0x0BE0..0x0BE7 */
540 0x8d, 0x8e, 0x8f, 0x90, 0x95, 0x96, 0x97, 0x98, /* 0x0BE8..0x0BEF */
541 0x9d, 0x9e, 0x9f, 0, 0, 0, 0, 0, /* 0x0BF0..0x0BF7 */
542 0, 0, 0, 0, 0, 0, 0, 0 /* 0x0BF8..0x0BFF */
545 static const uint8_t consonant_with_u
[18] =
547 0xcc, 0x99, 0xcd, 0x9a, 0xce, 0xcf, 0xd0, 0xd1, 0xd2,
548 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb
551 static const uint8_t consonant_with_uu
[18] =
553 0xdc, 0x9b, 0xdd, 0x9c, 0xde, 0xdf, 0xe0, 0xe1, 0xe2,
554 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb
557 static const uint8_t consonant_with_virama
[18] =
559 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4,
560 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
563 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
564 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
565 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
566 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
567 #define LOOPFCT TO_LOOP
570 uint32_t ch = get32 (inptr); \
572 if ((*statep >> 3) != 0) \
574 /* Attempt to combine the last character with this one. */ \
575 uint32_t last = *statep >> 3; \
577 if (last >= 0xb8 && last <= 0xc9) \
581 *outptr++ = consonant_with_u[last - 0xb8]; \
588 *outptr++ = consonant_with_uu[last - 0xb8]; \
595 if (__glibc_likely (outptr + 2 <= outend)) \
605 result = __GCONV_FULL_OUTPUT; \
611 if (__glibc_likely (outptr + 2 <= outend)) \
621 result = __GCONV_FULL_OUTPUT; \
627 if (__glibc_likely (outptr + 2 <= outend)) \
637 result = __GCONV_FULL_OUTPUT; \
643 if (__glibc_likely (outptr + 3 <= outend)) \
654 result = __GCONV_FULL_OUTPUT; \
660 if (__glibc_likely (outptr + 3 <= outend)) \
671 result = __GCONV_FULL_OUTPUT; \
677 if (__glibc_likely (outptr + 3 <= outend)) \
688 result = __GCONV_FULL_OUTPUT; \
696 *outptr++ = consonant_with_virama[last - 0xb8]; \
700 *statep = 0xec << 3; \
704 if (last == 0xbc && (ch == 0x0BBF || ch == 0x0BC0)) \
706 *outptr++ = ch - 0x0af5; \
712 else if (last >= 0x83 && last <= 0x86) \
714 if (last >= 0x85 && (ch == 0x0BC1 || ch == 0x0BC2)) \
716 *outptr++ = last + 5; \
724 *outptr++ = last + 5; \
728 *statep = 0x8a << 3; \
733 else if (last == 0xec) \
737 *statep = 0x87 << 3; \
742 else if (last == 0x8a) \
746 *statep = 0xc38a << 3; \
751 else if (last == 0x87) \
763 assert (last == 0xc38a); \
773 /* Output the buffered character. */ \
774 if (__glibc_unlikely (last >> 8)) \
776 if (__glibc_likely (outptr + 2 <= outend)) \
778 *outptr++ = last & 0xff; \
779 *outptr++ = (last >> 8) & 0xff; \
783 result = __GCONV_FULL_OUTPUT; \
788 *outptr++ = last & 0xff; \
794 /* Plain ASCII character. */ \
796 else if (ch >= 0x0B80 && ch <= 0x0BFF) \
798 /* Tamil character. */ \
799 uint8_t t = ucs4_to_tscii[ch - 0x0B80]; \
803 if ((t >= 0xb8 && t <= 0xc9) || (t >= 0x83 && t <= 0x86)) \
804 *statep = (uint32_t) t << 3; \
808 else if (ch >= 0x0BCA && ch <= 0x0BCC) \
810 /* See whether we have room for two bytes. */ \
811 if (__glibc_likely (outptr + 2 <= outend)) \
813 *outptr++ = (ch == 0x0BCA ? 0xa6 : 0xa7); \
814 *outptr++ = (ch != 0x0BCC ? 0xa1 : 0xaa); \
818 result = __GCONV_FULL_OUTPUT; \
824 /* Illegal character. */ \
825 STANDARD_TO_LOOP_ERR_HANDLER (4); \
828 else if (ch == 0x00A9) \
830 else if (ch == 0x2018 || ch == 0x2019) \
831 *outptr++ = ch - 0x1f87; \
832 else if (ch == 0x201C || ch == 0x201D) \
833 *outptr++ = ch - 0x1f89; \
836 UNICODE_TAG_HANDLER (ch, 4); \
838 /* Illegal character. */ \
839 STANDARD_TO_LOOP_ERR_HANDLER (4); \
842 /* Now that we wrote the output increment the input pointer. */ \
845 #define LOOP_NEED_FLAGS
846 #define EXTRA_LOOP_DECLS , int *statep
847 #include <iconv/loop.c>
850 /* Now define the toplevel functions. */
851 #include <iconv/skeleton.c>