xref: /freebsd/lib/libc/locale/utf8.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*-
2  * Copyright (c) 2002-2004 Tim J. Robbins
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <errno.h>
31 #include <runetype.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <wchar.h>
35 #include "mblocal.h"
36 
37 size_t	_UTF8_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
38 	    mbstate_t * __restrict);
39 int	_UTF8_mbsinit(const mbstate_t *);
40 size_t	_UTF8_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
41 
42 typedef struct {
43 	int	count;
44 	u_char	bytes[6];
45 } _UTF8State;
46 
47 int
48 _UTF8_init(_RuneLocale *rl)
49 {
50 
51 	__mbrtowc = _UTF8_mbrtowc;
52 	__wcrtomb = _UTF8_wcrtomb;
53 	__mbsinit = _UTF8_mbsinit;
54 	_CurrentRuneLocale = rl;
55 	__mb_cur_max = 6;
56 
57 	return (0);
58 }
59 
60 int
61 _UTF8_mbsinit(const mbstate_t *ps)
62 {
63 
64 	return (ps == NULL || ((const _UTF8State *)ps)->count == 0);
65 }
66 
67 size_t
68 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
69     mbstate_t * __restrict ps)
70 {
71 	_UTF8State *us;
72 	int ch, i, len, mask, ocount;
73 	wchar_t lbound, wch;
74 	size_t ncopy;
75 
76 	us = (_UTF8State *)ps;
77 
78 	if (us->count < 0 || us->count > sizeof(us->bytes)) {
79 		errno = EINVAL;
80 		return ((size_t)-1);
81 	}
82 
83 	if (s == NULL) {
84 		s = "";
85 		n = 1;
86 		pwc = NULL;
87 	}
88 
89 	ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(us->bytes) - us->count);
90 	memcpy(us->bytes + us->count, s, ncopy);
91 	ocount = us->count;
92 	us->count += ncopy;
93 	s = (char *)us->bytes;
94 	n = us->count;
95 
96 	if (n == 0)
97 		/* Incomplete multibyte sequence */
98 		return ((size_t)-2);
99 
100 	/*
101 	 * Determine the number of octets that make up this character from
102 	 * the first octet, and a mask that extracts the interesting bits of
103 	 * the first octet.
104 	 *
105 	 * We also specify a lower bound for the character code to detect
106 	 * redundant, non-"shortest form" encodings. For example, the
107 	 * sequence C0 80 is _not_ a legal representation of the null
108 	 * character. This enforces a 1-to-1 mapping between character
109 	 * codes and their multibyte representations.
110 	 */
111 	ch = (unsigned char)*s;
112 	if ((ch & 0x80) == 0) {
113 		mask = 0x7f;
114 		len = 1;
115 		lbound = 0;
116 	} else if ((ch & 0xe0) == 0xc0) {
117 		mask = 0x1f;
118 		len = 2;
119 		lbound = 0x80;
120 	} else if ((ch & 0xf0) == 0xe0) {
121 		mask = 0x0f;
122 		len = 3;
123 		lbound = 0x800;
124 	} else if ((ch & 0xf8) == 0xf0) {
125 		mask = 0x07;
126 		len = 4;
127 		lbound = 0x10000;
128 	} else if ((ch & 0xfc) == 0xf8) {
129 		mask = 0x03;
130 		len = 5;
131 		lbound = 0x200000;
132 	} else if ((ch & 0xfc) == 0xfc) {
133 		mask = 0x01;
134 		len = 6;
135 		lbound = 0x4000000;
136 	} else {
137 		/*
138 		 * Malformed input; input is not UTF-8.
139 		 */
140 		errno = EILSEQ;
141 		return ((size_t)-1);
142 	}
143 
144 	if (n < (size_t)len)
145 		/* Incomplete multibyte sequence */
146 		return ((size_t)-2);
147 
148 	/*
149 	 * Decode the octet sequence representing the character in chunks
150 	 * of 6 bits, most significant first.
151 	 */
152 	wch = (unsigned char)*s++ & mask;
153 	i = len;
154 	while (--i != 0) {
155 		if ((*s & 0xc0) != 0x80) {
156 			/*
157 			 * Malformed input; bad characters in the middle
158 			 * of a character.
159 			 */
160 			errno = EILSEQ;
161 			return ((size_t)-1);
162 		}
163 		wch <<= 6;
164 		wch |= *s++ & 0x3f;
165 	}
166 	if (wch < lbound) {
167 		/*
168 		 * Malformed input; redundant encoding.
169 		 */
170 		errno = EILSEQ;
171 		return ((size_t)-1);
172 	}
173 	if (pwc != NULL)
174 		*pwc = wch;
175 	us->count = 0;
176 	return (wch == L'\0' ? 0 : len - ocount);
177 }
178 
179 size_t
180 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
181 {
182 	_UTF8State *us;
183 	unsigned char lead;
184 	int i, len;
185 
186 	us = (_UTF8State *)ps;
187 
188 	if (us->count < 0 || us->count > sizeof(us->bytes)) {
189 		errno = EINVAL;
190 		return ((size_t)-1);
191 	}
192 
193 	if (s == NULL)
194 		/* Reset to initial shift state (no-op) */
195 		return (1);
196 
197 	/*
198 	 * Determine the number of octets needed to represent this character.
199 	 * We always output the shortest sequence possible. Also specify the
200 	 * first few bits of the first octet, which contains the information
201 	 * about the sequence length.
202 	 */
203 	if ((wc & ~0x7f) == 0) {
204 		lead = 0;
205 		len = 1;
206 	} else if ((wc & ~0x7ff) == 0) {
207 		lead = 0xc0;
208 		len = 2;
209 	} else if ((wc & ~0xffff) == 0) {
210 		lead = 0xe0;
211 		len = 3;
212 	} else if ((wc & ~0x1fffff) == 0) {
213 		lead = 0xf0;
214 		len = 4;
215 	} else if ((wc & ~0x3ffffff) == 0) {
216 		lead = 0xf8;
217 		len = 5;
218 	} else if ((wc & ~0x7fffffff) == 0) {
219 		lead = 0xfc;
220 		len = 6;
221 	} else {
222 		errno = EILSEQ;
223 		return ((size_t)-1);
224 	}
225 
226 	/*
227 	 * Output the octets representing the character in chunks
228 	 * of 6 bits, least significant last. The first octet is
229 	 * a special case because it contains the sequence length
230 	 * information.
231 	 */
232 	for (i = len - 1; i > 0; i--) {
233 		s[i] = (wc & 0x3f) | 0x80;
234 		wc >>= 6;
235 	}
236 	*s = (wc & 0xff) | lead;
237 
238 	return (len);
239 }
240