gbk.c revision 165903
1/*-
2 * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
3 * Copyright (c) 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/lib/libc/locale/gbk.c 165903 2007-01-09 00:28:16Z imp $");
36
37#include <sys/types.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
45static size_t	_GBK_mbrtowc(wchar_t * __restrict, const char * __restrict,
46		    size_t, mbstate_t * __restrict);
47static int	_GBK_mbsinit(const mbstate_t *);
48static size_t	_GBK_wcrtomb(char * __restrict, wchar_t,
49		    mbstate_t * __restrict);
50
51typedef struct {
52	wchar_t	ch;
53} _GBKState;
54
55int
56_GBK_init(_RuneLocale *rl)
57{
58
59	__mbrtowc = _GBK_mbrtowc;
60	__wcrtomb = _GBK_wcrtomb;
61	__mbsinit = _GBK_mbsinit;
62	_CurrentRuneLocale = rl;
63	__mb_cur_max = 2;
64	return (0);
65}
66
67static int
68_GBK_mbsinit(const mbstate_t *ps)
69{
70
71	return (ps == NULL || ((const _GBKState *)ps)->ch == 0);
72}
73
74static __inline int
75_gbk_check(u_int c)
76{
77
78	c &= 0xff;
79	return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
80}
81
82static size_t
83_GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
84    mbstate_t * __restrict ps)
85{
86	_GBKState *gs;
87	wchar_t wc;
88	size_t len;
89
90	gs = (_GBKState *)ps;
91
92	if ((gs->ch & ~0xFF) != 0) {
93		/* Bad conversion state. */
94		errno = EINVAL;
95		return ((size_t)-1);
96	}
97
98	if (s == NULL) {
99		s = "";
100		n = 1;
101		pwc = NULL;
102	}
103
104	if (n == 0)
105		/* Incomplete multibyte sequence */
106		return ((size_t)-2);
107
108	if (gs->ch != 0) {
109		if (*s == '\0') {
110			errno = EILSEQ;
111			return ((size_t)-1);
112		}
113		wc = (gs->ch << 8) | (*s & 0xFF);
114		if (pwc != NULL)
115			*pwc = wc;
116		gs->ch = 0;
117		return (1);
118	}
119
120	len = (size_t)_gbk_check(*s);
121	wc = *s++ & 0xff;
122	if (len == 2) {
123		if (n < 2) {
124			/* Incomplete multibyte sequence */
125			gs->ch = wc;
126			return ((size_t)-2);
127		}
128		if (*s == '\0') {
129			errno = EILSEQ;
130			return ((size_t)-1);
131		}
132		wc = (wc << 8) | (*s++ & 0xff);
133		if (pwc != NULL)
134			*pwc = wc;
135                return (2);
136	} else {
137		if (pwc != NULL)
138			*pwc = wc;
139		return (wc == L'\0' ? 0 : 1);
140	}
141}
142
143static size_t
144_GBK_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
145{
146	_GBKState *gs;
147
148	gs = (_GBKState *)ps;
149
150	if (gs->ch != 0) {
151		errno = EINVAL;
152		return ((size_t)-1);
153	}
154
155	if (s == NULL)
156		/* Reset to initial shift state (no-op) */
157		return (1);
158	if (wc & 0x8000) {
159		*s++ = (wc >> 8) & 0xff;
160		*s = wc & 0xff;
161		return (2);
162	}
163	*s = wc & 0xff;
164	return (1);
165}
166