1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
5 * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
6 * Copyright (c) 2004 Tim J. Robbins. All rights reserved.
7 * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
8 * All rights reserved.
9 *
10 * Copyright (c) 2011 The FreeBSD Foundation
11 * All rights reserved.
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/param.h>
38__FBSDID("$FreeBSD$");
39
40#include <errno.h>
41#include <runetype.h>
42#include <stdlib.h>
43#include <string.h>
44#include <wchar.h>
45#include "mblocal.h"
46
47static size_t	_GB2312_mbrtowc(wchar_t * __restrict, const char * __restrict,
48		    size_t, mbstate_t * __restrict);
49static int	_GB2312_mbsinit(const mbstate_t *);
50static size_t	_GB2312_wcrtomb(char * __restrict, wchar_t,
51		    mbstate_t * __restrict);
52static size_t	_GB2312_mbsnrtowcs(wchar_t * __restrict,
53		    const char ** __restrict, size_t, size_t,
54		    mbstate_t * __restrict);
55static size_t	_GB2312_wcsnrtombs(char * __restrict,
56		    const wchar_t ** __restrict, size_t, size_t,
57		    mbstate_t * __restrict);
58
59
60typedef struct {
61	int	count;
62	u_char	bytes[2];
63} _GB2312State;
64
65int
66_GB2312_init(struct xlocale_ctype *l, _RuneLocale *rl)
67{
68
69	l->runes = rl;
70	l->__mbrtowc = _GB2312_mbrtowc;
71	l->__wcrtomb = _GB2312_wcrtomb;
72	l->__mbsinit = _GB2312_mbsinit;
73	l->__mbsnrtowcs = _GB2312_mbsnrtowcs;
74	l->__wcsnrtombs = _GB2312_wcsnrtombs;
75	l->__mb_cur_max = 2;
76	l->__mb_sb_limit = 128;
77	return (0);
78}
79
80static int
81_GB2312_mbsinit(const mbstate_t *ps)
82{
83
84	return (ps == NULL || ((const _GB2312State *)ps)->count == 0);
85}
86
87static int
88_GB2312_check(const char *str, size_t n)
89{
90	const u_char *s = (const u_char *)str;
91
92	if (n == 0)
93		/* Incomplete multibyte sequence */
94		return (-2);
95	if (s[0] >= 0xa1 && s[0] <= 0xfe) {
96		if (n < 2)
97			/* Incomplete multibyte sequence */
98			return (-2);
99		if (s[1] < 0xa1 || s[1] > 0xfe)
100			/* Invalid multibyte sequence */
101			return (-1);
102		return (2);
103	} else if (s[0] & 0x80) {
104		/* Invalid multibyte sequence */
105		return (-1);
106	}
107	return (1);
108}
109
110static size_t
111_GB2312_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
112    mbstate_t * __restrict ps)
113{
114	_GB2312State *gs;
115	wchar_t wc;
116	int i, len, ocount;
117	size_t ncopy;
118
119	gs = (_GB2312State *)ps;
120
121	if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
122		errno = EINVAL;
123		return ((size_t)-1);
124	}
125
126	if (s == NULL) {
127		s = "";
128		n = 1;
129		pwc = NULL;
130	}
131
132	ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
133	memcpy(gs->bytes + gs->count, s, ncopy);
134	ocount = gs->count;
135	gs->count += ncopy;
136	s = (char *)gs->bytes;
137	n = gs->count;
138
139	if ((len = _GB2312_check(s, n)) < 0)
140		return ((size_t)len);
141	wc = 0;
142	i = len;
143	while (i-- > 0)
144		wc = (wc << 8) | (unsigned char)*s++;
145	if (pwc != NULL)
146		*pwc = wc;
147	gs->count = 0;
148	return (wc == L'\0' ? 0 : len - ocount);
149}
150
151static size_t
152_GB2312_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
153{
154	_GB2312State *gs;
155
156	gs = (_GB2312State *)ps;
157
158	if (gs->count != 0) {
159		errno = EINVAL;
160		return ((size_t)-1);
161	}
162
163	if (s == NULL)
164		/* Reset to initial shift state (no-op) */
165		return (1);
166	if (wc & 0x8000) {
167		*s++ = (wc >> 8) & 0xff;
168		*s = wc & 0xff;
169		return (2);
170	}
171	*s = wc & 0xff;
172	return (1);
173}
174
175static size_t
176_GB2312_mbsnrtowcs(wchar_t * __restrict dst,
177    const char ** __restrict src, size_t nms, size_t len,
178    mbstate_t * __restrict ps)
179{
180	return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GB2312_mbrtowc));
181}
182
183static size_t
184_GB2312_wcsnrtombs(char * __restrict dst,
185    const wchar_t ** __restrict src, size_t nwc, size_t len,
186    mbstate_t * __restrict ps)
187{
188	return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GB2312_wcrtomb));
189}
190