]>
Commit | Line | Data |
---|---|---|
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 | * 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 | ||
38 | #if defined(LIBC_SCCS) && !defined(lint) | |
39 | static char sccsid[] = "@(#)euc.c 8.1 (Berkeley) 6/4/93"; | |
40 | #endif /* LIBC_SCCS and not lint */ | |
41 | #include <sys/param.h> | |
42 | __FBSDID("$FreeBSD: src/lib/libc/locale/euc.c,v 1.20 2004/06/23 07:01:43 tjr Exp $"); | |
43 | ||
44 | #include "xlocale_private.h" | |
45 | ||
46 | #include <errno.h> | |
47 | #include <limits.h> | |
48 | #include <runetype.h> | |
49 | #include <stdlib.h> | |
50 | #include <string.h> | |
51 | #include <wchar.h> | |
52 | #include "mblocal.h" | |
53 | ||
54 | __private_extern__ int _EUC_init(struct __xlocale_st_runelocale *); | |
55 | static size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, | |
56 | mbstate_t * __restrict, locale_t); | |
57 | static int _EUC_mbsinit(const mbstate_t *, locale_t); | |
58 | static size_t _EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict, | |
59 | locale_t); | |
60 | ||
61 | typedef struct { | |
62 | int count[4]; | |
63 | wchar_t bits[4]; | |
64 | wchar_t mask; | |
65 | } _EucInfo; | |
66 | ||
67 | typedef struct { | |
68 | wchar_t ch; | |
69 | int set; | |
70 | int want; | |
71 | } _EucState; | |
72 | ||
73 | /* This will be called by the XL_RELEASE() macro to free the extra storage */ | |
74 | static void | |
75 | _EUC_free_extra(struct __xlocale_st_runelocale *xrl) | |
76 | { | |
77 | free(xrl->_CurrentRuneLocale.__variable); | |
78 | } | |
79 | ||
80 | __private_extern__ int | |
81 | _EUC_init(struct __xlocale_st_runelocale *xrl) | |
82 | { | |
83 | _EucInfo *ei; | |
84 | int x, new__mb_cur_max; | |
85 | char *v, *e; | |
86 | _RuneLocale *rl = &xrl->_CurrentRuneLocale; | |
87 | ||
88 | if (rl->__variable == NULL) | |
89 | return (EFTYPE); | |
90 | ||
91 | v = (char *)rl->__variable; | |
92 | ||
93 | while (*v == ' ' || *v == '\t') | |
94 | ++v; | |
95 | ||
96 | if ((ei = malloc(sizeof(_EucInfo))) == NULL) | |
97 | return (errno == 0 ? ENOMEM : errno); | |
98 | ||
99 | new__mb_cur_max = 0; | |
100 | for (x = 0; x < 4; ++x) { | |
101 | ei->count[x] = (int)strtol(v, &e, 0); | |
102 | if (v == e || !(v = e)) { | |
103 | free(ei); | |
104 | return (EFTYPE); | |
105 | } | |
106 | if (new__mb_cur_max < ei->count[x]) | |
107 | new__mb_cur_max = ei->count[x]; | |
108 | while (*v == ' ' || *v == '\t') | |
109 | ++v; | |
110 | ei->bits[x] = (int)strtol(v, &e, 0); | |
111 | if (v == e || !(v = e)) { | |
112 | free(ei); | |
113 | return (EFTYPE); | |
114 | } | |
115 | while (*v == ' ' || *v == '\t') | |
116 | ++v; | |
117 | } | |
118 | ei->mask = (int)strtol(v, &e, 0); | |
119 | if (v == e || !(v = e)) { | |
120 | free(ei); | |
121 | return (EFTYPE); | |
122 | } | |
123 | rl->__variable = ei; | |
124 | rl->__variable_len = sizeof(_EucInfo); | |
125 | xrl->__mb_cur_max = new__mb_cur_max; | |
126 | xrl->__mbrtowc = _EUC_mbrtowc; | |
127 | xrl->__wcrtomb = _EUC_wcrtomb; | |
128 | xrl->__mbsinit = _EUC_mbsinit; | |
129 | xrl->__free_extra = (__free_extra_t)_EUC_free_extra; | |
130 | return (0); | |
131 | } | |
132 | ||
133 | static int | |
134 | _EUC_mbsinit(const mbstate_t *ps, locale_t loc) | |
135 | { | |
136 | ||
137 | return (ps == NULL || ((const _EucState *)ps)->want == 0); | |
138 | } | |
139 | ||
140 | #define _SS2 0x008e | |
141 | #define _SS3 0x008f | |
142 | ||
143 | #define GR_BITS 0x80808080 /* XXX: to be fixed */ | |
144 | ||
145 | static __inline int | |
146 | _euc_set(u_int c) | |
147 | { | |
148 | c &= 0xff; | |
149 | return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); | |
150 | } | |
151 | ||
152 | static size_t | |
153 | _EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, | |
154 | mbstate_t * __restrict ps, locale_t loc) | |
155 | { | |
156 | _EucState *es; | |
157 | int i, set, want; | |
158 | wchar_t wc; | |
159 | const char *os; | |
160 | struct __xlocale_st_runelocale *rl = loc->__lc_ctype; | |
161 | _EucInfo *CEI = (_EucInfo *)rl->_CurrentRuneLocale.__variable; | |
162 | ||
163 | es = (_EucState *)ps; | |
164 | ||
165 | if (es->want < 0 || es->want > rl->__mb_cur_max || es->set < 0 || | |
166 | es->set > 3) { | |
167 | errno = EINVAL; | |
168 | return ((size_t)-1); | |
169 | } | |
170 | ||
171 | if (s == NULL) { | |
172 | s = ""; | |
173 | n = 1; | |
174 | pwc = NULL; | |
175 | } | |
176 | ||
177 | if (n == 0) | |
178 | /* Incomplete multibyte sequence */ | |
179 | return ((size_t)-2); | |
180 | ||
181 | os = s; | |
182 | ||
183 | if (es->want == 0) { | |
184 | want = CEI->count[set = _euc_set(*s)]; | |
185 | if (set == 2 || set == 3) { | |
186 | --want; | |
187 | if (--n == 0) { | |
188 | /* Incomplete multibyte sequence */ | |
189 | es->set = set; | |
190 | es->want = want; | |
191 | es->ch = 0; | |
192 | return ((size_t)-2); | |
193 | } | |
194 | ++s; | |
195 | if (*s == '\0') { | |
196 | errno = EILSEQ; | |
197 | return ((size_t)-1); | |
198 | } | |
199 | } | |
200 | wc = (unsigned char)*s++; | |
201 | } else { | |
202 | set = es->set; | |
203 | want = es->want; | |
204 | wc = es->ch; | |
205 | } | |
206 | for (i = (es->want == 0) ? 1 : 0; i < MIN(want, n); i++) { | |
207 | if (*s == '\0') { | |
208 | errno = EILSEQ; | |
209 | return ((size_t)-1); | |
210 | } | |
211 | wc = (wc << 8) | (unsigned char)*s++; | |
212 | } | |
213 | if (i < want) { | |
214 | /* Incomplete multibyte sequence */ | |
215 | es->set = set; | |
216 | es->want = want - i; | |
217 | es->ch = wc; | |
218 | return ((size_t)-2); | |
219 | } | |
220 | wc = (wc & ~CEI->mask) | CEI->bits[set]; | |
221 | if (pwc != NULL) | |
222 | *pwc = wc; | |
223 | es->want = 0; | |
224 | return (wc == L'\0' ? 0 : s - os); | |
225 | } | |
226 | ||
227 | static size_t | |
228 | _EUC_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps, | |
229 | locale_t loc) | |
230 | { | |
231 | _EucState *es; | |
232 | wchar_t m, nm; | |
233 | int i, len; | |
234 | _EucInfo *CEI = (_EucInfo *)loc->__lc_ctype->_CurrentRuneLocale.__variable; | |
235 | ||
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 | ||
250 | if (m == CEI->bits[1]) { | |
251 | CodeSet1: | |
252 | /* Codeset 1: The first byte must have 0x80 in it. */ | |
253 | i = len = CEI->count[1]; | |
254 | while (i-- > 0) | |
255 | *s++ = (nm >> (i << 3)) | 0x80; | |
256 | } else { | |
257 | if (m == CEI->bits[0]) | |
258 | i = len = CEI->count[0]; | |
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; | |
271 | } else | |
272 | goto CodeSet1; /* Bletch */ | |
273 | while (i-- > 0) | |
274 | *s++ = (nm >> (i << 3)) & 0xff; | |
275 | } | |
276 | return (len); | |
277 | } |