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