citrus_big5.c revision 1.2 1 /* $NetBSD: citrus_big5.c,v 1.2 2002/03/27 15:01:17 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.2 2002/03/27 15:01:17 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_wcstombs;
109 _BIG5State s_wctomb;
110 } states;
111 } _BIG5CTypeInfo;
112
113 #define _TO_EI(_cl_) ((_BIG5EncodingInfo *)(_cl_))
114 #define _TO_CEI(_cl_) ((_BIG5CTypeInfo *)(_cl_))
115 #define _TO_STATE(_ps_) ((_BIG5State *)(_ps_))
116 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
117 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
118
119 #define _FUNCNAME(m) _citrus_BIG5_##m
120 #define _ENCODING_INFO _BIG5EncodingInfo
121 #define _CTYPE_INFO _BIG5CTypeInfo
122 #define _ENCODING_STATE _BIG5State
123 #define _ENCODING_MB_CUR_MAX(_ei_) 2
124 #define _ENCODING_IS_STATE_DEPENDENT 0
125
126
127 static __inline void
128 /*ARGSUSED*/
129 _citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei,
130 _BIG5State * __restrict s)
131 {
132 memset(s, 0, sizeof(*s));
133 }
134
135 static __inline void
136 /*ARGSUSED*/
137 _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei,
138 void * __restrict pspriv,
139 const _BIG5State * __restrict s)
140 {
141 memcpy(pspriv, (const void *)s, sizeof(*s));
142 }
143
144 static __inline void
145 /*ARGSUSED*/
146 _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei,
147 _BIG5State * __restrict s,
148 const void * __restrict pspriv)
149 {
150 memcpy((void *)s, pspriv, sizeof(*s));
151 }
152
153 static __inline int
154 _citrus_BIG5_check(u_int c)
155 {
156 c &= 0xff;
157 return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1);
158 }
159
160 static __inline int
161 _citrus_BIG5_check2(u_int c)
162 {
163 c &= 0xff;
164 if ((c >= 0x40 && c <= 0x7f) || (c >= 0xa1 && c <= 0xfe))
165 return 1;
166 else
167 return 0;
168 }
169
170 static int
171 /*ARGSUSED*/
172 _citrus_BIG5_stdencoding_init(_BIG5EncodingInfo * __restrict ei,
173 const void * __restrict var, size_t lenvar)
174 {
175 _DIAGASSERT(ei != NULL);
176
177 memset((void *)ei, 0, sizeof(*ei));
178
179 return (0);
180 }
181
182 static void
183 /*ARGSUSED*/
184 _citrus_BIG5_stdencoding_uninit(_BIG5EncodingInfo *ei)
185 {
186 }
187
188 static int
189 /*ARGSUSED*/
190 _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
191 wchar_t * __restrict pwc,
192 const char ** __restrict s, size_t n,
193 _BIG5State * __restrict psenc,
194 size_t * __restrict nresult)
195 {
196 wchar_t wchar;
197 int c;
198 int chlenbak;
199 const char *s0;
200
201 _DIAGASSERT(nresult != 0);
202 _DIAGASSERT(ei != NULL);
203 _DIAGASSERT(psenc != NULL);
204 _DIAGASSERT(s != NULL && *s != NULL);
205
206 s0 = *s;
207
208 if (s0 == NULL) {
209 _citrus_BIG5_init_state(ei, psenc);
210 *nresult = 0;
211 return (0);
212 }
213
214 chlenbak = psenc->chlen;
215
216 /* make sure we have the first byte in the buffer */
217 switch (psenc->chlen) {
218 case 0:
219 if (n < 1)
220 goto restart;
221 psenc->ch[0] = *s0++;
222 psenc->chlen = 1;
223 n--;
224 break;
225 case 1:
226 break;
227 default:
228 /* illegal state */
229 goto ilseq;
230 }
231
232 c = _citrus_BIG5_check(psenc->ch[0] & 0xff);
233 if (c == 0)
234 goto ilseq;
235 while (psenc->chlen < c) {
236 if (n < 1) {
237 goto restart;
238 }
239 psenc->ch[psenc->chlen] = *s0++;
240 psenc->chlen++;
241 n--;
242 }
243
244 switch (c) {
245 case 1:
246 wchar = psenc->ch[0] & 0xff;
247 break;
248 case 2:
249 if (!_citrus_BIG5_check2(psenc->ch[1] & 0xff))
250 goto ilseq;
251 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
252 break;
253 default:
254 /* illegal state */
255 goto ilseq;
256 }
257
258 *s = s0;
259 psenc->chlen = 0;
260 if (pwc)
261 *pwc = wchar;
262 if (!wchar)
263 *nresult = 0;
264 else
265 *nresult = c - chlenbak;
266
267 return (0);
268
269 ilseq:
270 psenc->chlen = 0;
271 *nresult = (size_t)-1;
272 return (EILSEQ);
273
274 restart:
275 *s = s0;
276 *nresult = (size_t)-2;
277 return (0);
278 }
279
280 static int
281 /*ARGSUSED*/
282 _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
283 char * __restrict s,
284 size_t n, wchar_t wc, _BIG5State * __restrict psenc,
285 size_t * __restrict nresult)
286 {
287 int l;
288
289 _DIAGASSERT(ei != NULL);
290 _DIAGASSERT(nresult != 0);
291 _DIAGASSERT(s != NULL);
292
293 /* check invalid sequence */
294 if (wc & ~0xffff)
295 goto ilseq;
296
297 if (wc & 0x8000) {
298 if (_citrus_BIG5_check((wc >> 8) & 0xff) != 2 ||
299 !_citrus_BIG5_check2(wc & 0xff))
300 goto ilseq;
301 l = 2;
302 } else {
303 if (wc & ~0xff || !_citrus_BIG5_check(wc & 0xff))
304 goto ilseq;
305 l = 1;
306 }
307
308 if (n < l) {
309 /* bound check failure */
310 goto ilseq;
311 }
312
313 if (l == 2) {
314 s[0] = (wc >> 8) & 0xff;
315 s[1] = wc & 0xff;
316 } else
317 s[0] = wc & 0xff;
318
319 *nresult = l;
320
321 return (0);
322
323 ilseq:
324 *nresult = (size_t)-1;
325 return (EILSEQ);
326 }
327
328
329 /* ----------------------------------------------------------------------
330 * public interface for ctype
331 */
332
333 _CITRUS_CTYPE_DECLS(BIG5);
334 _CITRUS_CTYPE_DEF_OPS(BIG5);
335
336 #include "citrus_ctype_template.h"
337