xref: /haiku/src/libs/iconv/shift_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  * SHIFT_JISX0213
23*aef5731fSOliver Tappe  */
24*aef5731fSOliver Tappe 
25*aef5731fSOliver Tappe /* The structure of Shift_JISX0213 is as follows:
26*aef5731fSOliver Tappe 
27*aef5731fSOliver Tappe    0x00..0x7F: ISO646-JP, an ASCII variant
28*aef5731fSOliver Tappe 
29*aef5731fSOliver Tappe    0x{A1..DF}: JISX0201 Katakana.
30*aef5731fSOliver Tappe 
31*aef5731fSOliver Tappe    0x{81..9F,E0..EF}{40..7E,80..FC}: JISX0213 plane 1.
32*aef5731fSOliver Tappe 
33*aef5731fSOliver Tappe    0x{F0..FC}{40..7E,80..FC}: JISX0213 plane 2, with irregular row mapping.
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
shift_jisx0213_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)43*aef5731fSOliver Tappe shift_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 ISO646-JP character. */
55*aef5731fSOliver Tappe       if (c == 0x5c)
56*aef5731fSOliver Tappe         *pwc = (ucs4_t) 0x00a5;
57*aef5731fSOliver Tappe       else if (c == 0x7e)
58*aef5731fSOliver Tappe         *pwc = (ucs4_t) 0x203e;
59*aef5731fSOliver Tappe       else
60*aef5731fSOliver Tappe         *pwc = (ucs4_t) c;
61*aef5731fSOliver Tappe       return 1;
62*aef5731fSOliver Tappe     } else if (c >= 0xa1 && c <= 0xdf) {
63*aef5731fSOliver Tappe       *pwc = c + 0xfec0;
64*aef5731fSOliver Tappe       return 1;
65*aef5731fSOliver Tappe     } else {
66*aef5731fSOliver Tappe       if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) {
67*aef5731fSOliver Tappe         /* Two byte character. */
68*aef5731fSOliver Tappe         if (n >= 2) {
69*aef5731fSOliver Tappe           unsigned char c2 = s[1];
70*aef5731fSOliver Tappe           if ((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0x80 && c2 <= 0xfc)) {
71*aef5731fSOliver Tappe             unsigned int c1;
72*aef5731fSOliver Tappe             ucs4_t wc;
73*aef5731fSOliver Tappe             /* Convert to row and column. */
74*aef5731fSOliver Tappe             if (c < 0xe0)
75*aef5731fSOliver Tappe               c -= 0x81;
76*aef5731fSOliver Tappe             else
77*aef5731fSOliver Tappe               c -= 0xc1;
78*aef5731fSOliver Tappe             if (c2 < 0x80)
79*aef5731fSOliver Tappe               c2 -= 0x40;
80*aef5731fSOliver Tappe             else
81*aef5731fSOliver Tappe               c2 -= 0x41;
82*aef5731fSOliver Tappe             /* Now 0 <= c <= 0x3b, 0 <= c2 <= 0xbb. */
83*aef5731fSOliver Tappe             c1 = 2 * c;
84*aef5731fSOliver Tappe             if (c2 >= 0x5e)
85*aef5731fSOliver Tappe               c2 -= 0x5e, c1++;
86*aef5731fSOliver Tappe             c2 += 0x21;
87*aef5731fSOliver Tappe             if (c1 >= 0x5e) {
88*aef5731fSOliver Tappe               /* Handling of JISX 0213 plane 2 rows. */
89*aef5731fSOliver Tappe               if (c1 >= 0x67)
90*aef5731fSOliver Tappe                 c1 += 230;
91*aef5731fSOliver Tappe               else if (c1 >= 0x63 || c1 == 0x5f)
92*aef5731fSOliver Tappe                 c1 += 168;
93*aef5731fSOliver Tappe               else
94*aef5731fSOliver Tappe                 c1 += 162;
95*aef5731fSOliver Tappe             }
96*aef5731fSOliver Tappe             wc = jisx0213_to_ucs4(0x121+c1,c2);
97*aef5731fSOliver Tappe             if (wc) {
98*aef5731fSOliver Tappe               if (wc < 0x80) {
99*aef5731fSOliver Tappe                 /* It's a combining character. */
100*aef5731fSOliver Tappe                 ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
101*aef5731fSOliver Tappe                 ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
102*aef5731fSOliver Tappe                 /* We cannot output two Unicode characters at once. So,
103*aef5731fSOliver Tappe                    output the first character and buffer the second one. */
104*aef5731fSOliver Tappe                 *pwc = wc1;
105*aef5731fSOliver Tappe                 conv->istate = wc2;
106*aef5731fSOliver Tappe               } else
107*aef5731fSOliver Tappe                 *pwc = wc;
108*aef5731fSOliver Tappe               return 2;
109*aef5731fSOliver Tappe             }
110*aef5731fSOliver Tappe           }
111*aef5731fSOliver Tappe         } else
112*aef5731fSOliver Tappe           return RET_TOOFEW(0);
113*aef5731fSOliver Tappe       }
114*aef5731fSOliver Tappe       return RET_ILSEQ;
115*aef5731fSOliver Tappe     }
116*aef5731fSOliver Tappe   }
117*aef5731fSOliver Tappe }
118*aef5731fSOliver Tappe 
119*aef5731fSOliver Tappe #define shift_jisx0213_flushwc normal_flushwc
120*aef5731fSOliver Tappe 
121*aef5731fSOliver Tappe /* Composition tables for each of the relevant combining characters.  */
122*aef5731fSOliver Tappe static const struct { unsigned short base; unsigned short composed; } shift_jisx0213_comp_table_data[] = {
123*aef5731fSOliver Tappe #define shift_jisx0213_comp_table02e5_idx 0
124*aef5731fSOliver Tappe #define shift_jisx0213_comp_table02e5_len 1
125*aef5731fSOliver Tappe   { 0x8684, 0x8685 }, /* 0x12B65 = 0x12B64 U+02E5 */
126*aef5731fSOliver Tappe #define shift_jisx0213_comp_table02e9_idx (shift_jisx0213_comp_table02e5_idx+shift_jisx0213_comp_table02e5_len)
127*aef5731fSOliver Tappe #define shift_jisx0213_comp_table02e9_len 1
128*aef5731fSOliver Tappe   { 0x8680, 0x8686 }, /* 0x12B66 = 0x12B60 U+02E9 */
129*aef5731fSOliver Tappe #define shift_jisx0213_comp_table0300_idx (shift_jisx0213_comp_table02e9_idx+shift_jisx0213_comp_table02e9_len)
130*aef5731fSOliver Tappe #define shift_jisx0213_comp_table0300_len 5
131*aef5731fSOliver Tappe   { 0x857b, 0x8663 }, /* 0x12B44 = 0x1295C U+0300 */
132*aef5731fSOliver Tappe   { 0x8657, 0x8667 }, /* 0x12B48 = 0x12B38 U+0300 */
133*aef5731fSOliver Tappe   { 0x8656, 0x8669 }, /* 0x12B4A = 0x12B37 U+0300 */
134*aef5731fSOliver Tappe   { 0x864f, 0x866b }, /* 0x12B4C = 0x12B30 U+0300 */
135*aef5731fSOliver Tappe   { 0x8662, 0x866d }, /* 0x12B4E = 0x12B43 U+0300 */
136*aef5731fSOliver Tappe #define shift_jisx0213_comp_table0301_idx (shift_jisx0213_comp_table0300_idx+shift_jisx0213_comp_table0300_len)
137*aef5731fSOliver Tappe #define shift_jisx0213_comp_table0301_len 4
138*aef5731fSOliver Tappe   { 0x8657, 0x8668 }, /* 0x12B49 = 0x12B38 U+0301 */
139*aef5731fSOliver Tappe   { 0x8656, 0x866a }, /* 0x12B4B = 0x12B37 U+0301 */
140*aef5731fSOliver Tappe   { 0x864f, 0x866c }, /* 0x12B4D = 0x12B30 U+0301 */
141*aef5731fSOliver Tappe   { 0x8662, 0x866e }, /* 0x12B4F = 0x12B43 U+0301 */
142*aef5731fSOliver Tappe #define shift_jisx0213_comp_table309a_idx (shift_jisx0213_comp_table0301_idx+shift_jisx0213_comp_table0301_len)
143*aef5731fSOliver Tappe #define shift_jisx0213_comp_table309a_len 14
144*aef5731fSOliver Tappe   { 0x82a9, 0x82f5 }, /* 0x12477 = 0x1242B U+309A */
145*aef5731fSOliver Tappe   { 0x82ab, 0x82f6 }, /* 0x12478 = 0x1242D U+309A */
146*aef5731fSOliver Tappe   { 0x82ad, 0x82f7 }, /* 0x12479 = 0x1242F U+309A */
147*aef5731fSOliver Tappe   { 0x82af, 0x82f8 }, /* 0x1247A = 0x12431 U+309A */
148*aef5731fSOliver Tappe   { 0x82b1, 0x82f9 }, /* 0x1247B = 0x12433 U+309A */
149*aef5731fSOliver Tappe   { 0x834a, 0x8397 }, /* 0x12577 = 0x1252B U+309A */
150*aef5731fSOliver Tappe   { 0x834c, 0x8398 }, /* 0x12578 = 0x1252D U+309A */
151*aef5731fSOliver Tappe   { 0x834e, 0x8399 }, /* 0x12579 = 0x1252F U+309A */
152*aef5731fSOliver Tappe   { 0x8350, 0x839a }, /* 0x1257A = 0x12531 U+309A */
153*aef5731fSOliver Tappe   { 0x8352, 0x839b }, /* 0x1257B = 0x12533 U+309A */
154*aef5731fSOliver Tappe   { 0x835a, 0x839c }, /* 0x1257C = 0x1253B U+309A */
155*aef5731fSOliver Tappe   { 0x8363, 0x839d }, /* 0x1257D = 0x12544 U+309A */
156*aef5731fSOliver Tappe   { 0x8367, 0x839e }, /* 0x1257E = 0x12548 U+309A */
157*aef5731fSOliver Tappe   { 0x83f3, 0x83f6 }, /* 0x12678 = 0x12675 U+309A */
158*aef5731fSOliver Tappe };
159*aef5731fSOliver Tappe 
160*aef5731fSOliver Tappe static int
shift_jisx0213_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)161*aef5731fSOliver Tappe shift_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
162*aef5731fSOliver Tappe {
163*aef5731fSOliver Tappe   int count = 0;
164*aef5731fSOliver Tappe   unsigned short lasttwo = conv->ostate;
165*aef5731fSOliver Tappe 
166*aef5731fSOliver Tappe   if (lasttwo) {
167*aef5731fSOliver Tappe     /* Attempt to combine the last character with this one. */
168*aef5731fSOliver Tappe     unsigned int idx;
169*aef5731fSOliver Tappe     unsigned int len;
170*aef5731fSOliver Tappe 
171*aef5731fSOliver Tappe     if (wc == 0x02e5)
172*aef5731fSOliver Tappe       idx = shift_jisx0213_comp_table02e5_idx,
173*aef5731fSOliver Tappe       len = shift_jisx0213_comp_table02e5_len;
174*aef5731fSOliver Tappe     else if (wc == 0x02e9)
175*aef5731fSOliver Tappe       idx = shift_jisx0213_comp_table02e9_idx,
176*aef5731fSOliver Tappe       len = shift_jisx0213_comp_table02e9_len;
177*aef5731fSOliver Tappe     else if (wc == 0x0300)
178*aef5731fSOliver Tappe       idx = shift_jisx0213_comp_table0300_idx,
179*aef5731fSOliver Tappe       len = shift_jisx0213_comp_table0300_len;
180*aef5731fSOliver Tappe     else if (wc == 0x0301)
181*aef5731fSOliver Tappe       idx = shift_jisx0213_comp_table0301_idx,
182*aef5731fSOliver Tappe       len = shift_jisx0213_comp_table0301_len;
183*aef5731fSOliver Tappe     else if (wc == 0x309a)
184*aef5731fSOliver Tappe       idx = shift_jisx0213_comp_table309a_idx,
185*aef5731fSOliver Tappe       len = shift_jisx0213_comp_table309a_len;
186*aef5731fSOliver Tappe     else
187*aef5731fSOliver Tappe       goto not_combining;
188*aef5731fSOliver Tappe 
189*aef5731fSOliver Tappe     do
190*aef5731fSOliver Tappe       if (shift_jisx0213_comp_table_data[idx].base == lasttwo)
191*aef5731fSOliver Tappe         break;
192*aef5731fSOliver Tappe     while (++idx, --len > 0);
193*aef5731fSOliver Tappe 
194*aef5731fSOliver Tappe     if (len > 0) {
195*aef5731fSOliver Tappe       /* Output the combined character. */
196*aef5731fSOliver Tappe       if (n >= 2) {
197*aef5731fSOliver Tappe         lasttwo = shift_jisx0213_comp_table_data[idx].composed;
198*aef5731fSOliver Tappe         r[0] = (lasttwo >> 8) & 0xff;
199*aef5731fSOliver Tappe         r[1] = lasttwo & 0xff;
200*aef5731fSOliver Tappe         conv->ostate = 0;
201*aef5731fSOliver Tappe         return 2;
202*aef5731fSOliver Tappe       } else
203*aef5731fSOliver Tappe         return RET_TOOSMALL;
204*aef5731fSOliver Tappe     }
205*aef5731fSOliver Tappe 
206*aef5731fSOliver Tappe   not_combining:
207*aef5731fSOliver Tappe     /* Output the buffered character. */
208*aef5731fSOliver Tappe     if (n < 2)
209*aef5731fSOliver Tappe       return RET_TOOSMALL;
210*aef5731fSOliver Tappe     r[0] = (lasttwo >> 8) & 0xff;
211*aef5731fSOliver Tappe     r[1] = lasttwo & 0xff;
212*aef5731fSOliver Tappe     r += 2;
213*aef5731fSOliver Tappe     count = 2;
214*aef5731fSOliver Tappe   }
215*aef5731fSOliver Tappe 
216*aef5731fSOliver Tappe   if (wc < 0x80 && wc != 0x5c && wc != 0x7e) {
217*aef5731fSOliver Tappe     /* Plain ISO646-JP character. */
218*aef5731fSOliver Tappe     if (n > count) {
219*aef5731fSOliver Tappe       r[0] = (unsigned char) wc;
220*aef5731fSOliver Tappe       conv->ostate = 0;
221*aef5731fSOliver Tappe       return count+1;
222*aef5731fSOliver Tappe     } else
223*aef5731fSOliver Tappe       return RET_TOOSMALL;
224*aef5731fSOliver Tappe   } else if (wc == 0x00a5) {
225*aef5731fSOliver Tappe     if (n > count) {
226*aef5731fSOliver Tappe       r[0] = 0x5c;
227*aef5731fSOliver Tappe       conv->ostate = 0;
228*aef5731fSOliver Tappe       return count+1;
229*aef5731fSOliver Tappe     } else
230*aef5731fSOliver Tappe       return RET_TOOSMALL;
231*aef5731fSOliver Tappe   } else if (wc == 0x203e) {
232*aef5731fSOliver Tappe     if (n > count) {
233*aef5731fSOliver Tappe       r[0] = 0x7e;
234*aef5731fSOliver Tappe       conv->ostate = 0;
235*aef5731fSOliver Tappe       return count+1;
236*aef5731fSOliver Tappe     } else
237*aef5731fSOliver Tappe       return RET_TOOSMALL;
238*aef5731fSOliver Tappe   } else if (wc >= 0xff61 && wc <= 0xff9f) {
239*aef5731fSOliver Tappe     /* Half-width katakana. */
240*aef5731fSOliver Tappe     if (n > count) {
241*aef5731fSOliver Tappe       r[0] = wc - 0xfec0;
242*aef5731fSOliver Tappe       conv->ostate = 0;
243*aef5731fSOliver Tappe       return count+1;
244*aef5731fSOliver Tappe     } else
245*aef5731fSOliver Tappe       return RET_TOOSMALL;
246*aef5731fSOliver Tappe   } else {
247*aef5731fSOliver Tappe     unsigned int s1, s2;
248*aef5731fSOliver Tappe     unsigned short jch = ucs4_to_jisx0213(wc);
249*aef5731fSOliver Tappe     if (jch != 0) {
250*aef5731fSOliver Tappe       /* Convert it to shifted representation. */
251*aef5731fSOliver Tappe       s1 = jch >> 8;
252*aef5731fSOliver Tappe       s2 = jch & 0x7f;
253*aef5731fSOliver Tappe       s1 -= 0x21;
254*aef5731fSOliver Tappe       s2 -= 0x21;
255*aef5731fSOliver Tappe       if (s1 >= 0x5e) {
256*aef5731fSOliver Tappe         /* Handling of JISX 0213 plane 2 rows. */
257*aef5731fSOliver Tappe         if (s1 >= 0xcd) /* rows 0x26E..0x27E */
258*aef5731fSOliver Tappe           s1 -= 102;
259*aef5731fSOliver Tappe         else if (s1 >= 0x8b || s1 == 0x87) /* rows 0x228, 0x22C..0x22F */
260*aef5731fSOliver Tappe           s1 -= 40;
261*aef5731fSOliver Tappe         else /* rows 0x221, 0x223..0x225 */
262*aef5731fSOliver Tappe           s1 -= 34;
263*aef5731fSOliver Tappe         /* Now 0x5e <= s1 <= 0x77. */
264*aef5731fSOliver Tappe       }
265*aef5731fSOliver Tappe       if (s1 & 1)
266*aef5731fSOliver Tappe         s2 += 0x5e;
267*aef5731fSOliver Tappe       s1 = s1 >> 1;
268*aef5731fSOliver Tappe       if (s1 < 0x1f)
269*aef5731fSOliver Tappe         s1 += 0x81;
270*aef5731fSOliver Tappe       else
271*aef5731fSOliver Tappe         s1 += 0xc1;
272*aef5731fSOliver Tappe       if (s2 < 0x3f)
273*aef5731fSOliver Tappe         s2 += 0x40;
274*aef5731fSOliver Tappe       else
275*aef5731fSOliver Tappe         s2 += 0x41;
276*aef5731fSOliver Tappe       if (jch & 0x0080) {
277*aef5731fSOliver Tappe         /* A possible match in comp_table_data. We have to buffer it. */
278*aef5731fSOliver Tappe         /* We know it's a JISX 0213 plane 1 character. */
279*aef5731fSOliver Tappe         if (jch & 0x8000) abort();
280*aef5731fSOliver Tappe         conv->ostate = (s1 << 8) | s2;
281*aef5731fSOliver Tappe         return count+0;
282*aef5731fSOliver Tappe       }
283*aef5731fSOliver Tappe       /* Output the shifted representation. */
284*aef5731fSOliver Tappe       if (n >= count+2) {
285*aef5731fSOliver Tappe         r[0] = s1;
286*aef5731fSOliver Tappe         r[1] = s2;
287*aef5731fSOliver Tappe         conv->ostate = 0;
288*aef5731fSOliver Tappe         return count+2;
289*aef5731fSOliver Tappe       } else
290*aef5731fSOliver Tappe         return RET_TOOSMALL;
291*aef5731fSOliver Tappe     }
292*aef5731fSOliver Tappe     return RET_ILUNI;
293*aef5731fSOliver Tappe   }
294*aef5731fSOliver Tappe }
295*aef5731fSOliver Tappe 
296*aef5731fSOliver Tappe static int
shift_jisx0213_reset(conv_t conv,unsigned char * r,int n)297*aef5731fSOliver Tappe shift_jisx0213_reset (conv_t conv, unsigned char *r, int n)
298*aef5731fSOliver Tappe {
299*aef5731fSOliver Tappe   state_t lasttwo = conv->ostate;
300*aef5731fSOliver Tappe 
301*aef5731fSOliver Tappe   if (lasttwo) {
302*aef5731fSOliver Tappe     if (n < 2)
303*aef5731fSOliver Tappe       return RET_TOOSMALL;
304*aef5731fSOliver Tappe     r[0] = (lasttwo >> 8) & 0xff;
305*aef5731fSOliver Tappe     r[1] = lasttwo & 0xff;
306*aef5731fSOliver Tappe     /* conv->ostate = 0; will be done by the caller */
307*aef5731fSOliver Tappe     return 2;
308*aef5731fSOliver Tappe   } else
309*aef5731fSOliver Tappe     return 0;
310*aef5731fSOliver Tappe }
311