1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
#include <stdlib.h>
#include <ctype.h>
#include <gc.h>
#define New_N(type,n) ((type*)GC_MALLOC((n)*sizeof(type)))
#include "wc.h"
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
wc_locale WcLocale = 0;
static struct {
char *lang;
wc_ces ces;
} lang_ces_table[] = {
{ "cs", WC_CES_ISO_8859_2 }, /* cs_CZ */
{ "el", WC_CES_ISO_8859_7 }, /* el_GR */
{ "iw", WC_CES_ISO_8859_8 }, /* iw_IL */
{ "ja", WC_CES_EUC_JP }, /* ja_JP */
{ "ko", WC_CES_EUC_KR }, /* ko_KR */
{ "hu", WC_CES_ISO_8859_2 }, /* hu_HU */
{ "pl", WC_CES_ISO_8859_2 }, /* pl_PL */
{ "ro", WC_CES_ISO_8859_2 }, /* ro_RO */
{ "ru", WC_CES_ISO_8859_5 }, /* ru_SU */
{ "sk", WC_CES_ISO_8859_2 }, /* sk_SK */
{ "sl", WC_CES_ISO_8859_2 }, /* sl_CS */
{ "tr", WC_CES_ISO_8859_9 }, /* tr_TR */
{ "zh", WC_CES_EUC_CN }, /* zh_CN */
{ NULL, 0 }
};
wc_ces
wc_guess_charset(char *charset, wc_ces orig)
{
wc_ces guess;
if (charset == NULL || *charset == '\0')
return orig;
guess = wc_charset_to_ces(charset);
return guess ? guess : orig;
}
wc_ces
wc_guess_charset_short(char *charset, wc_ces orig)
{
wc_ces guess;
if (charset == NULL || *charset == '\0')
return orig;
guess = wc_charset_short_to_ces(charset);
return guess ? guess : orig;
}
wc_ces
wc_guess_locale_charset(char *locale, wc_ces orig)
{
wc_ces guess;
if (locale == NULL || *locale == '\0')
return orig;
guess = wc_locale_to_ces(locale);
return guess ? guess : orig;
}
wc_ces
wc_charset_to_ces(char *charset)
{
char *p = charset;
char buf[16];
int n;
if (tolower(*p) == 'x' && *(p+1) == '-')
p += 2;
for (n = 0; *p && n < 15; p++) {
if ((unsigned char)*p > 0x20 && *p != '_' && *p != '-')
buf[n++] = tolower(*p);
}
buf[n] = 0;
p = buf;
switch (*p) {
case 'e':
if (! strncmp(p, "euc", 3)) {
p += 3;
switch (*p) {
case 'j': return WC_CES_EUC_JP;
case 'c': return WC_CES_EUC_CN;
case 't': return WC_CES_EUC_TW;
case 'k': return WC_CES_EUC_KR;
}
switch (WcLocale) {
case WC_LOCALE_JA_JP: return WC_CES_EUC_JP;
case WC_LOCALE_ZH_CN: return WC_CES_EUC_CN;
case WC_LOCALE_ZH_TW: return WC_CES_EUC_TW;
case WC_LOCALE_ZH_HK: return WC_CES_EUC_CN;
case WC_LOCALE_KO_KR: return WC_CES_EUC_KR;
}
return WC_CES_EUC_JP;
}
break;
case 'i':
if (! strncmp(p, "iso2022", 7)) {
p += 7;
switch (*p) {
case 'j':
if (! strncmp(p, "jp2", 3))
return WC_CES_ISO_2022_JP_2;
if (! strncmp(p, "jp3", 3))
return WC_CES_ISO_2022_JP_3;
return WC_CES_ISO_2022_JP;
case 'c': return WC_CES_ISO_2022_CN;
case 'k': return WC_CES_ISO_2022_KR;
}
return WC_CES_ISO_2022_JP;
} else if (! strncmp(p, "iso8859", 7)) {
n = atoi(p + 7);
if (n >= 1 && n <= 16 && n != 12)
return (WC_CES_E_ISO_8859 | n);
return WC_CES_ISO_8859_1;
}
break;
case 'j':
if (! strncmp(p, "johab", 5))
return WC_CES_JOHAB;
if (! strncmp(p, "jis", 3))
return WC_CES_ISO_2022_JP;
break;
case 's':
if (! strncmp(p, "shiftjisx0213", 13) ||
! strncmp(p, "sjisx0213", 9))
return WC_CES_SHIFT_JISX0213;
if (! strncmp(p, "shiftjis", 8) ||
! strncmp(p, "sjis", 4))
return WC_CES_SHIFT_JIS;
break;
case 'g':
if (! strncmp(p, "gb18030", 7) ||
! strncmp(p, "gbk2k", 5))
return WC_CES_GB18030;
if (! strncmp(p, "gbk", 3))
return WC_CES_GBK;
if (! strncmp(p, "gb2312", 6))
return WC_CES_EUC_CN;
break;
case 'b':
if (! strncmp(p, "big5hkscs", 9))
return WC_CES_HKSCS;
if (! strncmp(p, "big5", 4))
return WC_CES_BIG5;
break;
case 'h':
if (! strncmp(p, "hz", 2))
return WC_CES_HZ_GB_2312;
if (! strncmp(p, "hkscs", 5))
return WC_CES_HKSCS;
break;
case 'k':
if (! strncmp(p, "koi8r", 5))
return WC_CES_KOI8_R;
if (! strncmp(p, "koi8u", 5))
return WC_CES_KOI8_U;
if (! strncmp(p, "ksx1001", 7))
return WC_CES_EUC_KR;
if (! strncmp(p, "ksc5601", 7))
return WC_CES_EUC_KR;
break;
case 't':
if (! strncmp(p, "tis620", 6))
return WC_CES_TIS_620;
if (! strncmp(p, "tcvn", 4))
return WC_CES_TCVN_5712;
break;
case 'n':
if (! strncmp(p, "next", 4))
return WC_CES_NEXTSTEP;
break;
case 'v':
if (! strncmp(p, "viet", 4)) {
p += 4;
if (! strncmp(p, "tcvn", 4))
return WC_CES_TCVN_5712;
}
if (! strncmp(p, "viscii", 6))
return WC_CES_VISCII_11;
if (! strncmp(p, "vps", 3))
return WC_CES_VPS;
break;
case 'u':
#ifdef USE_UNICODE
if (! strncmp(p, "utf8", 4))
return WC_CES_UTF_8;
if (! strncmp(p, "utf7", 4))
return WC_CES_UTF_7;
#endif
if (! strncmp(p, "uhc", 3))
return WC_CES_UHC;
if (! strncmp(p, "ujis", 4))
return WC_CES_EUC_JP;
if (! strncmp(p, "usascii", 7))
return WC_CES_US_ASCII;
break;
case 'a':
if (! strncmp(p, "ascii", 5))
return WC_CES_US_ASCII;
break;
case 'c':
if (! strncmp(p, "cngb", 4))
return WC_CES_EUC_CN;
if (*(p+1) != 'p')
break;
n = atoi(p + 2);
switch (n) {
case 437: return WC_CES_CP437;
case 737: return WC_CES_CP737;
case 775: return WC_CES_CP775;
case 850: return WC_CES_CP850;
case 852: return WC_CES_CP852;
case 855: return WC_CES_CP855;
case 856: return WC_CES_CP856;
case 857: return WC_CES_CP857;
case 860: return WC_CES_CP860;
case 861: return WC_CES_CP861;
case 862: return WC_CES_CP862;
case 863: return WC_CES_CP863;
case 864: return WC_CES_CP864;
case 865: return WC_CES_CP865;
case 866: return WC_CES_CP866;
case 869: return WC_CES_CP869;
case 874: return WC_CES_CP874;
case 932: return WC_CES_CP932; /* CP932 = Shift_JIS */
case 936: return WC_CES_CP936; /* CP936 = GBK > EUC_CN */
case 949: return WC_CES_CP949; /* CP949 = UHC > EUC_KR */
case 950: return WC_CES_CP950; /* CP950 = Big5 */
case 1006: return WC_CES_CP1006;
case 1250: return WC_CES_CP1250;
case 1251: return WC_CES_CP1251;
case 1252: return WC_CES_CP1252;
case 1253: return WC_CES_CP1253;
case 1254: return WC_CES_CP1254;
case 1255: return WC_CES_CP1255;
case 1256: return WC_CES_CP1256;
case 1257: return WC_CES_CP1257;
case 1258: return WC_CES_CP1258;
}
break;
case 'w':
if (strncmp(p, "windows", 7))
break;
if (! strncmp(p, "31j", 3))
return WC_CES_CP932;
n = atoi(p + 7);
switch (n) {
case 1250: return WC_CES_CP1250;
case 1251: return WC_CES_CP1251;
case 1252: return WC_CES_CP1252;
case 1253: return WC_CES_CP1253;
case 1254: return WC_CES_CP1254;
case 1255: return WC_CES_CP1255;
case 1256: return WC_CES_CP1256;
case 1257: return WC_CES_CP1257;
case 1258: return WC_CES_CP1258;
}
break;
}
return 0;
}
wc_ces
wc_charset_short_to_ces(char *charset)
{
char *p = charset;
char buf[16];
wc_ces ces;
int n;
ces = wc_charset_to_ces(charset);
if (ces)
return ces;
for (n = 0; *p && n < 15; p++) {
if ((unsigned char)*p > 0x20 && *p != '_' && *p != '-')
buf[n++] = tolower(*p);
}
buf[n] = 0;
p = buf;
switch (*p) {
case 'e':
switch (*(p+1)) {
case 'j': return WC_CES_EUC_JP;
case 'c': return WC_CES_EUC_CN;
case 't': return WC_CES_EUC_TW;
case 'k': return WC_CES_EUC_KR;
}
return WC_CES_EUC_JP;
case 'j':
p++;
if (*p == 'o')
return WC_CES_JOHAB;
if (*p == 'p')
p++;
if (*p == '2')
return WC_CES_ISO_2022_JP_2;
if (*p == '3')
return WC_CES_ISO_2022_JP_3;
return WC_CES_ISO_2022_JP;
case 's':
return WC_CES_SHIFT_JIS;
case 'g':
return WC_CES_EUC_CN;
case 'b':
return WC_CES_BIG5;
case 'h':
if (*(p+1) == 'k')
return WC_CES_HKSCS;
return WC_CES_HZ_GB_2312;
case 'k':
if (*(p+1) == 'o')
return WC_CES_KOI8_R;
return WC_CES_ISO_2022_KR;
case 'l':
n = atoi(p + 1);
if (n >= 1 && n <= 16 && n != 12)
return (WC_CES_E_ISO_8859 | n);
return WC_CES_ISO_8859_1;
case 't':
if (*(p+1) == 'c')
return WC_CES_TCVN_5712;
return WC_CES_TIS_620;
case 'n':
return WC_CES_NEXTSTEP;
case 'v':
if (*(p+1) == 'p')
return WC_CES_VPS;
return WC_CES_VISCII_11;
#ifdef USE_UNICODE
case 'u':
if (*(p+1) == '7')
return WC_CES_UTF_7;
return WC_CES_UTF_8;
#endif
case 'a':
return WC_CES_US_ASCII;
case 'c':
return WC_CES_ISO_2022_CN;
case 'w':
n = atoi(p + 1);
switch (n) {
case 1250: return WC_CES_CP1250;
case 1251: return WC_CES_CP1251;
case 1252: return WC_CES_CP1252;
case 1253: return WC_CES_CP1253;
case 1254: return WC_CES_CP1254;
case 1255: return WC_CES_CP1255;
case 1256: return WC_CES_CP1256;
case 1257: return WC_CES_CP1257;
case 1258: return WC_CES_CP1258;
}
break;
case 'r':
return WC_CES_RAW;
}
return 0;
}
wc_ces
wc_locale_to_ces(char *locale)
{
char *p = locale;
char buf[6];
int n;
if (*p == 'C' && *(p+1) == '\0')
return WC_CES_US_ASCII;
#ifdef HAVE_LANGINFO_CODESET
{
char *cs = nl_langinfo(CODESET);
if (cs && strcmp(cs, "US-ASCII"))
return wc_charset_to_ces(cs);
}
#endif
for (n = 0; *p && *p != '.' && n < 5; p++) {
if ((unsigned char)*p > 0x20)
buf[n++] = tolower(*p);
}
buf[n] = 0;
if (*p == '.') {
p++;
if (! strcasecmp(p, "euc")) {
switch (buf[0]) {
case 'j':
WcLocale = WC_LOCALE_JA_JP;
break;
case 'k':
WcLocale = WC_LOCALE_KO_KR;
break;
case 'z':
if (!strcmp(buf, "zh_tw"))
WcLocale = WC_LOCALE_ZH_TW;
else if (!strcmp(buf, "zh_hk"))
WcLocale = WC_LOCALE_ZH_HK;
else
WcLocale = WC_LOCALE_ZH_CN;
break;
default:
WcLocale = 0;
break;
}
}
return wc_charset_to_ces(p);
}
if (!strcmp(buf, "japanese"))
return WC_CES_SHIFT_JIS;
if (!strcmp(buf, "zh_tw") ||
!strcmp(buf, "zh_hk"))
return WC_CES_BIG5;
for (n = 0; lang_ces_table[n].lang; n++) {
if (!strncmp(buf, lang_ces_table[n].lang, 2))
return lang_ces_table[n].ces;
}
return WC_CES_ISO_8859_1;
}
char *
wc_ces_to_charset(wc_ces ces)
{
if (ces == WC_CES_WTF)
return "WTF";
return WcCesInfo[WC_CES_INDEX(ces)].name;
}
char *
wc_ces_to_charset_desc(wc_ces ces)
{
if (ces == WC_CES_WTF)
return "W3M Transfer Format";
return WcCesInfo[WC_CES_INDEX(ces)].desc;
}
wc_ces
wc_guess_8bit_charset(wc_ces orig)
{
switch (orig) {
case WC_CES_ISO_2022_JP:
case WC_CES_ISO_2022_JP_2:
case WC_CES_ISO_2022_JP_3:
return WC_CES_EUC_JP;
case WC_CES_ISO_2022_KR:
return WC_CES_EUC_KR;
case WC_CES_ISO_2022_CN:
case WC_CES_HZ_GB_2312:
return WC_CES_EUC_CN;
case WC_CES_US_ASCII:
return WC_CES_ISO_8859_1;
}
return orig;
}
wc_bool
wc_check_ces(wc_ces ces)
{
size_t i = WC_CES_INDEX(ces);
return (i <= WC_CES_END && WcCesInfo[i].id == ces);
}
static int
wc_ces_list_cmp(const void *a, const void *b)
{
return strcasecmp(((wc_ces_list *)a)->desc, ((wc_ces_list *)b)->desc);
}
static wc_ces_list *list = NULL;
wc_ces_list *
wc_get_ces_list(void)
{
wc_ces_info *info;
size_t n;
if (list)
return list;
for (info = WcCesInfo, n = 0; info->id; info++) {
if (info->name != NULL)
n++;
}
list = New_N(wc_ces_list, n + 1);
for (info = WcCesInfo, n = 0; info->id; info++) {
if (info->name != NULL) {
list[n].id = info->id;
list[n].name = info->name;
list[n].desc = info->desc;
n++;
}
}
list[n].id = 0;
list[n].name = NULL;
list[n].desc = NULL;
qsort(list, n, sizeof(wc_ces_list), wc_ces_list_cmp);
return list;
}
|