xref: /freebsd/lib/libc/locale/euc.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
1 /*-
2  * Copyright (c) 2002, 2003 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/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/types.h>
45 
46 #include <errno.h>
47 #include <runetype.h>
48 #include <stdlib.h>
49 #include <wchar.h>
50 
51 extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
52     size_t, mbstate_t * __restrict);
53 extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
54 
55 int	_EUC_init(_RuneLocale *);
56 size_t	_EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
57 	    mbstate_t * __restrict);
58 size_t	_EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
59 
60 typedef struct {
61 	int	count[4];
62 	wchar_t	bits[4];
63 	wchar_t	mask;
64 } _EucInfo;
65 
66 int
67 _EUC_init(_RuneLocale *rl)
68 {
69 	_EucInfo *ei;
70 	int x, new__mb_cur_max;
71 	char *v, *e;
72 
73 	if (rl->variable == NULL)
74 		return (EFTYPE);
75 
76 	v = (char *)rl->variable;
77 
78 	while (*v == ' ' || *v == '\t')
79 		++v;
80 
81 	if ((ei = malloc(sizeof(_EucInfo))) == NULL)
82 		return (errno == 0 ? ENOMEM : errno);
83 
84 	new__mb_cur_max = 0;
85 	for (x = 0; x < 4; ++x) {
86 		ei->count[x] = (int)strtol(v, &e, 0);
87 		if (v == e || !(v = e)) {
88 			free(ei);
89 			return (EFTYPE);
90 		}
91 		if (new__mb_cur_max < ei->count[x])
92 			new__mb_cur_max = ei->count[x];
93 		while (*v == ' ' || *v == '\t')
94 			++v;
95 		ei->bits[x] = (int)strtol(v, &e, 0);
96 		if (v == e || !(v = e)) {
97 			free(ei);
98 			return (EFTYPE);
99 		}
100 		while (*v == ' ' || *v == '\t')
101 			++v;
102 	}
103 	ei->mask = (int)strtol(v, &e, 0);
104 	if (v == e || !(v = e)) {
105 		free(ei);
106 		return (EFTYPE);
107 	}
108 	rl->variable = ei;
109 	rl->variable_len = sizeof(_EucInfo);
110 	_CurrentRuneLocale = rl;
111 	__mb_cur_max = new__mb_cur_max;
112 	__mbrtowc = _EUC_mbrtowc;
113 	__wcrtomb = _EUC_wcrtomb;
114 	return (0);
115 }
116 
117 #define	CEI	((_EucInfo *)(_CurrentRuneLocale->variable))
118 
119 #define	_SS2	0x008e
120 #define	_SS3	0x008f
121 
122 #define	GR_BITS	0x80808080 /* XXX: to be fixed */
123 
124 static __inline int
125 _euc_set(u_int c)
126 {
127 	c &= 0xff;
128 	return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
129 }
130 
131 size_t
132 _EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
133     mbstate_t * __restrict ps __unused)
134 {
135 	int len, remain, set;
136 	wchar_t wc;
137 
138 	if (s == NULL)
139 		/* Reset to initial shift state (no-op) */
140 		return (0);
141 	if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n)
142 		/* Incomplete multibyte sequence */
143 		return ((size_t)-2);
144 	wc = 0;
145 	remain = len;
146 	switch (set) {
147 	case 3:
148 	case 2:
149 		--remain;
150 		++s;
151 		/* FALLTHROUGH */
152 	case 1:
153 	case 0:
154 		while (remain-- > 0)
155 			wc = (wc << 8) | (unsigned char)*s++;
156 		break;
157 	}
158 	wc = (wc & ~CEI->mask) | CEI->bits[set];
159 	if (pwc != NULL)
160 		*pwc = wc;
161 	return (wc == L'\0' ? 0 : len);
162 }
163 
164 size_t
165 _EUC_wcrtomb(char * __restrict s, wchar_t wc,
166     mbstate_t * __restrict ps __unused)
167 {
168 	wchar_t m, nm;
169 	int i, len;
170 
171 	if (s == NULL)
172 		/* Reset to initial shift state (no-op) */
173 		return (1);
174 
175 	m = wc & CEI->mask;
176 	nm = wc & ~m;
177 
178 	if (m == CEI->bits[1]) {
179 CodeSet1:
180 		/* Codeset 1: The first byte must have 0x80 in it. */
181 		i = len = CEI->count[1];
182 		while (i-- > 0)
183 			*s++ = (nm >> (i << 3)) | 0x80;
184 	} else {
185 		if (m == CEI->bits[0])
186 			i = len = CEI->count[0];
187 		else if (m == CEI->bits[2]) {
188 			i = len = CEI->count[2];
189 			*s++ = _SS2;
190 			--i;
191 			/* SS2 designates G2 into GR */
192 			nm |= GR_BITS;
193 		} else if (m == CEI->bits[3]) {
194 			i = len = CEI->count[3];
195 			*s++ = _SS3;
196 			--i;
197 			/* SS3 designates G3 into GR */
198 			nm |= GR_BITS;
199 		} else
200 			goto CodeSet1;	/* Bletch */
201 		while (i-- > 0)
202 			*s++ = (nm >> (i << 3)) & 0xff;
203 	}
204 	return (len);
205 }
206