xref: /freebsd/lib/libc/locale/utf8.c (revision 7773002178c8dbc52b44e4d705f07706409af8e4)
1 /*-
2  * Copyright (c) 2002, 2003 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/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <errno.h>
31 #include <runetype.h>
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <wchar.h>
36 
37 extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
38     size_t, mbstate_t * __restrict);
39 extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
40 
41 size_t  _UTF8_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
42 	    mbstate_t * __restrict);
43 size_t  _UTF8_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
44 
45 int
46 _UTF8_init(_RuneLocale *rl)
47 {
48 
49 	__mbrtowc = _UTF8_mbrtowc;
50 	__wcrtomb = _UTF8_wcrtomb;
51 	_CurrentRuneLocale = rl;
52 	__mb_cur_max = 6;
53 
54 	return (0);
55 }
56 
57 size_t
58 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
59     mbstate_t * __restrict ps __unused)
60 {
61 	int ch, i, len, mask;
62 	wchar_t lbound, wch;
63 
64 	if (s == NULL)
65 		/* Reset to initial shift state (no-op) */
66 		return (0);
67 	if (n == 0)
68 		/* Incomplete multibyte sequence */
69 		return ((size_t)-2);
70 
71 	/*
72 	 * Determine the number of octets that make up this character from
73 	 * the first octet, and a mask that extracts the interesting bits of
74 	 * the first octet.
75 	 *
76 	 * We also specify a lower bound for the character code to detect
77 	 * redundant, non-"shortest form" encodings. For example, the
78 	 * sequence C0 80 is _not_ a legal representation of the null
79 	 * character. This enforces a 1-to-1 mapping between character
80 	 * codes and their multibyte representations.
81 	 */
82 	ch = (unsigned char)*s;
83 	if ((ch & 0x80) == 0) {
84 		mask = 0x7f;
85 		len = 1;
86 		lbound = 0;
87 	} else if ((ch & 0xe0) == 0xc0) {
88 		mask = 0x1f;
89 		len = 2;
90 		lbound = 0x80;
91 	} else if ((ch & 0xf0) == 0xe0) {
92 		mask = 0x0f;
93 		len = 3;
94 		lbound = 0x800;
95 	} else if ((ch & 0xf8) == 0xf0) {
96 		mask = 0x07;
97 		len = 4;
98 		lbound = 0x10000;
99 	} else if ((ch & 0xfc) == 0xf8) {
100 		mask = 0x03;
101 		len = 5;
102 		lbound = 0x200000;
103 	} else if ((ch & 0xfc) == 0xfc) {
104 		mask = 0x01;
105 		len = 6;
106 		lbound = 0x4000000;
107 	} else {
108 		/*
109 		 * Malformed input; input is not UTF-8.
110 		 */
111 		errno = EILSEQ;
112 		return ((size_t)-1);
113 	}
114 
115 	if (n < (size_t)len)
116 		/* Incomplete multibyte sequence */
117 		return ((size_t)-2);
118 
119 	/*
120 	 * Decode the octet sequence representing the character in chunks
121 	 * of 6 bits, most significant first.
122 	 */
123 	wch = (unsigned char)*s++ & mask;
124 	i = len;
125 	while (--i != 0) {
126 		if ((*s & 0xc0) != 0x80) {
127 			/*
128 			 * Malformed input; bad characters in the middle
129 			 * of a character.
130 			 */
131 			errno = EILSEQ;
132 			return ((size_t)-1);
133 		}
134 		wch <<= 6;
135 		wch |= *s++ & 0x3f;
136 	}
137 	if (wch < lbound) {
138 		/*
139 		 * Malformed input; redundant encoding.
140 		 */
141 		errno = EILSEQ;
142 		return ((size_t)-1);
143 	}
144 	if (pwc != NULL)
145 		*pwc = wch;
146 	return (wch == L'\0' ? 0 : i);
147 }
148 
149 size_t
150 _UTF8_wcrtomb(char * __restrict s, wchar_t wc,
151     mbstate_t * __restrict ps __unused)
152 {
153 	unsigned char lead;
154 	int i, len;
155 
156 	if (s == NULL)
157 		/* Reset to initial shift state (no-op) */
158 		return (1);
159 
160 	/*
161 	 * Determine the number of octets needed to represent this character.
162 	 * We always output the shortest sequence possible. Also specify the
163 	 * first few bits of the first octet, which contains the information
164 	 * about the sequence length.
165 	 */
166 	if ((wc & ~0x7f) == 0) {
167 		lead = 0;
168 		len = 1;
169 	} else if ((wc & ~0x7ff) == 0) {
170 		lead = 0xc0;
171 		len = 2;
172 	} else if ((wc & ~0xffff) == 0) {
173 		lead = 0xe0;
174 		len = 3;
175 	} else if ((wc & ~0x1fffff) == 0) {
176 		lead = 0xf0;
177 		len = 4;
178 	} else if ((wc & ~0x3ffffff) == 0) {
179 		lead = 0xf8;
180 		len = 5;
181 	} else if ((wc & ~0x7fffffff) == 0) {
182 		lead = 0xfc;
183 		len = 6;
184 	} else {
185 		errno = EILSEQ;
186 		return ((size_t)-1);
187 	}
188 
189 	/*
190 	 * Output the octets representing the character in chunks
191 	 * of 6 bits, least significant last. The first octet is
192 	 * a special case because it contains the sequence length
193 	 * information.
194 	 */
195 	for (i = len - 1; i > 0; i--) {
196 		s[i] = (wc & 0x3f) | 0x80;
197 		wc >>= 6;
198 	}
199 	*s = (wc & 0xff) | lead;
200 
201 	return (len);
202 }
203