1115626Sache/*-
2128004Stjr * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
3115626Sache * Copyright (c) 1993
4115626Sache *	The Regents of the University of California.  All rights reserved.
5115626Sache *
6115626Sache * This code is derived from software contributed to Berkeley by
7115626Sache * Paul Borman at Krystal Technologies.
8115626Sache *
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 *
14115626Sache * Redistribution and use in source and binary forms, with or without
15115626Sache * modification, are permitted provided that the following conditions
16115626Sache * are met:
17115626Sache * 1. Redistributions of source code must retain the above copyright
18115626Sache *    notice, this list of conditions and the following disclaimer.
19115626Sache * 2. Redistributions in binary form must reproduce the above copyright
20115626Sache *    notice, this list of conditions and the following disclaimer in the
21115626Sache *    documentation and/or other materials provided with the distribution.
22115626Sache * 4. Neither the name of the University nor the names of its contributors
23115626Sache *    may be used to endorse or promote products derived from this software
24115626Sache *    without specific prior written permission.
25115626Sache *
26115626Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27115626Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28115626Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29115626Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30115626Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31115626Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32115626Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33115626Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34115626Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35115626Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36115626Sache * SUCH DAMAGE.
37115626Sache */
38115626Sache
39142654Sphantom#include <sys/cdefs.h>
40115626Sache__FBSDID("$FreeBSD$");
41115626Sache
42142654Sphantom#include <sys/types.h>
43128155Stjr#include <errno.h>
44122103Stjr#include <runetype.h>
45115626Sache#include <stdlib.h>
46128004Stjr#include <string.h>
47122103Stjr#include <wchar.h>
48129153Stjr#include "mblocal.h"
49115626Sache
50172619Sacheextern int __mb_sb_limit;
51172619Sache
52142654Sphantomstatic size_t	_GBK_mbrtowc(wchar_t * __restrict, const char * __restrict,
53142654Sphantom		    size_t, mbstate_t * __restrict);
54142654Sphantomstatic int	_GBK_mbsinit(const mbstate_t *);
55142654Sphantomstatic size_t	_GBK_wcrtomb(char * __restrict, wchar_t,
56142654Sphantom		    mbstate_t * __restrict);
57122103Stjr
58128004Stjrtypedef struct {
59129334Stjr	wchar_t	ch;
60128004Stjr} _GBKState;
61128004Stjr
62115626Sacheint
63227753Stheraven_GBK_init(struct xlocale_ctype *l, _RuneLocale *rl)
64115626Sache{
65122103Stjr
66227753Stheraven	l->__mbrtowc = _GBK_mbrtowc;
67227753Stheraven	l->__wcrtomb = _GBK_wcrtomb;
68227753Stheraven	l->__mbsinit = _GBK_mbsinit;
69227753Stheraven	l->runes = rl;
70227753Stheraven	l->__mb_cur_max = 2;
71227753Stheraven	l->__mb_sb_limit = 128;
72115626Sache	return (0);
73115626Sache}
74115626Sache
75142654Sphantomstatic int
76128004Stjr_GBK_mbsinit(const mbstate_t *ps)
77128004Stjr{
78128004Stjr
79129334Stjr	return (ps == NULL || ((const _GBKState *)ps)->ch == 0);
80128004Stjr}
81128004Stjr
82122103Stjrstatic __inline int
83122103Stjr_gbk_check(u_int c)
84115626Sache{
85122103Stjr
86115626Sache	c &= 0xff;
87123665Sache	return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
88115626Sache}
89115626Sache
90142654Sphantomstatic size_t
91122103Stjr_GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
92128004Stjr    mbstate_t * __restrict ps)
93115626Sache{
94128004Stjr	_GBKState *gs;
95122103Stjr	wchar_t wc;
96129334Stjr	size_t len;
97115626Sache
98128004Stjr	gs = (_GBKState *)ps;
99128004Stjr
100129334Stjr	if ((gs->ch & ~0xFF) != 0) {
101129334Stjr		/* Bad conversion state. */
102128155Stjr		errno = EINVAL;
103128155Stjr		return ((size_t)-1);
104128155Stjr	}
105128155Stjr
106128004Stjr	if (s == NULL) {
107128004Stjr		s = "";
108128004Stjr		n = 1;
109128004Stjr		pwc = NULL;
110128004Stjr	}
111128004Stjr
112129334Stjr	if (n == 0)
113122103Stjr		/* Incomplete multibyte sequence */
114122103Stjr		return ((size_t)-2);
115129334Stjr
116129334Stjr	if (gs->ch != 0) {
117129334Stjr		if (*s == '\0') {
118129334Stjr			errno = EILSEQ;
119129334Stjr			return ((size_t)-1);
120129334Stjr		}
121129334Stjr		wc = (gs->ch << 8) | (*s & 0xFF);
122129334Stjr		if (pwc != NULL)
123129334Stjr			*pwc = wc;
124129334Stjr		gs->ch = 0;
125129334Stjr		return (1);
126129117Stjr	}
127129334Stjr
128129334Stjr	len = (size_t)_gbk_check(*s);
129129334Stjr	wc = *s++ & 0xff;
130129334Stjr	if (len == 2) {
131129334Stjr		if (n < 2) {
132129334Stjr			/* Incomplete multibyte sequence */
133129334Stjr			gs->ch = wc;
134129334Stjr			return ((size_t)-2);
135129334Stjr		}
136129334Stjr		if (*s == '\0') {
137129334Stjr			errno = EILSEQ;
138129334Stjr			return ((size_t)-1);
139129334Stjr		}
140129334Stjr		wc = (wc << 8) | (*s++ & 0xff);
141129334Stjr		if (pwc != NULL)
142129334Stjr			*pwc = wc;
143129334Stjr                return (2);
144129334Stjr	} else {
145129334Stjr		if (pwc != NULL)
146129334Stjr			*pwc = wc;
147129334Stjr		return (wc == L'\0' ? 0 : 1);
148129334Stjr	}
149115626Sache}
150115626Sache
151142654Sphantomstatic size_t
152128155Stjr_GBK_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
153115626Sache{
154128155Stjr	_GBKState *gs;
155122103Stjr
156128155Stjr	gs = (_GBKState *)ps;
157128155Stjr
158129334Stjr	if (gs->ch != 0) {
159128155Stjr		errno = EINVAL;
160128155Stjr		return ((size_t)-1);
161128155Stjr	}
162128155Stjr
163122103Stjr	if (s == NULL)
164122103Stjr		/* Reset to initial shift state (no-op) */
165122103Stjr		return (1);
166122103Stjr	if (wc & 0x8000) {
167122103Stjr		*s++ = (wc >> 8) & 0xff;
168122103Stjr		*s = wc & 0xff;
169122103Stjr		return (2);
170115626Sache	}
171122103Stjr	*s = wc & 0xff;
172122103Stjr	return (1);
173115626Sache}
174