| 1 | /* Access functions for CNS 11643, plane 1 handling. | 
|---|
| 2 | Copyright (C) 1998-2020 Free Software Foundation, Inc. | 
|---|
| 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 | 
|---|
| 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. | 
|---|
| 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 | 
|---|
| 14 | Lesser General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 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 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #include <stdint.h> | 
|---|
| 21 | #include <gconv.h> | 
|---|
| 22 |  | 
|---|
| 23 | /* Table for CNS 11643, plane 1 to UCS4 conversion.  */ | 
|---|
| 24 | extern const uint16_t __cns11643l1_to_ucs4_tab[]; | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | static inline uint32_t | 
|---|
| 28 | __attribute ((always_inline)) | 
|---|
| 29 | cns11643l1_to_ucs4 (const unsigned char **s, size_t avail, | 
|---|
| 30 | unsigned char offset) | 
|---|
| 31 | { | 
|---|
| 32 | unsigned char ch = *(*s); | 
|---|
| 33 | unsigned char ch2; | 
|---|
| 34 | int idx; | 
|---|
| 35 |  | 
|---|
| 36 | if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d) | 
|---|
| 37 | return __UNKNOWN_10646_CHAR; | 
|---|
| 38 |  | 
|---|
| 39 | if (avail < 2) | 
|---|
| 40 | return 0; | 
|---|
| 41 |  | 
|---|
| 42 | ch2 = (*s)[1]; | 
|---|
| 43 | if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) | 
|---|
| 44 | return __UNKNOWN_10646_CHAR; | 
|---|
| 45 |  | 
|---|
| 46 | idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset); | 
|---|
| 47 | if (idx > 0x21f2) | 
|---|
| 48 | return __UNKNOWN_10646_CHAR; | 
|---|
| 49 |  | 
|---|
| 50 | (*s) += 2; | 
|---|
| 51 |  | 
|---|
| 52 | return __cns11643l1_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | /* Tables for the UCS4 -> CNS conversion.  */ | 
|---|
| 57 | extern const char __cns11643l1_from_ucs4_tab1[][2]; | 
|---|
| 58 | extern const char __cns11643l1_from_ucs4_tab2[][2]; | 
|---|
| 59 | extern const char __cns11643l1_from_ucs4_tab3[][2]; | 
|---|
| 60 | extern const char __cns11643l1_from_ucs4_tab4[][2]; | 
|---|
| 61 | extern const char __cns11643l1_from_ucs4_tab5[][2]; | 
|---|
| 62 | extern const char __cns11643l1_from_ucs4_tab6[][2]; | 
|---|
| 63 | extern const char __cns11643l1_from_ucs4_tab7[][2]; | 
|---|
| 64 | extern const char __cns11643l1_from_ucs4_tab8[][2]; | 
|---|
| 65 | extern const char __cns11643l1_from_ucs4_tab9[][2]; | 
|---|
| 66 | extern const char __cns11643l1_from_ucs4_tab10[][2]; | 
|---|
| 67 | extern const char __cns11643l1_from_ucs4_tab11[][2]; | 
|---|
| 68 | extern const char __cns11643l1_from_ucs4_tab12[][2]; | 
|---|
| 69 | extern const char __cns11643l1_from_ucs4_tab13[][2]; | 
|---|
| 70 | extern const char __cns11643l1_from_ucs4_tab14[][2]; | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 | static inline size_t | 
|---|
| 74 | __attribute ((always_inline)) | 
|---|
| 75 | ucs4_to_cns11643l1 (uint32_t wch, unsigned char *s, size_t avail) | 
|---|
| 76 | { | 
|---|
| 77 | unsigned int ch = (unsigned int) wch; | 
|---|
| 78 | char buf[2]; | 
|---|
| 79 | const char *cp = buf; | 
|---|
| 80 |  | 
|---|
| 81 | switch (ch) | 
|---|
| 82 | { | 
|---|
| 83 | case 0xa7 ... 0xf7: | 
|---|
| 84 | cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7]; | 
|---|
| 85 | break; | 
|---|
| 86 | case 0x2c7 ... 0x2d9: | 
|---|
| 87 | cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7]; | 
|---|
| 88 | break; | 
|---|
| 89 | case 0x391 ... 0x3c9: | 
|---|
| 90 | cp = __cns11643l1_from_ucs4_tab3[ch - 0x391]; | 
|---|
| 91 | break; | 
|---|
| 92 | case 0x2013 ... 0x203e: | 
|---|
| 93 | cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013]; | 
|---|
| 94 | break; | 
|---|
| 95 | case 0x2103: | 
|---|
| 96 | cp = "\x22\x6a"; | 
|---|
| 97 | break; | 
|---|
| 98 | case 0x2105: | 
|---|
| 99 | cp = "\x22\x22"; | 
|---|
| 100 | break; | 
|---|
| 101 | case 0x2109: | 
|---|
| 102 | cp = "\x22\x6b"; | 
|---|
| 103 | break; | 
|---|
| 104 | case 0x2160 ... 0x2169: | 
|---|
| 105 | buf[0] = '\x24'; | 
|---|
| 106 | buf[1] = '\x2b' + (ch - 0x2160); | 
|---|
| 107 | break; | 
|---|
| 108 | case 0x2170 ... 0x2179: | 
|---|
| 109 | buf[0] = '\x26'; | 
|---|
| 110 | buf[1] = '\x35' + (ch - 0x2170); | 
|---|
| 111 | break; | 
|---|
| 112 | case 0x2190 ... 0x2199: | 
|---|
| 113 | cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190]; | 
|---|
| 114 | break; | 
|---|
| 115 | case 0x2215 ... 0x2267: | 
|---|
| 116 | cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215]; | 
|---|
| 117 | break; | 
|---|
| 118 | case 0x22a5: | 
|---|
| 119 | cp = "\x22\x47"; | 
|---|
| 120 | break; | 
|---|
| 121 | case 0x22bf: | 
|---|
| 122 | cp = "\x22\x4a"; | 
|---|
| 123 | break; | 
|---|
| 124 | case 0x2400 ... 0x2421: | 
|---|
| 125 | cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400]; | 
|---|
| 126 | break; | 
|---|
| 127 | case 0x2460 ... 0x247d: | 
|---|
| 128 | cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460]; | 
|---|
| 129 | break; | 
|---|
| 130 | case 0x2500 ... 0x2642: | 
|---|
| 131 | cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500]; | 
|---|
| 132 | break; | 
|---|
| 133 | case 0x3000 ... 0x3029: | 
|---|
| 134 | cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000]; | 
|---|
| 135 | break; | 
|---|
| 136 | case 0x30fb: | 
|---|
| 137 | cp = "\x21\x26"; | 
|---|
| 138 | break; | 
|---|
| 139 | case 0x3105 ... 0x3129: | 
|---|
| 140 | buf[0] = '\x25'; | 
|---|
| 141 | buf[1] = '\x47' + (ch - 0x3105); | 
|---|
| 142 | break; | 
|---|
| 143 | case 0x32a3: | 
|---|
| 144 | cp = "\x22\x21"; | 
|---|
| 145 | break; | 
|---|
| 146 | case 0x338e ... 0x33d5: | 
|---|
| 147 | cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e]; | 
|---|
| 148 | break; | 
|---|
| 149 | case 0x4e00 ... 0x9f9c: | 
|---|
| 150 | cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00]; | 
|---|
| 151 | break; | 
|---|
| 152 | case 0xfe30 ... 0xfe6b: | 
|---|
| 153 | cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30]; | 
|---|
| 154 | break; | 
|---|
| 155 | case 0xff01 ... 0xff5d: | 
|---|
| 156 | cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01]; | 
|---|
| 157 | break; | 
|---|
| 158 | case 0xffe0: | 
|---|
| 159 | cp = "\x22\x66"; | 
|---|
| 160 | break; | 
|---|
| 161 | case 0xffe1: | 
|---|
| 162 | cp = "\x22\x67"; | 
|---|
| 163 | break; | 
|---|
| 164 | case 0xffe5: | 
|---|
| 165 | cp = "\x22\x64"; | 
|---|
| 166 | break; | 
|---|
| 167 | default: | 
|---|
| 168 | return __UNKNOWN_10646_CHAR; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | if (cp[0] == '\0') | 
|---|
| 172 | return __UNKNOWN_10646_CHAR; | 
|---|
| 173 |  | 
|---|
| 174 | if (avail < 2) | 
|---|
| 175 | return 0; | 
|---|
| 176 |  | 
|---|
| 177 | s[0] = cp[0]; | 
|---|
| 178 | s[1] = cp[1]; | 
|---|
| 179 |  | 
|---|
| 180 | return 2; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|