xref: /freebsd/lib/libc/locale/euc.c (revision 7773002178c8dbc52b44e4d705f07706409af8e4)
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 <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <wchar.h>
53 
54 extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
55     size_t, mbstate_t * __restrict);
56 extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
57 
58 int	_EUC_init(_RuneLocale *);
59 size_t	_EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
60 	    mbstate_t * __restrict);
61 size_t	_EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
62 
63 typedef struct {
64 	int	count[4];
65 	wchar_t	bits[4];
66 	wchar_t	mask;
67 } _EucInfo;
68 
69 int
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 	return (0);
118 }
119 
120 #define	CEI	((_EucInfo *)(_CurrentRuneLocale->variable))
121 
122 #define	_SS2	0x008e
123 #define	_SS3	0x008f
124 
125 #define	GR_BITS	0x80808080 /* XXX: to be fixed */
126 
127 static __inline int
128 _euc_set(u_int c)
129 {
130 	c &= 0xff;
131 	return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
132 }
133 
134 size_t
135 _EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
136     mbstate_t * __restrict ps __unused)
137 {
138 	int len, remain, set;
139 	wchar_t wc;
140 
141 	if (s == NULL)
142 		/* Reset to initial shift state (no-op) */
143 		return (0);
144 	if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n)
145 		/* Incomplete multibyte sequence */
146 		return ((size_t)-2);
147 	wc = 0;
148 	remain = len;
149 	switch (set) {
150 	case 3:
151 	case 2:
152 		--remain;
153 		++s;
154 		/* FALLTHROUGH */
155 	case 1:
156 	case 0:
157 		while (remain-- > 0)
158 			wc = (wc << 8) | (unsigned char)*s++;
159 		break;
160 	}
161 	wc = (wc & ~CEI->mask) | CEI->bits[set];
162 	if (pwc != NULL)
163 		*pwc = wc;
164 	return (wc == L'\0' ? 0 : len);
165 }
166 
167 size_t
168 _EUC_wcrtomb(char * __restrict s, wchar_t wc,
169     mbstate_t * __restrict ps __unused)
170 {
171 	wchar_t m, nm;
172 	int i, len;
173 
174 	if (s == NULL)
175 		/* Reset to initial shift state (no-op) */
176 		return (1);
177 
178 	m = wc & CEI->mask;
179 	nm = wc & ~m;
180 
181 	if (m == CEI->bits[1]) {
182 CodeSet1:
183 		/* Codeset 1: The first byte must have 0x80 in it. */
184 		i = len = CEI->count[1];
185 		while (i-- > 0)
186 			*s++ = (nm >> (i << 3)) | 0x80;
187 	} else {
188 		if (m == CEI->bits[0])
189 			i = len = CEI->count[0];
190 		else if (m == CEI->bits[2]) {
191 			i = len = CEI->count[2];
192 			*s++ = _SS2;
193 			--i;
194 			/* SS2 designates G2 into GR */
195 			nm |= GR_BITS;
196 		} else if (m == CEI->bits[3]) {
197 			i = len = CEI->count[3];
198 			*s++ = _SS3;
199 			--i;
200 			/* SS3 designates G3 into GR */
201 			nm |= GR_BITS;
202 		} else
203 			goto CodeSet1;	/* Bletch */
204 		while (i-- > 0)
205 			*s++ = (nm >> (i << 3)) & 0xff;
206 	}
207 	return (len);
208 }
209