citrus_mskanji.c revision 1.8 1 /* $NetBSD: citrus_mskanji.c,v 1.8 2003/12/29 17:59:03 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2002 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * ja_JP.SJIS locale table for BSD4.4/rune
31 * version 1.0
32 * (C) Sin'ichiro MIYATANI / Phase One, Inc
33 * May 12, 1995
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Phase One, Inc.
46 * 4. The name of Phase One, Inc. may be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 */
61
62
63 #include <sys/cdefs.h>
64 #if defined(LIBC_SCCS) && !defined(lint)
65 __RCSID("$NetBSD: citrus_mskanji.c,v 1.8 2003/12/29 17:59:03 yamt Exp $");
66 #endif /* LIBC_SCCS and not lint */
67
68 #include <assert.h>
69 #include <errno.h>
70 #include <string.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <stddef.h>
74 #include <locale.h>
75 #include <wchar.h>
76 #include <sys/types.h>
77 #include <limits.h>
78
79 #include "citrus_namespace.h"
80 #include "citrus_types.h"
81 #include "citrus_module.h"
82 #include "citrus_ctype.h"
83 #include "citrus_stdenc.h"
84 #include "citrus_mskanji.h"
85
86
87 /* ----------------------------------------------------------------------
88 * private stuffs used by templates
89 */
90
91 typedef struct _MSKanjiState {
92 char ch[2];
93 int chlen;
94 } _MSKanjiState;
95
96 typedef struct {
97 int dummy;
98 } _MSKanjiEncodingInfo;
99
100 typedef struct {
101 _MSKanjiEncodingInfo ei;
102 struct {
103 /* for future multi-locale facility */
104 _MSKanjiState s_mblen;
105 _MSKanjiState s_mbrlen;
106 _MSKanjiState s_mbrtowc;
107 _MSKanjiState s_mbtowc;
108 _MSKanjiState s_mbsrtowcs;
109 _MSKanjiState s_wcrtomb;
110 _MSKanjiState s_wcsrtombs;
111 _MSKanjiState s_wctomb;
112 } states;
113 } _MSKanjiCTypeInfo;
114
115 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
116 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
117
118 #define _FUNCNAME(m) _citrus_MSKanji_##m
119 #define _ENCODING_INFO _MSKanjiEncodingInfo
120 #define _CTYPE_INFO _MSKanjiCTypeInfo
121 #define _ENCODING_STATE _MSKanjiState
122 #define _ENCODING_MB_CUR_MAX(_ei_) 2
123 #define _ENCODING_IS_STATE_DEPENDENT 0
124 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
125
126
127 static int
128 _mskanji1(int c)
129 {
130
131 if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef))
132 return 1;
133 else
134 return 0;
135 }
136
137 static int
138 _mskanji2(int c)
139 {
140
141 if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
142 return 1;
143 else
144 return 0;
145 }
146
147 static __inline void
148 /*ARGSUSED*/
149 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei,
150 _MSKanjiState * __restrict s)
151 {
152 memset(s, 0, sizeof(*s));
153 }
154
155 static __inline void
156 /*ARGSUSED*/
157 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei,
158 void * __restrict pspriv,
159 const _MSKanjiState * __restrict s)
160 {
161 memcpy(pspriv, (const void *)s, sizeof(*s));
162 }
163
164 static __inline void
165 /*ARGSUSED*/
166 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei,
167 _MSKanjiState * __restrict s,
168 const void * __restrict pspriv)
169 {
170 memcpy((void *)s, pspriv, sizeof(*s));
171 }
172
173 static int
174 /*ARGSUSED*/
175 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
176 wchar_t * __restrict pwc,
177 const char ** __restrict s, size_t n,
178 _MSKanjiState * __restrict psenc,
179 size_t * __restrict nresult)
180 {
181 wchar_t wchar;
182 int len;
183 int chlenbak;
184 const char *s0;
185
186 _DIAGASSERT(nresult != 0);
187 _DIAGASSERT(ei != NULL);
188 _DIAGASSERT(s != NULL);
189 _DIAGASSERT(psenc != NULL);
190
191 s0 = *s;
192
193 if (s0 == NULL) {
194 _citrus_MSKanji_init_state(ei, psenc);
195 *nresult = 0; /* state independent */
196 return (0);
197 }
198
199 chlenbak = psenc->chlen;
200
201 /* make sure we have the first byte in the buffer */
202 switch (psenc->chlen) {
203 case 0:
204 if (n < 1)
205 goto restart;
206 psenc->ch[0] = *s0++;
207 psenc->chlen = 1;
208 n--;
209 break;
210 case 1:
211 break;
212 default:
213 /* illegal state */
214 goto encoding_error;
215 }
216
217 len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
218 while (psenc->chlen < len) {
219 if (n < 1)
220 goto restart;
221 psenc->ch[psenc->chlen] = *s0++;
222 psenc->chlen++;
223 n--;
224 }
225
226 *s = s0;
227
228 switch (len) {
229 case 1:
230 wchar = psenc->ch[0] & 0xff;
231 break;
232 case 2:
233 if (!_mskanji2(psenc->ch[1] & 0xff))
234 goto encoding_error;
235 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
236 break;
237 default:
238 /* illegal state */
239 goto encoding_error;
240 }
241
242 psenc->chlen = 0;
243
244 if (pwc)
245 *pwc = wchar;
246
247 if (!wchar)
248 *nresult = 0;
249 else
250 *nresult = len - chlenbak;
251
252 return (0);
253
254 encoding_error:
255 psenc->chlen = 0;
256 *nresult = (size_t)-1;
257 return (EILSEQ);
258
259 restart:
260 *nresult = (size_t)-2;
261 *s = s0;
262 return (0);
263 }
264
265
266 static int
267 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei,
268 char * __restrict s, size_t n, wchar_t wc,
269 _MSKanjiState * __restrict psenc,
270 size_t * __restrict nresult)
271 {
272 int ret;
273
274 _DIAGASSERT(ei != NULL);
275 _DIAGASSERT(psenc != NULL);
276 _DIAGASSERT(s != NULL);
277
278 /* check invalid sequence */
279 if (wc & ~0xffff) {
280 ret = EILSEQ;
281 goto err;
282 }
283
284 if (wc & 0xff00) {
285 if (n < 2) {
286 ret = E2BIG;
287 goto err;
288 }
289
290 s[0] = (wc >> 8) & 0xff;
291 s[1] = wc & 0xff;
292 if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) {
293 ret = EILSEQ;
294 goto err;
295 }
296
297 *nresult = 2;
298 return 0;
299 } else {
300 if (n < 1) {
301 ret = E2BIG;
302 goto err;
303 }
304
305 s[0] = wc & 0xff;
306 if (_mskanji1(s[0] & 0xff)) {
307 ret = EILSEQ;
308 goto err;
309 }
310
311 *nresult = 1;
312 return 0;
313 }
314
315 err:
316 *nresult = (size_t)-1;
317 return ret;
318 }
319
320
321 static __inline int
322 /*ARGSUSED*/
323 _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei,
324 _csid_t * __restrict csid,
325 _index_t * __restrict idx, wchar_t wc)
326 {
327 _index_t row, col;
328
329 _DIAGASSERT(csid != NULL && idx != NULL);
330
331 if ((_wc_t)wc < 0x80) {
332 /* ISO-646 */
333 *csid = 0;
334 *idx = (_index_t)wc;
335 } else if ((_wc_t)wc < 0x100) {
336 /* KANA */
337 *csid = 1;
338 *idx = (_index_t)wc & 0x7F;
339 } else if ((0x8140 <= (_wc_t)wc && (_wc_t)wc <= 0x9FFC) ||
340 (0xE040 <= (_wc_t)wc && (_wc_t)wc <= 0xFCFC)) {
341 /* Kanji (containing Gaiji zone) */
342 /*
343 * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)):
344 * 0x8140 - 0x817E -> 0x2121 - 0x215F
345 * 0x8180 - 0x819E -> 0x2160 - 0x217E
346 * 0x819F - 0x81FC -> 0x2221 - 0x227E
347 *
348 * 0x8240 - 0x827E -> 0x2321 - 0x235F
349 * ...
350 * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E
351 *
352 * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F
353 * ...
354 * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E
355 *
356 * extended Gaiji zone:
357 * 0xF040 - 0xFCFC
358 */
359 *csid = 2;
360 row = ((_wc_t)wc >> 8) - 0x81;
361 if (row >= 0x5F)
362 row -= 0x40;
363 row = row * 2 + 0x21;
364 col = (wc & 0xFF) - 0x1F;
365 if (col >= 0x61)
366 col -= 1;
367 if (col > 0x7E) {
368 row += 1;
369 col -= 0x5E;
370 }
371 *idx = ((_index_t)row << 8) | col;
372 } else
373 return EILSEQ;
374
375 return 0;
376 }
377
378 static __inline int
379 /*ARGSUSED*/
380 _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei,
381 wchar_t * __restrict wc,
382 _csid_t csid, _index_t idx)
383 {
384 u_int32_t row, col;
385
386 _DIAGASSERT(wc != NULL);
387
388 switch (csid) {
389 case 0:
390 /* ISO-646 */
391 if (idx >= 0x80)
392 return EILSEQ;
393 *wc = (wchar_t)idx;
394 break;
395 case 1:
396 /* kana */
397 if (idx >= 0x80)
398 return EILSEQ;
399 *wc = (wchar_t)idx + 0x80;
400 break;
401 case 2:
402 /* kanji */
403 row = (idx >> 8);
404 col = idx & 0x7F;
405 if (row<0x21 || row>0x97 || col<0x21 || col>0x7E)
406 return EILSEQ;
407 row -= 0x21; col -= 0x21;
408 if ((row & 1)==0) {
409 col += 0x40;
410 if (col>=0x7F)
411 col += 1;
412 } else
413 col += 0x9F;
414 if (row<0x3E)
415 row = row/2 + 0x81;
416 else
417 row = row/2 + 0xc1;
418 *wc = ((wchar_t)row << 8) | col;
419 break;
420 default:
421 return EILSEQ;
422 }
423
424 return 0;
425 }
426
427 static int
428 /*ARGSUSED*/
429 _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei,
430 const void * __restrict var,
431 size_t lenvar)
432 {
433
434 _DIAGASSERT(cl != NULL);
435
436 return (0);
437 }
438
439 static void
440 _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei)
441 {
442 }
443
444 /* ----------------------------------------------------------------------
445 * public interface for ctype
446 */
447
448 _CITRUS_CTYPE_DECLS(MSKanji);
449 _CITRUS_CTYPE_DEF_OPS(MSKanji);
450
451 #include "citrus_ctype_template.h"
452
453 /* ----------------------------------------------------------------------
454 * public interface for stdenc
455 */
456
457 _CITRUS_STDENC_DECLS(MSKanji);
458 _CITRUS_STDENC_DEF_OPS(MSKanji);
459
460 #include "citrus_stdenc_template.h"
461