Deleted Added
full compact
euc.c (129117) euc.c (129153)
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)
39static char sccsid[] = "@(#)euc.c 8.1 (Berkeley) 6/4/93";
40#endif /* LIBC_SCCS and not lint */
41#include <sys/param.h>
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)
39static 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: head/lib/libc/locale/euc.c 129117 2004-05-11 14:08:22Z tjr $");
42__FBSDID("$FreeBSD: head/lib/libc/locale/euc.c 129153 2004-05-12 14:09:04Z tjr $");
43
44#include <errno.h>
45#include <limits.h>
46#include <runetype.h>
47#include <stdlib.h>
48#include <string.h>
49#include <wchar.h>
43
44#include <errno.h>
45#include <limits.h>
46#include <runetype.h>
47#include <stdlib.h>
48#include <string.h>
49#include <wchar.h>
50#include "mblocal.h"
50
51
51extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
52 size_t, mbstate_t * __restrict);
53extern int (*__mbsinit)(const mbstate_t *);
54extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
55
56int _EUC_init(_RuneLocale *);
57size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
58 mbstate_t * __restrict);
59int _EUC_mbsinit(const mbstate_t *);
60size_t _EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
61
62typedef struct {
63 int count[4];
64 wchar_t bits[4];
65 wchar_t mask;
66} _EucInfo;
67
68typedef struct {
69 int count;
70 u_char bytes[MB_LEN_MAX];
71} _EucState;
72
73int
74_EUC_init(_RuneLocale *rl)
75{
76 _EucInfo *ei;
77 int x, new__mb_cur_max;
78 char *v, *e;
79
80 if (rl->variable == NULL)
81 return (EFTYPE);
82
83 v = (char *)rl->variable;
84
85 while (*v == ' ' || *v == '\t')
86 ++v;
87
88 if ((ei = malloc(sizeof(_EucInfo))) == NULL)
89 return (errno == 0 ? ENOMEM : errno);
90
91 new__mb_cur_max = 0;
92 for (x = 0; x < 4; ++x) {
93 ei->count[x] = (int)strtol(v, &e, 0);
94 if (v == e || !(v = e)) {
95 free(ei);
96 return (EFTYPE);
97 }
98 if (new__mb_cur_max < ei->count[x])
99 new__mb_cur_max = ei->count[x];
100 while (*v == ' ' || *v == '\t')
101 ++v;
102 ei->bits[x] = (int)strtol(v, &e, 0);
103 if (v == e || !(v = e)) {
104 free(ei);
105 return (EFTYPE);
106 }
107 while (*v == ' ' || *v == '\t')
108 ++v;
109 }
110 ei->mask = (int)strtol(v, &e, 0);
111 if (v == e || !(v = e)) {
112 free(ei);
113 return (EFTYPE);
114 }
115 rl->variable = ei;
116 rl->variable_len = sizeof(_EucInfo);
117 _CurrentRuneLocale = rl;
118 __mb_cur_max = new__mb_cur_max;
119 __mbrtowc = _EUC_mbrtowc;
120 __wcrtomb = _EUC_wcrtomb;
121 __mbsinit = _EUC_mbsinit;
122 return (0);
123}
124
125int
126_EUC_mbsinit(const mbstate_t *ps)
127{
128
129 return (ps == NULL || ((const _EucState *)ps)->count == 0);
130}
131
132#define CEI ((_EucInfo *)(_CurrentRuneLocale->variable))
133
134#define _SS2 0x008e
135#define _SS3 0x008f
136
137#define GR_BITS 0x80808080 /* XXX: to be fixed */
138
139static __inline int
140_euc_set(u_int c)
141{
142 c &= 0xff;
143 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
144}
145
146size_t
147_EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
148 mbstate_t * __restrict ps)
149{
150 _EucState *es;
151 int len, ocount, remain, set;
152 wchar_t wc;
153 size_t ncopy;
154
155 es = (_EucState *)ps;
156
157 if (es->count < 0 || es->count > sizeof(es->bytes)) {
158 errno = EINVAL;
159 return ((size_t)-1);
160 }
161
162 if (s == NULL) {
163 s = "";
164 n = 1;
165 pwc = NULL;
166 }
167
168 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(es->bytes) - es->count);
169 memcpy(es->bytes + es->count, s, ncopy);
170 ocount = es->count;
171 es->count += ncopy;
172 s = (char *)es->bytes;
173 n = es->count;
174
175 if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n)
176 /* Incomplete multibyte sequence */
177 return ((size_t)-2);
178 wc = 0;
179 remain = len;
180 switch (set) {
181 case 3:
182 case 2:
183 --remain;
184 ++s;
185 /* FALLTHROUGH */
186 case 1:
187 case 0:
188 wc = (unsigned char)*s++;
189 while (--remain > 0) {
190 if (*s == '\0') {
191 errno = EILSEQ;
192 return ((size_t)-1);
193 }
194 wc = (wc << 8) | (unsigned char)*s++;
195 }
196 break;
197 }
198 wc = (wc & ~CEI->mask) | CEI->bits[set];
199 if (pwc != NULL)
200 *pwc = wc;
201 es->count = 0;
202 return (wc == L'\0' ? 0 : len - ocount);
203}
204
205size_t
206_EUC_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
207{
208 _EucState *es;
209 wchar_t m, nm;
210 int i, len;
211
212 es = (_EucState *)ps;
213
214 if (es->count != 0) {
215 errno = EINVAL;
216 return ((size_t)-1);
217 }
218
219 if (s == NULL)
220 /* Reset to initial shift state (no-op) */
221 return (1);
222
223 m = wc & CEI->mask;
224 nm = wc & ~m;
225
226 if (m == CEI->bits[1]) {
227CodeSet1:
228 /* Codeset 1: The first byte must have 0x80 in it. */
229 i = len = CEI->count[1];
230 while (i-- > 0)
231 *s++ = (nm >> (i << 3)) | 0x80;
232 } else {
233 if (m == CEI->bits[0])
234 i = len = CEI->count[0];
235 else if (m == CEI->bits[2]) {
236 i = len = CEI->count[2];
237 *s++ = _SS2;
238 --i;
239 /* SS2 designates G2 into GR */
240 nm |= GR_BITS;
241 } else if (m == CEI->bits[3]) {
242 i = len = CEI->count[3];
243 *s++ = _SS3;
244 --i;
245 /* SS3 designates G3 into GR */
246 nm |= GR_BITS;
247 } else
248 goto CodeSet1; /* Bletch */
249 while (i-- > 0)
250 *s++ = (nm >> (i << 3)) & 0xff;
251 }
252 return (len);
253}
52int _EUC_init(_RuneLocale *);
53size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
54 mbstate_t * __restrict);
55int _EUC_mbsinit(const mbstate_t *);
56size_t _EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
57
58typedef struct {
59 int count[4];
60 wchar_t bits[4];
61 wchar_t mask;
62} _EucInfo;
63
64typedef struct {
65 int count;
66 u_char bytes[MB_LEN_MAX];
67} _EucState;
68
69int
70_EUC_init(_RuneLocale *rl)
71{
72 _EucInfo *ei;
73 int x, new__mb_cur_max;
74 char *v, *e;
75
76 if (rl->variable == NULL)
77 return (EFTYPE);
78
79 v = (char *)rl->variable;
80
81 while (*v == ' ' || *v == '\t')
82 ++v;
83
84 if ((ei = malloc(sizeof(_EucInfo))) == NULL)
85 return (errno == 0 ? ENOMEM : errno);
86
87 new__mb_cur_max = 0;
88 for (x = 0; x < 4; ++x) {
89 ei->count[x] = (int)strtol(v, &e, 0);
90 if (v == e || !(v = e)) {
91 free(ei);
92 return (EFTYPE);
93 }
94 if (new__mb_cur_max < ei->count[x])
95 new__mb_cur_max = ei->count[x];
96 while (*v == ' ' || *v == '\t')
97 ++v;
98 ei->bits[x] = (int)strtol(v, &e, 0);
99 if (v == e || !(v = e)) {
100 free(ei);
101 return (EFTYPE);
102 }
103 while (*v == ' ' || *v == '\t')
104 ++v;
105 }
106 ei->mask = (int)strtol(v, &e, 0);
107 if (v == e || !(v = e)) {
108 free(ei);
109 return (EFTYPE);
110 }
111 rl->variable = ei;
112 rl->variable_len = sizeof(_EucInfo);
113 _CurrentRuneLocale = rl;
114 __mb_cur_max = new__mb_cur_max;
115 __mbrtowc = _EUC_mbrtowc;
116 __wcrtomb = _EUC_wcrtomb;
117 __mbsinit = _EUC_mbsinit;
118 return (0);
119}
120
121int
122_EUC_mbsinit(const mbstate_t *ps)
123{
124
125 return (ps == NULL || ((const _EucState *)ps)->count == 0);
126}
127
128#define CEI ((_EucInfo *)(_CurrentRuneLocale->variable))
129
130#define _SS2 0x008e
131#define _SS3 0x008f
132
133#define GR_BITS 0x80808080 /* XXX: to be fixed */
134
135static __inline int
136_euc_set(u_int c)
137{
138 c &= 0xff;
139 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
140}
141
142size_t
143_EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
144 mbstate_t * __restrict ps)
145{
146 _EucState *es;
147 int len, ocount, remain, set;
148 wchar_t wc;
149 size_t ncopy;
150
151 es = (_EucState *)ps;
152
153 if (es->count < 0 || es->count > sizeof(es->bytes)) {
154 errno = EINVAL;
155 return ((size_t)-1);
156 }
157
158 if (s == NULL) {
159 s = "";
160 n = 1;
161 pwc = NULL;
162 }
163
164 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(es->bytes) - es->count);
165 memcpy(es->bytes + es->count, s, ncopy);
166 ocount = es->count;
167 es->count += ncopy;
168 s = (char *)es->bytes;
169 n = es->count;
170
171 if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n)
172 /* Incomplete multibyte sequence */
173 return ((size_t)-2);
174 wc = 0;
175 remain = len;
176 switch (set) {
177 case 3:
178 case 2:
179 --remain;
180 ++s;
181 /* FALLTHROUGH */
182 case 1:
183 case 0:
184 wc = (unsigned char)*s++;
185 while (--remain > 0) {
186 if (*s == '\0') {
187 errno = EILSEQ;
188 return ((size_t)-1);
189 }
190 wc = (wc << 8) | (unsigned char)*s++;
191 }
192 break;
193 }
194 wc = (wc & ~CEI->mask) | CEI->bits[set];
195 if (pwc != NULL)
196 *pwc = wc;
197 es->count = 0;
198 return (wc == L'\0' ? 0 : len - ocount);
199}
200
201size_t
202_EUC_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
203{
204 _EucState *es;
205 wchar_t m, nm;
206 int i, len;
207
208 es = (_EucState *)ps;
209
210 if (es->count != 0) {
211 errno = EINVAL;
212 return ((size_t)-1);
213 }
214
215 if (s == NULL)
216 /* Reset to initial shift state (no-op) */
217 return (1);
218
219 m = wc & CEI->mask;
220 nm = wc & ~m;
221
222 if (m == CEI->bits[1]) {
223CodeSet1:
224 /* Codeset 1: The first byte must have 0x80 in it. */
225 i = len = CEI->count[1];
226 while (i-- > 0)
227 *s++ = (nm >> (i << 3)) | 0x80;
228 } else {
229 if (m == CEI->bits[0])
230 i = len = CEI->count[0];
231 else if (m == CEI->bits[2]) {
232 i = len = CEI->count[2];
233 *s++ = _SS2;
234 --i;
235 /* SS2 designates G2 into GR */
236 nm |= GR_BITS;
237 } else if (m == CEI->bits[3]) {
238 i = len = CEI->count[3];
239 *s++ = _SS3;
240 --i;
241 /* SS3 designates G3 into GR */
242 nm |= GR_BITS;
243 } else
244 goto CodeSet1; /* Bletch */
245 while (i-- > 0)
246 *s++ = (nm >> (i << 3)) & 0xff;
247 }
248 return (len);
249}