xref: /haiku/src/libs/iconv/iso2022_jp1.h (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright (C) 1999-2001 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  * ISO-2022-JP-1
23  */
24 
25 /* Specification: RFC 2237 */
26 
27 #define ESC 0x1b
28 
29 /*
30  * The state can be one of the following values.
31  */
32 #define STATE_ASCII          0
33 #define STATE_JISX0201ROMAN  1
34 #define STATE_JISX0208       2
35 #define STATE_JISX0212       3
36 
37 static int
38 iso2022_jp1_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
39 {
40   state_t state = conv->istate;
41   int count = 0;
42   unsigned char c;
43   for (;;) {
44     c = *s;
45     if (c == ESC) {
46       if (n < count+3)
47         goto none;
48       if (s[1] == '(') {
49         if (s[2] == 'B') {
50           state = STATE_ASCII;
51           s += 3; count += 3;
52           if (n < count+1)
53             goto none;
54           continue;
55         }
56         if (s[2] == 'J') {
57           state = STATE_JISX0201ROMAN;
58           s += 3; count += 3;
59           if (n < count+1)
60             goto none;
61           continue;
62         }
63         return RET_ILSEQ;
64       }
65       if (s[1] == '$') {
66         if (s[2] == '@' || s[2] == 'B') {
67           /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
68           state = STATE_JISX0208;
69           s += 3; count += 3;
70           if (n < count+1)
71             goto none;
72           continue;
73         }
74         if (s[2] == '(') {
75           if (n < count+4)
76             goto none;
77           if (s[3] == 'D') {
78             state = STATE_JISX0212;
79             s += 4; count += 4;
80             if (n < count+1)
81               goto none;
82             continue;
83           }
84         }
85         return RET_ILSEQ;
86       }
87       return RET_ILSEQ;
88     }
89     break;
90   }
91   switch (state) {
92     case STATE_ASCII:
93       if (c < 0x80) {
94         int ret = ascii_mbtowc(conv,pwc,s,1);
95         if (ret == RET_ILSEQ)
96           return RET_ILSEQ;
97         if (ret != 1) abort();
98         conv->istate = state;
99         return count+1;
100       } else
101         return RET_ILSEQ;
102     case STATE_JISX0201ROMAN:
103       if (c < 0x80) {
104         int ret = jisx0201_mbtowc(conv,pwc,s,1);
105         if (ret == RET_ILSEQ)
106           return RET_ILSEQ;
107         if (ret != 1) abort();
108         conv->istate = state;
109         return count+1;
110       } else
111         return RET_ILSEQ;
112     case STATE_JISX0208:
113       if (n < count+2)
114         goto none;
115       if (s[0] < 0x80 && s[1] < 0x80) {
116         int ret = jisx0208_mbtowc(conv,pwc,s,2);
117         if (ret == RET_ILSEQ)
118           return RET_ILSEQ;
119         if (ret != 2) abort();
120         conv->istate = state;
121         return count+2;
122       } else
123         return RET_ILSEQ;
124     case STATE_JISX0212:
125       if (n < count+2)
126         goto none;
127       if (s[0] < 0x80 && s[1] < 0x80) {
128         int ret = jisx0212_mbtowc(conv,pwc,s,2);
129         if (ret == RET_ILSEQ)
130           return RET_ILSEQ;
131         if (ret != 2) abort();
132         conv->istate = state;
133         return count+2;
134       } else
135         return RET_ILSEQ;
136     default: abort();
137   }
138 
139 none:
140   conv->istate = state;
141   return RET_TOOFEW(count);
142 }
143 
144 static int
145 iso2022_jp1_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
146 {
147   state_t state = conv->ostate;
148   unsigned char buf[2];
149   int ret;
150 
151   /* Try ASCII. */
152   ret = ascii_wctomb(conv,buf,wc,1);
153   if (ret != RET_ILUNI) {
154     if (ret != 1) abort();
155     if (buf[0] < 0x80) {
156       int count = (state == STATE_ASCII ? 1 : 4);
157       if (n < count)
158         return RET_TOOSMALL;
159       if (state != STATE_ASCII) {
160         r[0] = ESC;
161         r[1] = '(';
162         r[2] = 'B';
163         r += 3;
164         state = STATE_ASCII;
165       }
166       r[0] = buf[0];
167       conv->ostate = state;
168       return count;
169     }
170   }
171 
172   /* Try JIS X 0201-1976 Roman. */
173   ret = jisx0201_wctomb(conv,buf,wc,1);
174   if (ret != RET_ILUNI) {
175     if (ret != 1) abort();
176     if (buf[0] < 0x80) {
177       int count = (state == STATE_JISX0201ROMAN ? 1 : 4);
178       if (n < count)
179         return RET_TOOSMALL;
180       if (state != STATE_JISX0201ROMAN) {
181         r[0] = ESC;
182         r[1] = '(';
183         r[2] = 'J';
184         r += 3;
185         state = STATE_JISX0201ROMAN;
186       }
187       r[0] = buf[0];
188       conv->ostate = state;
189       return count;
190     }
191   }
192 
193   /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
194   ret = jisx0208_wctomb(conv,buf,wc,2);
195   if (ret != RET_ILUNI) {
196     if (ret != 2) abort();
197     if (buf[0] < 0x80 && buf[1] < 0x80) {
198       int count = (state == STATE_JISX0208 ? 2 : 5);
199       if (n < count)
200         return RET_TOOSMALL;
201       if (state != STATE_JISX0208) {
202         r[0] = ESC;
203         r[1] = '$';
204         r[2] = 'B';
205         r += 3;
206         state = STATE_JISX0208;
207       }
208       r[0] = buf[0];
209       r[1] = buf[1];
210       conv->ostate = state;
211       return count;
212     }
213   }
214 
215   /* Try JIS X 0212-1990. */
216   ret = jisx0212_wctomb(conv,buf,wc,2);
217   if (ret != RET_ILUNI) {
218     if (ret != 2) abort();
219     if (buf[0] < 0x80 && buf[1] < 0x80) {
220       int count = (state == STATE_JISX0212 ? 2 : 6);
221       if (n < count)
222         return RET_TOOSMALL;
223       if (state != STATE_JISX0212) {
224         r[0] = ESC;
225         r[1] = '$';
226         r[2] = '(';
227         r[3] = 'D';
228         r += 4;
229         state = STATE_JISX0212;
230       }
231       r[0] = buf[0];
232       r[1] = buf[1];
233       conv->ostate = state;
234       return count;
235     }
236   }
237 
238   return RET_ILUNI;
239 }
240 
241 static int
242 iso2022_jp1_reset (conv_t conv, unsigned char *r, int n)
243 {
244   state_t state = conv->ostate;
245   if (state != STATE_ASCII) {
246     if (n < 3)
247       return RET_TOOSMALL;
248     r[0] = ESC;
249     r[1] = '(';
250     r[2] = 'B';
251     /* conv->ostate = 0; will be done by the caller */
252     return 3;
253   } else
254     return 0;
255 }
256 
257 #undef STATE_JISX0212
258 #undef STATE_JISX0208
259 #undef STATE_JISX0201ROMAN
260 #undef STATE_ASCII
261