2 * Copyright (c) 2002-2004 Tim J. Robbins
5 * Copyright (c) 2011 The FreeBSD Foundation
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: head/lib/libc/locale/gb18030.c 227753 2011-11-20 14:45:42Z theraven $
34 * PRC National Standard GB 18030-2000 encoding of Chinese text.
36 * See gb18030(5) for details.
39 #include <sys/param.h>
48 static size_t _GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict,
49 size_t, mbstate_t * __restrict);
50 static int _GB18030_mbsinit(const mbstate_t *);
51 static size_t _GB18030_wcrtomb(char * __restrict, wchar_t,
52 mbstate_t * __restrict);
60 _GB18030_init(struct xlocale_ctype *l, _RuneLocale *rl)
63 l->__mbrtowc = _GB18030_mbrtowc;
64 l->__wcrtomb = _GB18030_wcrtomb;
65 l->__mbsinit = _GB18030_mbsinit;
68 l->__mb_sb_limit = 128;
74 _GB18030_mbsinit(const mbstate_t *ps)
77 return (ps == NULL || ((const _GB18030State *)ps)->count == 0);
81 _GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
82 size_t n, mbstate_t * __restrict ps)
89 gs = (_GB18030State *)ps;
91 if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
102 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
103 memcpy(gs->bytes + gs->count, s, ncopy);
106 s = (char *)gs->bytes;
110 /* Incomplete multibyte sequence */
114 * Single byte: [00-7f]
115 * Two byte: [81-fe][40-7e,80-fe]
116 * Four byte: [81-fe][30-39][81-fe][30-39]
118 ch = (unsigned char)*s++;
122 } else if (ch >= 0x81 && ch <= 0xfe) {
126 ch = (unsigned char)*s++;
127 if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
128 wch = (wch << 8) | ch;
130 } else if (ch >= 0x30 && ch <= 0x39) {
132 * Strip high bit off the wide character we will
133 * eventually output so that it is positive when
134 * cast to wint_t on 32-bit twos-complement machines.
136 wch = ((wch & 0x7f) << 8) | ch;
139 ch = (unsigned char)*s++;
140 if (ch < 0x81 || ch > 0xfe)
142 wch = (wch << 8) | ch;
145 ch = (unsigned char)*s++;
146 if (ch < 0x30 || ch > 0x39)
148 wch = (wch << 8) | ch;
158 return (wch == L'\0' ? 0 : len - ocount);
165 _GB18030_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
171 gs = (_GB18030State *)ps;
173 if (gs->count != 0) {
179 /* Reset to initial shift state (no-op) */
181 if ((wc & ~0x7fffffff) != 0)
183 if (wc & 0x7f000000) {
184 /* Replace high bit that mbrtowc() removed. */
186 c = (wc >> 24) & 0xff;
187 if (c < 0x81 || c > 0xfe)
190 c = (wc >> 16) & 0xff;
191 if (c < 0x30 || c > 0x39)
194 c = (wc >> 8) & 0xff;
195 if (c < 0x81 || c > 0xfe)
199 if (c < 0x30 || c > 0x39)
203 } else if (wc & 0x00ff0000)
205 else if (wc & 0x0000ff00) {
206 c = (wc >> 8) & 0xff;
207 if (c < 0x81 || c > 0xfe)
211 if (c < 0x40 || c == 0x7f || c == 0xff)
215 } else if (wc <= 0x7f) {