xref: /haiku/src/libs/iconv/big5hkscs2004.h (revision aef5731f38da6f7b913e0f64acd8a40555491ce5)
1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe  * Copyright (C) 1999-2002, 2006 Free Software Foundation, Inc.
3*aef5731fSOliver Tappe  * This file is part of the GNU LIBICONV Library.
4*aef5731fSOliver Tappe  *
5*aef5731fSOliver Tappe  * The GNU LIBICONV Library is free software; you can redistribute it
6*aef5731fSOliver Tappe  * and/or modify it under the terms of the GNU Library General Public
7*aef5731fSOliver Tappe  * License as published by the Free Software Foundation; either version 2
8*aef5731fSOliver Tappe  * of the License, or (at your option) any later version.
9*aef5731fSOliver Tappe  *
10*aef5731fSOliver Tappe  * The GNU LIBICONV Library is distributed in the hope that it will be
11*aef5731fSOliver Tappe  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12*aef5731fSOliver Tappe  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*aef5731fSOliver Tappe  * Library General Public License for more details.
14*aef5731fSOliver Tappe  *
15*aef5731fSOliver Tappe  * You should have received a copy of the GNU Library General Public
16*aef5731fSOliver Tappe  * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17*aef5731fSOliver Tappe  * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18*aef5731fSOliver Tappe  * Fifth Floor, Boston, MA 02110-1301, USA.
19*aef5731fSOliver Tappe  */
20*aef5731fSOliver Tappe 
21*aef5731fSOliver Tappe /*
22*aef5731fSOliver Tappe  * BIG5-HKSCS:2004
23*aef5731fSOliver Tappe  */
24*aef5731fSOliver Tappe 
25*aef5731fSOliver Tappe /*
26*aef5731fSOliver Tappe  * BIG5-HKSCS:2004 can be downloaded from
27*aef5731fSOliver Tappe  *   http://www.info.gov.hk/digital21/eng/hkscs/download.html
28*aef5731fSOliver Tappe  *   http://www.info.gov.hk/digital21/eng/hkscs/index.html
29*aef5731fSOliver Tappe  *
30*aef5731fSOliver Tappe  * It extends BIG5-HKSCS:2001 through 123 characters.
31*aef5731fSOliver Tappe  *
32*aef5731fSOliver Tappe  * It extends BIG5 (without the rows 0xC6..0xC7) through the ranges
33*aef5731fSOliver Tappe  *
34*aef5731fSOliver Tappe  *   0x{87..8D}{40..7E,A1..FE}      880 characters
35*aef5731fSOliver Tappe  *   0x{8E..A0}{40..7E,A1..FE}     2898 characters
36*aef5731fSOliver Tappe  *   0x{C6..C8}{40..7E,A1..FE}      359 characters
37*aef5731fSOliver Tappe  *   0xF9{D6..FE}                    41 characters
38*aef5731fSOliver Tappe  *   0x{FA..FE}{40..7E,A1..FE}      763 characters
39*aef5731fSOliver Tappe  *
40*aef5731fSOliver Tappe  * Note that some HKSCS characters are not contained in Unicode 3.2
41*aef5731fSOliver Tappe  * and are therefore best represented as sequences of Unicode characters:
42*aef5731fSOliver Tappe  *   0x8862  U+00CA U+0304  LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON
43*aef5731fSOliver Tappe  *   0x8864  U+00CA U+030C  LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON
44*aef5731fSOliver Tappe  *   0x88A3  U+00EA U+0304  LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON
45*aef5731fSOliver Tappe  *   0x88A5  U+00EA U+030C  LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON
46*aef5731fSOliver Tappe  */
47*aef5731fSOliver Tappe 
48*aef5731fSOliver Tappe #include "hkscs2004.h"
49*aef5731fSOliver Tappe #include "flushwc.h"
50*aef5731fSOliver Tappe 
51*aef5731fSOliver Tappe static int
big5hkscs2004_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)52*aef5731fSOliver Tappe big5hkscs2004_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
53*aef5731fSOliver Tappe {
54*aef5731fSOliver Tappe   ucs4_t last_wc = conv->istate;
55*aef5731fSOliver Tappe   if (last_wc) {
56*aef5731fSOliver Tappe     /* Output the buffered character. */
57*aef5731fSOliver Tappe     conv->istate = 0;
58*aef5731fSOliver Tappe     *pwc = last_wc;
59*aef5731fSOliver Tappe     return 0; /* Don't advance the input pointer. */
60*aef5731fSOliver Tappe   } else {
61*aef5731fSOliver Tappe     unsigned char c = *s;
62*aef5731fSOliver Tappe     /* Code set 0 (ASCII) */
63*aef5731fSOliver Tappe     if (c < 0x80)
64*aef5731fSOliver Tappe       return ascii_mbtowc(conv,pwc,s,n);
65*aef5731fSOliver Tappe     /* Code set 1 (BIG5 extended) */
66*aef5731fSOliver Tappe     if (c >= 0xa1 && c < 0xff) {
67*aef5731fSOliver Tappe       if (n < 2)
68*aef5731fSOliver Tappe         return RET_TOOFEW(0);
69*aef5731fSOliver Tappe       {
70*aef5731fSOliver Tappe         unsigned char c2 = s[1];
71*aef5731fSOliver Tappe         if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
72*aef5731fSOliver Tappe           if (!((c == 0xc6 && c2 >= 0xa1) || c == 0xc7)) {
73*aef5731fSOliver Tappe             int ret = big5_mbtowc(conv,pwc,s,2);
74*aef5731fSOliver Tappe             if (ret != RET_ILSEQ)
75*aef5731fSOliver Tappe               return ret;
76*aef5731fSOliver Tappe           }
77*aef5731fSOliver Tappe         }
78*aef5731fSOliver Tappe       }
79*aef5731fSOliver Tappe     }
80*aef5731fSOliver Tappe     {
81*aef5731fSOliver Tappe       int ret = hkscs1999_mbtowc(conv,pwc,s,n);
82*aef5731fSOliver Tappe       if (ret != RET_ILSEQ)
83*aef5731fSOliver Tappe         return ret;
84*aef5731fSOliver Tappe     }
85*aef5731fSOliver Tappe     {
86*aef5731fSOliver Tappe       int ret = hkscs2001_mbtowc(conv,pwc,s,n);
87*aef5731fSOliver Tappe       if (ret != RET_ILSEQ)
88*aef5731fSOliver Tappe         return ret;
89*aef5731fSOliver Tappe     }
90*aef5731fSOliver Tappe     {
91*aef5731fSOliver Tappe       int ret = hkscs2004_mbtowc(conv,pwc,s,n);
92*aef5731fSOliver Tappe       if (ret != RET_ILSEQ)
93*aef5731fSOliver Tappe         return ret;
94*aef5731fSOliver Tappe     }
95*aef5731fSOliver Tappe     if (c == 0x88) {
96*aef5731fSOliver Tappe       if (n < 2)
97*aef5731fSOliver Tappe         return RET_TOOFEW(0);
98*aef5731fSOliver Tappe       {
99*aef5731fSOliver Tappe         unsigned char c2 = s[1];
100*aef5731fSOliver Tappe         if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
101*aef5731fSOliver Tappe           /* It's a composed character. */
102*aef5731fSOliver Tappe           ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
103*aef5731fSOliver Tappe           ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
104*aef5731fSOliver Tappe           /* We cannot output two Unicode characters at once. So,
105*aef5731fSOliver Tappe              output the first character and buffer the second one. */
106*aef5731fSOliver Tappe           *pwc = wc1;
107*aef5731fSOliver Tappe           conv->istate = wc2;
108*aef5731fSOliver Tappe           return 2;
109*aef5731fSOliver Tappe         }
110*aef5731fSOliver Tappe       }
111*aef5731fSOliver Tappe     }
112*aef5731fSOliver Tappe     return RET_ILSEQ;
113*aef5731fSOliver Tappe   }
114*aef5731fSOliver Tappe }
115*aef5731fSOliver Tappe 
116*aef5731fSOliver Tappe #define big5hkscs2004_flushwc normal_flushwc
117*aef5731fSOliver Tappe 
118*aef5731fSOliver Tappe static int
big5hkscs2004_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)119*aef5731fSOliver Tappe big5hkscs2004_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
120*aef5731fSOliver Tappe {
121*aef5731fSOliver Tappe   int count = 0;
122*aef5731fSOliver Tappe   unsigned char last = conv->ostate;
123*aef5731fSOliver Tappe 
124*aef5731fSOliver Tappe   if (last) {
125*aef5731fSOliver Tappe     /* last is = 0x66 or = 0xa7. */
126*aef5731fSOliver Tappe     if (wc == 0x0304 || wc == 0x030c) {
127*aef5731fSOliver Tappe       /* Output the combined character. */
128*aef5731fSOliver Tappe       if (n >= 2) {
129*aef5731fSOliver Tappe         r[0] = 0x88;
130*aef5731fSOliver Tappe         r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
131*aef5731fSOliver Tappe         conv->ostate = 0;
132*aef5731fSOliver Tappe         return 2;
133*aef5731fSOliver Tappe       } else
134*aef5731fSOliver Tappe         return RET_TOOSMALL;
135*aef5731fSOliver Tappe     }
136*aef5731fSOliver Tappe 
137*aef5731fSOliver Tappe     /* Output the buffered character. */
138*aef5731fSOliver Tappe     if (n < 2)
139*aef5731fSOliver Tappe       return RET_TOOSMALL;
140*aef5731fSOliver Tappe     r[0] = 0x88;
141*aef5731fSOliver Tappe     r[1] = last;
142*aef5731fSOliver Tappe     r += 2;
143*aef5731fSOliver Tappe     count = 2;
144*aef5731fSOliver Tappe   }
145*aef5731fSOliver Tappe 
146*aef5731fSOliver Tappe   /* Code set 0 (ASCII) */
147*aef5731fSOliver Tappe   if (wc < 0x0080) {
148*aef5731fSOliver Tappe     /* Plain ASCII character. */
149*aef5731fSOliver Tappe     if (n > count) {
150*aef5731fSOliver Tappe       r[0] = (unsigned char) wc;
151*aef5731fSOliver Tappe       conv->ostate = 0;
152*aef5731fSOliver Tappe       return count+1;
153*aef5731fSOliver Tappe     } else
154*aef5731fSOliver Tappe       return RET_TOOSMALL;
155*aef5731fSOliver Tappe   } else {
156*aef5731fSOliver Tappe     unsigned char buf[2];
157*aef5731fSOliver Tappe     int ret;
158*aef5731fSOliver Tappe 
159*aef5731fSOliver Tappe     /* Code set 1 (BIG5 extended) */
160*aef5731fSOliver Tappe     ret = big5_wctomb(conv,buf,wc,2);
161*aef5731fSOliver Tappe     if (ret != RET_ILUNI) {
162*aef5731fSOliver Tappe       if (ret != 2) abort();
163*aef5731fSOliver Tappe       if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
164*aef5731fSOliver Tappe         if (n >= count+2) {
165*aef5731fSOliver Tappe           r[0] = buf[0];
166*aef5731fSOliver Tappe           r[1] = buf[1];
167*aef5731fSOliver Tappe           conv->ostate = 0;
168*aef5731fSOliver Tappe           return count+2;
169*aef5731fSOliver Tappe         } else
170*aef5731fSOliver Tappe           return RET_TOOSMALL;
171*aef5731fSOliver Tappe       }
172*aef5731fSOliver Tappe     }
173*aef5731fSOliver Tappe     ret = hkscs1999_wctomb(conv,buf,wc,2);
174*aef5731fSOliver Tappe     if (ret != RET_ILUNI) {
175*aef5731fSOliver Tappe       if (ret != 2) abort();
176*aef5731fSOliver Tappe       if ((wc & ~0x0020) == 0x00ca) {
177*aef5731fSOliver Tappe         /* A possible first character of a multi-character sequence. We have to
178*aef5731fSOliver Tappe            buffer it. */
179*aef5731fSOliver Tappe         if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
180*aef5731fSOliver Tappe         conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
181*aef5731fSOliver Tappe         return count+0;
182*aef5731fSOliver Tappe       }
183*aef5731fSOliver Tappe       if (n >= count+2) {
184*aef5731fSOliver Tappe         r[0] = buf[0];
185*aef5731fSOliver Tappe         r[1] = buf[1];
186*aef5731fSOliver Tappe         conv->ostate = 0;
187*aef5731fSOliver Tappe         return count+2;
188*aef5731fSOliver Tappe       } else
189*aef5731fSOliver Tappe         return RET_TOOSMALL;
190*aef5731fSOliver Tappe     }
191*aef5731fSOliver Tappe     ret = hkscs2001_wctomb(conv,buf,wc,2);
192*aef5731fSOliver Tappe     if (ret != RET_ILUNI) {
193*aef5731fSOliver Tappe       if (ret != 2) abort();
194*aef5731fSOliver Tappe       if (n >= count+2) {
195*aef5731fSOliver Tappe         r[0] = buf[0];
196*aef5731fSOliver Tappe         r[1] = buf[1];
197*aef5731fSOliver Tappe         conv->ostate = 0;
198*aef5731fSOliver Tappe         return count+2;
199*aef5731fSOliver Tappe       } else
200*aef5731fSOliver Tappe         return RET_TOOSMALL;
201*aef5731fSOliver Tappe     }
202*aef5731fSOliver Tappe     ret = hkscs2004_wctomb(conv,buf,wc,2);
203*aef5731fSOliver Tappe     if (ret != RET_ILUNI) {
204*aef5731fSOliver Tappe       if (ret != 2) abort();
205*aef5731fSOliver Tappe       if (n >= count+2) {
206*aef5731fSOliver Tappe         r[0] = buf[0];
207*aef5731fSOliver Tappe         r[1] = buf[1];
208*aef5731fSOliver Tappe         conv->ostate = 0;
209*aef5731fSOliver Tappe         return count+2;
210*aef5731fSOliver Tappe       } else
211*aef5731fSOliver Tappe         return RET_TOOSMALL;
212*aef5731fSOliver Tappe     }
213*aef5731fSOliver Tappe     return RET_ILUNI;
214*aef5731fSOliver Tappe   }
215*aef5731fSOliver Tappe }
216*aef5731fSOliver Tappe 
217*aef5731fSOliver Tappe static int
big5hkscs2004_reset(conv_t conv,unsigned char * r,int n)218*aef5731fSOliver Tappe big5hkscs2004_reset (conv_t conv, unsigned char *r, int n)
219*aef5731fSOliver Tappe {
220*aef5731fSOliver Tappe   unsigned char last = conv->ostate;
221*aef5731fSOliver Tappe 
222*aef5731fSOliver Tappe   if (last) {
223*aef5731fSOliver Tappe     if (n < 2)
224*aef5731fSOliver Tappe       return RET_TOOSMALL;
225*aef5731fSOliver Tappe     r[0] = 0x88;
226*aef5731fSOliver Tappe     r[1] = last;
227*aef5731fSOliver Tappe     /* conv->ostate = 0; will be done by the caller */
228*aef5731fSOliver Tappe     return 2;
229*aef5731fSOliver Tappe   } else
230*aef5731fSOliver Tappe     return 0;
231*aef5731fSOliver Tappe }
232