1/*-
2 * Copyright (c) 2002-2004 Tim J. Robbins
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26/*
27 * PRC National Standard GB 18030-2000 encoding of Chinese text.
28 *
29 * See gb18030(5) for details.
30 */
31
32#include <sys/param.h>
33__FBSDID("$FreeBSD: src/lib/libc/locale/gb18030.c,v 1.8 2007/10/13 16:28:21 ache Exp $");
34
35#include "xlocale_private.h"
36
37#include <errno.h>
38#include <runetype.h>
39#include <stdlib.h>
40#include <string.h>
41#include <wchar.h>
42#include "mblocal.h"
43
44#define GB18030_MB_CUR_MAX	4
45
46static size_t	_GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict,
47		    size_t, mbstate_t * __restrict, locale_t);
48static int	_GB18030_mbsinit(const mbstate_t *, locale_t);
49static size_t	_GB18030_wcrtomb(char * __restrict, wchar_t,
50		    mbstate_t * __restrict, locale_t);
51
52typedef struct {
53	int	count;
54	u_char	bytes[4];
55} _GB18030State;
56
57int
58_GB18030_init(struct __xlocale_st_runelocale *xrl)
59{
60
61	xrl->__mbrtowc = _GB18030_mbrtowc;
62	xrl->__wcrtomb = _GB18030_wcrtomb;
63	xrl->__mbsinit = _GB18030_mbsinit;
64	xrl->__mb_cur_max = GB18030_MB_CUR_MAX;
65	xrl->__mb_sb_limit = 128;
66
67	return (0);
68}
69
70static int
71_GB18030_mbsinit(const mbstate_t *ps, locale_t loc __unused)
72{
73
74	return (ps == NULL || ((const _GB18030State *)ps)->count == 0);
75}
76
77static size_t
78_GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
79    size_t n, mbstate_t * __restrict ps, locale_t loc __unused)
80{
81	_GB18030State *gs;
82	wchar_t wch;
83	int ch, len, ocount;
84	size_t ncopy;
85
86	gs = (_GB18030State *)ps;
87
88	if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
89		errno = EINVAL;
90		return ((size_t)-1);
91	}
92
93	if (s == NULL) {
94		s = "";
95		n = 1;
96		pwc = NULL;
97	}
98
99	ncopy = MIN(MIN(n, GB18030_MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
100	memcpy(gs->bytes + gs->count, s, ncopy);
101	ocount = gs->count;
102	gs->count += ncopy;
103	s = (char *)gs->bytes;
104	n = gs->count;
105
106	if (n == 0)
107		/* Incomplete multibyte sequence */
108		return ((size_t)-2);
109
110	/*
111	 * Single byte:		[00-7f]
112	 * Two byte:		[81-fe][40-7e,80-fe]
113	 * Four byte:		[81-fe][30-39][81-fe][30-39]
114	 */
115	ch = (unsigned char)*s++;
116	if (ch <= 0x7f) {
117		len = 1;
118		wch = ch;
119	} else if (ch >= 0x81 && ch <= 0xfe) {
120		wch = ch;
121		if (n < 2)
122			return ((size_t)-2);
123		ch = (unsigned char)*s++;
124		if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
125			wch = (wch << 8) | ch;
126			len = 2;
127		} else if (ch >= 0x30 && ch <= 0x39) {
128			/*
129			 * Strip high bit off the wide character we will
130			 * eventually output so that it is positive when
131			 * cast to wint_t on 32-bit twos-complement machines.
132			 */
133			wch = ((wch & 0x7f) << 8) | ch;
134			if (n < 3)
135				return ((size_t)-2);
136			ch = (unsigned char)*s++;
137			if (ch < 0x81 || ch > 0xfe)
138				goto ilseq;
139			wch = (wch << 8) | ch;
140			if (n < 4)
141				return ((size_t)-2);
142			ch = (unsigned char)*s++;
143			if (ch < 0x30 || ch > 0x39)
144				goto ilseq;
145			wch = (wch << 8) | ch;
146			len = 4;
147		} else
148			goto ilseq;
149	} else
150		goto ilseq;
151
152	if (pwc != NULL)
153		*pwc = wch;
154	gs->count = 0;
155	return (wch == L'\0' ? 0 : len - ocount);
156ilseq:
157	errno = EILSEQ;
158	return ((size_t)-1);
159}
160
161static size_t
162_GB18030_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps, locale_t loc __unused)
163{
164	_GB18030State *gs;
165	size_t len;
166	int c;
167
168	gs = (_GB18030State *)ps;
169
170	if (gs->count != 0) {
171		errno = EINVAL;
172		return ((size_t)-1);
173	}
174
175	if (s == NULL)
176		/* Reset to initial shift state (no-op) */
177		return (1);
178	if ((wc & ~0x7fffffff) != 0)
179		goto ilseq;
180	if (wc & 0x7f000000) {
181		/* Replace high bit that mbrtowc() removed. */
182		wc |= 0x80000000;
183		c = (wc >> 24) & 0xff;
184		if (c < 0x81 || c > 0xfe)
185			goto ilseq;
186		*s++ = c;
187		c = (wc >> 16) & 0xff;
188		if (c < 0x30 || c > 0x39)
189			goto ilseq;
190		*s++ = c;
191		c = (wc >> 8) & 0xff;
192		if (c < 0x81 || c > 0xfe)
193			goto ilseq;
194		*s++ = c;
195		c = wc & 0xff;
196		if (c < 0x30 || c > 0x39)
197			goto ilseq;
198		*s++ = c;
199		len = 4;
200	} else if (wc & 0x00ff0000)
201		goto ilseq;
202	else if (wc & 0x0000ff00) {
203		c = (wc >> 8) & 0xff;
204		if (c < 0x81 || c > 0xfe)
205			goto ilseq;
206		*s++ = c;
207		c = wc & 0xff;
208		if (c < 0x40 || c == 0x7f || c == 0xff)
209			goto ilseq;
210		*s++ = c;
211		len = 2;
212	} else if (wc <= 0x7f) {
213		*s++ = wc;
214		len = 1;
215	} else
216		goto ilseq;
217
218	return (len);
219ilseq:
220	errno = EILSEQ;
221	return ((size_t)-1);
222}
223