Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / citrus / modules / citrus_gbk2k.c
blobdb9f8277462f1952394a5dec3da0f9c487a7e5ba
1 /* $NetBSD: citrus_gbk2k.c,v 1.6 2006/02/15 19:50:27 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_gbk2k.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
4 /*-
5 * Copyright (c)2003 Citrus Project,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <locale.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_bcs.h"
44 #include "citrus_module.h"
45 #include "citrus_ctype.h"
46 #include "citrus_stdenc.h"
47 #include "citrus_gbk2k.h"
50 /* ----------------------------------------------------------------------
51 * private stuffs used by templates
54 typedef struct _GBK2KState {
55 char ch[4];
56 int chlen;
57 } _GBK2KState;
59 typedef struct {
60 int mb_cur_max;
61 } _GBK2KEncodingInfo;
63 typedef struct {
64 _GBK2KEncodingInfo ei;
65 struct {
66 /* for future multi-locale facility */
67 _GBK2KState s_mblen;
68 _GBK2KState s_mbrlen;
69 _GBK2KState s_mbrtowc;
70 _GBK2KState s_mbtowc;
71 _GBK2KState s_mbsrtowcs;
72 _GBK2KState s_wcrtomb;
73 _GBK2KState s_wcsrtombs;
74 _GBK2KState s_wctomb;
75 } states;
76 } _GBK2KCTypeInfo;
78 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
79 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
81 #define _FUNCNAME(m) _citrus_GBK2K_##m
82 #define _ENCODING_INFO _GBK2KEncodingInfo
83 #define _CTYPE_INFO _GBK2KCTypeInfo
84 #define _ENCODING_STATE _GBK2KState
85 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
86 #define _ENCODING_IS_STATE_DEPENDENT 0
87 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
89 static __inline void
90 /*ARGSUSED*/
91 _citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei,
92 _GBK2KState * __restrict s)
94 memset(s, 0, sizeof(*s));
97 static __inline void
98 /*ARGSUSED*/
99 _citrus_GBK2K_pack_state(_GBK2KEncodingInfo * __restrict ei,
100 void * __restrict pspriv,
101 const _GBK2KState * __restrict s)
103 memcpy(pspriv, (const void *)s, sizeof(*s));
106 static __inline void
107 /*ARGSUSED*/
108 _citrus_GBK2K_unpack_state(_GBK2KEncodingInfo * __restrict ei,
109 _GBK2KState * __restrict s,
110 const void * __restrict pspriv)
112 memcpy((void *)s, pspriv, sizeof(*s));
115 static __inline int
116 _mb_singlebyte(int c)
118 c &= 0xff;
119 return (c <= 0x7f);
122 static __inline int
123 _mb_leadbyte(int c)
125 c &= 0xff;
126 return (0x81 <= c && c <= 0xfe);
129 static __inline int
130 _mb_trailbyte(int c)
132 c &= 0xff;
133 return ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfe));
136 static __inline int
137 _mb_surrogate(int c)
139 c &= 0xff;
140 return (0x30 <= c && c <= 0x39);
143 static __inline int
144 _mb_count(wchar_t v)
146 u_int32_t c;
148 c = (u_int32_t)v; /* XXX */
149 if (!(c & 0xffffff00))
150 return (1);
151 if (!(c & 0xffff0000))
152 return (2);
153 return (4);
156 #define _PSENC (psenc->ch[psenc->chlen - 1])
157 #define _PUSH_PSENC(c) (psenc->ch[psenc->chlen++] = (c))
159 static int
160 _citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei,
161 wchar_t * __restrict pwc,
162 const char ** __restrict s, size_t n,
163 _GBK2KState * __restrict psenc,
164 size_t * __restrict nresult)
166 int chlenbak, len;
167 const char *s0, *s1;
168 wchar_t wc;
170 _DIAGASSERT(ei != NULL);
171 /* pwc may be NULL */
172 _DIAGASSERT(s != NULL);
173 _DIAGASSERT(psenc != NULL);
175 s0 = *s;
177 if (s0 == NULL) {
178 /* _citrus_GBK2K_init_state(ei, psenc); */
179 psenc->chlen = 0;
180 *nresult = 0;
181 return (0);
184 chlenbak = psenc->chlen;
186 switch (psenc->chlen) {
187 case 3:
188 if (!_mb_leadbyte (_PSENC))
189 goto invalid;
190 /* FALLTHROUGH */
191 case 2:
192 if (!_mb_surrogate(_PSENC) || _mb_trailbyte(_PSENC))
193 goto invalid;
194 /* FALLTHROUGH */
195 case 1:
196 if (!_mb_leadbyte (_PSENC))
197 goto invalid;
198 /* FALLTHOROUGH */
199 case 0:
200 break;
201 default:
202 goto invalid;
205 for (;;) {
206 if (n-- < 1)
207 goto restart;
209 _PUSH_PSENC(*s0++);
211 switch (psenc->chlen) {
212 case 1:
213 if (_mb_singlebyte(_PSENC))
214 goto convert;
215 if (_mb_leadbyte (_PSENC))
216 continue;
217 goto ilseq;
218 case 2:
219 if (_mb_trailbyte (_PSENC))
220 goto convert;
221 if (ei->mb_cur_max == 4 &&
222 _mb_surrogate (_PSENC))
223 continue;
224 goto ilseq;
225 case 3:
226 if (_mb_leadbyte (_PSENC))
227 continue;
228 goto ilseq;
229 case 4:
230 if (_mb_surrogate (_PSENC))
231 goto convert;
232 goto ilseq;
236 convert:
237 len = psenc->chlen;
238 s1 = &psenc->ch[0];
239 wc = 0;
240 while (len-- > 0)
241 wc = (wc << 8) | (*s1++ & 0xff);
243 if (pwc != NULL)
244 *pwc = wc;
245 *s = s0;
246 *nresult = (wc == 0) ? 0 : psenc->chlen - chlenbak;
247 /* _citrus_GBK2K_init_state(ei, psenc); */
248 psenc->chlen = 0;
250 return (0);
252 restart:
253 *s = s0;
254 *nresult = (size_t)-2;
256 return (0);
258 invalid:
259 return (EINVAL);
261 ilseq:
262 *nresult = (size_t)-1;
263 return (EILSEQ);
266 static int
267 _citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei,
268 char * __restrict s, size_t n, wchar_t wc,
269 _GBK2KState * __restrict psenc,
270 size_t * __restrict nresult)
272 int len, ret;
274 _DIAGASSERT(ei != NULL);
275 _DIAGASSERT(s != NULL);
276 _DIAGASSERT(psenc != NULL);
278 if (psenc->chlen != 0) {
279 ret = EINVAL;
280 goto err;
283 len = _mb_count(wc);
284 if (n < len) {
285 ret = E2BIG;
286 goto err;
289 switch (len) {
290 case 1:
291 if (!_mb_singlebyte(_PUSH_PSENC(wc ))) {
292 ret = EILSEQ;
293 goto err;
295 break;
296 case 2:
297 if (!_mb_leadbyte (_PUSH_PSENC(wc >> 8)) ||
298 !_mb_trailbyte (_PUSH_PSENC(wc ))) {
299 ret = EILSEQ;
300 goto err;
302 break;
303 case 4:
304 if (ei->mb_cur_max != 4 ||
305 !_mb_leadbyte (_PUSH_PSENC(wc >> 24)) ||
306 !_mb_surrogate (_PUSH_PSENC(wc >> 16)) ||
307 !_mb_leadbyte (_PUSH_PSENC(wc >> 8)) ||
308 !_mb_surrogate (_PUSH_PSENC(wc ))) {
309 ret = EILSEQ;
310 goto err;
312 break;
315 _DIAGASSERT(len == psenc->chlen);
317 memcpy(s, psenc->ch, psenc->chlen);
318 *nresult = psenc->chlen;
319 /* _citrus_GBK2K_init_state(ei, psenc); */
320 psenc->chlen = 0;
322 return (0);
324 err:
325 *nresult = (size_t)-1;
326 return ret;
329 static __inline int
330 /*ARGSUSED*/
331 _citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei,
332 _csid_t * __restrict csid,
333 _index_t * __restrict idx, wchar_t wc)
335 u_int8_t ch, cl;
337 _DIAGASSERT(csid != NULL && idx != NULL);
339 if ((u_int32_t)wc<0x80) {
340 /* ISO646 */
341 *csid = 0;
342 *idx = (_index_t)wc;
343 } else if ((u_int32_t)wc>=0x10000) {
344 /* GBKUCS : XXX */
345 *csid = 3;
346 *idx = (_index_t)wc;
347 } else {
348 ch = (u_int8_t)(wc >> 8);
349 cl = (u_int8_t)wc;
350 if (ch>=0xA1 && cl>=0xA1) {
351 /* EUC G1 */
352 *csid = 1;
353 *idx = (_index_t)wc & 0x7F7FU;
354 } else {
355 /* extended area (0x8140-) */
356 *csid = 2;
357 *idx = (_index_t)wc;
361 return 0;
364 static __inline int
365 /*ARGSUSED*/
366 _citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei,
367 wchar_t * __restrict wc,
368 _csid_t csid, _index_t idx)
371 _DIAGASSERT(wc != NULL);
373 switch (csid) {
374 case 0:
375 /* ISO646 */
376 *wc = (wchar_t)idx;
377 break;
378 case 1:
379 /* EUC G1 */
380 *wc = (wchar_t)idx | 0x8080U;
381 break;
382 case 2:
383 /* extended area */
384 *wc = (wchar_t)idx;
385 break;
386 case 3:
387 /* GBKUCS : XXX */
388 if (ei->mb_cur_max != 4)
389 return EINVAL;
390 *wc = (wchar_t)idx;
391 break;
392 default:
393 return EILSEQ;
396 return 0;
399 static __inline int
400 /*ARGSUSED*/
401 _citrus_GBK2K_stdenc_get_state_desc_generic(_GBK2KEncodingInfo * __restrict ei,
402 _GBK2KState * __restrict psenc,
403 int * __restrict rstate)
406 if (psenc->chlen == 0)
407 *rstate = _STDENC_SDGEN_INITIAL;
408 else
409 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
411 return 0;
414 static int
415 /*ARGSUSED*/
416 _citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei,
417 const void * __restrict var, size_t lenvar)
419 const char *p;
421 _DIAGASSERT(ei != NULL);
423 p = var;
424 #define MATCH(x, act) \
425 do { \
426 if (lenvar >= (sizeof(#x)-1) && \
427 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
428 act; \
429 lenvar -= sizeof(#x)-1; \
430 p += sizeof(#x)-1; \
432 } while (/*CONSTCOND*/0)
433 memset((void *)ei, 0, sizeof(*ei));
434 ei->mb_cur_max = 4;
435 while (lenvar>0) {
436 switch (_bcs_tolower(*p)) {
437 case '2':
438 MATCH("2byte", ei->mb_cur_max = 2);
439 break;
441 p++;
442 lenvar--;
445 return (0);
448 static void
449 /*ARGSUSED*/
450 _citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo *ei)
455 /* ----------------------------------------------------------------------
456 * public interface for ctype
459 _CITRUS_CTYPE_DECLS(GBK2K);
460 _CITRUS_CTYPE_DEF_OPS(GBK2K);
462 #include "citrus_ctype_template.h"
464 /* ----------------------------------------------------------------------
465 * public interface for stdenc
468 _CITRUS_STDENC_DECLS(GBK2K);
469 _CITRUS_STDENC_DEF_OPS(GBK2K);
471 #include "citrus_stdenc_template.h"