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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright(c) 1995 Sun Microsystems, Inc.
* All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#ifdef DEBUG
#include <sys/fcntl.h>
#include <sys/stat.h>
#endif
#include <public_struc.h>
#include <gb2312_unicode.h>
#include <cns11643_unicode_CN.h>
#define MSB 0x80 /* most significant bit */
#define MBYTE 0x8e /* multi-byte (4 byte character) */
#define PMASK 0xa0 /* plane number mask */
#define ONEBYTE 0xff /* right most byte */
#define MSB_OFF 0x7f /* mask off MBS */
#define SI 0x0f /* shift in */
#define SO 0x0e /* shift out */
#define ESC 0x1b /* escape */
#define SS2 0x4e /* SS2 shift out */
#define SS3 0x4f /* SS3 shift out */
#define UTF8_NON_ID_CHAR1 0xEF /* non-identified character */
#define UTF8_NON_ID_CHAR2 0xBF /* non-identified character */
#define UTF8_NON_ID_CHAR3 0xBD /* non-identified character */
typedef struct _icv_state {
char _buf[10];
size_t _bufcont;
char _keepc[4]; /* maximum # byte of CNS11643 code */
short _gstate; /* state machine id */
short _istate; /* state for shift in/out */
int _plane; /* plane number for Chinese character */
int _last_plane; /* last charactor's plane # */
int _errno; /* internal errno */
} _iconv_st;
enum _GSTATE { G0, G1, G2, G3, G4, G5, G6, G7, G8, G9, \
G10,G11,G12,G13,G14,G15,G16,G17,G18,G19, \
G20,G21,G22,G23,G24,G25,G26,G27,G28,G29 };
enum _ISTATE { IN, OUT };
int iso_to_utf8(_iconv_st * st, char* buf, size_t buflen);
int binsearch(unsigned long x, table_t v[], int n);
int flush_buf(_iconv_st * st, char ** outbuf, size_t * outbytesleft);
int flush_buf(_iconv_st * st, char ** outbuf, size_t * outbytesleft) {
if (!st->_bufcont)
return 0;
if (st->_bufcont > *outbytesleft) {
st->_errno = E2BIG;
return -1;
}
strncpy(st->_buf, *outbuf, st->_bufcont);
(*outbuf)+=(st->_bufcont);
(*outbytesleft)-=(st->_bufcont);
st->_bufcont = 0;
return st->_bufcont;
}
/*
* Open; called from iconv_open()
*/
void *
_icv_open()
{
_iconv_st *st;
if ((st = (_iconv_st *)malloc(sizeof(_iconv_st))) == NULL) {
errno = ENOMEM;
return ((void *) -1);
}
st->_gstate = G0;
st->_istate = IN;
st->_last_plane = st->_plane = -1;
st->_errno = 0;
st->_bufcont = 0;
return ((void *) st);
}
/*
* Close; called from iconv_close()
*/
void
_icv_close(_iconv_st *st)
{
if (st == NULL)
errno = EBADF;
else
free(st);
}
/*
* Actual conversion; called from iconv()
*/
size_t _icv_iconv(_iconv_st *st, \
char **inbuf, size_t *inbytesleft, \
char **outbuf, size_t *outbytesleft) {
int n;
char c;
if (st == NULL) {
errno = EBADF;
return ((size_t) -1);
}
if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */
st->_errno = 0;
return ((size_t) 0);
}
errno = st->_errno = 0; /* reset internal and external errno */
/* a state machine for interpreting ISO 2022-7 code */
while (*inbytesleft > 0 && *outbytesleft > 0) {
switch (st->_gstate) {
case G0: /* assuming ASCII in the beginning */
if (**inbuf == ESC) {
st->_gstate = G1;
st->_buf[st->_bufcont++] = ESC;
} else { /* real ASCII */
**outbuf = **inbuf;
(*outbuf)++;
(*outbytesleft)--;
}
break;
case G1: /* got ESC, expecting $ */
if (**inbuf == '$') {
st->_gstate = G2;
st->_buf[st->_bufcont++] = '$';
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_errno = 0;
st->_istate = IN;
continue; /* don't advance inbuf */
}
break;
case G2: /* got $, expecting ) * or + */
if (**inbuf == ')') {
st->_gstate = G3;
} else if (**inbuf == '*') {
st->_gstate = G12;
st->_plane = 2;
} else if (**inbuf == '+') {
st->_gstate = G19;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_errno = 0;
st->_istate = IN;
continue; /* don't advance inbuf */
}
st->_buf[st->_bufcont++] = **inbuf;
break;
case G3: /* got ) expecting A,G,H */
/* H is for the bug of and zh_TW.BIG5 */
if (**inbuf == 'A') {
st->_plane = 0;
st->_gstate = G4;
} else if (**inbuf == 'G') {
st->_plane = 1;
st->_gstate = G8;
} else if (**inbuf == 'H') {
st->_plane = 2;
st->_gstate = G8;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_errno = 0;
st->_istate = IN;
continue;
}
st->_buf[st->_bufcont++] = **inbuf;
break;
case G4: /* ESC $ ) A got, and SO is expected */
if (**inbuf == SO) {
st->_gstate = G5;
st->_istate = OUT;
st->_bufcont = 0;
st->_last_plane = st->_plane;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_errno = 0;
st->_istate = IN;
st->_plane = st->_last_plane;
continue;
}
break;
case G5: /* SO (Shift Out) */
if (**inbuf == SI) {
st->_istate = IN;
st->_gstate = G7;
st->_last_plane = st->_plane;
} else if (**inbuf == ESC) {
st->_bufcont = 0;
st->_gstate = G0;
continue;
} else { /* Chinese Charactors */
st->_keepc[0] = **inbuf;
st->_gstate = G6;
}
break;
case G6: /* GB2312: 2nd Chinese character */
st->_keepc[1] = **inbuf;
n = iso_to_utf8(st, *outbuf, *outbytesleft);
if (n > 0) {
(*outbuf) += n;
(*outbytesleft) -= n;
} else {
errno = st->_errno;
return (size_t)-1;
}
st->_gstate = G5;
break;
case G7: /* Shift in */
if (**inbuf == SO) {
st->_gstate = G5;
st->_istate = OUT;
st->_last_plane = st->_plane;
st->_bufcont = 0;
} else if (**inbuf == ESC) {
st->_gstate = G0;
continue;
} else {
**outbuf = **inbuf;
(*outbuf)++;
(*outbytesleft) --;
}
break;
case G8: /* BIG5: Chinese character */
if (**inbuf == SO) {
st->_istate = OUT;
st->_gstate = G9;
st->_bufcont = 0;
st->_last_plane = st->_plane;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_errno = 0;
st->_plane = st->_last_plane;
st->_istate = IN;
continue;
}
break;
case G9:
if (**inbuf == SI) {
st->_istate = IN;
st->_gstate = G11;
st->_last_plane = st->_plane;
} else if (**inbuf == ESC) {
if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
}
st->_gstate = G0;
continue;
} else { /* Chinese Charactor */
st->_keepc[0] = **inbuf;
st->_gstate = G10;
}
break;
case G10:
st->_keepc[1] = **inbuf;
n = iso_to_utf8(st, *outbuf, *outbytesleft);
if (n > 0) {
(*outbuf) += n;
(*outbytesleft) -= n;
} else {
errno = st->_errno;
return (size_t)-1;
}
st->_gstate = G9;
break;
case G11:
st->_bufcont = 0;
if (**inbuf == SO) {
st->_istate = OUT;
st->_gstate = G9;
} else if (**inbuf == ESC) {
st->_gstate = G0;
continue;
} else {
**outbuf = **inbuf;
(*outbuf)++;
(*outbytesleft)--;
}
break;
case G12:
if (**inbuf == 'H') {
st->_buf[st->_bufcont++] = 'H';
st->_gstate = G13;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_istate = IN;
st->_plane = st->_last_plane;
st->_gstate = G0;
continue;
}
break;
case G13:
if (**inbuf == ESC) {
st->_buf[st->_bufcont++] = **inbuf;
st->_gstate = G14;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_istate = IN;
st->_plane = st->_last_plane;
continue;
}
break;
case G14:
if (**inbuf == SS2) {
st->_istate = OUT;
st->_gstate = G15;
st->_bufcont = 0;
st->_last_plane = st->_plane = 2;
} else if (**inbuf == '$') {
st->_bufcont --;
if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G1;
st->_plane = st->_last_plane;
st->_istate = IN;
continue;
}
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_istate = IN;
st->_plane = st->_last_plane;
continue;
}
break;
case G15:
if (**inbuf == SI) {
st->_gstate = G16;
st->_istate = IN;
st->_last_plane = st->_plane;
} else if (**inbuf == ESC) {
st->_bufcont = 0;
st->_gstate = G0;
continue;
} else {
st->_keepc[0] = **inbuf;
st->_gstate = G18;
}
break;
case G16:
if (**inbuf == ESC) {
st->_gstate = G17;
st->_buf[st->_bufcont++] = ESC;
} else {
**outbuf = **inbuf;
(*outbuf) ++;
(*outbytesleft) --;
st->_bufcont = 0;
}
break;
case G17:
if (**inbuf == '$') {
st->_gstate = G1;
st->_buf[st->_bufcont++] = '$';
continue;
} else if (**inbuf == SS2) {
st->_bufcont = 0;
st->_gstate = G15;
st->_istate = OUT;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G16;
st->_istate = IN;
}
break;
case G18:
st->_keepc[1] = **inbuf;
st->_gstate = G15;
if ((n = iso_to_utf8(st, \
*outbuf, \
*outbytesleft)) > 0) {
(*outbuf)+=n;
(*outbytesleft)-=n;
} else {
errno = st->_errno;
return (size_t)-1;
}
break;
case G19: /* Plane #: 3 - 16 */
c = **inbuf;
if (c == 'I' || \
c == 'J' || \
c == 'K' || \
c == 'L' || \
c == 'M' || \
c == 'N' || \
c == 'O' || \
c == 'P' || \
c == 'Q' || \
c == 'R' || \
c == 'S' || \
c == 'T' || \
c == 'U' || \
c == 'V') {
st->_plane = c - 'I' + 3;
st->_gstate = G20;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_errno = 0;
st->_istate = IN;
st->_plane = st->_last_plane;
continue;
}
st->_buf[st->_bufcont++] = c;
break;
case G20:
if (**inbuf == ESC) {
st->_buf[st->_bufcont++] = **inbuf;
st->_gstate = G21;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_istate = IN;
st->_last_plane = st->_plane;
continue;
}
break;
case G21:
if (**inbuf == SS3) {
st->_istate = OUT;
st->_gstate = G22;
st->_bufcont = 0;
} else if (**inbuf == '$') {
st->_bufcont --;
if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_istate = IN;
st->_last_plane = st->_plane;
st->_gstate = G1;
continue;
}
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G0;
st->_istate = IN;
st->_last_plane = st->_plane;
continue;
}
break;
case G22:
if (**inbuf == SI) {
st->_istate = IN;
st->_gstate = G24;
st->_last_plane = st->_plane;
} else {
st->_keepc[0] = (char)MBYTE;
st->_keepc[1] = (char)(PMASK + st->_plane);
st->_keepc[2] = **inbuf;
st->_gstate = G23;
}
break;
case G23:
st->_keepc[3] = **inbuf;
if ((n = iso_to_utf8(st, \
*outbuf, \
*outbytesleft)) > 0) {
(*outbuf)+=n;
(*outbytesleft-=n);
} else {
errno = st->_errno;
return (size_t)-1;
}
st->_gstate = G22;
break;
case G24:
if (**inbuf == ESC) {
st->_gstate = G25;
st->_buf[st->_bufcont++] = ESC;
} else {
**outbuf = **inbuf;
(*outbuf)++;
(*outbytesleft)--;
st->_bufcont = 0;
}
break;
case G25:
if (**inbuf == '$') {
st->_gstate = G1;
continue;
} else if (**inbuf == SS3) {
st->_gstate = G22;
st->_bufcont = 0;
st->_istate = OUT;
} else if (flush_buf(st, outbuf, outbytesleft) == -1) {
errno = st->_errno;
return (size_t)-1;
} else {
st->_gstate = G24;
st->_istate = IN;
}
break;
default: /* should never come here */
st->_errno = errno = EILSEQ;
st->_gstate = G0; /* reset state */
break;
} /* end of switch */
(*inbuf)++;
(*inbytesleft)--;
if (st->_errno) {
break;
}
if (errno)
return((size_t)(-1));
}
if (*inbytesleft > 0 && *outbytesleft == 0) {
errno = E2BIG;
return((size_t)(-1));
}
return (size_t)((*inbytesleft));
}
/*
* ISO 2022-7 code --> UTF-8 code
* Return: > 0 - converted with enough space in output buffer
* = 0 - no space in outbuf
*/
int iso_to_utf8(_iconv_st * st, char* buf, size_t buflen) {
unsigned long iso_val;
int unidx;
unsigned long unicode;
switch (st->_plane) {
case 0:
unidx = (((st->_keepc[0] & 0xff) - 0x21) * 94) + \
(st->_keepc[1] & 0xff) - 0x21;
if (unidx < 0 || unidx >= GBMAX) {
st->_errno = EILSEQ;
return (0);
}
unicode = Unicode[unidx];
break;
case 1:
iso_val = ((st->_keepc[0] & MSB_OFF) << 8) + \
(st->_keepc[1] & MSB_OFF);
unidx = binsearch(iso_val, cns1_utf_tab, MAX_CNS1_NUM);
if (unidx >= 0) {
unicode = cns1_utf_tab[unidx].value;
break;
}
st->_errno = EILSEQ;
return (0);
case 2:
iso_val = ((st->_keepc[0] & MSB_OFF) << 8) + \
(st->_keepc[1] & MSB_OFF);
unidx = binsearch(iso_val, cns2_utf_tab, MAX_CNS2_NUM);
if (unidx >= 0) {
unicode = cns2_utf_tab[unidx].value;
break;
}
st->_errno = EILSEQ;
return (0);
default:
st->_errno = EILSEQ;
return (0);
}
if (unidx >=0) {
if (unicode > 0x0080 && unicode <= 0x07ff) {
if (buflen < 2) {
st->_errno = E2BIG;
return (0);
}
*buf = (char)((unicode >> 6) & 0x1f) | 0xc0;
*(buf+1) = (char)(unicode & 0x3f) | 0x80;
return 2;
}
if (unicode > 0x0800 && unicode <= 0xffff) {
if (buflen < 3) {
st->_errno = E2BIG;
return (0);
}
*buf = (char)((unicode >> 12) & 0xf) | 0xe0;
*(buf+1) = (char)((unicode >>6) & 0x3f) | 0x80;
*(buf+2) = (char)(unicode & 0x3f) | 0x80;
return 3;
}
}
if (buflen < 3) {
st->_errno = E2BIG;
return (0);
}
*buf = (char)UTF8_NON_ID_CHAR1;
*(buf + 2) = (char)UTF8_NON_ID_CHAR2;
*(buf + 2) = (char)UTF8_NON_ID_CHAR3;
return (3);
}
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
int binsearch(unsigned long x, table_t v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (x < v[mid].key)
high = mid - 1;
else if (x > v[mid].key)
low = mid + 1;
else /* found match */
return mid;
}
return (-1); /* no match */
}
#ifdef DEBUG
main(int argc, char ** argv) {
char *inbuf, *outbuf, *in_tmp, *out_tmp;
size_t inbytesleft, outbytesleft;
int fd;
int i;
struct stat s;
_iconv_st * st;
if (argc < 2) {
fprintf(stderr, "Usage: %s input\n", argv[0]);
exit(-1);
}
if ((fd = open(argv[1], O_RDONLY)) == -1) {
perror("open");
exit(-2);
}
if (fstat(fd, &s) == -1) {
perror("stat");
exit(-3);
}
inbytesleft = outbytesleft = s.st_size;
in_tmp = inbuf = (char *)malloc(inbytesleft);
out_tmp = outbuf = (char *)malloc(outbytesleft);
if (!inbuf || !outbuf) {
perror("malloc");
exit(-1);
}
if (read(fd, inbuf, inbytesleft) != inbytesleft) {
perror("read");
exit(-4);
}
for (i = 0; i < inbytesleft; i++)
fprintf(stderr, "%x\t", *(inbuf+i));
fprintf(stderr, "\n");
st = (_iconv_st *)_icv_open();
if (st == (_iconv_st *) -1) {
perror("_icv_open");
exit(-1);
}
if (_icv_iconv(st, \
&inbuf, &inbytesleft, \
&outbuf, &outbytesleft) == -1) {
perror("icv_iconv");
fprintf(stderr, "\ninbytesleft = %d\n", inbytesleft);
exit(-2);
}
if (write(1, out_tmp, s.st_size - outbytesleft) == -1) {
perror("write");
exit(-1);
}
free(in_tmp);
free(out_tmp);
close(fd);
_icv_close(st);
}
#endif
|