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