xref: /freebsd/lib/libc/locale/gb2312.c (revision a4e5e0106ac7145f56eb39a691e302cabb4635be)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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  *
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 #include <errno.h>
39 #include <runetype.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <wchar.h>
43 #include "mblocal.h"
44 
45 static size_t	_GB2312_mbrtowc(wchar_t * __restrict, const char * __restrict,
46 		    size_t, mbstate_t * __restrict);
47 static int	_GB2312_mbsinit(const mbstate_t *);
48 static size_t	_GB2312_wcrtomb(char * __restrict, wchar_t,
49 		    mbstate_t * __restrict);
50 static size_t	_GB2312_mbsnrtowcs(wchar_t * __restrict,
51 		    const char ** __restrict, size_t, size_t,
52 		    mbstate_t * __restrict);
53 static size_t	_GB2312_wcsnrtombs(char * __restrict,
54 		    const wchar_t ** __restrict, size_t, size_t,
55 		    mbstate_t * __restrict);
56 
57 
58 typedef struct {
59 	int	count;
60 	u_char	bytes[2];
61 } _GB2312State;
62 
63 int
64 _GB2312_init(struct xlocale_ctype *l, _RuneLocale *rl)
65 {
66 
67 	l->runes = rl;
68 	l->__mbrtowc = _GB2312_mbrtowc;
69 	l->__wcrtomb = _GB2312_wcrtomb;
70 	l->__mbsinit = _GB2312_mbsinit;
71 	l->__mbsnrtowcs = _GB2312_mbsnrtowcs;
72 	l->__wcsnrtombs = _GB2312_wcsnrtombs;
73 	l->__mb_cur_max = 2;
74 	l->__mb_sb_limit = 128;
75 	return (0);
76 }
77 
78 static int
79 _GB2312_mbsinit(const mbstate_t *ps)
80 {
81 
82 	return (ps == NULL || ((const _GB2312State *)ps)->count == 0);
83 }
84 
85 static int
86 _GB2312_check(const char *str, size_t n)
87 {
88 	const u_char *s = (const u_char *)str;
89 
90 	if (n == 0)
91 		/* Incomplete multibyte sequence */
92 		return (-2);
93 	if (s[0] >= 0xa1 && s[0] <= 0xfe) {
94 		if (n < 2)
95 			/* Incomplete multibyte sequence */
96 			return (-2);
97 		if (s[1] < 0xa1 || s[1] > 0xfe)
98 			/* Invalid multibyte sequence */
99 			return (-1);
100 		return (2);
101 	} else if (s[0] & 0x80) {
102 		/* Invalid multibyte sequence */
103 		return (-1);
104 	}
105 	return (1);
106 }
107 
108 static size_t
109 _GB2312_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
110     mbstate_t * __restrict ps)
111 {
112 	_GB2312State *gs;
113 	wchar_t wc;
114 	int i, len, ocount;
115 	size_t ncopy;
116 
117 	gs = (_GB2312State *)ps;
118 
119 	if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
120 		errno = EINVAL;
121 		return ((size_t)-1);
122 	}
123 
124 	if (s == NULL) {
125 		s = "";
126 		n = 1;
127 		pwc = NULL;
128 	}
129 
130 	ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
131 	memcpy(gs->bytes + gs->count, s, ncopy);
132 	ocount = gs->count;
133 	gs->count += ncopy;
134 	s = (char *)gs->bytes;
135 	n = gs->count;
136 
137 	if ((len = _GB2312_check(s, n)) < 0)
138 		return ((size_t)len);
139 	wc = 0;
140 	i = len;
141 	while (i-- > 0)
142 		wc = (wc << 8) | (unsigned char)*s++;
143 	if (pwc != NULL)
144 		*pwc = wc;
145 	gs->count = 0;
146 	return (wc == L'\0' ? 0 : len - ocount);
147 }
148 
149 static size_t
150 _GB2312_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
151 {
152 	_GB2312State *gs;
153 
154 	gs = (_GB2312State *)ps;
155 
156 	if (gs->count != 0) {
157 		errno = EINVAL;
158 		return ((size_t)-1);
159 	}
160 
161 	if (s == NULL)
162 		/* Reset to initial shift state (no-op) */
163 		return (1);
164 	if (wc & 0x8000) {
165 		*s++ = (wc >> 8) & 0xff;
166 		*s = wc & 0xff;
167 		return (2);
168 	}
169 	*s = wc & 0xff;
170 	return (1);
171 }
172 
173 static size_t
174 _GB2312_mbsnrtowcs(wchar_t * __restrict dst,
175     const char ** __restrict src, size_t nms, size_t len,
176     mbstate_t * __restrict ps)
177 {
178 	return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GB2312_mbrtowc));
179 }
180 
181 static size_t
182 _GB2312_wcsnrtombs(char * __restrict dst,
183     const wchar_t ** __restrict src, size_t nwc, size_t len,
184     mbstate_t * __restrict ps)
185 {
186 	return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GB2312_wcrtomb));
187 }
188