citrus_mskanji.c revision 1.1.2.3 1 1.1.2.3 nathanw /* $NetBSD: citrus_mskanji.c,v 1.1.2.3 2002/04/25 04:01:40 nathanw Exp $ */
2 1.1.2.2 nathanw
3 1.1.2.2 nathanw /*-
4 1.1.2.2 nathanw * Copyright (c)2002 Citrus Project,
5 1.1.2.2 nathanw * All rights reserved.
6 1.1.2.2 nathanw *
7 1.1.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.1.2.2 nathanw * are met:
10 1.1.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.1.2.2 nathanw *
16 1.1.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 nathanw * SUCH DAMAGE.
27 1.1.2.2 nathanw */
28 1.1.2.2 nathanw
29 1.1.2.2 nathanw /*
30 1.1.2.2 nathanw * ja_JP.SJIS locale table for BSD4.4/rune
31 1.1.2.2 nathanw * version 1.0
32 1.1.2.2 nathanw * (C) Sin'ichiro MIYATANI / Phase One, Inc
33 1.1.2.2 nathanw * May 12, 1995
34 1.1.2.2 nathanw *
35 1.1.2.2 nathanw * Redistribution and use in source and binary forms, with or without
36 1.1.2.2 nathanw * modification, are permitted provided that the following conditions
37 1.1.2.2 nathanw * are met:
38 1.1.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
39 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer.
40 1.1.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
41 1.1.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
42 1.1.2.2 nathanw * documentation and/or other materials provided with the distribution.
43 1.1.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
44 1.1.2.2 nathanw * must display the following acknowledgement:
45 1.1.2.2 nathanw * This product includes software developed by Phase One, Inc.
46 1.1.2.2 nathanw * 4. The name of Phase One, Inc. may be used to endorse or promote products
47 1.1.2.2 nathanw * derived from this software without specific prior written permission.
48 1.1.2.2 nathanw *
49 1.1.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1.2.2 nathanw * SUCH DAMAGE.
60 1.1.2.2 nathanw */
61 1.1.2.2 nathanw
62 1.1.2.2 nathanw
63 1.1.2.2 nathanw #include <sys/cdefs.h>
64 1.1.2.2 nathanw #if defined(LIBC_SCCS) && !defined(lint)
65 1.1.2.3 nathanw __RCSID("$NetBSD: citrus_mskanji.c,v 1.1.2.3 2002/04/25 04:01:40 nathanw Exp $");
66 1.1.2.2 nathanw #endif /* LIBC_SCCS and not lint */
67 1.1.2.2 nathanw
68 1.1.2.2 nathanw #include <assert.h>
69 1.1.2.2 nathanw #include <errno.h>
70 1.1.2.2 nathanw #include <string.h>
71 1.1.2.2 nathanw #include <stdio.h>
72 1.1.2.2 nathanw #include <stdlib.h>
73 1.1.2.2 nathanw #include <stddef.h>
74 1.1.2.2 nathanw #include <locale.h>
75 1.1.2.2 nathanw #include <wchar.h>
76 1.1.2.2 nathanw #include <sys/types.h>
77 1.1.2.2 nathanw #include <limits.h>
78 1.1.2.2 nathanw #include "citrus_module.h"
79 1.1.2.2 nathanw #include "citrus_ctype.h"
80 1.1.2.2 nathanw #include "citrus_mskanji.h"
81 1.1.2.2 nathanw
82 1.1.2.2 nathanw
83 1.1.2.2 nathanw /* ----------------------------------------------------------------------
84 1.1.2.2 nathanw * private stuffs used by templates
85 1.1.2.2 nathanw */
86 1.1.2.2 nathanw
87 1.1.2.2 nathanw typedef struct _MSKanjiState {
88 1.1.2.2 nathanw char ch[2];
89 1.1.2.2 nathanw int chlen;
90 1.1.2.2 nathanw } _MSKanjiState;
91 1.1.2.2 nathanw
92 1.1.2.2 nathanw typedef struct {
93 1.1.2.2 nathanw int dummy;
94 1.1.2.2 nathanw } _MSKanjiEncodingInfo;
95 1.1.2.2 nathanw
96 1.1.2.2 nathanw typedef struct {
97 1.1.2.2 nathanw _MSKanjiEncodingInfo ei;
98 1.1.2.2 nathanw struct {
99 1.1.2.2 nathanw /* for future multi-locale facility */
100 1.1.2.2 nathanw _MSKanjiState s_mblen;
101 1.1.2.2 nathanw _MSKanjiState s_mbrlen;
102 1.1.2.2 nathanw _MSKanjiState s_mbrtowc;
103 1.1.2.2 nathanw _MSKanjiState s_mbtowc;
104 1.1.2.2 nathanw _MSKanjiState s_mbsrtowcs;
105 1.1.2.2 nathanw _MSKanjiState s_wcrtomb;
106 1.1.2.2 nathanw _MSKanjiState s_wcsrtombs;
107 1.1.2.2 nathanw _MSKanjiState s_wctomb;
108 1.1.2.2 nathanw } states;
109 1.1.2.2 nathanw } _MSKanjiCTypeInfo;
110 1.1.2.2 nathanw
111 1.1.2.2 nathanw #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
112 1.1.2.2 nathanw #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
113 1.1.2.2 nathanw
114 1.1.2.2 nathanw #define _FUNCNAME(m) _citrus_MSKanji_##m
115 1.1.2.2 nathanw #define _ENCODING_INFO _MSKanjiEncodingInfo
116 1.1.2.2 nathanw #define _CTYPE_INFO _MSKanjiCTypeInfo
117 1.1.2.2 nathanw #define _ENCODING_STATE _MSKanjiState
118 1.1.2.3 nathanw #define _ENCODING_MB_CUR_MAX(_ei_) 2
119 1.1.2.2 nathanw #define _ENCODING_IS_STATE_DEPENDENT 0
120 1.1.2.3 nathanw #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
121 1.1.2.2 nathanw
122 1.1.2.2 nathanw
123 1.1.2.2 nathanw static int
124 1.1.2.2 nathanw _mskanji1(int c)
125 1.1.2.2 nathanw {
126 1.1.2.2 nathanw
127 1.1.2.2 nathanw if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef))
128 1.1.2.2 nathanw return 1;
129 1.1.2.2 nathanw else
130 1.1.2.2 nathanw return 0;
131 1.1.2.2 nathanw }
132 1.1.2.2 nathanw
133 1.1.2.2 nathanw static int
134 1.1.2.2 nathanw _mskanji2(int c)
135 1.1.2.2 nathanw {
136 1.1.2.2 nathanw
137 1.1.2.2 nathanw if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
138 1.1.2.2 nathanw return 1;
139 1.1.2.2 nathanw else
140 1.1.2.2 nathanw return 0;
141 1.1.2.2 nathanw }
142 1.1.2.2 nathanw
143 1.1.2.2 nathanw static __inline void
144 1.1.2.2 nathanw /*ARGSUSED*/
145 1.1.2.2 nathanw _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei,
146 1.1.2.2 nathanw _MSKanjiState * __restrict s)
147 1.1.2.2 nathanw {
148 1.1.2.2 nathanw memset(s, 0, sizeof(*s));
149 1.1.2.2 nathanw }
150 1.1.2.2 nathanw
151 1.1.2.2 nathanw static __inline void
152 1.1.2.2 nathanw /*ARGSUSED*/
153 1.1.2.2 nathanw _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei,
154 1.1.2.2 nathanw void * __restrict pspriv,
155 1.1.2.2 nathanw const _MSKanjiState * __restrict s)
156 1.1.2.2 nathanw {
157 1.1.2.2 nathanw memcpy(pspriv, (const void *)s, sizeof(*s));
158 1.1.2.2 nathanw }
159 1.1.2.2 nathanw
160 1.1.2.2 nathanw static __inline void
161 1.1.2.2 nathanw /*ARGSUSED*/
162 1.1.2.2 nathanw _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei,
163 1.1.2.2 nathanw _MSKanjiState * __restrict s,
164 1.1.2.2 nathanw const void * __restrict pspriv)
165 1.1.2.2 nathanw {
166 1.1.2.2 nathanw memcpy((void *)s, pspriv, sizeof(*s));
167 1.1.2.2 nathanw }
168 1.1.2.2 nathanw
169 1.1.2.2 nathanw static int
170 1.1.2.2 nathanw /*ARGSUSED*/
171 1.1.2.2 nathanw _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
172 1.1.2.2 nathanw wchar_t * __restrict pwc,
173 1.1.2.2 nathanw const char ** __restrict s, size_t n,
174 1.1.2.2 nathanw _MSKanjiState * __restrict psenc,
175 1.1.2.2 nathanw size_t * __restrict nresult)
176 1.1.2.2 nathanw {
177 1.1.2.2 nathanw wchar_t wchar;
178 1.1.2.2 nathanw int len;
179 1.1.2.2 nathanw int chlenbak;
180 1.1.2.2 nathanw const char *s0;
181 1.1.2.2 nathanw
182 1.1.2.2 nathanw _DIAGASSERT(nresult != 0);
183 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
184 1.1.2.2 nathanw _DIAGASSERT(s != NULL);
185 1.1.2.2 nathanw _DIAGASSERT(psenc != NULL);
186 1.1.2.2 nathanw
187 1.1.2.2 nathanw s0 = *s;
188 1.1.2.2 nathanw
189 1.1.2.2 nathanw if (s0 == NULL) {
190 1.1.2.2 nathanw _citrus_MSKanji_init_state(ei, psenc);
191 1.1.2.2 nathanw *nresult = 0; /* state independent */
192 1.1.2.2 nathanw return (0);
193 1.1.2.2 nathanw }
194 1.1.2.2 nathanw
195 1.1.2.2 nathanw chlenbak = psenc->chlen;
196 1.1.2.2 nathanw
197 1.1.2.2 nathanw /* make sure we have the first byte in the buffer */
198 1.1.2.2 nathanw switch (psenc->chlen) {
199 1.1.2.2 nathanw case 0:
200 1.1.2.2 nathanw if (n < 1)
201 1.1.2.2 nathanw goto restart;
202 1.1.2.2 nathanw psenc->ch[0] = *s0++;
203 1.1.2.2 nathanw psenc->chlen = 1;
204 1.1.2.2 nathanw n--;
205 1.1.2.2 nathanw break;
206 1.1.2.2 nathanw case 1:
207 1.1.2.2 nathanw break;
208 1.1.2.2 nathanw default:
209 1.1.2.2 nathanw /* illegal state */
210 1.1.2.2 nathanw goto encoding_error;
211 1.1.2.2 nathanw }
212 1.1.2.2 nathanw
213 1.1.2.2 nathanw len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
214 1.1.2.2 nathanw while (psenc->chlen < len) {
215 1.1.2.2 nathanw if (n < 1)
216 1.1.2.2 nathanw goto restart;
217 1.1.2.2 nathanw psenc->ch[psenc->chlen] = *s0++;
218 1.1.2.2 nathanw psenc->chlen++;
219 1.1.2.2 nathanw n--;
220 1.1.2.2 nathanw }
221 1.1.2.2 nathanw
222 1.1.2.2 nathanw *s = s0;
223 1.1.2.2 nathanw
224 1.1.2.2 nathanw switch (len) {
225 1.1.2.2 nathanw case 1:
226 1.1.2.2 nathanw wchar = psenc->ch[0] & 0xff;
227 1.1.2.2 nathanw break;
228 1.1.2.2 nathanw case 2:
229 1.1.2.2 nathanw if (!_mskanji2(psenc->ch[1] & 0xff))
230 1.1.2.2 nathanw goto encoding_error;
231 1.1.2.2 nathanw wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
232 1.1.2.2 nathanw break;
233 1.1.2.2 nathanw default:
234 1.1.2.2 nathanw /* illegal state */
235 1.1.2.2 nathanw goto encoding_error;
236 1.1.2.2 nathanw }
237 1.1.2.2 nathanw
238 1.1.2.2 nathanw psenc->chlen = 0;
239 1.1.2.2 nathanw
240 1.1.2.2 nathanw if (pwc)
241 1.1.2.2 nathanw *pwc = wchar;
242 1.1.2.2 nathanw
243 1.1.2.2 nathanw if (!wchar)
244 1.1.2.2 nathanw *nresult = 0;
245 1.1.2.2 nathanw else
246 1.1.2.2 nathanw *nresult = len - chlenbak;
247 1.1.2.2 nathanw
248 1.1.2.2 nathanw return (0);
249 1.1.2.2 nathanw
250 1.1.2.2 nathanw encoding_error:
251 1.1.2.2 nathanw psenc->chlen = 0;
252 1.1.2.2 nathanw *nresult = (size_t)-1;
253 1.1.2.2 nathanw return (EILSEQ);
254 1.1.2.2 nathanw
255 1.1.2.2 nathanw restart:
256 1.1.2.2 nathanw *nresult = (size_t)-2;
257 1.1.2.2 nathanw *s = s0;
258 1.1.2.2 nathanw return (0);
259 1.1.2.2 nathanw }
260 1.1.2.2 nathanw
261 1.1.2.2 nathanw
262 1.1.2.2 nathanw static int
263 1.1.2.2 nathanw _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei,
264 1.1.2.2 nathanw char * __restrict s, size_t n, wchar_t wc,
265 1.1.2.2 nathanw _MSKanjiState * __restrict psenc,
266 1.1.2.2 nathanw size_t * __restrict nresult)
267 1.1.2.2 nathanw {
268 1.1.2.2 nathanw
269 1.1.2.2 nathanw _DIAGASSERT(ei != NULL);
270 1.1.2.2 nathanw _DIAGASSERT(psenc != NULL);
271 1.1.2.2 nathanw _DIAGASSERT(s != NULL);
272 1.1.2.2 nathanw
273 1.1.2.2 nathanw /* check invalid sequence */
274 1.1.2.2 nathanw if (wc & ~0xffff)
275 1.1.2.2 nathanw goto ilseq;
276 1.1.2.2 nathanw
277 1.1.2.2 nathanw if (wc & 0xff00) {
278 1.1.2.2 nathanw if (n < 2)
279 1.1.2.2 nathanw goto ilseq;
280 1.1.2.2 nathanw
281 1.1.2.2 nathanw s[0] = (wc >> 8) & 0xff;
282 1.1.2.2 nathanw s[1] = wc & 0xff;
283 1.1.2.2 nathanw if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff))
284 1.1.2.2 nathanw goto ilseq;
285 1.1.2.2 nathanw
286 1.1.2.2 nathanw *nresult = 2;
287 1.1.2.2 nathanw return (0);
288 1.1.2.2 nathanw } else {
289 1.1.2.2 nathanw s[0] = wc & 0xff;
290 1.1.2.2 nathanw if (_mskanji1(s[0] & 0xff))
291 1.1.2.2 nathanw goto ilseq;
292 1.1.2.2 nathanw
293 1.1.2.2 nathanw *nresult = 1;
294 1.1.2.2 nathanw return (0);
295 1.1.2.2 nathanw }
296 1.1.2.2 nathanw
297 1.1.2.2 nathanw ilseq:
298 1.1.2.2 nathanw *nresult = (size_t)-1;
299 1.1.2.2 nathanw return EILSEQ;
300 1.1.2.2 nathanw }
301 1.1.2.2 nathanw
302 1.1.2.2 nathanw
303 1.1.2.2 nathanw static int
304 1.1.2.2 nathanw /*ARGSUSED*/
305 1.1.2.2 nathanw _citrus_MSKanji_stdencoding_init(_MSKanjiEncodingInfo * __restrict ei,
306 1.1.2.2 nathanw const void * __restrict var, size_t lenvar)
307 1.1.2.2 nathanw {
308 1.1.2.2 nathanw
309 1.1.2.2 nathanw _DIAGASSERT(cl != NULL);
310 1.1.2.2 nathanw
311 1.1.2.2 nathanw return (0);
312 1.1.2.2 nathanw }
313 1.1.2.2 nathanw
314 1.1.2.2 nathanw static void
315 1.1.2.2 nathanw _citrus_MSKanji_stdencoding_uninit(_MSKanjiEncodingInfo *ei)
316 1.1.2.2 nathanw {
317 1.1.2.2 nathanw }
318 1.1.2.2 nathanw
319 1.1.2.2 nathanw /* ----------------------------------------------------------------------
320 1.1.2.2 nathanw * public interface for ctype
321 1.1.2.2 nathanw */
322 1.1.2.2 nathanw
323 1.1.2.2 nathanw _CITRUS_CTYPE_DECLS(MSKanji);
324 1.1.2.2 nathanw _CITRUS_CTYPE_DEF_OPS(MSKanji);
325 1.1.2.2 nathanw
326 1.1.2.2 nathanw #include "citrus_ctype_template.h"
327