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 wchar_t ch; 44 int want; 45 wchar_t lbound; 46 } _UTF8State; 47 48 int 49 _UTF8_init(_RuneLocale *rl) 50 { 51 52 __mbrtowc = _UTF8_mbrtowc; 53 __wcrtomb = _UTF8_wcrtomb; 54 __mbsinit = _UTF8_mbsinit; 55 _CurrentRuneLocale = rl; 56 __mb_cur_max = 6; 57 58 return (0); 59 } 60 61 int 62 _UTF8_mbsinit(const mbstate_t *ps) 63 { 64 65 return (ps == NULL || ((const _UTF8State *)ps)->want == 0); 66 } 67 68 size_t 69 _UTF8_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, 70 mbstate_t * __restrict ps) 71 { 72 _UTF8State *us; 73 int ch, i, mask, want; 74 wchar_t lbound, wch; 75 76 us = (_UTF8State *)ps; 77 78 if (us->want < 0 || us->want > 6) { 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 if (n == 0) 90 /* Incomplete multibyte sequence */ 91 return ((size_t)-2); 92 93 if (us->want == 0 && ((ch = (unsigned char)*s) & ~0x7f) == 0) { 94 /* Fast path for plain ASCII characters. */ 95 if (pwc != NULL) 96 *pwc = ch; 97 return (ch != '\0' ? 1 : 0); 98 } 99 100 if (us->want == 0) { 101 /* 102 * Determine the number of octets that make up this character 103 * from the first octet, and a mask that extracts the 104 * interesting bits of the first octet. We already know 105 * the character is at least two bytes long. 106 * 107 * We also specify a lower bound for the character code to 108 * detect redundant, non-"shortest form" encodings. For 109 * example, the sequence C0 80 is _not_ a legal representation 110 * of the null character. This enforces a 1-to-1 mapping 111 * between character codes and their multibyte representations. 112 */ 113 ch = (unsigned char)*s; 114 if ((ch & 0x80) == 0) { 115 mask = 0x7f; 116 want = 1; 117 lbound = 0; 118 } else if ((ch & 0xe0) == 0xc0) { 119 mask = 0x1f; 120 want = 2; 121 lbound = 0x80; 122 } else if ((ch & 0xf0) == 0xe0) { 123 mask = 0x0f; 124 want = 3; 125 lbound = 0x800; 126 } else if ((ch & 0xf8) == 0xf0) { 127 mask = 0x07; 128 want = 4; 129 lbound = 0x10000; 130 } else if ((ch & 0xfc) == 0xf8) { 131 mask = 0x03; 132 want = 5; 133 lbound = 0x200000; 134 } else if ((ch & 0xfc) == 0xfc) { 135 mask = 0x01; 136 want = 6; 137 lbound = 0x4000000; 138 } else { 139 /* 140 * Malformed input; input is not UTF-8. 141 */ 142 errno = EILSEQ; 143 return ((size_t)-1); 144 } 145 } else { 146 want = us->want; 147 lbound = us->lbound; 148 } 149 150 /* 151 * Decode the octet sequence representing the character in chunks 152 * of 6 bits, most significant first. 153 */ 154 if (us->want == 0) 155 wch = (unsigned char)*s++ & mask; 156 else 157 wch = us->ch; 158 for (i = (us->want == 0) ? 1 : 0; i < MIN(want, n); i++) { 159 if ((*s & 0xc0) != 0x80) { 160 /* 161 * Malformed input; bad characters in the middle 162 * of a character. 163 */ 164 errno = EILSEQ; 165 return ((size_t)-1); 166 } 167 wch <<= 6; 168 wch |= *s++ & 0x3f; 169 } 170 if (i < want) { 171 /* Incomplete multibyte sequence. */ 172 us->want = want - i; 173 us->lbound = lbound; 174 us->ch = wch; 175 return ((size_t)-2); 176 } 177 if (wch < lbound) { 178 /* 179 * Malformed input; redundant encoding. 180 */ 181 errno = EILSEQ; 182 return ((size_t)-1); 183 } 184 if (pwc != NULL) 185 *pwc = wch; 186 us->want = 0; 187 return (wch == L'\0' ? 0 : want); 188 } 189 190 size_t 191 _UTF8_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps) 192 { 193 _UTF8State *us; 194 unsigned char lead; 195 int i, len; 196 197 us = (_UTF8State *)ps; 198 199 if (us->want != 0) { 200 errno = EINVAL; 201 return ((size_t)-1); 202 } 203 204 if (s == NULL) 205 /* Reset to initial shift state (no-op) */ 206 return (1); 207 208 if ((wc & ~0x7f) == 0) { 209 /* Fast path for plain ASCII characters. */ 210 *s = (char)wc; 211 return (1); 212 } 213 214 /* 215 * Determine the number of octets needed to represent this character. 216 * We always output the shortest sequence possible. Also specify the 217 * first few bits of the first octet, which contains the information 218 * about the sequence length. 219 */ 220 if ((wc & ~0x7f) == 0) { 221 lead = 0; 222 len = 1; 223 } else if ((wc & ~0x7ff) == 0) { 224 lead = 0xc0; 225 len = 2; 226 } else if ((wc & ~0xffff) == 0) { 227 lead = 0xe0; 228 len = 3; 229 } else if ((wc & ~0x1fffff) == 0) { 230 lead = 0xf0; 231 len = 4; 232 } else if ((wc & ~0x3ffffff) == 0) { 233 lead = 0xf8; 234 len = 5; 235 } else if ((wc & ~0x7fffffff) == 0) { 236 lead = 0xfc; 237 len = 6; 238 } else { 239 errno = EILSEQ; 240 return ((size_t)-1); 241 } 242 243 /* 244 * Output the octets representing the character in chunks 245 * of 6 bits, least significant last. The first octet is 246 * a special case because it contains the sequence length 247 * information. 248 */ 249 for (i = len - 1; i > 0; i--) { 250 s[i] = (wc & 0x3f) | 0x80; 251 wc >>= 6; 252 } 253 *s = (wc & 0xff) | lead; 254 255 return (len); 256 } 257