138333Sphk/*-
2128004Stjr * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
338333Sphk * Copyright (c) 1993
438333Sphk *	The Regents of the University of California.  All rights reserved.
538333Sphk *
638333Sphk * This code is derived from software contributed to Berkeley by
738333Sphk * Paul Borman at Krystal Technologies.
838333Sphk *
9227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
10227753Stheraven * All rights reserved.
11227753Stheraven * Portions of this software were developed by David Chisnall
12227753Stheraven * under sponsorship from the FreeBSD Foundation.
13227753Stheraven *
1438333Sphk * Redistribution and use in source and binary forms, with or without
1538333Sphk * modification, are permitted provided that the following conditions
1638333Sphk * are met:
1738333Sphk * 1. Redistributions of source code must retain the above copyright
1838333Sphk *    notice, this list of conditions and the following disclaimer.
1938333Sphk * 2. Redistributions in binary form must reproduce the above copyright
2038333Sphk *    notice, this list of conditions and the following disclaimer in the
2138333Sphk *    documentation and/or other materials provided with the distribution.
2238333Sphk * 3. All advertising materials mentioning features or use of this software
2338333Sphk *    must display the following acknowledgement:
2438333Sphk *	This product includes software developed by the University of
2538333Sphk *	California, Berkeley and its contributors.
2638333Sphk * 4. Neither the name of the University nor the names of its contributors
2738333Sphk *    may be used to endorse or promote products derived from this software
2838333Sphk *    without specific prior written permission.
2938333Sphk *
3038333Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3138333Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3238333Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3338333Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3438333Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3538333Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3638333Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3738333Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3838333Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3938333Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4038333Sphk * SUCH DAMAGE.
4138333Sphk */
4238333Sphk
4338333Sphk#if defined(LIBC_SCCS) && !defined(lint)
4438333Sphkstatic char sccsid[] = "@(#)big5.c	8.1 (Berkeley) 6/4/93";
4538333Sphk#endif /* LIBC_SCCS and not lint */
46142654Sphantom#include <sys/cdefs.h>
4792986Sobrien__FBSDID("$FreeBSD$");
4838333Sphk
49142654Sphantom#include <sys/types.h>
50128155Stjr#include <errno.h>
51121893Stjr#include <runetype.h>
5238333Sphk#include <stdlib.h>
53128004Stjr#include <string.h>
54121893Stjr#include <wchar.h>
55129153Stjr#include "mblocal.h"
5638333Sphk
57172619Sacheextern int __mb_sb_limit;
58172619Sache
59142654Sphantomstatic size_t	_BIG5_mbrtowc(wchar_t * __restrict, const char * __restrict,
60142654Sphantom		    size_t, mbstate_t * __restrict);
61142654Sphantomstatic int	_BIG5_mbsinit(const mbstate_t *);
62142654Sphantomstatic size_t	_BIG5_wcrtomb(char * __restrict, wchar_t,
63142654Sphantom		    mbstate_t * __restrict);
64121893Stjr
65128004Stjrtypedef struct {
66129334Stjr	wchar_t	ch;
67128004Stjr} _BIG5State;
68128004Stjr
6938333Sphkint
70227753Stheraven_BIG5_init(struct xlocale_ctype *l, _RuneLocale *rl)
7138333Sphk{
72121893Stjr
73227753Stheraven	l->__mbrtowc = _BIG5_mbrtowc;
74227753Stheraven	l->__wcrtomb = _BIG5_wcrtomb;
75227753Stheraven	l->__mbsinit = _BIG5_mbsinit;
76227753Stheraven	l->runes = rl;
77227753Stheraven	l->__mb_cur_max = 2;
78227753Stheraven	l->__mb_sb_limit = 128;
7938333Sphk	return (0);
8038333Sphk}
8138333Sphk
82142654Sphantomstatic int
83128004Stjr_BIG5_mbsinit(const mbstate_t *ps)
84128004Stjr{
85128004Stjr
86129334Stjr	return (ps == NULL || ((const _BIG5State *)ps)->ch == 0);
87128004Stjr}
88128004Stjr
89121893Stjrstatic __inline int
90121893Stjr_big5_check(u_int c)
9138333Sphk{
92121893Stjr
9338333Sphk	c &= 0xff;
9438333Sphk	return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1);
9538333Sphk}
9638333Sphk
97142654Sphantomstatic size_t
98121893Stjr_BIG5_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
99128004Stjr    mbstate_t * __restrict ps)
10038333Sphk{
101128004Stjr	_BIG5State *bs;
102121893Stjr	wchar_t wc;
103129334Stjr	size_t len;
10438333Sphk
105128004Stjr	bs = (_BIG5State *)ps;
106128004Stjr
107129334Stjr	if ((bs->ch & ~0xFF) != 0) {
108129334Stjr		/* Bad conversion state. */
109128155Stjr		errno = EINVAL;
110128155Stjr		return ((size_t)-1);
111128155Stjr	}
112128155Stjr
113128004Stjr	if (s == NULL) {
114128004Stjr		s = "";
115128004Stjr		n = 1;
116128004Stjr		pwc = NULL;
117128004Stjr	}
118128004Stjr
119129334Stjr	if (n == 0)
120121893Stjr		/* Incomplete multibyte sequence */
121121893Stjr		return ((size_t)-2);
122129334Stjr
123129334Stjr	if (bs->ch != 0) {
124129334Stjr		if (*s == '\0') {
125129334Stjr			errno = EILSEQ;
126129334Stjr			return ((size_t)-1);
127129334Stjr		}
128129334Stjr		wc = (bs->ch << 8) | (*s & 0xFF);
129129334Stjr		if (pwc != NULL)
130129334Stjr			*pwc = wc;
131129334Stjr		bs->ch = 0;
132129334Stjr		return (1);
133129117Stjr	}
134129334Stjr
135129334Stjr	len = (size_t)_big5_check(*s);
136129334Stjr	wc = *s++ & 0xff;
137129334Stjr	if (len == 2) {
138129334Stjr		if (n < 2) {
139129334Stjr			/* Incomplete multibyte sequence */
140129334Stjr			bs->ch = wc;
141129334Stjr			return ((size_t)-2);
142129334Stjr		}
143129334Stjr		if (*s == '\0') {
144129334Stjr			errno = EILSEQ;
145129334Stjr			return ((size_t)-1);
146129334Stjr		}
147129334Stjr		wc = (wc << 8) | (*s++ & 0xff);
148129334Stjr		if (pwc != NULL)
149129334Stjr			*pwc = wc;
150129334Stjr                return (2);
151129334Stjr	} else {
152129334Stjr		if (pwc != NULL)
153129334Stjr			*pwc = wc;
154129334Stjr		return (wc == L'\0' ? 0 : 1);
155129334Stjr	}
15638333Sphk}
15738333Sphk
158142654Sphantomstatic size_t
159128155Stjr_BIG5_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
16038333Sphk{
161128155Stjr	_BIG5State *bs;
162121893Stjr
163128155Stjr	bs = (_BIG5State *)ps;
164128155Stjr
165129334Stjr	if (bs->ch != 0) {
166128155Stjr		errno = EINVAL;
167128155Stjr		return ((size_t)-1);
168128155Stjr	}
169128155Stjr
170121893Stjr	if (s == NULL)
171121893Stjr		/* Reset to initial shift state (no-op) */
172121893Stjr		return (1);
173121893Stjr	if (wc & 0x8000) {
174121893Stjr		*s++ = (wc >> 8) & 0xff;
175121893Stjr		*s = wc & 0xff;
176121893Stjr		return (2);
17738333Sphk	}
178121893Stjr	*s = wc & 0xff;
179121893Stjr	return (1);
18038333Sphk}
181