1145256Sjkoshy/*-
2177107Sjkoshy * Copyright (c) 2002-2004 Tim J. Robbins
3145256Sjkoshy * All rights reserved.
4145256Sjkoshy *
5145256Sjkoshy * Copyright (c) 2011 The FreeBSD Foundation
6145256Sjkoshy * All rights reserved.
7145256Sjkoshy * Portions of this software were developed by David Chisnall
8145256Sjkoshy * under sponsorship from the FreeBSD Foundation.
9145256Sjkoshy *
10145256Sjkoshy * Redistribution and use in source and binary forms, with or without
11145256Sjkoshy * modification, are permitted provided that the following conditions
12145256Sjkoshy * are met:
13145256Sjkoshy * 1. Redistributions of source code must retain the above copyright
14145256Sjkoshy *    notice, this list of conditions and the following disclaimer.
15145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
16145256Sjkoshy *    notice, this list of conditions and the following disclaimer in the
17145256Sjkoshy *    documentation and/or other materials provided with the distribution.
18145256Sjkoshy *
19145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22145256Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29145256Sjkoshy * SUCH DAMAGE.
30145256Sjkoshy */
31145256Sjkoshy/*
32145256Sjkoshy * PRC National Standard GB 18030-2000 encoding of Chinese text.
33145256Sjkoshy *
34145256Sjkoshy * See gb18030(5) for details.
35145256Sjkoshy */
36145256Sjkoshy
37145256Sjkoshy#include <sys/param.h>
38145256Sjkoshy__FBSDID("$FreeBSD$");
39145256Sjkoshy
40145256Sjkoshy#include <errno.h>
41145256Sjkoshy#include <runetype.h>
42145256Sjkoshy#include <stdlib.h>
43145256Sjkoshy#include <string.h>
44145256Sjkoshy#include <wchar.h>
45185363Sjkoshy#include "mblocal.h"
46185363Sjkoshy
47145256Sjkoshystatic size_t	_GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict,
48145340Smarcel		    size_t, mbstate_t * __restrict);
49145256Sjkoshystatic int	_GB18030_mbsinit(const mbstate_t *);
50145256Sjkoshystatic size_t	_GB18030_wcrtomb(char * __restrict, wchar_t,
51147191Sjkoshy		    mbstate_t * __restrict);
52147759Sjkoshy
53185363Sjkoshytypedef struct {
54185363Sjkoshy	int	count;
55185363Sjkoshy	u_char	bytes[4];
56185363Sjkoshy} _GB18030State;
57206089Sfabient
58206089Sfabientint
59206089Sfabient_GB18030_init(struct xlocale_ctype *l, _RuneLocale *rl)
60206089Sfabient{
61147191Sjkoshy
62145256Sjkoshy	l->__mbrtowc = _GB18030_mbrtowc;
63147759Sjkoshy	l->__wcrtomb = _GB18030_wcrtomb;
64147759Sjkoshy	l->__mbsinit = _GB18030_mbsinit;
65147191Sjkoshy	l->runes = rl;
66147191Sjkoshy	l->__mb_cur_max = 4;
67145256Sjkoshy	l->__mb_sb_limit = 128;
68145256Sjkoshy
69147191Sjkoshy	return (0);
70145256Sjkoshy}
71145256Sjkoshy
72183725Sjkoshystatic int
73183725Sjkoshy_GB18030_mbsinit(const mbstate_t *ps)
74183725Sjkoshy{
75183725Sjkoshy
76200928Srpaulo	return (ps == NULL || ((const _GB18030State *)ps)->count == 0);
77200928Srpaulo}
78200928Srpaulo
79200928Srpaulostatic size_t
80204635Sgnn_GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
81233320Sgonzo    size_t n, mbstate_t * __restrict ps)
82204635Sgnn{
83204635Sgnn	_GB18030State *gs;
84233628Sfabient	wchar_t wch;
85233628Sfabient	int ch, len, ocount;
86204635Sgnn	size_t ncopy;
87228869Sjhibbits
88228869Sjhibbits	gs = (_GB18030State *)ps;
89228869Sjhibbits
90228869Sjhibbits	if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
91204635Sgnn		errno = EINVAL;
92145256Sjkoshy		return ((size_t)-1);
93145256Sjkoshy	}
94145256Sjkoshy
95145256Sjkoshy	if (s == NULL) {
96145256Sjkoshy		s = "";
97145256Sjkoshy		n = 1;
98145256Sjkoshy		pwc = NULL;
99145256Sjkoshy	}
100145256Sjkoshy
101145256Sjkoshy	ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
102145256Sjkoshy	memcpy(gs->bytes + gs->count, s, ncopy);
103145256Sjkoshy	ocount = gs->count;
104145256Sjkoshy	gs->count += ncopy;
105145256Sjkoshy	s = (char *)gs->bytes;
106145256Sjkoshy	n = gs->count;
107145256Sjkoshy
108145256Sjkoshy	if (n == 0)
109183725Sjkoshy		/* Incomplete multibyte sequence */
110145256Sjkoshy		return ((size_t)-2);
111145256Sjkoshy
112145256Sjkoshy	/*
113145256Sjkoshy	 * Single byte:		[00-7f]
114145256Sjkoshy	 * Two byte:		[81-fe][40-7e,80-fe]
115145256Sjkoshy	 * Four byte:		[81-fe][30-39][81-fe][30-39]
116145256Sjkoshy	 */
117183725Sjkoshy	ch = (unsigned char)*s++;
118183725Sjkoshy	if (ch <= 0x7f) {
119183725Sjkoshy		len = 1;
120183725Sjkoshy		wch = ch;
121183725Sjkoshy	} else if (ch >= 0x81 && ch <= 0xfe) {
122183725Sjkoshy		wch = ch;
123183725Sjkoshy		if (n < 2)
124183725Sjkoshy			return ((size_t)-2);
125183725Sjkoshy		ch = (unsigned char)*s++;
126183725Sjkoshy		if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
127183725Sjkoshy			wch = (wch << 8) | ch;
128183725Sjkoshy			len = 2;
129183725Sjkoshy		} else if (ch >= 0x30 && ch <= 0x39) {
130183725Sjkoshy			/*
131183725Sjkoshy			 * Strip high bit off the wide character we will
132183725Sjkoshy			 * eventually output so that it is positive when
133183725Sjkoshy			 * cast to wint_t on 32-bit twos-complement machines.
134183725Sjkoshy			 */
135183725Sjkoshy			wch = ((wch & 0x7f) << 8) | ch;
136183725Sjkoshy			if (n < 3)
137183725Sjkoshy				return ((size_t)-2);
138185363Sjkoshy			ch = (unsigned char)*s++;
139183725Sjkoshy			if (ch < 0x81 || ch > 0xfe)
140185363Sjkoshy				goto ilseq;
141183725Sjkoshy			wch = (wch << 8) | ch;
142185363Sjkoshy			if (n < 4)
143183725Sjkoshy				return ((size_t)-2);
144183725Sjkoshy			ch = (unsigned char)*s++;
145183725Sjkoshy			if (ch < 0x30 || ch > 0x39)
146185363Sjkoshy				goto ilseq;
147185363Sjkoshy			wch = (wch << 8) | ch;
148185363Sjkoshy			len = 4;
149185363Sjkoshy		} else
150185363Sjkoshy			goto ilseq;
151185363Sjkoshy	} else
152185363Sjkoshy		goto ilseq;
153185363Sjkoshy
154200928Srpaulo	if (pwc != NULL)
155204635Sgnn		*pwc = wch;
156233335Sgonzo	gs->count = 0;
157206089Sfabient	return (wch == L'\0' ? 0 : len - ocount);
158228869Sjhibbitsilseq:
159185363Sjkoshy	errno = EILSEQ;
160233628Sfabient	return ((size_t)-1);
161233628Sfabient}
162185363Sjkoshy
163185363Sjkoshystatic size_t
164185363Sjkoshy_GB18030_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
165185363Sjkoshy{
166185363Sjkoshy	_GB18030State *gs;
167185363Sjkoshy	size_t len;
168185363Sjkoshy	int c;
169185363Sjkoshy
170185363Sjkoshy	gs = (_GB18030State *)ps;
171185363Sjkoshy
172185363Sjkoshy	if (gs->count != 0) {
173185363Sjkoshy		errno = EINVAL;
174185363Sjkoshy		return ((size_t)-1);
175185363Sjkoshy	}
176185363Sjkoshy
177185363Sjkoshy	if (s == NULL)
178185363Sjkoshy		/* Reset to initial shift state (no-op) */
179185363Sjkoshy		return (1);
180185363Sjkoshy	if ((wc & ~0x7fffffff) != 0)
181187761Sjeff		goto ilseq;
182187761Sjeff	if (wc & 0x7f000000) {
183187761Sjeff		/* Replace high bit that mbrtowc() removed. */
184187761Sjeff		wc |= 0x80000000;
185187761Sjeff		c = (wc >> 24) & 0xff;
186240164Sfabient		if (c < 0x81 || c > 0xfe)
187240164Sfabient			goto ilseq;
188240164Sfabient		*s++ = c;
189240164Sfabient		c = (wc >> 16) & 0xff;
190240164Sfabient		if (c < 0x30 || c > 0x39)
191232366Sdavide			goto ilseq;
192232366Sdavide		*s++ = c;
193232366Sdavide		c = (wc >> 8) & 0xff;
194232366Sdavide		if (c < 0x81 || c > 0xfe)
195232366Sdavide			goto ilseq;
196241738Ssbruno		*s++ = c;
197241738Ssbruno		c = wc & 0xff;
198241738Ssbruno		if (c < 0x30 || c > 0x39)
199241738Ssbruno			goto ilseq;
200241738Ssbruno		*s++ = c;
201206089Sfabient		len = 4;
202206089Sfabient	} else if (wc & 0x00ff0000)
203206089Sfabient		goto ilseq;
204206089Sfabient	else if (wc & 0x0000ff00) {
205206089Sfabient		c = (wc >> 8) & 0xff;
206206089Sfabient		if (c < 0x81 || c > 0xfe)
207206089Sfabient			goto ilseq;
208206089Sfabient		*s++ = c;
209206089Sfabient		c = wc & 0xff;
210206089Sfabient		if (c < 0x40 || c == 0x7f || c == 0xff)
211232366Sdavide			goto ilseq;
212232366Sdavide		*s++ = c;
213232366Sdavide		len = 2;
214232366Sdavide	} else if (wc <= 0x7f) {
215232366Sdavide		*s++ = wc;
216206089Sfabient		len = 1;
217206089Sfabient	} else
218206089Sfabient		goto ilseq;
219206089Sfabient
220206089Sfabient	return (len);
221185363Sjkoshyilseq:
222185363Sjkoshy	errno = EILSEQ;
223185363Sjkoshy	return ((size_t)-1);
224185363Sjkoshy}
225185363Sjkoshy