xref: /haiku/src/libs/iconv/euc_jp.h (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright (C) 1999-2001, 2005 Free Software Foundation, Inc.
3  * This file is part of the GNU LIBICONV Library.
4  *
5  * The GNU LIBICONV Library is free software; you can redistribute it
6  * and/or modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * The GNU LIBICONV Library is distributed in the hope that it will be
11  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17  * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18  * Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 /*
22  * EUC-JP
23  */
24 
25 static int
26 euc_jp_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
27 {
28   unsigned char c = *s;
29   /* Code set 0 (ASCII or JIS X 0201-1976 Roman) */
30   if (c < 0x80)
31     return ascii_mbtowc(conv,pwc,s,n);
32   /* Code set 1 (JIS X 0208) */
33   if (c >= 0xa1 && c < 0xff) {
34     if (n < 2)
35       return RET_TOOFEW(0);
36     if (c < 0xf5) {
37       unsigned char c2 = s[1];
38       if (c2 >= 0xa1 && c2 < 0xff) {
39         unsigned char buf[2];
40         buf[0] = c-0x80; buf[1] = c2-0x80;
41         return jisx0208_mbtowc(conv,pwc,buf,2);
42       } else
43         return RET_ILSEQ;
44     } else {
45       /* User-defined range. See
46        * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
47       unsigned char c2 = s[1];
48       if (c2 >= 0xa1 && c2 < 0xff) {
49         *pwc = 0xe000 + 94*(c-0xf5) + (c2-0xa1);
50         return 2;
51       } else
52         return RET_ILSEQ;
53     }
54   }
55   /* Code set 2 (half-width katakana) */
56   if (c == 0x8e) {
57     if (n < 2)
58       return RET_TOOFEW(0);
59     {
60       unsigned char c2 = s[1];
61       if (c2 >= 0xa1 && c2 < 0xe0) {
62         int ret = jisx0201_mbtowc(conv,pwc,s+1,n-1);
63         if (ret == RET_ILSEQ)
64           return RET_ILSEQ;
65         if (ret != 1) abort();
66         return 2;
67       } else
68         return RET_ILSEQ;
69     }
70   }
71   /* Code set 3 (JIS X 0212-1990) */
72   if (c == 0x8f) {
73     if (n < 2)
74       return RET_TOOFEW(0);
75     {
76       unsigned char c2 = s[1];
77       if (c2 >= 0xa1 && c2 < 0xff) {
78         if (n < 3)
79           return RET_TOOFEW(0);
80         if (c2 < 0xf5) {
81           unsigned char c3 = s[2];
82           if (c3 >= 0xa1 && c3 < 0xff) {
83             unsigned char buf[2];
84             int ret;
85             buf[0] = c2-0x80; buf[1] = c3-0x80;
86             ret = jisx0212_mbtowc(conv,pwc,buf,2);
87             if (ret == RET_ILSEQ)
88               return RET_ILSEQ;
89             if (ret != 2) abort();
90             return 3;
91           } else
92             return RET_ILSEQ;
93         } else {
94           /* User-defined range. See
95            * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
96           unsigned char c3 = s[2];
97           if (c3 >= 0xa1 && c3 < 0xff) {
98             *pwc = 0xe3ac + 94*(c2-0xf5) + (c3-0xa1);
99             return 3;
100           } else
101             return RET_ILSEQ;
102         }
103       } else
104         return RET_ILSEQ;
105     }
106   }
107   return RET_ILSEQ;
108 }
109 
110 static int
111 euc_jp_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
112 {
113   unsigned char buf[2];
114   int ret;
115 
116   /* Code set 0 (ASCII or JIS X 0201-1976 Roman) */
117   ret = ascii_wctomb(conv,r,wc,n);
118   if (ret != RET_ILUNI)
119     return ret;
120 
121   /* Code set 1 (JIS X 0208) */
122   ret = jisx0208_wctomb(conv,buf,wc,2);
123   if (ret != RET_ILUNI) {
124     if (ret != 2) abort();
125     if (n < 2)
126       return RET_TOOSMALL;
127     r[0] = buf[0]+0x80;
128     r[1] = buf[1]+0x80;
129     return 2;
130   }
131 
132   /* Code set 2 (half-width katakana) */
133   ret = jisx0201_wctomb(conv,buf,wc,1);
134   if (ret != RET_ILUNI && buf[0] >= 0x80) {
135     if (ret != 1) abort();
136     if (n < 2)
137       return RET_TOOSMALL;
138     r[0] = 0x8e;
139     r[1] = buf[0];
140     return 2;
141   }
142 
143   /* Code set 3 (JIS X 0212-1990) */
144   ret = jisx0212_wctomb(conv,buf,wc,2);
145   if (ret != RET_ILUNI) {
146     if (ret != 2) abort();
147     if (n < 3)
148       return RET_TOOSMALL;
149     r[0] = 0x8f;
150     r[1] = buf[0]+0x80;
151     r[2] = buf[1]+0x80;
152     return 3;
153   }
154 
155   /* Extra compatibility with Shift_JIS.  */
156   if (wc == 0x00a5) {
157     r[0] = 0x5c;
158     return 1;
159   }
160   if (wc == 0x203e) {
161     r[0] = 0x7e;
162     return 1;
163   }
164 
165   /* User-defined range. See
166    * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
167   if (wc >= 0xe000 && wc < 0xe758) {
168     if (wc < 0xe3ac) {
169       unsigned char c1, c2;
170       if (n < 2)
171         return RET_TOOSMALL;
172       c1 = (unsigned int) (wc - 0xe000) / 94;
173       c2 = (unsigned int) (wc - 0xe000) % 94;
174       r[0] = c1+0xf5;
175       r[1] = c2+0xa1;
176       return 2;
177     } else {
178       unsigned char c1, c2;
179       if (n < 3)
180         return RET_TOOSMALL;
181       c1 = (unsigned int) (wc - 0xe3ac) / 94;
182       c2 = (unsigned int) (wc - 0xe3ac) % 94;
183       r[0] = 0x8f;
184       r[1] = c1+0xf5;
185       r[2] = c2+0xa1;
186       return 3;
187     }
188   }
189 
190   return RET_ILUNI;
191 }
192