xref: /freebsd/lib/libc/locale/big5.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*-
2  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
4  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Paul Borman at Krystal Technologies.
10  *
11  * Copyright (c) 2011 The FreeBSD Foundation
12  * All rights reserved.
13  * Portions of this software were developed by David Chisnall
14  * under sponsorship from the FreeBSD Foundation.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid[] = "@(#)big5.c	8.1 (Berkeley) 6/4/93";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/types.h>
48 #include <errno.h>
49 #include <runetype.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <wchar.h>
53 #include "mblocal.h"
54 
55 extern int __mb_sb_limit;
56 
57 static size_t	_BIG5_mbrtowc(wchar_t * __restrict, const char * __restrict,
58 		    size_t, mbstate_t * __restrict);
59 static int	_BIG5_mbsinit(const mbstate_t *);
60 static size_t	_BIG5_wcrtomb(char * __restrict, wchar_t,
61 		    mbstate_t * __restrict);
62 static size_t	_BIG5_mbsnrtowcs(wchar_t * __restrict,
63 		    const char ** __restrict, size_t, size_t,
64 		    mbstate_t * __restrict);
65 static size_t	_BIG5_wcsnrtombs(char * __restrict,
66 		    const wchar_t ** __restrict, size_t, size_t,
67 		    mbstate_t * __restrict);
68 
69 typedef struct {
70 	wchar_t	ch;
71 } _BIG5State;
72 
73 int
74 _BIG5_init(struct xlocale_ctype *l, _RuneLocale *rl)
75 {
76 
77 	l->__mbrtowc = _BIG5_mbrtowc;
78 	l->__wcrtomb = _BIG5_wcrtomb;
79 	l->__mbsnrtowcs = _BIG5_mbsnrtowcs;
80 	l->__wcsnrtombs = _BIG5_wcsnrtombs;
81 	l->__mbsinit = _BIG5_mbsinit;
82 	l->runes = rl;
83 	l->__mb_cur_max = 2;
84 	l->__mb_sb_limit = 128;
85 	return (0);
86 }
87 
88 static int
89 _BIG5_mbsinit(const mbstate_t *ps)
90 {
91 
92 	return (ps == NULL || ((const _BIG5State *)ps)->ch == 0);
93 }
94 
95 static __inline int
96 _big5_check(u_int c)
97 {
98 
99 	c &= 0xff;
100 	return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1);
101 }
102 
103 static size_t
104 _BIG5_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
105     mbstate_t * __restrict ps)
106 {
107 	_BIG5State *bs;
108 	wchar_t wc;
109 	size_t len;
110 
111 	bs = (_BIG5State *)ps;
112 
113 	if ((bs->ch & ~0xFF) != 0) {
114 		/* Bad conversion state. */
115 		errno = EINVAL;
116 		return ((size_t)-1);
117 	}
118 
119 	if (s == NULL) {
120 		s = "";
121 		n = 1;
122 		pwc = NULL;
123 	}
124 
125 	if (n == 0)
126 		/* Incomplete multibyte sequence */
127 		return ((size_t)-2);
128 
129 	if (bs->ch != 0) {
130 		if (*s == '\0') {
131 			errno = EILSEQ;
132 			return ((size_t)-1);
133 		}
134 		wc = (bs->ch << 8) | (*s & 0xFF);
135 		if (pwc != NULL)
136 			*pwc = wc;
137 		bs->ch = 0;
138 		return (1);
139 	}
140 
141 	len = (size_t)_big5_check(*s);
142 	wc = *s++ & 0xff;
143 	if (len == 2) {
144 		if (n < 2) {
145 			/* Incomplete multibyte sequence */
146 			bs->ch = wc;
147 			return ((size_t)-2);
148 		}
149 		if (*s == '\0') {
150 			errno = EILSEQ;
151 			return ((size_t)-1);
152 		}
153 		wc = (wc << 8) | (*s++ & 0xff);
154 		if (pwc != NULL)
155 			*pwc = wc;
156 		return (2);
157 	} else {
158 		if (pwc != NULL)
159 			*pwc = wc;
160 		return (wc == L'\0' ? 0 : 1);
161 	}
162 }
163 
164 static size_t
165 _BIG5_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
166 {
167 	_BIG5State *bs;
168 
169 	bs = (_BIG5State *)ps;
170 
171 	if (bs->ch != 0) {
172 		errno = EINVAL;
173 		return ((size_t)-1);
174 	}
175 
176 	if (s == NULL)
177 		/* Reset to initial shift state (no-op) */
178 		return (1);
179 	if (wc & 0x8000) {
180 		*s++ = (wc >> 8) & 0xff;
181 		*s = wc & 0xff;
182 		return (2);
183 	}
184 	*s = wc & 0xff;
185 	return (1);
186 }
187 
188 static size_t
189 _BIG5_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
190     size_t nms, size_t len, mbstate_t * __restrict ps)
191 {
192 	return (__mbsnrtowcs_std(dst, src, nms, len, ps, _BIG5_mbrtowc));
193 }
194 
195 static size_t
196 _BIG5_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
197     size_t nwc, size_t len, mbstate_t * __restrict ps)
198 {
199 	return (__wcsnrtombs_std(dst, src, nwc, len, ps, _BIG5_wcrtomb));
200 }
201