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