]> git.saurik.com Git - apple/libc.git/blame - locale/FreeBSD/euc.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / locale / FreeBSD / euc.c
CommitLineData
5b2abdfb 1/*-
3d9156a7 2 * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
e9ce8d39
A
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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
5b2abdfb
A
38#if defined(LIBC_SCCS) && !defined(lint)
39static char sccsid[] = "@(#)euc.c 8.1 (Berkeley) 6/4/93";
40#endif /* LIBC_SCCS and not lint */
3d9156a7 41#include <sys/param.h>
1f2f436a 42__FBSDID("$FreeBSD: src/lib/libc/locale/euc.c,v 1.22 2007/10/13 16:28:21 ache Exp $");
e9ce8d39 43
ad3c9f2a
A
44#include "xlocale_private.h"
45
e9ce8d39 46#include <errno.h>
3d9156a7
A
47#include <limits.h>
48#include <runetype.h>
e9ce8d39 49#include <stdlib.h>
5b2abdfb 50#include <string.h>
3d9156a7
A
51#include <wchar.h>
52#include "mblocal.h"
e9ce8d39 53
1f2f436a 54static size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict,
ad3c9f2a
A
55 size_t, mbstate_t * __restrict, locale_t);
56static int _EUC_mbsinit(const mbstate_t *, locale_t);
1f2f436a 57static size_t _EUC_wcrtomb(char * __restrict, wchar_t,
ad3c9f2a 58 mbstate_t * __restrict, locale_t);
e9ce8d39
A
59
60typedef struct {
61 int count[4];
3d9156a7
A
62 wchar_t bits[4];
63 wchar_t mask;
e9ce8d39
A
64} _EucInfo;
65
3d9156a7
A
66typedef struct {
67 wchar_t ch;
68 int set;
69 int want;
70} _EucState;
71
ad3c9f2a
A
72/* This will be called by the XL_RELEASE() macro to free the extra storage */
73static void
74_EUC_free_extra(struct __xlocale_st_runelocale *xrl)
75{
76 free(xrl->_CurrentRuneLocale.__variable);
77}
78
23e20b00 79int
ad3c9f2a 80_EUC_init(struct __xlocale_st_runelocale *xrl)
e9ce8d39
A
81{
82 _EucInfo *ei;
9385eb3d 83 int x, new__mb_cur_max;
e9ce8d39 84 char *v, *e;
ad3c9f2a 85 _RuneLocale *rl = &xrl->_CurrentRuneLocale;
e9ce8d39 86
3d9156a7 87 if (rl->__variable == NULL)
e9ce8d39 88 return (EFTYPE);
9385eb3d 89
3d9156a7 90 v = (char *)rl->__variable;
e9ce8d39
A
91
92 while (*v == ' ' || *v == '\t')
93 ++v;
94
9385eb3d
A
95 if ((ei = malloc(sizeof(_EucInfo))) == NULL)
96 return (errno == 0 ? ENOMEM : errno);
97
98 new__mb_cur_max = 0;
e9ce8d39 99 for (x = 0; x < 4; ++x) {
9385eb3d 100 ei->count[x] = (int)strtol(v, &e, 0);
e9ce8d39 101 if (v == e || !(v = e)) {
e9ce8d39
A
102 free(ei);
103 return (EFTYPE);
104 }
9385eb3d
A
105 if (new__mb_cur_max < ei->count[x])
106 new__mb_cur_max = ei->count[x];
e9ce8d39
A
107 while (*v == ' ' || *v == '\t')
108 ++v;
9385eb3d 109 ei->bits[x] = (int)strtol(v, &e, 0);
e9ce8d39 110 if (v == e || !(v = e)) {
e9ce8d39
A
111 free(ei);
112 return (EFTYPE);
113 }
114 while (*v == ' ' || *v == '\t')
115 ++v;
116 }
117 ei->mask = (int)strtol(v, &e, 0);
118 if (v == e || !(v = e)) {
e9ce8d39
A
119 free(ei);
120 return (EFTYPE);
121 }
3d9156a7
A
122 rl->__variable = ei;
123 rl->__variable_len = sizeof(_EucInfo);
ad3c9f2a
A
124 xrl->__mb_cur_max = new__mb_cur_max;
125 xrl->__mbrtowc = _EUC_mbrtowc;
126 xrl->__wcrtomb = _EUC_wcrtomb;
127 xrl->__mbsinit = _EUC_mbsinit;
128 xrl->__mb_sb_limit = 256;
129 xrl->__free_extra = (__free_extra_t)_EUC_free_extra;
e9ce8d39
A
130 return (0);
131}
132
1f2f436a 133static int
ad3c9f2a 134_EUC_mbsinit(const mbstate_t *ps, locale_t loc __unused)
3d9156a7
A
135{
136
137 return (ps == NULL || ((const _EucState *)ps)->want == 0);
138}
139
e9ce8d39
A
140#define _SS2 0x008e
141#define _SS3 0x008f
142
9385eb3d
A
143#define GR_BITS 0x80808080 /* XXX: to be fixed */
144
3d9156a7
A
145static __inline int
146_euc_set(u_int c)
e9ce8d39 147{
1f2f436a 148
e9ce8d39 149 c &= 0xff;
e9ce8d39
A
150 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
151}
3d9156a7 152
1f2f436a 153static size_t
3d9156a7 154_EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
ad3c9f2a 155 mbstate_t * __restrict ps, locale_t loc)
e9ce8d39 156{
3d9156a7
A
157 _EucState *es;
158 int i, set, want;
159 wchar_t wc;
160 const char *os;
ad3c9f2a
A
161 struct __xlocale_st_runelocale *rl = loc->__lc_ctype;
162 _EucInfo *CEI = (_EucInfo *)rl->_CurrentRuneLocale.__variable;
e9ce8d39 163
3d9156a7
A
164 es = (_EucState *)ps;
165
ad3c9f2a 166 if (es->want < 0 || es->want > rl->__mb_cur_max || es->set < 0 ||
3d9156a7
A
167 es->set > 3) {
168 errno = EINVAL;
169 return ((size_t)-1);
e9ce8d39 170 }
3d9156a7
A
171
172 if (s == NULL) {
173 s = "";
174 n = 1;
175 pwc = NULL;
e9ce8d39 176 }
3d9156a7
A
177
178 if (n == 0)
179 /* Incomplete multibyte sequence */
180 return ((size_t)-2);
181
182 os = s;
183
184 if (es->want == 0) {
185 want = CEI->count[set = _euc_set(*s)];
186 if (set == 2 || set == 3) {
187 --want;
188 if (--n == 0) {
189 /* Incomplete multibyte sequence */
190 es->set = set;
191 es->want = want;
192 es->ch = 0;
193 return ((size_t)-2);
194 }
195 ++s;
196 if (*s == '\0') {
197 errno = EILSEQ;
198 return ((size_t)-1);
199 }
200 }
201 wc = (unsigned char)*s++;
202 } else {
203 set = es->set;
204 want = es->want;
205 wc = es->ch;
206 }
207 for (i = (es->want == 0) ? 1 : 0; i < MIN(want, n); i++) {
208 if (*s == '\0') {
209 errno = EILSEQ;
210 return ((size_t)-1);
211 }
212 wc = (wc << 8) | (unsigned char)*s++;
213 }
214 if (i < want) {
215 /* Incomplete multibyte sequence */
216 es->set = set;
217 es->want = want - i;
218 es->ch = wc;
219 return ((size_t)-2);
220 }
221 wc = (wc & ~CEI->mask) | CEI->bits[set];
222 if (pwc != NULL)
223 *pwc = wc;
224 es->want = 0;
225 return (wc == L'\0' ? 0 : s - os);
e9ce8d39
A
226}
227
1f2f436a 228static size_t
ad3c9f2a 229_EUC_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps, locale_t loc)
e9ce8d39 230{
3d9156a7
A
231 _EucState *es;
232 wchar_t m, nm;
e9ce8d39 233 int i, len;
ad3c9f2a 234 _EucInfo *CEI = (_EucInfo *)loc->__lc_ctype->_CurrentRuneLocale.__variable;
e9ce8d39 235
3d9156a7
A
236 es = (_EucState *)ps;
237
238 if (es->want != 0) {
239 errno = EINVAL;
240 return ((size_t)-1);
241 }
242
243 if (s == NULL)
244 /* Reset to initial shift state (no-op) */
245 return (1);
246
247 m = wc & CEI->mask;
248 nm = wc & ~m;
249
e9ce8d39
A
250 if (m == CEI->bits[1]) {
251CodeSet1:
252 /* Codeset 1: The first byte must have 0x80 in it. */
253 i = len = CEI->count[1];
3d9156a7
A
254 while (i-- > 0)
255 *s++ = (nm >> (i << 3)) | 0x80;
e9ce8d39 256 } else {
3d9156a7 257 if (m == CEI->bits[0])
e9ce8d39 258 i = len = CEI->count[0];
3d9156a7
A
259 else if (m == CEI->bits[2]) {
260 i = len = CEI->count[2];
261 *s++ = _SS2;
262 --i;
263 /* SS2 designates G2 into GR */
264 nm |= GR_BITS;
265 } else if (m == CEI->bits[3]) {
266 i = len = CEI->count[3];
267 *s++ = _SS3;
268 --i;
269 /* SS3 designates G3 into GR */
270 nm |= GR_BITS;
e9ce8d39 271 } else
3d9156a7 272 goto CodeSet1; /* Bletch */
e9ce8d39 273 while (i-- > 0)
3d9156a7 274 *s++ = (nm >> (i << 3)) & 0xff;
e9ce8d39
A
275 }
276 return (len);
277}