citrus_mskanji.c revision 1.1 1 /* $NetBSD: citrus_mskanji.c,v 1.1 2002/03/17 22:14:23 tshiozak 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.1 2002/03/17 22:14:23 tshiozak 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 #include "citrus_module.h"
79 #include "citrus_ctype.h"
80 #include "citrus_mskanji.h"
81
82
83 /* ----------------------------------------------------------------------
84 * private stuffs used by templates
85 */
86
87 typedef struct _MSKanjiState {
88 char ch[2];
89 int chlen;
90 } _MSKanjiState;
91
92 typedef struct {
93 int dummy;
94 } _MSKanjiEncodingInfo;
95
96 typedef struct {
97 _MSKanjiEncodingInfo ei;
98 struct {
99 /* for future multi-locale facility */
100 _MSKanjiState s_mblen;
101 _MSKanjiState s_mbrlen;
102 _MSKanjiState s_mbrtowc;
103 _MSKanjiState s_mbtowc;
104 _MSKanjiState s_mbsrtowcs;
105 _MSKanjiState s_wcrtomb;
106 _MSKanjiState s_wcsrtombs;
107 _MSKanjiState s_wcstombs;
108 _MSKanjiState s_wctomb;
109 } states;
110 } _MSKanjiCTypeInfo;
111
112 #define _TO_EI(_cl_) ((_MSKanjiEncodingInfo *)(_cl_))
113 #define _TO_CEI(_cl_) ((_MSKanjiCTypeInfo *)(_cl_))
114 #define _TO_STATE(_ps_) ((_MSKanjiState *)(_ps_))
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(_cl_) 2
123 #define _ENCODING_IS_STATE_DEPENDENT 0
124
125
126 static int
127 _mskanji1(int c)
128 {
129
130 if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef))
131 return 1;
132 else
133 return 0;
134 }
135
136 static int
137 _mskanji2(int c)
138 {
139
140 if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
141 return 1;
142 else
143 return 0;
144 }
145
146 static __inline void
147 /*ARGSUSED*/
148 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei,
149 _MSKanjiState * __restrict s)
150 {
151 memset(s, 0, sizeof(*s));
152 }
153
154 static __inline void
155 /*ARGSUSED*/
156 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei,
157 void * __restrict pspriv,
158 const _MSKanjiState * __restrict s)
159 {
160 memcpy(pspriv, (const void *)s, sizeof(*s));
161 }
162
163 static __inline void
164 /*ARGSUSED*/
165 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei,
166 _MSKanjiState * __restrict s,
167 const void * __restrict pspriv)
168 {
169 memcpy((void *)s, pspriv, sizeof(*s));
170 }
171
172 static int
173 /*ARGSUSED*/
174 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
175 wchar_t * __restrict pwc,
176 const char ** __restrict s, size_t n,
177 _MSKanjiState * __restrict psenc,
178 size_t * __restrict nresult)
179 {
180 wchar_t wchar;
181 int len;
182 int chlenbak;
183 const char *s0;
184
185 _DIAGASSERT(nresult != 0);
186 _DIAGASSERT(ei != NULL);
187 _DIAGASSERT(s != NULL);
188 _DIAGASSERT(psenc != NULL);
189
190 s0 = *s;
191
192 if (s0 == NULL) {
193 _citrus_MSKanji_init_state(ei, psenc);
194 *nresult = 0; /* state independent */
195 return (0);
196 }
197
198 chlenbak = psenc->chlen;
199
200 /* make sure we have the first byte in the buffer */
201 switch (psenc->chlen) {
202 case 0:
203 if (n < 1)
204 goto restart;
205 psenc->ch[0] = *s0++;
206 psenc->chlen = 1;
207 n--;
208 break;
209 case 1:
210 break;
211 default:
212 /* illegal state */
213 goto encoding_error;
214 }
215
216 len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
217 while (psenc->chlen < len) {
218 if (n < 1)
219 goto restart;
220 psenc->ch[psenc->chlen] = *s0++;
221 psenc->chlen++;
222 n--;
223 }
224
225 *s = s0;
226
227 switch (len) {
228 case 1:
229 wchar = psenc->ch[0] & 0xff;
230 break;
231 case 2:
232 if (!_mskanji2(psenc->ch[1] & 0xff))
233 goto encoding_error;
234 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
235 break;
236 default:
237 /* illegal state */
238 goto encoding_error;
239 }
240
241 psenc->chlen = 0;
242
243 if (pwc)
244 *pwc = wchar;
245
246 if (!wchar)
247 *nresult = 0;
248 else
249 *nresult = len - chlenbak;
250
251 return (0);
252
253 encoding_error:
254 psenc->chlen = 0;
255 *nresult = (size_t)-1;
256 return (EILSEQ);
257
258 restart:
259 *nresult = (size_t)-2;
260 *s = s0;
261 return (0);
262 }
263
264
265 static int
266 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei,
267 char * __restrict s, size_t n, wchar_t wc,
268 _MSKanjiState * __restrict psenc,
269 size_t * __restrict nresult)
270 {
271
272 _DIAGASSERT(ei != NULL);
273 _DIAGASSERT(psenc != NULL);
274 _DIAGASSERT(s != NULL);
275
276 /* check invalid sequence */
277 if (wc & ~0xffff)
278 goto ilseq;
279
280 if (wc & 0xff00) {
281 if (n < 2)
282 goto ilseq;
283
284 s[0] = (wc >> 8) & 0xff;
285 s[1] = wc & 0xff;
286 if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff))
287 goto ilseq;
288
289 *nresult = 2;
290 return (0);
291 } else {
292 s[0] = wc & 0xff;
293 if (_mskanji1(s[0] & 0xff))
294 goto ilseq;
295
296 *nresult = 1;
297 return (0);
298 }
299
300 ilseq:
301 *nresult = (size_t)-1;
302 return EILSEQ;
303 }
304
305
306 static int
307 /*ARGSUSED*/
308 _citrus_MSKanji_stdencoding_init(_MSKanjiEncodingInfo * __restrict ei,
309 const void * __restrict var, size_t lenvar)
310 {
311
312 _DIAGASSERT(cl != NULL);
313
314 return (0);
315 }
316
317 static void
318 _citrus_MSKanji_stdencoding_uninit(_MSKanjiEncodingInfo *ei)
319 {
320 }
321
322 /* ----------------------------------------------------------------------
323 * public interface for ctype
324 */
325
326 _CITRUS_CTYPE_DECLS(MSKanji);
327 _CITRUS_CTYPE_DEF_OPS(MSKanji);
328
329 #include "citrus_ctype_template.h"
330