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