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