2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
5 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
6 * Copyright (c) 2002-2004 Tim J. Robbins
9 * Copyright (c) 2011 The FreeBSD Foundation
10 * All rights reserved.
11 * Portions of this software were developed by David Chisnall
12 * under sponsorship from the FreeBSD Foundation.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * PRC National Standard GB 18030-2000 encoding of Chinese text.
39 * See gb18030(5) for details.
42 #include <sys/param.h>
43 __FBSDID("$FreeBSD$");
52 static size_t _GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict,
53 size_t, mbstate_t * __restrict);
54 static int _GB18030_mbsinit(const mbstate_t *);
55 static size_t _GB18030_wcrtomb(char * __restrict, wchar_t,
56 mbstate_t * __restrict);
57 static size_t _GB18030_mbsnrtowcs(wchar_t * __restrict,
58 const char ** __restrict, size_t, size_t,
59 mbstate_t * __restrict);
60 static size_t _GB18030_wcsnrtombs(char * __restrict,
61 const wchar_t ** __restrict, size_t, size_t,
62 mbstate_t * __restrict);
71 _GB18030_init(struct xlocale_ctype *l, _RuneLocale *rl)
74 l->__mbrtowc = _GB18030_mbrtowc;
75 l->__wcrtomb = _GB18030_wcrtomb;
76 l->__mbsinit = _GB18030_mbsinit;
77 l->__mbsnrtowcs = _GB18030_mbsnrtowcs;
78 l->__wcsnrtombs = _GB18030_wcsnrtombs;
81 l->__mb_sb_limit = 128;
87 _GB18030_mbsinit(const mbstate_t *ps)
90 return (ps == NULL || ((const _GB18030State *)ps)->count == 0);
94 _GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
95 size_t n, mbstate_t * __restrict ps)
102 gs = (_GB18030State *)ps;
104 if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
115 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
116 memcpy(gs->bytes + gs->count, s, ncopy);
119 s = (char *)gs->bytes;
123 /* Incomplete multibyte sequence */
127 * Single byte: [00-7f]
128 * Two byte: [81-fe][40-7e,80-fe]
129 * Four byte: [81-fe][30-39][81-fe][30-39]
131 ch = (unsigned char)*s++;
135 } else if (ch >= 0x81 && ch <= 0xfe) {
139 ch = (unsigned char)*s++;
140 if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
141 wch = (wch << 8) | ch;
143 } else if (ch >= 0x30 && ch <= 0x39) {
145 * Strip high bit off the wide character we will
146 * eventually output so that it is positive when
147 * cast to wint_t on 32-bit twos-complement machines.
149 wch = ((wch & 0x7f) << 8) | ch;
152 ch = (unsigned char)*s++;
153 if (ch < 0x81 || ch > 0xfe)
155 wch = (wch << 8) | ch;
158 ch = (unsigned char)*s++;
159 if (ch < 0x30 || ch > 0x39)
161 wch = (wch << 8) | ch;
171 return (wch == L'\0' ? 0 : len - ocount);
178 _GB18030_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
184 gs = (_GB18030State *)ps;
186 if (gs->count != 0) {
192 /* Reset to initial shift state (no-op) */
194 if ((wc & ~0x7fffffff) != 0)
196 if (wc & 0x7f000000) {
197 /* Replace high bit that mbrtowc() removed. */
199 c = (wc >> 24) & 0xff;
200 if (c < 0x81 || c > 0xfe)
203 c = (wc >> 16) & 0xff;
204 if (c < 0x30 || c > 0x39)
207 c = (wc >> 8) & 0xff;
208 if (c < 0x81 || c > 0xfe)
212 if (c < 0x30 || c > 0x39)
216 } else if (wc & 0x00ff0000)
218 else if (wc & 0x0000ff00) {
219 c = (wc >> 8) & 0xff;
220 if (c < 0x81 || c > 0xfe)
224 if (c < 0x40 || c == 0x7f || c == 0xff)
228 } else if (wc <= 0x7f) {
241 _GB18030_mbsnrtowcs(wchar_t * __restrict dst,
242 const char ** __restrict src, size_t nms, size_t len,
243 mbstate_t * __restrict ps)
245 return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GB18030_mbrtowc));
249 _GB18030_wcsnrtombs(char * __restrict dst,
250 const wchar_t ** __restrict src, size_t nwc, size_t len,
251 mbstate_t * __restrict ps)
253 return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GB18030_wcrtomb));