xref: /haiku/src/libs/iconv/euc_jisx0213.h (revision aef5731f38da6f7b913e0f64acd8a40555491ce5)
1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe  * Copyright (C) 1999-2002 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  * EUC-JISX0213
23*aef5731fSOliver Tappe  */
24*aef5731fSOliver Tappe 
25*aef5731fSOliver Tappe /* The structure of EUC-JISX0213 is as follows:
26*aef5731fSOliver Tappe 
27*aef5731fSOliver Tappe    0x00..0x7F: ASCII
28*aef5731fSOliver Tappe 
29*aef5731fSOliver Tappe    0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.
30*aef5731fSOliver Tappe 
31*aef5731fSOliver Tappe    0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.
32*aef5731fSOliver Tappe 
33*aef5731fSOliver Tappe    0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.
34*aef5731fSOliver Tappe 
35*aef5731fSOliver Tappe    Note that some JISX0213 characters are not contained in Unicode 3.2
36*aef5731fSOliver Tappe    and are therefore best represented as sequences of Unicode characters.
37*aef5731fSOliver Tappe */
38*aef5731fSOliver Tappe 
39*aef5731fSOliver Tappe #include "jisx0213.h"
40*aef5731fSOliver Tappe #include "flushwc.h"
41*aef5731fSOliver Tappe 
42*aef5731fSOliver Tappe static int
euc_jisx0213_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)43*aef5731fSOliver Tappe euc_jisx0213_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
44*aef5731fSOliver Tappe {
45*aef5731fSOliver Tappe   ucs4_t last_wc = conv->istate;
46*aef5731fSOliver Tappe   if (last_wc) {
47*aef5731fSOliver Tappe     /* Output the buffered character. */
48*aef5731fSOliver Tappe     conv->istate = 0;
49*aef5731fSOliver Tappe     *pwc = last_wc;
50*aef5731fSOliver Tappe     return 0; /* Don't advance the input pointer. */
51*aef5731fSOliver Tappe   } else {
52*aef5731fSOliver Tappe     unsigned char c = *s;
53*aef5731fSOliver Tappe     if (c < 0x80) {
54*aef5731fSOliver Tappe       /* Plain ASCII character. */
55*aef5731fSOliver Tappe       *pwc = (ucs4_t) c;
56*aef5731fSOliver Tappe       return 1;
57*aef5731fSOliver Tappe     } else {
58*aef5731fSOliver Tappe       if ((c >= 0xa1 && c <= 0xfe) || c == 0x8e || c == 0x8f) {
59*aef5731fSOliver Tappe         /* Two or three byte character. */
60*aef5731fSOliver Tappe         if (n >= 2) {
61*aef5731fSOliver Tappe           unsigned char c2 = s[1];
62*aef5731fSOliver Tappe           if (c2 >= 0xa1 && c2 <= 0xfe) {
63*aef5731fSOliver Tappe             if (c == 0x8e) {
64*aef5731fSOliver Tappe               /* Half-width katakana. */
65*aef5731fSOliver Tappe               if (c2 <= 0xdf) {
66*aef5731fSOliver Tappe                 *pwc = c2 + 0xfec0;
67*aef5731fSOliver Tappe                 return 2;
68*aef5731fSOliver Tappe               }
69*aef5731fSOliver Tappe             } else {
70*aef5731fSOliver Tappe               ucs4_t wc;
71*aef5731fSOliver Tappe               if (c == 0x8f) {
72*aef5731fSOliver Tappe                 /* JISX 0213 plane 2. */
73*aef5731fSOliver Tappe                 if (n >= 3) {
74*aef5731fSOliver Tappe                   unsigned char c3 = s[2];
75*aef5731fSOliver Tappe                   wc = jisx0213_to_ucs4(0x200-0x80+c2,c3^0x80);
76*aef5731fSOliver Tappe                 } else
77*aef5731fSOliver Tappe                   return RET_TOOFEW(0);
78*aef5731fSOliver Tappe               } else {
79*aef5731fSOliver Tappe                 /* JISX 0213 plane 1. */
80*aef5731fSOliver Tappe                 wc = jisx0213_to_ucs4(0x100-0x80+c,c2^0x80);
81*aef5731fSOliver Tappe               }
82*aef5731fSOliver Tappe               if (wc) {
83*aef5731fSOliver Tappe                 if (wc < 0x80) {
84*aef5731fSOliver Tappe                   /* It's a combining character. */
85*aef5731fSOliver Tappe                   ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
86*aef5731fSOliver Tappe                   ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
87*aef5731fSOliver Tappe                   /* We cannot output two Unicode characters at once. So,
88*aef5731fSOliver Tappe                      output the first character and buffer the second one. */
89*aef5731fSOliver Tappe                   *pwc = wc1;
90*aef5731fSOliver Tappe                   conv->istate = wc2;
91*aef5731fSOliver Tappe                 } else
92*aef5731fSOliver Tappe                   *pwc = wc;
93*aef5731fSOliver Tappe                 return (c == 0x8f ? 3 : 2);
94*aef5731fSOliver Tappe               }
95*aef5731fSOliver Tappe             }
96*aef5731fSOliver Tappe           }
97*aef5731fSOliver Tappe         } else
98*aef5731fSOliver Tappe           return RET_TOOFEW(0);
99*aef5731fSOliver Tappe       }
100*aef5731fSOliver Tappe       return RET_ILSEQ;
101*aef5731fSOliver Tappe     }
102*aef5731fSOliver Tappe   }
103*aef5731fSOliver Tappe }
104*aef5731fSOliver Tappe 
105*aef5731fSOliver Tappe #define euc_jisx0213_flushwc normal_flushwc
106*aef5731fSOliver Tappe 
107*aef5731fSOliver Tappe /* Composition tables for each of the relevant combining characters.  */
108*aef5731fSOliver Tappe static const struct { unsigned short base; unsigned short composed; } euc_jisx0213_comp_table_data[] = {
109*aef5731fSOliver Tappe #define euc_jisx0213_comp_table02e5_idx 0
110*aef5731fSOliver Tappe #define euc_jisx0213_comp_table02e5_len 1
111*aef5731fSOliver Tappe   { 0xabe4, 0xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
112*aef5731fSOliver Tappe #define euc_jisx0213_comp_table02e9_idx (euc_jisx0213_comp_table02e5_idx+euc_jisx0213_comp_table02e5_len)
113*aef5731fSOliver Tappe #define euc_jisx0213_comp_table02e9_len 1
114*aef5731fSOliver Tappe   { 0xabe0, 0xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
115*aef5731fSOliver Tappe #define euc_jisx0213_comp_table0300_idx (euc_jisx0213_comp_table02e9_idx+euc_jisx0213_comp_table02e9_len)
116*aef5731fSOliver Tappe #define euc_jisx0213_comp_table0300_len 5
117*aef5731fSOliver Tappe   { 0xa9dc, 0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
118*aef5731fSOliver Tappe   { 0xabb8, 0xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
119*aef5731fSOliver Tappe   { 0xabb7, 0xabca }, /* 0x12B4A = 0x12B37 U+0300 */
120*aef5731fSOliver Tappe   { 0xabb0, 0xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
121*aef5731fSOliver Tappe   { 0xabc3, 0xabce }, /* 0x12B4E = 0x12B43 U+0300 */
122*aef5731fSOliver Tappe #define euc_jisx0213_comp_table0301_idx (euc_jisx0213_comp_table0300_idx+euc_jisx0213_comp_table0300_len)
123*aef5731fSOliver Tappe #define euc_jisx0213_comp_table0301_len 4
124*aef5731fSOliver Tappe   { 0xabb8, 0xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
125*aef5731fSOliver Tappe   { 0xabb7, 0xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
126*aef5731fSOliver Tappe   { 0xabb0, 0xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
127*aef5731fSOliver Tappe   { 0xabc3, 0xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
128*aef5731fSOliver Tappe #define euc_jisx0213_comp_table309a_idx (euc_jisx0213_comp_table0301_idx+euc_jisx0213_comp_table0301_len)
129*aef5731fSOliver Tappe #define euc_jisx0213_comp_table309a_len 14
130*aef5731fSOliver Tappe   { 0xa4ab, 0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
131*aef5731fSOliver Tappe   { 0xa4ad, 0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
132*aef5731fSOliver Tappe   { 0xa4af, 0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
133*aef5731fSOliver Tappe   { 0xa4b1, 0xa4fa }, /* 0x1247A = 0x12431 U+309A */
134*aef5731fSOliver Tappe   { 0xa4b3, 0xa4fb }, /* 0x1247B = 0x12433 U+309A */
135*aef5731fSOliver Tappe   { 0xa5ab, 0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
136*aef5731fSOliver Tappe   { 0xa5ad, 0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
137*aef5731fSOliver Tappe   { 0xa5af, 0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
138*aef5731fSOliver Tappe   { 0xa5b1, 0xa5fa }, /* 0x1257A = 0x12531 U+309A */
139*aef5731fSOliver Tappe   { 0xa5b3, 0xa5fb }, /* 0x1257B = 0x12533 U+309A */
140*aef5731fSOliver Tappe   { 0xa5bb, 0xa5fc }, /* 0x1257C = 0x1253B U+309A */
141*aef5731fSOliver Tappe   { 0xa5c4, 0xa5fd }, /* 0x1257D = 0x12544 U+309A */
142*aef5731fSOliver Tappe   { 0xa5c8, 0xa5fe }, /* 0x1257E = 0x12548 U+309A */
143*aef5731fSOliver Tappe   { 0xa6f5, 0xa6f8 }, /* 0x12678 = 0x12675 U+309A */
144*aef5731fSOliver Tappe };
145*aef5731fSOliver Tappe 
146*aef5731fSOliver Tappe static int
euc_jisx0213_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)147*aef5731fSOliver Tappe euc_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
148*aef5731fSOliver Tappe {
149*aef5731fSOliver Tappe   int count = 0;
150*aef5731fSOliver Tappe   unsigned short lasttwo = conv->ostate;
151*aef5731fSOliver Tappe 
152*aef5731fSOliver Tappe   if (lasttwo) {
153*aef5731fSOliver Tappe     /* Attempt to combine the last character with this one. */
154*aef5731fSOliver Tappe     unsigned int idx;
155*aef5731fSOliver Tappe     unsigned int len;
156*aef5731fSOliver Tappe 
157*aef5731fSOliver Tappe     if (wc == 0x02e5)
158*aef5731fSOliver Tappe       idx = euc_jisx0213_comp_table02e5_idx,
159*aef5731fSOliver Tappe       len = euc_jisx0213_comp_table02e5_len;
160*aef5731fSOliver Tappe     else if (wc == 0x02e9)
161*aef5731fSOliver Tappe       idx = euc_jisx0213_comp_table02e9_idx,
162*aef5731fSOliver Tappe       len = euc_jisx0213_comp_table02e9_len;
163*aef5731fSOliver Tappe     else if (wc == 0x0300)
164*aef5731fSOliver Tappe       idx = euc_jisx0213_comp_table0300_idx,
165*aef5731fSOliver Tappe       len = euc_jisx0213_comp_table0300_len;
166*aef5731fSOliver Tappe     else if (wc == 0x0301)
167*aef5731fSOliver Tappe       idx = euc_jisx0213_comp_table0301_idx,
168*aef5731fSOliver Tappe       len = euc_jisx0213_comp_table0301_len;
169*aef5731fSOliver Tappe     else if (wc == 0x309a)
170*aef5731fSOliver Tappe       idx = euc_jisx0213_comp_table309a_idx,
171*aef5731fSOliver Tappe       len = euc_jisx0213_comp_table309a_len;
172*aef5731fSOliver Tappe     else
173*aef5731fSOliver Tappe       goto not_combining;
174*aef5731fSOliver Tappe 
175*aef5731fSOliver Tappe     do
176*aef5731fSOliver Tappe       if (euc_jisx0213_comp_table_data[idx].base == lasttwo)
177*aef5731fSOliver Tappe         break;
178*aef5731fSOliver Tappe     while (++idx, --len > 0);
179*aef5731fSOliver Tappe 
180*aef5731fSOliver Tappe     if (len > 0) {
181*aef5731fSOliver Tappe       /* Output the combined character. */
182*aef5731fSOliver Tappe       if (n >= 2) {
183*aef5731fSOliver Tappe         lasttwo = euc_jisx0213_comp_table_data[idx].composed;
184*aef5731fSOliver Tappe         r[0] = (lasttwo >> 8) & 0xff;
185*aef5731fSOliver Tappe         r[1] = lasttwo & 0xff;
186*aef5731fSOliver Tappe         conv->ostate = 0;
187*aef5731fSOliver Tappe         return 2;
188*aef5731fSOliver Tappe       } else
189*aef5731fSOliver Tappe         return RET_TOOSMALL;
190*aef5731fSOliver Tappe     }
191*aef5731fSOliver Tappe 
192*aef5731fSOliver Tappe   not_combining:
193*aef5731fSOliver Tappe     /* Output the buffered character. */
194*aef5731fSOliver Tappe     if (n < 2)
195*aef5731fSOliver Tappe       return RET_TOOSMALL;
196*aef5731fSOliver Tappe     r[0] = (lasttwo >> 8) & 0xff;
197*aef5731fSOliver Tappe     r[1] = lasttwo & 0xff;
198*aef5731fSOliver Tappe     r += 2;
199*aef5731fSOliver Tappe     count = 2;
200*aef5731fSOliver Tappe   }
201*aef5731fSOliver Tappe 
202*aef5731fSOliver Tappe   if (wc < 0x80) {
203*aef5731fSOliver Tappe     /* Plain ASCII character. */
204*aef5731fSOliver Tappe     if (n > count) {
205*aef5731fSOliver Tappe       r[0] = (unsigned char) wc;
206*aef5731fSOliver Tappe       conv->ostate = 0;
207*aef5731fSOliver Tappe       return count+1;
208*aef5731fSOliver Tappe     } else
209*aef5731fSOliver Tappe       return RET_TOOSMALL;
210*aef5731fSOliver Tappe   } else if (wc >= 0xff61 && wc <= 0xff9f) {
211*aef5731fSOliver Tappe     /* Half-width katakana. */
212*aef5731fSOliver Tappe     if (n >= count+2) {
213*aef5731fSOliver Tappe       r[0] = 0x8e;
214*aef5731fSOliver Tappe       r[1] = wc - 0xfec0;
215*aef5731fSOliver Tappe       conv->ostate = 0;
216*aef5731fSOliver Tappe       return count+2;
217*aef5731fSOliver Tappe     } else
218*aef5731fSOliver Tappe       return RET_TOOSMALL;
219*aef5731fSOliver Tappe   } else {
220*aef5731fSOliver Tappe     unsigned short jch = ucs4_to_jisx0213(wc);
221*aef5731fSOliver Tappe     if (jch != 0) {
222*aef5731fSOliver Tappe       if (jch & 0x0080) {
223*aef5731fSOliver Tappe         /* A possible match in comp_table_data. We have to buffer it. */
224*aef5731fSOliver Tappe         /* We know it's a JISX 0213 plane 1 character. */
225*aef5731fSOliver Tappe         if (jch & 0x8000) abort();
226*aef5731fSOliver Tappe         conv->ostate = jch | 0x8080;
227*aef5731fSOliver Tappe         return count+0;
228*aef5731fSOliver Tappe       }
229*aef5731fSOliver Tappe       if (jch & 0x8000) {
230*aef5731fSOliver Tappe         /* JISX 0213 plane 2. */
231*aef5731fSOliver Tappe         if (n >= count+3) {
232*aef5731fSOliver Tappe           r[0] = 0x8f;
233*aef5731fSOliver Tappe           r[1] = (jch >> 8) | 0x80;
234*aef5731fSOliver Tappe           r[2] = (jch & 0xff) | 0x80;
235*aef5731fSOliver Tappe           conv->ostate = 0;
236*aef5731fSOliver Tappe           return count+3;
237*aef5731fSOliver Tappe         } else
238*aef5731fSOliver Tappe           return RET_TOOSMALL;
239*aef5731fSOliver Tappe       } else {
240*aef5731fSOliver Tappe         /* JISX 0213 plane 1. */
241*aef5731fSOliver Tappe         if (n >= count+2) {
242*aef5731fSOliver Tappe           r[0] = (jch >> 8) | 0x80;
243*aef5731fSOliver Tappe           r[1] = (jch & 0xff) | 0x80;
244*aef5731fSOliver Tappe           conv->ostate = 0;
245*aef5731fSOliver Tappe           return count+2;
246*aef5731fSOliver Tappe         } else
247*aef5731fSOliver Tappe           return RET_TOOSMALL;
248*aef5731fSOliver Tappe       }
249*aef5731fSOliver Tappe     }
250*aef5731fSOliver Tappe     return RET_ILUNI;
251*aef5731fSOliver Tappe   }
252*aef5731fSOliver Tappe }
253*aef5731fSOliver Tappe 
254*aef5731fSOliver Tappe static int
euc_jisx0213_reset(conv_t conv,unsigned char * r,int n)255*aef5731fSOliver Tappe euc_jisx0213_reset (conv_t conv, unsigned char *r, int n)
256*aef5731fSOliver Tappe {
257*aef5731fSOliver Tappe   state_t lasttwo = conv->ostate;
258*aef5731fSOliver Tappe 
259*aef5731fSOliver Tappe   if (lasttwo) {
260*aef5731fSOliver Tappe     if (n < 2)
261*aef5731fSOliver Tappe       return RET_TOOSMALL;
262*aef5731fSOliver Tappe     r[0] = (lasttwo >> 8) & 0xff;
263*aef5731fSOliver Tappe     r[1] = lasttwo & 0xff;
264*aef5731fSOliver Tappe     /* conv->ostate = 0; will be done by the caller */
265*aef5731fSOliver Tappe     return 2;
266*aef5731fSOliver Tappe   } else
267*aef5731fSOliver Tappe     return 0;
268*aef5731fSOliver Tappe }
269