OLD | NEW |
1 #include <iconv.h> | 1 #include <iconv.h> |
2 #include <errno.h> | 2 #include <errno.h> |
3 #include <wchar.h> | 3 #include <wchar.h> |
4 #include <string.h> | 4 #include <string.h> |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <limits.h> | 6 #include <limits.h> |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include "locale_impl.h" | 8 #include "locale_impl.h" |
9 | 9 |
10 #define UTF_32BE 0300 | 10 #define UTF_32BE 0300 |
11 #define UTF_16LE 0301 | 11 #define UTF_16LE 0301 |
12 #define UTF_16BE 0302 | 12 #define UTF_16BE 0302 |
13 #define UTF_32LE 0303 | 13 #define UTF_32LE 0303 |
14 #define UCS2BE 0304 | 14 #define UCS2BE 0304 |
15 #define UCS2LE 0305 | 15 #define UCS2LE 0305 |
16 #define WCHAR_T 0306 | 16 #define WCHAR_T 0306 |
17 #define US_ASCII 0307 | 17 #define US_ASCII 0307 |
18 #define UTF_8 0310 | 18 #define UTF_8 0310 |
19 #define EUC_JP 0320 | 19 #define EUC_JP 0320 |
20 #define SHIFT_JIS 0321 | 20 #define SHIFT_JIS 0321 |
21 #define GB18030 0330 | 21 #define GB18030 0330 |
22 #define GBK 0331 | 22 #define GBK 0331 |
23 #define GB2312 0332 | 23 #define GB2312 0332 |
24 #define BIG5 0340 | 24 #define BIG5 0340 |
25 #define EUC_KR 0350 | 25 #define EUC_KR 0350 |
26 | 26 |
27 /* Definitions of charmaps. Each charmap consists of: | 27 /* Definitions of charmaps. Each charmap consists of: |
28 * 1. Empty-string-terminated list of null-terminated aliases. | 28 * 1. Empty-string-terminated list of null-terminated aliases. |
29 * 2. Special type code or number of elided entries. | 29 * 2. Special type code or number of elided entries. |
30 * 3. Character table (size determined by field 2). */ | 30 * 3. Character table (size determined by field 2). */ |
31 | 31 |
32 static const unsigned char charmaps[] = | 32 static const unsigned char charmaps[] = |
33 "utf8\0char\0\0\310" | 33 "utf8\0char\0\0\310" |
34 "wchart\0\0\306" | 34 "wchart\0\0\306" |
35 "ucs2\0ucs2be\0\0\304" | 35 "ucs2\0ucs2be\0\0\304" |
36 "ucs2le\0\0\305" | 36 "ucs2le\0\0\305" |
37 "utf16\0utf16be\0\0\302" | 37 "utf16\0utf16be\0\0\302" |
38 "utf16le\0\0\301" | 38 "utf16le\0\0\301" |
39 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300" | 39 "ucs4\0ucs4be\0utf32\0utf32be\0\0\300" |
40 "ucs4le\0utf32le\0\0\303" | 40 "ucs4le\0utf32le\0\0\303" |
41 "ascii\0usascii\0iso646\0iso646us\0\0\307" | 41 "ascii\0usascii\0iso646\0iso646us\0\0\307" |
42 "eucjp\0\0\320" | 42 "eucjp\0\0\320" |
43 "shiftjis\0sjis\0\0\321" | 43 "shiftjis\0sjis\0\0\321" |
44 "gb18030\0\0\330" | 44 "gb18030\0\0\330" |
45 "gbk\0\0\331" | 45 "gbk\0\0\331" |
46 "gb2312\0\0\332" | 46 "gb2312\0\0\332" |
47 "big5\0bigfive\0cp950\0big5hkscs\0\0\340" | 47 "big5\0bigfive\0cp950\0big5hkscs\0\0\340" |
48 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350" | 48 "euckr\0ksc5601\0ksx1001\0cp949\0\0\350" |
49 #include "codepages.h" | 49 #include "codepages.h" |
50 ; | 50 ; |
51 | 51 |
52 static const unsigned short legacy_chars[] = { | 52 static const unsigned short legacy_chars[] = { |
53 #include "legacychars.h" | 53 #include "legacychars.h" |
54 }; | 54 }; |
55 | 55 |
56 static const unsigned short jis0208[84][94] = { | 56 static const unsigned short jis0208[84][94] = { |
57 #include "jis0208.h" | 57 #include "jis0208.h" |
58 }; | 58 }; |
59 | 59 |
60 static const unsigned short gb18030[126][190] = { | 60 static const unsigned short gb18030[126][190] = { |
61 #include "gb18030.h" | 61 #include "gb18030.h" |
62 }; | 62 }; |
63 | 63 |
64 static const unsigned short big5[89][157] = { | 64 static const unsigned short big5[89][157] = { |
65 #include "big5.h" | 65 #include "big5.h" |
66 }; | 66 }; |
67 | 67 |
68 static const unsigned short hkscs[] = { | 68 static const unsigned short hkscs[] = { |
69 #include "hkscs.h" | 69 #include "hkscs.h" |
70 }; | 70 }; |
71 | 71 |
72 static const unsigned short ksc[93][94] = { | 72 static const unsigned short ksc[93][94] = { |
73 #include "ksc.h" | 73 #include "ksc.h" |
74 }; | 74 }; |
75 | 75 |
76 static int fuzzycmp(const unsigned char *a, const unsigned char *b) | 76 static int fuzzycmp(const unsigned char* a, const unsigned char* b) { |
77 { | 77 for (; *a && *b; a++, b++) { |
78 » for (; *a && *b; a++, b++) { | 78 while (*a && (*a | 32U) - 'a' > 26 && *a - '0' > 10U) |
79 » » while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++; | 79 a++; |
80 » » if ((*a|32U) != *b) return 1; | 80 if ((*a | 32U) != *b) |
81 » } | 81 return 1; |
82 » return *a != *b; | 82 } |
83 } | 83 return *a != *b; |
84 | 84 } |
85 static size_t find_charmap(const void *name) | 85 |
86 { | 86 static size_t find_charmap(const void* name) { |
87 » const unsigned char *s; | 87 const unsigned char* s; |
88 » if (!*(char *)name) name=charmaps; /* "utf8" */ | 88 if (!*(char*)name) |
89 » for (s=charmaps; *s; ) { | 89 name = charmaps; /* "utf8" */ |
90 » » if (!fuzzycmp(name, s)) { | 90 for (s = charmaps; *s;) { |
91 » » » for (; *s; s+=strlen((void *)s)+1); | 91 if (!fuzzycmp(name, s)) { |
92 » » » return s+1-charmaps; | 92 for (; *s; s += strlen((void*)s) + 1) |
93 » » } | 93 ; |
94 » » s += strlen((void *)s)+1; | 94 return s + 1 - charmaps; |
95 » » if (!*s) { | 95 } |
96 » » » if (s[1] > 0200) s+=2; | 96 s += strlen((void*)s) + 1; |
97 » » » else s+=2+(128U-s[1])/4*5; | 97 if (!*s) { |
98 » » } | 98 if (s[1] > 0200) |
99 » } | 99 s += 2; |
100 » return -1; | 100 else |
101 } | 101 s += 2 + (128U - s[1]) / 4 * 5; |
102 | 102 } |
103 iconv_t iconv_open(const char *to, const char *from) | 103 } |
104 { | 104 return -1; |
105 » size_t f, t; | 105 } |
106 | 106 |
107 » if ((t = find_charmap(to))==-1 | 107 iconv_t iconv_open(const char* to, const char* from) { |
108 » || (f = find_charmap(from))==-1 | 108 size_t f, t; |
109 » || (charmaps[t] >= 0320)) { | 109 |
110 » » errno = EINVAL; | 110 if ((t = find_charmap(to)) == -1 || (f = find_charmap(from)) == -1 || |
111 » » return (iconv_t)-1; | 111 (charmaps[t] >= 0320)) { |
112 » } | 112 errno = EINVAL; |
113 | 113 return (iconv_t)-1; |
114 » return (void *)(f<<16 | t); | 114 } |
115 } | 115 |
116 | 116 return (void*)(f << 16 | t); |
117 int iconv_close(iconv_t cd) | 117 } |
118 { | 118 |
119 » return 0; | 119 int iconv_close(iconv_t cd) { |
120 } | 120 return 0; |
121 | 121 } |
122 static unsigned get_16(const unsigned char *s, int e) | 122 |
123 { | 123 static unsigned get_16(const unsigned char* s, int e) { |
124 » e &= 1; | 124 e &= 1; |
125 » return s[e]<<8 | s[1-e]; | 125 return s[e] << 8 | s[1 - e]; |
126 } | 126 } |
127 | 127 |
128 static void put_16(unsigned char *s, unsigned c, int e) | 128 static void put_16(unsigned char* s, unsigned c, int e) { |
129 { | 129 e &= 1; |
130 » e &= 1; | 130 s[e] = c >> 8; |
131 » s[e] = c>>8; | 131 s[1 - e] = c; |
132 » s[1-e] = c; | 132 } |
133 } | 133 |
134 | 134 static unsigned get_32(const unsigned char* s, int e) { |
135 static unsigned get_32(const unsigned char *s, int e) | 135 e &= 3; |
136 { | 136 return ((unsigned)s[e]) << 24 | s[e ^ 1] << 16 | s[e ^ 2] << 8 | s[e ^ 3]; |
137 » e &= 3; | 137 } |
138 » return ((unsigned)s[e])<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3]; | 138 |
139 } | 139 static void put_32(unsigned char* s, unsigned c, int e) { |
140 | 140 e &= 3; |
141 static void put_32(unsigned char *s, unsigned c, int e) | 141 s[e ^ 0] = c >> 24; |
142 { | 142 s[e ^ 1] = c >> 16; |
143 » e &= 3; | 143 s[e ^ 2] = c >> 8; |
144 » s[e^0] = c>>24; | 144 s[e ^ 3] = c; |
145 » s[e^1] = c>>16; | |
146 » s[e^2] = c>>8; | |
147 » s[e^3] = c; | |
148 } | 145 } |
149 | 146 |
150 /* Adapt as needed */ | 147 /* Adapt as needed */ |
151 #define mbrtowc_utf8 mbrtowc | 148 #define mbrtowc_utf8 mbrtowc |
152 #define wctomb_utf8 wctomb | 149 #define wctomb_utf8 wctomb |
153 | 150 |
154 size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
ict out, size_t *restrict outb) | 151 size_t iconv(iconv_t cd0, |
155 { | 152 char** restrict in, |
156 size_t x=0; | 153 size_t* restrict inb, |
157 unsigned long cd = (unsigned long)cd0; | 154 char** restrict out, |
158 unsigned to = cd & 0xffff; | 155 size_t* restrict outb) { |
159 unsigned from = cd >> 16; | 156 size_t x = 0; |
160 const unsigned char *map = charmaps+from+1; | 157 unsigned long cd = (unsigned long)cd0; |
161 const unsigned char *tomap = charmaps+to+1; | 158 unsigned to = cd & 0xffff; |
162 mbstate_t st = {0}; | 159 unsigned from = cd >> 16; |
163 wchar_t wc; | 160 const unsigned char* map = charmaps + from + 1; |
164 unsigned c, d; | 161 const unsigned char* tomap = charmaps + to + 1; |
165 size_t k, l; | 162 mbstate_t st = {0}; |
166 int err; | 163 wchar_t wc; |
167 unsigned char type = map[-1]; | 164 unsigned c, d; |
168 unsigned char totype = tomap[-1]; | 165 size_t k, l; |
169 locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; | 166 int err; |
170 | 167 unsigned char type = map[-1]; |
171 if (!in || !*in || !*inb) return 0; | 168 unsigned char totype = tomap[-1]; |
172 | 169 locale_t *ploc = &CURRENT_LOCALE, loc = *ploc; |
173 *ploc = UTF8_LOCALE; | 170 |
174 | 171 if (!in || !*in || !*inb) |
175 for (; *inb; *in+=l, *inb-=l) { | 172 return 0; |
176 c = *(unsigned char *)*in; | 173 |
177 l = 1; | 174 *ploc = UTF8_LOCALE; |
178 | 175 |
179 if (c >= 128 || type-UTF_32BE < 7U) switch (type) { | 176 for (; *inb; *in += l, *inb -= l) { |
180 case UTF_8: | 177 c = *(unsigned char*)*in; |
181 l = mbrtowc_utf8(&wc, *in, *inb, &st); | 178 l = 1; |
182 if (!l) l++; | 179 |
183 else if (l == (size_t)-1) goto ilseq; | 180 if (c >= 128 || type - UTF_32BE < 7U) |
184 else if (l == (size_t)-2) goto starved; | 181 switch (type) { |
185 c = wc; | 182 case UTF_8: |
186 break; | 183 l = mbrtowc_utf8(&wc, *in, *inb, &st); |
187 case US_ASCII: | 184 if (!l) |
188 goto ilseq; | 185 l++; |
189 case WCHAR_T: | 186 else if (l == (size_t)-1) |
190 l = sizeof(wchar_t); | 187 goto ilseq; |
191 if (*inb < l) goto starved; | 188 else if (l == (size_t)-2) |
192 c = *(wchar_t *)*in; | 189 goto starved; |
193 if (0) { | 190 c = wc; |
194 case UTF_32BE: | 191 break; |
195 case UTF_32LE: | 192 case US_ASCII: |
196 l = 4; | 193 goto ilseq; |
197 if (*inb < 4) goto starved; | 194 case WCHAR_T: |
198 c = get_32((void *)*in, type); | 195 l = sizeof(wchar_t); |
199 } | 196 if (*inb < l) |
200 if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq; | 197 goto starved; |
201 break; | 198 c = *(wchar_t*)*in; |
202 case UCS2BE: | 199 if (0) { |
203 case UCS2LE: | 200 case UTF_32BE: |
204 case UTF_16BE: | 201 case UTF_32LE: |
205 case UTF_16LE: | 202 l = 4; |
206 l = 2; | 203 if (*inb < 4) |
207 if (*inb < 2) goto starved; | 204 goto starved; |
208 c = get_16((void *)*in, type); | 205 c = get_32((void*)*in, type); |
209 if ((unsigned)(c-0xdc00) < 0x400) goto ilseq; | 206 } |
210 if ((unsigned)(c-0xd800) < 0x400) { | 207 if (c - 0xd800u < 0x800u || c >= 0x110000u) |
211 if (type-UCS2BE < 2U) goto ilseq; | 208 goto ilseq; |
212 l = 4; | 209 break; |
213 if (*inb < 4) goto starved; | 210 case UCS2BE: |
214 d = get_16((void *)(*in + 2), type); | 211 case UCS2LE: |
215 if ((unsigned)(d-0xdc00) >= 0x400) goto ilseq; | 212 case UTF_16BE: |
216 c = ((c-0xd7c0)<<10) + (d-0xdc00); | 213 case UTF_16LE: |
217 } | 214 l = 2; |
218 break; | 215 if (*inb < 2) |
219 case SHIFT_JIS: | 216 goto starved; |
220 if (c-0xa1 <= 0xdf-0xa1) { | 217 c = get_16((void*)*in, type); |
221 c += 0xff61-0xa1; | 218 if ((unsigned)(c - 0xdc00) < 0x400) |
222 break; | 219 goto ilseq; |
223 } | 220 if ((unsigned)(c - 0xd800) < 0x400) { |
224 l = 2; | 221 if (type - UCS2BE < 2U) |
225 if (*inb < 2) goto starved; | 222 goto ilseq; |
226 d = *((unsigned char *)*in + 1); | 223 l = 4; |
227 if (c-129 <= 159-129) c -= 129; | 224 if (*inb < 4) |
228 else if (c-224 <= 239-224) c -= 193; | 225 goto starved; |
229 else goto ilseq; | 226 d = get_16((void*)(*in + 2), type); |
230 c *= 2; | 227 if ((unsigned)(d - 0xdc00) >= 0x400) |
231 if (d-64 <= 158-64) { | 228 goto ilseq; |
232 if (d==127) goto ilseq; | 229 c = ((c - 0xd7c0) << 10) + (d - 0xdc00); |
233 if (d>127) d--; | 230 } |
234 d -= 64; | 231 break; |
235 } else if (d-159 <= 252-159) { | 232 case SHIFT_JIS: |
236 c++; | 233 if (c - 0xa1 <= 0xdf - 0xa1) { |
237 d -= 159; | 234 c += 0xff61 - 0xa1; |
238 } | 235 break; |
239 c = jis0208[c][d]; | 236 } |
240 if (!c) goto ilseq; | 237 l = 2; |
241 break; | 238 if (*inb < 2) |
242 case EUC_JP: | 239 goto starved; |
243 l = 2; | 240 d = *((unsigned char*)*in + 1); |
244 if (*inb < 2) goto starved; | 241 if (c - 129 <= 159 - 129) |
245 d = *((unsigned char *)*in + 1); | 242 c -= 129; |
246 if (c==0x8e) { | 243 else if (c - 224 <= 239 - 224) |
247 c = d; | 244 c -= 193; |
248 if (c-0xa1 > 0xdf-0xa1) goto ilseq; | 245 else |
249 c += 0xff61 - 0xa1; | 246 goto ilseq; |
250 break; | 247 c *= 2; |
251 } | 248 if (d - 64 <= 158 - 64) { |
252 c -= 0xa1; | 249 if (d == 127) |
253 d -= 0xa1; | 250 goto ilseq; |
254 if (c >= 84 || d >= 94) goto ilseq; | 251 if (d > 127) |
255 c = jis0208[c][d]; | 252 d--; |
256 if (!c) goto ilseq; | 253 d -= 64; |
257 break; | 254 } else if (d - 159 <= 252 - 159) { |
258 case GB2312: | 255 c++; |
259 if (c < 0xa1) goto ilseq; | 256 d -= 159; |
260 case GBK: | 257 } |
261 case GB18030: | 258 c = jis0208[c][d]; |
262 c -= 0x81; | 259 if (!c) |
263 if (c >= 126) goto ilseq; | 260 goto ilseq; |
264 l = 2; | 261 break; |
265 if (*inb < 2) goto starved; | 262 case EUC_JP: |
266 d = *((unsigned char *)*in + 1); | 263 l = 2; |
267 if (d < 0xa1 && type == GB2312) goto ilseq; | 264 if (*inb < 2) |
268 if (d-0x40>=191 || d==127) { | 265 goto starved; |
269 if (d-'0'>9 || type != GB18030) | 266 d = *((unsigned char*)*in + 1); |
270 goto ilseq; | 267 if (c == 0x8e) { |
271 l = 4; | 268 c = d; |
272 if (*inb < 4) goto starved; | 269 if (c - 0xa1 > 0xdf - 0xa1) |
273 c = (10*c + d-'0') * 1260; | 270 goto ilseq; |
274 d = *((unsigned char *)*in + 2); | 271 c += 0xff61 - 0xa1; |
275 if (d-0x81>126) goto ilseq; | 272 break; |
276 c += 10*(d-0x81); | 273 } |
277 d = *((unsigned char *)*in + 3); | 274 c -= 0xa1; |
278 if (d-'0'>9) goto ilseq; | 275 d -= 0xa1; |
279 c += d-'0'; | 276 if (c >= 84 || d >= 94) |
280 c += 128; | 277 goto ilseq; |
281 for (d=0; d<=c; ) { | 278 c = jis0208[c][d]; |
282 k = 0; | 279 if (!c) |
283 for (int i=0; i<126; i++) | 280 goto ilseq; |
284 for (int j=0; j<190; j++) | 281 break; |
285 if (gb18030[i][j]-d <= c
-d) | 282 case GB2312: |
286 k++; | 283 if (c < 0xa1) |
287 d = c+1; | 284 goto ilseq; |
288 c += k; | 285 case GBK: |
289 } | 286 case GB18030: |
290 break; | 287 c -= 0x81; |
291 } | 288 if (c >= 126) |
292 d -= 0x40; | 289 goto ilseq; |
293 if (d>63) d--; | 290 l = 2; |
294 c = gb18030[c][d]; | 291 if (*inb < 2) |
295 break; | 292 goto starved; |
296 case BIG5: | 293 d = *((unsigned char*)*in + 1); |
297 l = 2; | 294 if (d < 0xa1 && type == GB2312) |
298 if (*inb < 2) goto starved; | 295 goto ilseq; |
299 d = *((unsigned char *)*in + 1); | 296 if (d - 0x40 >= 191 || d == 127) { |
300 if (d-0x40>=0xff-0x40 || d-0x7f<0xa1-0x7f) goto ilseq; | 297 if (d - '0' > 9 || type != GB18030) |
301 d -= 0x40; | 298 goto ilseq; |
302 if (d > 0x3e) d -= 0x22; | 299 l = 4; |
303 if (c-0xa1>=0xfa-0xa1) { | 300 if (*inb < 4) |
304 if (c-0x87>=0xff-0x87) goto ilseq; | 301 goto starved; |
305 if (c < 0xa1) c -= 0x87; | 302 c = (10 * c + d - '0') * 1260; |
306 else c -= 0x87 + (0xfa-0xa1); | 303 d = *((unsigned char*)*in + 2); |
307 c = (hkscs[4867+(c*157+d)/16]>>(c*157+d)%16)%2<<
17 | 304 if (d - 0x81 > 126) |
308 | hkscs[c*157+d]; | 305 goto ilseq; |
309 /* A few HKSCS characters map to pairs of UCS | 306 c += 10 * (d - 0x81); |
310 * characters. These are mapped to surrogate | 307 d = *((unsigned char*)*in + 3); |
311 * range in the hkscs table then hard-coded | 308 if (d - '0' > 9) |
312 * here. Ugly, yes. */ | 309 goto ilseq; |
313 if (c/256 == 0xdc) { | 310 c += d - '0'; |
314 if (totype-0300U > 8) k = 2; | 311 c += 128; |
315 else k = "\10\4\4\10\4\4\10\2\4"[totype-
0300]; | 312 for (d = 0; d <= c;) { |
316 if (k > *outb) goto toobig; | 313 k = 0; |
317 x += iconv((iconv_t)(uintptr_t)to, | 314 for (int i = 0; i < 126; i++) |
318 &(char *){&"\303\212\314\204" | 315 for (int j = 0; j < 190; j++) |
319 "\303\212\314\214" | 316 if (gb18030[i][j] - d <= c - d) |
320 "\303\252\314\204" | 317 k++; |
321 "\303\252\314\214" | 318 d = c + 1; |
322 [c%256]}, &(size_t){4}, | 319 c += k; |
323 out, outb); | 320 } |
324 continue; | 321 break; |
325 } | 322 } |
326 if (!c) goto ilseq; | 323 d -= 0x40; |
327 break; | 324 if (d > 63) |
328 } | 325 d--; |
329 c -= 0xa1; | 326 c = gb18030[c][d]; |
330 c = big5[c][d]|(c==0x27&&(d==0x3a||d==0x3c||d==0x42))<<1
7; | 327 break; |
331 if (!c) goto ilseq; | 328 case BIG5: |
332 break; | 329 l = 2; |
333 case EUC_KR: | 330 if (*inb < 2) |
334 l = 2; | 331 goto starved; |
335 if (*inb < 2) goto starved; | 332 d = *((unsigned char*)*in + 1); |
336 d = *((unsigned char *)*in + 1); | 333 if (d - 0x40 >= 0xff - 0x40 || d - 0x7f < 0xa1 - 0x7f) |
337 c -= 0xa1; | 334 goto ilseq; |
338 d -= 0xa1; | 335 d -= 0x40; |
339 if (c >= 93 || d >= 94) { | 336 if (d > 0x3e) |
340 c += (0xa1-0x81); | 337 d -= 0x22; |
341 d += 0xa1; | 338 if (c - 0xa1 >= 0xfa - 0xa1) { |
342 if (c >= 93 || (c>=0xc6-0x81 && d>0x52)) | 339 if (c - 0x87 >= 0xff - 0x87) |
343 goto ilseq; | 340 goto ilseq; |
344 if (d-'A'<26) d = d-'A'; | 341 if (c < 0xa1) |
345 else if (d-'a'<26) d = d-'a'+26; | 342 c -= 0x87; |
346 else if (d-0x81<0xff-0x81) d = d-0x81+52; | 343 else |
347 else goto ilseq; | 344 c -= 0x87 + (0xfa - 0xa1); |
348 if (c < 0x20) c = 178*c + d; | 345 c = (hkscs[4867 + (c * 157 + d) / 16] >> (c * 157 + d) % 16) % 2 |
349 else c = 178*0x20 + 84*(c-0x20) + d; | 346 << 17 | |
350 c += 0xac00; | 347 hkscs[c * 157 + d]; |
351 for (d=0xac00; d<=c; ) { | 348 /* A few HKSCS characters map to pairs of UCS |
352 k = 0; | 349 * characters. These are mapped to surrogate |
353 for (int i=0; i<93; i++) | 350 * range in the hkscs table then hard-coded |
354 for (int j=0; j<94; j++) | 351 * here. Ugly, yes. */ |
355 if (ksc[i][j]-d <= c-d) | 352 if (c / 256 == 0xdc) { |
356 k++; | 353 if (totype - 0300U > 8) |
357 d = c+1; | 354 k = 2; |
358 c += k; | 355 else |
359 } | 356 k = "\10\4\4\10\4\4\10\2\4"[totype - 0300]; |
360 break; | 357 if (k > *outb) |
361 } | 358 goto toobig; |
362 c = ksc[c][d]; | 359 x += iconv((iconv_t)(uintptr_t)to, |
363 if (!c) goto ilseq; | 360 &(char*){&"\303\212\314\204" |
364 break; | 361 "\303\212\314\214" |
365 default: | 362 "\303\252\314\204" |
366 if (c < 128+type) break; | 363 "\303\252\314\214"[c % 256]}, |
367 c -= 128+type; | 364 &(size_t){4}, out, outb); |
368 c = legacy_chars[ (map[c*5/4]>>(2*c%8)) | | 365 continue; |
369 ((map[c*5/4+1]<<(8-2*c%8)) & 1023) ]; | 366 } |
370 if (!c) c = *(unsigned char *)*in; | 367 if (!c) |
371 if (c==1) goto ilseq; | 368 goto ilseq; |
372 } | 369 break; |
373 | 370 } |
374 switch (totype) { | 371 c -= 0xa1; |
375 case WCHAR_T: | 372 c = big5[c][d] | |
376 if (*outb < sizeof(wchar_t)) goto toobig; | 373 (c == 0x27 && (d == 0x3a || d == 0x3c || d == 0x42)) << 17; |
377 *(wchar_t *)*out = c; | 374 if (!c) |
378 *out += sizeof(wchar_t); | 375 goto ilseq; |
379 *outb -= sizeof(wchar_t); | 376 break; |
380 break; | 377 case EUC_KR: |
381 case UTF_8: | 378 l = 2; |
382 if (*outb < 4) { | 379 if (*inb < 2) |
383 char tmp[4]; | 380 goto starved; |
384 k = wctomb_utf8(tmp, c); | 381 d = *((unsigned char*)*in + 1); |
385 if (*outb < k) goto toobig; | 382 c -= 0xa1; |
386 memcpy(*out, tmp, k); | 383 d -= 0xa1; |
387 } else k = wctomb_utf8(*out, c); | 384 if (c >= 93 || d >= 94) { |
388 *out += k; | 385 c += (0xa1 - 0x81); |
389 *outb -= k; | 386 d += 0xa1; |
390 break; | 387 if (c >= 93 || (c >= 0xc6 - 0x81 && d > 0x52)) |
391 case US_ASCII: | 388 goto ilseq; |
392 if (c > 0x7f) subst: x++, c='*'; | 389 if (d - 'A' < 26) |
393 default: | 390 d = d - 'A'; |
394 if (*outb < 1) goto toobig; | 391 else if (d - 'a' < 26) |
395 if (c < 128+totype) { | 392 d = d - 'a' + 26; |
396 revout: | 393 else if (d - 0x81 < 0xff - 0x81) |
397 *(*out)++ = c; | 394 d = d - 0x81 + 52; |
398 *outb -= 1; | 395 else |
399 break; | 396 goto ilseq; |
400 } | 397 if (c < 0x20) |
401 d = c; | 398 c = 178 * c + d; |
402 for (c=0; c<128-totype; c++) { | 399 else |
403 if (d == legacy_chars[ (tomap[c*5/4]>>(2*c%8)) | | 400 c = 178 * 0x20 + 84 * (c - 0x20) + d; |
404 ((tomap[c*5/4+1]<<(8-2*c%8)) & 1023) ])
{ | 401 c += 0xac00; |
405 c += 128; | 402 for (d = 0xac00; d <= c;) { |
406 goto revout; | 403 k = 0; |
407 } | 404 for (int i = 0; i < 93; i++) |
408 } | 405 for (int j = 0; j < 94; j++) |
409 goto subst; | 406 if (ksc[i][j] - d <= c - d) |
410 case UCS2BE: | 407 k++; |
411 case UCS2LE: | 408 d = c + 1; |
412 case UTF_16BE: | 409 c += k; |
413 case UTF_16LE: | 410 } |
414 if (c < 0x10000 || type-UCS2BE < 2U) { | 411 break; |
415 if (c >= 0x10000) c = 0xFFFD; | 412 } |
416 if (*outb < 2) goto toobig; | 413 c = ksc[c][d]; |
417 put_16((void *)*out, c, totype); | 414 if (!c) |
418 *out += 2; | 415 goto ilseq; |
419 *outb -= 2; | 416 break; |
420 break; | 417 default: |
421 } | 418 if (c < 128 + type) |
422 if (*outb < 4) goto toobig; | 419 break; |
423 c -= 0x10000; | 420 c -= 128 + type; |
424 put_16((void *)*out, (c>>10)|0xd800, totype); | 421 c = legacy_chars[(map[c * 5 / 4] >> (2 * c % 8)) | |
425 put_16((void *)(*out + 2), (c&0x3ff)|0xdc00, totype); | 422 ((map[c * 5 / 4 + 1] << (8 - 2 * c % 8)) & 1023)]; |
426 *out += 4; | 423 if (!c) |
427 *outb -= 4; | 424 c = *(unsigned char*)*in; |
428 break; | 425 if (c == 1) |
429 case UTF_32BE: | 426 goto ilseq; |
430 case UTF_32LE: | 427 } |
431 if (*outb < 4) goto toobig; | 428 |
432 put_32((void *)*out, c, totype); | 429 switch (totype) { |
433 *out += 4; | 430 case WCHAR_T: |
434 *outb -= 4; | 431 if (*outb < sizeof(wchar_t)) |
435 break; | 432 goto toobig; |
436 } | 433 *(wchar_t*)*out = c; |
437 } | 434 *out += sizeof(wchar_t); |
438 *ploc = loc; | 435 *outb -= sizeof(wchar_t); |
439 return x; | 436 break; |
| 437 case UTF_8: |
| 438 if (*outb < 4) { |
| 439 char tmp[4]; |
| 440 k = wctomb_utf8(tmp, c); |
| 441 if (*outb < k) |
| 442 goto toobig; |
| 443 memcpy(*out, tmp, k); |
| 444 } else |
| 445 k = wctomb_utf8(*out, c); |
| 446 *out += k; |
| 447 *outb -= k; |
| 448 break; |
| 449 case US_ASCII: |
| 450 if (c > 0x7f) |
| 451 subst: |
| 452 x++, c = '*'; |
| 453 default: |
| 454 if (*outb < 1) |
| 455 goto toobig; |
| 456 if (c < 128 + totype) { |
| 457 revout: |
| 458 *(*out)++ = c; |
| 459 *outb -= 1; |
| 460 break; |
| 461 } |
| 462 d = c; |
| 463 for (c = 0; c < 128 - totype; c++) { |
| 464 if (d == legacy_chars[(tomap[c * 5 / 4] >> (2 * c % 8)) | |
| 465 ((tomap[c * 5 / 4 + 1] << (8 - 2 * c % 8)) & |
| 466 1023)]) { |
| 467 c += 128; |
| 468 goto revout; |
| 469 } |
| 470 } |
| 471 goto subst; |
| 472 case UCS2BE: |
| 473 case UCS2LE: |
| 474 case UTF_16BE: |
| 475 case UTF_16LE: |
| 476 if (c < 0x10000 || type - UCS2BE < 2U) { |
| 477 if (c >= 0x10000) |
| 478 c = 0xFFFD; |
| 479 if (*outb < 2) |
| 480 goto toobig; |
| 481 put_16((void*)*out, c, totype); |
| 482 *out += 2; |
| 483 *outb -= 2; |
| 484 break; |
| 485 } |
| 486 if (*outb < 4) |
| 487 goto toobig; |
| 488 c -= 0x10000; |
| 489 put_16((void*)*out, (c >> 10) | 0xd800, totype); |
| 490 put_16((void*)(*out + 2), (c & 0x3ff) | 0xdc00, totype); |
| 491 *out += 4; |
| 492 *outb -= 4; |
| 493 break; |
| 494 case UTF_32BE: |
| 495 case UTF_32LE: |
| 496 if (*outb < 4) |
| 497 goto toobig; |
| 498 put_32((void*)*out, c, totype); |
| 499 *out += 4; |
| 500 *outb -= 4; |
| 501 break; |
| 502 } |
| 503 } |
| 504 *ploc = loc; |
| 505 return x; |
440 ilseq: | 506 ilseq: |
441 » err = EILSEQ; | 507 err = EILSEQ; |
442 » x = -1; | 508 x = -1; |
443 » goto end; | 509 goto end; |
444 toobig: | 510 toobig: |
445 » err = E2BIG; | 511 err = E2BIG; |
446 » x = -1; | 512 x = -1; |
447 » goto end; | 513 goto end; |
448 starved: | 514 starved: |
449 » err = EINVAL; | 515 err = EINVAL; |
450 » x = -1; | 516 x = -1; |
451 end: | 517 end: |
452 » errno = err; | 518 errno = err; |
453 » *ploc = loc; | 519 *ploc = loc; |
454 » return x; | 520 return x; |
455 } | 521 } |
OLD | NEW |