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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
#include "wc.h"
#include "sjis.h"
#include "jis.h"
#include "wtf.h"
#include "ucs.h"
#include "map/jisx02132_sjis.map"
wc_uchar *wc_jisx0212_jisx02132_map = jisx02132_sjis_map;
#define C0 WC_SJIS_MAP_C0
#define GL WC_SJIS_MAP_GL
#define LB WC_SJIS_MAP_LB
#define S80 WC_SJIS_MAP_80
#define SK WC_SJIS_MAP_SK
#define SL WC_SJIS_MAP_SL
#define SH WC_SJIS_MAP_SH
#define SX WC_SJIS_MAP_SX
#define C1 WC_SJIS_MAP_C1
#define SA0 WC_SJIS_MAP_A0
wc_uint8 WC_SJIS_MAP[ 0x100 ] = {
C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0,
C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0,
GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB,
LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB,
LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB,
LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, C0,
S80,SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL,
SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL, SL,
SA0,SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK,
SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK,
SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK,
SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK, SK,
SH, SH, SH, SH, SH, SH, SH, SH, SH, SH, SH, SH, SH, SH, SH, SH,
SX, SX, SX, SX, SX, SX, SX, SX, SX, SX, SX, SX, SX, C1, C1, C1,
};
#define sjis_to_jisx0208(ub, lb) \
{ \
ub -= (ub < 0xa0) ? 0x81 : 0xc1; \
ub = (ub << 1) + 0x21; \
if (lb < 0x9f) { \
lb -= (lb > 0x7e) ? 0x20 : 0x1f; \
} else { \
ub++; \
lb -= 0x7e; \
} \
}
#define sjis_to_jisx02132(ub, lb) \
{ \
if (lb < 0x9f) { \
ub = sjis1_jisx02132_map[ub - 0xf0]; \
lb -= (lb > 0x7e) ? 0x20 : 0x1f; \
} else { \
ub = sjis2_jisx02132_map[ub - 0xf0]; \
lb -= 0x7e; \
} \
}
#define jisx0208_to_sjis(ub, lb) \
{ \
lb += (ub & 1) ? 0x1f : 0x7d; \
if (lb > 0x7e) \
lb++; \
ub = (ub - 0x21) >> 1; \
ub += (ub < 0x1f) ? 0x81 : 0xc1; \
}
#define jisx02132_to_sjis(ub, lb) \
{ \
lb += (ub & 1) ? 0x1f : 0x7d; \
if (lb > 0x7e) \
lb++; \
ub = jisx02132_sjis_map[ ub ]; \
}
wc_wchar_t
wc_sjis_to_jis(wc_wchar_t cc)
{
wc_uchar ub, lb;
ub = cc.code >> 8;
lb = cc.code & 0xff;
if (ub < 0xf0) {
sjis_to_jisx0208(ub, lb);
cc.ccs = WC_CCS_JIS_X_0208;
} else {
sjis_to_jisx02132(ub, lb);
cc.ccs = WC_CCS_JIS_X_0213_2;
}
cc.code = ((wc_uint32)ub << 8) | lb;
return cc;
}
wc_wchar_t
wc_jis_to_sjis(wc_wchar_t cc)
{
wc_uchar ub, lb;
ub = (cc.code >> 8) & 0x7f;
lb = cc.code & 0x7f;
if (cc.ccs == WC_CCS_JIS_X_0213_2) {
jisx02132_to_sjis(ub, lb);
if (! ub) {
cc.ccs = WC_CCS_UNKNOWN_W;
return cc;
}
} else {
jisx0208_to_sjis(ub, lb);
}
cc.code = ((wc_uint32)ub << 8) | lb;
return cc;
}
wc_wchar_t
wc_sjis_ext_to_cs94w(wc_wchar_t cc)
{
wc_uchar ub, lb;
ub = cc.code >> 8;
lb = cc.code & 0xff;
sjis_to_jisx0208(ub, lb);
if (ub <= 0x7e) {
cc.ccs = WC_CCS_SJIS_EXT_1;
} else {
ub -= 0x5e;
cc.ccs = WC_CCS_SJIS_EXT_2;
}
cc.code = ((wc_uint32)ub << 8) | lb;
return cc;
}
wc_wchar_t
wc_cs94w_to_sjis_ext(wc_wchar_t cc)
{
wc_uchar ub, lb;
ub = (cc.code >> 8) & 0x7f;
lb = cc.code & 0x7f;
if (cc.ccs == WC_CCS_SJIS_EXT_2)
ub += 0x5e;
jisx0208_to_sjis(ub, lb);
cc.ccs = WC_CCS_SJIS_EXT;
cc.code = ((wc_uint32)ub << 8) | lb;
return cc;
}
wc_uint32
wc_sjis_ext1_to_N(wc_uint32 c)
{
wc_uchar ub;
ub = (c >> 8) & 0x7f;
switch(ub) {
case 0x2D: /* 0x8740 - */
ub = 0;
break;
case 0x79: /* 0xED40 - */
case 0x7A: /* 0xED9F - */
case 0x7B: /* 0xEE40 - */
case 0x7C: /* 0xEE9F - */
ub -= 0x78;
break;
default:
return WC_C_SJIS_ERROR;
}
return ub * 0x5e + (c & 0x7f) - 0x21;
}
wc_uint32
wc_sjis_ext2_to_N(wc_uint32 c)
{
wc_uchar ub;
ub = (c >> 8) & 0x7f;
switch(ub) {
case 0x35: /* 0xFA40 - */
case 0x36: /* 0xFA9F - */
case 0x37: /* 0xFB40 - */
case 0x38: /* 0xFB9F - */
case 0x39: /* 0xFC40 - */
ub -= 0x30;
break;
default:
return WC_C_SJIS_ERROR;
}
return ub * 0x5e + (c & 0x7f) - 0x21;
}
Str
wc_conv_from_sjis(Str is, wc_ces ces)
{
Str os;
wc_uchar *sp = (wc_uchar *)is->ptr;
wc_uchar *ep = sp + is->length;
wc_uchar *p;
wc_uchar jis[2];
int state = WC_SJIS_NOSTATE;
wc_wchar_t cc;
for (p = sp; p < ep && *p < 0x80; p++)
;
if (p == ep)
return is;
os = Strnew_size(is->length);
if (p > sp)
Strcat_charp_n(os, is->ptr, (int)(p - sp));
for (; p < ep; p++) {
switch (state) {
case WC_SJIS_NOSTATE:
switch (WC_SJIS_MAP[*p]) {
case SL:
state = WC_SJIS_SHIFT_L;
break;
case SH:
state = WC_SJIS_SHIFT_H;
break;
case SX:
state = WC_SJIS_SHIFT_X;
break;
case SK:
wtf_push(os, WC_CCS_JIS_X_0201K, (wc_uint32)*p);
break;
case S80:
case SA0:
case C1:
wtf_push_unknown(os, p, 1);
break;
default:
Strcat_char(os, (char)*p);
break;
}
break;
case WC_SJIS_SHIFT_L:
case WC_SJIS_SHIFT_H:
if (WC_SJIS_MAP[*p] & LB) {
jis[0] = *(p-1);
jis[1] = *p;
sjis_to_jisx0208(jis[0], jis[1]);
cc.code = ((wc_uint32)jis[0] << 8) | jis[1];
cc.ccs = wc_jisx0208_or_jisx02131(cc.code);
if (cc.ccs == WC_CCS_JIS_X_0208)
wtf_push(os, cc.ccs, cc.code);
else
wtf_push(os, WC_CCS_SJIS_EXT, ((wc_uint32)*(p-1) << 8) | *p);
} else
wtf_push_unknown(os, p-1, 2);
state = WC_SJIS_NOSTATE;
break;
case WC_SJIS_SHIFT_X:
if (WC_SJIS_MAP[*p] & LB)
wtf_push(os, WC_CCS_SJIS_EXT, ((wc_uint32)*(p-1) << 8) | *p);
else
wtf_push_unknown(os, p-1, 2);
state = WC_SJIS_NOSTATE;
break;
}
}
switch (state) {
case WC_SJIS_SHIFT_L:
case WC_SJIS_SHIFT_H:
case WC_SJIS_SHIFT_X:
wtf_push_unknown(os, p-1, 1);
break;
}
return os;
}
Str
wc_conv_from_sjisx0213(Str is, wc_ces ces)
{
Str os;
wc_uchar *sp = (wc_uchar *)is->ptr;
wc_uchar *ep = sp + is->length;
wc_uchar *p;
wc_uchar jis[2];
int state = WC_SJIS_NOSTATE;
wc_wchar_t cc;
for (p = sp; p < ep && *p < 0x80; p++)
;
if (p == ep)
return is;
os = Strnew_size(is->length);
if (p > sp)
Strcat_charp_n(os, is->ptr, (int)(p - sp));
for (; p < ep; p++) {
switch (state) {
case WC_SJIS_NOSTATE:
switch (WC_SJIS_MAP[*p]) {
case SL:
state = WC_SJIS_SHIFT_L;
break;
case SH:
state = WC_SJIS_SHIFT_H;
break;
case SX:
state = WC_SJIS_SHIFT_X;
break;
case SK:
wtf_push(os, WC_CCS_JIS_X_0201K, (wc_uint32)*p);
break;
case S80:
case SA0:
case C1:
wtf_push_unknown(os, p, 1);
break;
default:
Strcat_char(os, (char)*p);
break;
}
break;
case WC_SJIS_SHIFT_L:
case WC_SJIS_SHIFT_H:
if (WC_SJIS_MAP[*p] & LB) {
jis[0] = *(p-1);
jis[1] = *p;
sjis_to_jisx0208(jis[0], jis[1]);
cc.code = ((wc_uint32)jis[0] << 8) | jis[1];
cc.ccs = wc_jisx0208_or_jisx02131(cc.code);
wtf_push(os, cc.ccs, cc.code);
} else
wtf_push_unknown(os, p-1, 2);
state = WC_SJIS_NOSTATE;
break;
case WC_SJIS_SHIFT_X:
if (WC_SJIS_MAP[*p] & LB) {
jis[0] = *(p-1);
jis[1] = *p;
sjis_to_jisx02132(jis[0], jis[1]);
wtf_push(os, WC_CCS_JIS_X_0213_2, ((wc_uint32)jis[0] << 8) | jis[1]);
} else
wtf_push_unknown(os, p-1, 2);
state = WC_SJIS_NOSTATE;
break;
}
}
switch (state) {
case WC_SJIS_SHIFT_L:
case WC_SJIS_SHIFT_H:
case WC_SJIS_SHIFT_X:
wtf_push_unknown(os, p-1, 1);
break;
}
return os;
}
void
wc_push_to_sjis(Str os, wc_wchar_t cc, wc_status *st)
{
wc_uchar ub, lb;
while (1) {
switch (cc.ccs) {
case WC_CCS_US_ASCII:
Strcat_char(os, cc.code);
return;
case WC_CCS_JIS_X_0201K:
if (WcOption.use_jisx0201k) {
Strcat_char(os, cc.code | 0x80);
return;
} else if (WcOption.fix_width_conv)
cc.ccs = WC_CCS_UNKNOWN;
else
cc = wc_jisx0201k_to_jisx0208(cc);
continue;
case WC_CCS_JIS_X_0208:
ub = (cc.code >> 8) & 0x7f;
lb = cc.code & 0x7f;
jisx0208_to_sjis(ub, lb);
Strcat_char(os, ub);
Strcat_char(os, lb);
return;
case WC_CCS_SJIS_EXT_1:
case WC_CCS_SJIS_EXT_2:
cc = wc_cs94w_to_sjis_ext(cc);
case WC_CCS_SJIS_EXT:
Strcat_char(os, (char)(cc.code >> 8));
Strcat_char(os, (char)(cc.code & 0xff));
return;
case WC_CCS_UNKNOWN_W:
if (!WcOption.no_replace)
Strcat_charp(os, WC_REPLACE_W);
return;
case WC_CCS_UNKNOWN:
if (!WcOption.no_replace)
Strcat_charp(os, WC_REPLACE);
return;
default:
#ifdef USE_UNICODE
if (WcOption.ucs_conv)
cc = wc_any_to_any_ces(cc, st);
else
#endif
cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
continue;
}
}
}
void
wc_push_to_sjisx0213(Str os, wc_wchar_t cc, wc_status *st)
{
wc_uchar ub, lb;
while (1) {
switch (cc.ccs) {
case WC_CCS_US_ASCII:
Strcat_char(os, cc.code);
return;
case WC_CCS_JIS_X_0201K:
if (WcOption.use_jisx0201k) {
Strcat_char(os, cc.code | 0x80);
return;
} else if (WcOption.fix_width_conv)
cc.ccs = WC_CCS_UNKNOWN;
else
cc = wc_jisx0201k_to_jisx0208(cc);
continue;
case WC_CCS_JIS_X_0213_1:
if (! WcOption.use_jisx0213) {
cc.ccs = WC_CCS_UNKNOWN_W;
continue;
}
case WC_CCS_JIS_X_0208:
ub = (cc.code >> 8) & 0x7f;
lb = cc.code & 0x7f;
jisx0208_to_sjis(ub, lb);
Strcat_char(os, ub);
Strcat_char(os, lb);
return;
case WC_CCS_JIS_X_0213_2:
if (! WcOption.use_jisx0213) {
cc.ccs = WC_CCS_UNKNOWN_W;
continue;
}
ub = (cc.code >> 8) & 0x7f;
lb = cc.code & 0x7f;
jisx02132_to_sjis(ub, lb);
if (ub) {
Strcat_char(os, ub);
Strcat_char(os, lb);
return;
}
case WC_CCS_UNKNOWN_W:
if (!WcOption.no_replace)
Strcat_charp(os, WC_REPLACE_W);
return;
case WC_CCS_UNKNOWN:
if (!WcOption.no_replace)
Strcat_charp(os, WC_REPLACE);
return;
default:
#ifdef USE_UNICODE
if (WcOption.ucs_conv)
cc = wc_any_to_any_ces(cc, st);
else
#endif
cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
continue;
}
}
}
Str
wc_char_conv_from_sjis(wc_uchar c, wc_status *st)
{
static Str os;
static wc_uchar jis[2];
wc_wchar_t cc;
if (st->state == -1) {
st->state = WC_SJIS_NOSTATE;
os = Strnew_size(8);
}
switch (st->state) {
case WC_SJIS_NOSTATE:
switch (WC_SJIS_MAP[c]) {
case SL:
jis[0] = c;
st->state = WC_SJIS_SHIFT_L;
return NULL;
case SH:
jis[0] = c;
st->state = WC_SJIS_SHIFT_H;
return NULL;
case SX:
jis[0] = c;
st->state = WC_SJIS_SHIFT_X;
return NULL;
case SK:
wtf_push(os, WC_CCS_JIS_X_0201K, (wc_uint32)c);
break;
case S80:
case SA0:
case C1:
break;
default:
Strcat_char(os, (char)c);
break;
}
break;
case WC_SJIS_SHIFT_L:
case WC_SJIS_SHIFT_H:
if (WC_SJIS_MAP[c] & LB) {
jis[1] = c;
sjis_to_jisx0208(jis[0], jis[1]);
cc.code = ((wc_uint32)jis[0] << 8) | jis[1];
cc.ccs = wc_jisx0208_or_jisx02131(cc.code);
if (cc.ccs == WC_CCS_JIS_X_0208)
wtf_push(os, cc.ccs, cc.code);
else
wtf_push(os, WC_CCS_SJIS_EXT, ((wc_uint32)jis[0] << 8) | jis[1]);
}
st->state = WC_SJIS_NOSTATE;
break;
case WC_SJIS_SHIFT_X:
if (WC_SJIS_MAP[c] & LB) {
jis[1] = c;
wtf_push(os, WC_CCS_SJIS_EXT, ((wc_uint32)jis[0] << 8) | jis[1]);
}
st->state = WC_SJIS_NOSTATE;
break;
}
st->state = -1;
return os;
}
Str
wc_char_conv_from_sjisx0213(wc_uchar c, wc_status *st)
{
static Str os;
static wc_uchar jis[2];
wc_wchar_t cc;
if (st->state == -1) {
st->state = WC_SJIS_NOSTATE;
os = Strnew_size(8);
}
switch (st->state) {
case WC_SJIS_NOSTATE:
switch (WC_SJIS_MAP[c]) {
case SL:
jis[0] = c;
st->state = WC_SJIS_SHIFT_L;
return NULL;
case SH:
jis[0] = c;
st->state = WC_SJIS_SHIFT_H;
return NULL;
case SX:
jis[0] = c;
st->state = WC_SJIS_SHIFT_X;
return NULL;
case SK:
wtf_push(os, WC_CCS_JIS_X_0201K, (wc_uint32)c);
break;
case S80:
case SA0:
case C1:
break;
default:
Strcat_char(os, (char)c);
break;
}
break;
case WC_SJIS_SHIFT_L:
case WC_SJIS_SHIFT_H:
if (WC_SJIS_MAP[c] & LB) {
jis[1] = c;
sjis_to_jisx0208(jis[0], jis[1]);
cc.code = ((wc_uint32)jis[0] << 8) | jis[1];
cc.ccs = wc_jisx0208_or_jisx02131(cc.code);
wtf_push(os, cc.ccs, cc.code);
}
st->state = WC_SJIS_NOSTATE;
break;
case WC_SJIS_SHIFT_X:
if (WC_SJIS_MAP[c] & LB) {
jis[1] = c;
sjis_to_jisx02132(jis[0], jis[1]);
wtf_push(os, WC_CCS_JIS_X_0213_2, ((wc_uint32)jis[0] << 8) | jis[1]);
}
st->state = WC_SJIS_NOSTATE;
break;
}
st->state = -1;
return os;
}
|