Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 17 | #define LOG_TAG "unicode" |
| 18 | |
Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 19 | #include <utils/Unicode.h> |
Sergio Giro | 9de6776 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 20 | #include <limits.h> |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 21 | |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 22 | #include <log/log.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | adbf442 | 2015-07-29 17:45:24 -0700 | [diff] [blame] | 24 | #if defined(_WIN32) |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 25 | # undef nhtol |
| 26 | # undef htonl |
| 27 | # undef nhtos |
| 28 | # undef htons |
| 29 | |
Elliott Hughes | 97ac0e1 | 2014-11-21 23:01:59 -0800 | [diff] [blame] | 30 | # define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) ) |
| 31 | # define htonl(x) ntohl(x) |
| 32 | # define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) ) |
| 33 | # define htons(x) ntohs(x) |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 34 | #else |
| 35 | # include <netinet/in.h> |
| 36 | #endif |
| 37 | |
| 38 | extern "C" { |
| 39 | |
| 40 | static const char32_t kByteMask = 0x000000BF; |
| 41 | static const char32_t kByteMark = 0x00000080; |
| 42 | |
| 43 | // Surrogates aren't valid for UTF-32 characters, so define some |
| 44 | // constants that will let us screen them out. |
| 45 | static const char32_t kUnicodeSurrogateHighStart = 0x0000D800; |
Andreas Gampe | a53c815 | 2014-11-24 09:42:07 -0800 | [diff] [blame] | 46 | // Unused, here for completeness: |
| 47 | // static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF; |
| 48 | // static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 49 | static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF; |
| 50 | static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart; |
| 51 | static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd; |
| 52 | static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF; |
| 53 | |
| 54 | // Mask used to set appropriate bits in first byte of UTF-8 sequence, |
| 55 | // indexed by number of bytes in the sequence. |
| 56 | // 0xxxxxxx |
| 57 | // -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000 |
| 58 | // 110yyyyx 10xxxxxx |
| 59 | // -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0 |
| 60 | // 1110yyyy 10yxxxxx 10xxxxxx |
| 61 | // -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0 |
| 62 | // 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx |
| 63 | // -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0 |
| 64 | static const char32_t kFirstByteMark[] = { |
| 65 | 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0 |
| 66 | }; |
| 67 | |
| 68 | // -------------------------------------------------------------------------- |
| 69 | // UTF-32 |
| 70 | // -------------------------------------------------------------------------- |
| 71 | |
| 72 | /** |
| 73 | * Return number of UTF-8 bytes required for the character. If the character |
| 74 | * is invalid, return size of 0. |
| 75 | */ |
| 76 | static inline size_t utf32_codepoint_utf8_length(char32_t srcChar) |
| 77 | { |
| 78 | // Figure out how many bytes the result will require. |
| 79 | if (srcChar < 0x00000080) { |
| 80 | return 1; |
| 81 | } else if (srcChar < 0x00000800) { |
| 82 | return 2; |
| 83 | } else if (srcChar < 0x00010000) { |
| 84 | if ((srcChar < kUnicodeSurrogateStart) || (srcChar > kUnicodeSurrogateEnd)) { |
| 85 | return 3; |
| 86 | } else { |
| 87 | // Surrogates are invalid UTF-32 characters. |
| 88 | return 0; |
| 89 | } |
| 90 | } |
| 91 | // Max code point for Unicode is 0x0010FFFF. |
| 92 | else if (srcChar <= kUnicodeMaxCodepoint) { |
| 93 | return 4; |
| 94 | } else { |
| 95 | // Invalid UTF-32 character. |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Write out the source character to <dstP>. |
| 101 | |
| 102 | static inline void utf32_codepoint_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes) |
| 103 | { |
| 104 | dstP += bytes; |
| 105 | switch (bytes) |
| 106 | { /* note: everything falls through. */ |
| 107 | case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6; |
| 108 | case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6; |
| 109 | case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6; |
| 110 | case 1: *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | size_t strlen32(const char32_t *s) |
| 115 | { |
| 116 | const char32_t *ss = s; |
| 117 | while ( *ss ) |
| 118 | ss++; |
| 119 | return ss-s; |
| 120 | } |
| 121 | |
| 122 | size_t strnlen32(const char32_t *s, size_t maxlen) |
| 123 | { |
| 124 | const char32_t *ss = s; |
| 125 | while ((maxlen > 0) && *ss) { |
| 126 | ss++; |
| 127 | maxlen--; |
| 128 | } |
| 129 | return ss-s; |
| 130 | } |
| 131 | |
| 132 | static inline int32_t utf32_at_internal(const char* cur, size_t *num_read) |
| 133 | { |
| 134 | const char first_char = *cur; |
| 135 | if ((first_char & 0x80) == 0) { // ASCII |
| 136 | *num_read = 1; |
| 137 | return *cur; |
| 138 | } |
| 139 | cur++; |
| 140 | char32_t mask, to_ignore_mask; |
| 141 | size_t num_to_read = 0; |
| 142 | char32_t utf32 = first_char; |
| 143 | for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80; |
| 144 | (first_char & mask); |
| 145 | num_to_read++, to_ignore_mask |= mask, mask >>= 1) { |
| 146 | // 0x3F == 00111111 |
| 147 | utf32 = (utf32 << 6) + (*cur++ & 0x3F); |
| 148 | } |
| 149 | to_ignore_mask |= mask; |
| 150 | utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1))); |
| 151 | |
| 152 | *num_read = num_to_read; |
| 153 | return static_cast<int32_t>(utf32); |
| 154 | } |
| 155 | |
| 156 | int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index) |
| 157 | { |
| 158 | if (index >= src_len) { |
| 159 | return -1; |
| 160 | } |
| 161 | size_t dummy_index; |
| 162 | if (next_index == NULL) { |
| 163 | next_index = &dummy_index; |
| 164 | } |
| 165 | size_t num_read; |
| 166 | int32_t ret = utf32_at_internal(src + index, &num_read); |
| 167 | if (ret >= 0) { |
| 168 | *next_index = index + num_read; |
| 169 | } |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len) |
| 175 | { |
| 176 | if (src == NULL || src_len == 0) { |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | size_t ret = 0; |
| 181 | const char32_t *end = src + src_len; |
| 182 | while (src < end) { |
| 183 | ret += utf32_codepoint_utf8_length(*src++); |
| 184 | } |
| 185 | return ret; |
| 186 | } |
| 187 | |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 188 | void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len) |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 189 | { |
| 190 | if (src == NULL || src_len == 0 || dst == NULL) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | const char32_t *cur_utf32 = src; |
| 195 | const char32_t *end_utf32 = src + src_len; |
| 196 | char *cur = dst; |
| 197 | while (cur_utf32 < end_utf32) { |
| 198 | size_t len = utf32_codepoint_utf8_length(*cur_utf32); |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 199 | LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 200 | utf32_codepoint_to_utf8((uint8_t *)cur, *cur_utf32++, len); |
| 201 | cur += len; |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 202 | dst_len -= len; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 203 | } |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 204 | LOG_ALWAYS_FATAL_IF(dst_len < 1, "dst_len < 1: %zu < 1", dst_len); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 205 | *cur = '\0'; |
| 206 | } |
| 207 | |
| 208 | // -------------------------------------------------------------------------- |
| 209 | // UTF-16 |
| 210 | // -------------------------------------------------------------------------- |
| 211 | |
| 212 | int strcmp16(const char16_t *s1, const char16_t *s2) |
| 213 | { |
| 214 | char16_t ch; |
| 215 | int d = 0; |
| 216 | |
| 217 | while ( 1 ) { |
| 218 | d = (int)(ch = *s1++) - (int)*s2++; |
| 219 | if ( d || !ch ) |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | return d; |
| 224 | } |
| 225 | |
| 226 | int strncmp16(const char16_t *s1, const char16_t *s2, size_t n) |
| 227 | { |
| 228 | char16_t ch; |
| 229 | int d = 0; |
| 230 | |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 231 | if (n == 0) { |
| 232 | return 0; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 235 | do { |
| 236 | d = (int)(ch = *s1++) - (int)*s2++; |
| 237 | if ( d || !ch ) { |
| 238 | break; |
| 239 | } |
| 240 | } while (--n); |
| 241 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 242 | return d; |
| 243 | } |
| 244 | |
| 245 | char16_t *strcpy16(char16_t *dst, const char16_t *src) |
| 246 | { |
| 247 | char16_t *q = dst; |
| 248 | const char16_t *p = src; |
| 249 | char16_t ch; |
| 250 | |
| 251 | do { |
| 252 | *q++ = ch = *p++; |
| 253 | } while ( ch ); |
| 254 | |
| 255 | return dst; |
| 256 | } |
| 257 | |
| 258 | size_t strlen16(const char16_t *s) |
| 259 | { |
| 260 | const char16_t *ss = s; |
| 261 | while ( *ss ) |
| 262 | ss++; |
| 263 | return ss-s; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | char16_t *strncpy16(char16_t *dst, const char16_t *src, size_t n) |
| 268 | { |
| 269 | char16_t *q = dst; |
| 270 | const char16_t *p = src; |
| 271 | char ch; |
| 272 | |
| 273 | while (n) { |
| 274 | n--; |
| 275 | *q++ = ch = *p++; |
| 276 | if ( !ch ) |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | *q = 0; |
| 281 | |
| 282 | return dst; |
| 283 | } |
| 284 | |
| 285 | size_t strnlen16(const char16_t *s, size_t maxlen) |
| 286 | { |
| 287 | const char16_t *ss = s; |
| 288 | |
| 289 | /* Important: the maxlen test must precede the reference through ss; |
| 290 | since the byte beyond the maximum may segfault */ |
| 291 | while ((maxlen > 0) && *ss) { |
| 292 | ss++; |
| 293 | maxlen--; |
| 294 | } |
| 295 | return ss-s; |
| 296 | } |
| 297 | |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 298 | char16_t* strstr16(const char16_t* src, const char16_t* target) |
| 299 | { |
Branislav Rankov | bf3fff1 | 2017-10-12 15:08:42 +0200 | [diff] [blame] | 300 | const char16_t needle = *target; |
| 301 | if (needle == '\0') return (char16_t*)src; |
| 302 | |
| 303 | const size_t target_len = strlen16(++target); |
| 304 | do { |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 305 | do { |
Branislav Rankov | bf3fff1 | 2017-10-12 15:08:42 +0200 | [diff] [blame] | 306 | if (*src == '\0') { |
| 307 | return nullptr; |
| 308 | } |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 309 | } while (*src++ != needle); |
Branislav Rankov | bf3fff1 | 2017-10-12 15:08:42 +0200 | [diff] [blame] | 310 | } while (strncmp16(src, target, target_len) != 0); |
| 311 | src--; |
Michael Wright | 5bacef3 | 2016-05-09 14:43:31 +0100 | [diff] [blame] | 312 | |
| 313 | return (char16_t*)src; |
| 314 | } |
| 315 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 316 | int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2) |
| 317 | { |
| 318 | const char16_t* e1 = s1+n1; |
| 319 | const char16_t* e2 = s2+n2; |
| 320 | |
| 321 | while (s1 < e1 && s2 < e2) { |
| 322 | const int d = (int)*s1++ - (int)*s2++; |
| 323 | if (d) { |
| 324 | return d; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return n1 < n2 |
| 329 | ? (0 - (int)*s2) |
| 330 | : (n1 > n2 |
| 331 | ? ((int)*s1 - 0) |
| 332 | : 0); |
| 333 | } |
| 334 | |
| 335 | int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2) |
| 336 | { |
| 337 | const char16_t* e1 = s1H+n1; |
| 338 | const char16_t* e2 = s2N+n2; |
| 339 | |
| 340 | while (s1H < e1 && s2N < e2) { |
| 341 | const char16_t c2 = ntohs(*s2N); |
| 342 | const int d = (int)*s1H++ - (int)c2; |
| 343 | s2N++; |
| 344 | if (d) { |
| 345 | return d; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return n1 < n2 |
| 350 | ? (0 - (int)ntohs(*s2N)) |
| 351 | : (n1 > n2 |
| 352 | ? ((int)*s1H - 0) |
| 353 | : 0); |
| 354 | } |
| 355 | |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 356 | void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len) |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 357 | { |
| 358 | if (src == NULL || src_len == 0 || dst == NULL) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | const char16_t* cur_utf16 = src; |
| 363 | const char16_t* const end_utf16 = src + src_len; |
| 364 | char *cur = dst; |
| 365 | while (cur_utf16 < end_utf16) { |
| 366 | char32_t utf32; |
| 367 | // surrogate pairs |
Cylen Yao | 72299bf | 2014-06-04 19:11:27 +0800 | [diff] [blame] | 368 | if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16 |
| 369 | && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 370 | utf32 = (*cur_utf16++ - 0xD800) << 10; |
| 371 | utf32 |= *cur_utf16++ - 0xDC00; |
| 372 | utf32 += 0x10000; |
| 373 | } else { |
| 374 | utf32 = (char32_t) *cur_utf16++; |
| 375 | } |
| 376 | const size_t len = utf32_codepoint_utf8_length(utf32); |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 377 | LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 378 | utf32_codepoint_to_utf8((uint8_t*)cur, utf32, len); |
| 379 | cur += len; |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 380 | dst_len -= len; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 381 | } |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 382 | LOG_ALWAYS_FATAL_IF(dst_len < 1, "%zu < 1", dst_len); |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 383 | *cur = '\0'; |
| 384 | } |
| 385 | |
| 386 | // -------------------------------------------------------------------------- |
| 387 | // UTF-8 |
| 388 | // -------------------------------------------------------------------------- |
| 389 | |
| 390 | ssize_t utf8_length(const char *src) |
| 391 | { |
| 392 | const char *cur = src; |
| 393 | size_t ret = 0; |
| 394 | while (*cur != '\0') { |
| 395 | const char first_char = *cur++; |
| 396 | if ((first_char & 0x80) == 0) { // ASCII |
| 397 | ret += 1; |
| 398 | continue; |
| 399 | } |
| 400 | // (UTF-8's character must not be like 10xxxxxx, |
| 401 | // but 110xxxxx, 1110xxxx, ... or 1111110x) |
| 402 | if ((first_char & 0x40) == 0) { |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | int32_t mask, to_ignore_mask; |
| 407 | size_t num_to_read = 0; |
| 408 | char32_t utf32 = 0; |
| 409 | for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80; |
| 410 | num_to_read < 5 && (first_char & mask); |
| 411 | num_to_read++, to_ignore_mask |= mask, mask >>= 1) { |
| 412 | if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx |
| 413 | return -1; |
| 414 | } |
| 415 | // 0x3F == 00111111 |
| 416 | utf32 = (utf32 << 6) + (*cur++ & 0x3F); |
| 417 | } |
| 418 | // "first_char" must be (110xxxxx - 11110xxx) |
| 419 | if (num_to_read == 5) { |
| 420 | return -1; |
| 421 | } |
| 422 | to_ignore_mask |= mask; |
| 423 | utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1)); |
| 424 | if (utf32 > kUnicodeMaxCodepoint) { |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | ret += num_to_read; |
| 429 | } |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len) |
| 434 | { |
| 435 | if (src == NULL || src_len == 0) { |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | size_t ret = 0; |
| 440 | const char16_t* const end = src + src_len; |
| 441 | while (src < end) { |
| 442 | if ((*src & 0xFC00) == 0xD800 && (src + 1) < end |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 443 | && (*(src + 1) & 0xFC00) == 0xDC00) { |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 444 | // surrogate pairs are always 4 bytes. |
| 445 | ret += 4; |
Sergio Giro | 1cfa56d | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 446 | src += 2; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 447 | } else { |
| 448 | ret += utf32_codepoint_utf8_length((char32_t) *src++); |
| 449 | } |
| 450 | } |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Returns 1-4 based on the number of leading bits. |
| 456 | * |
| 457 | * 1111 -> 4 |
| 458 | * 1110 -> 3 |
| 459 | * 110x -> 2 |
| 460 | * 10xx -> 1 |
| 461 | * 0xxx -> 1 |
| 462 | */ |
| 463 | static inline size_t utf8_codepoint_len(uint8_t ch) |
| 464 | { |
| 465 | return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1; |
| 466 | } |
| 467 | |
| 468 | static inline void utf8_shift_and_mask(uint32_t* codePoint, const uint8_t byte) |
| 469 | { |
| 470 | *codePoint <<= 6; |
| 471 | *codePoint |= 0x3F & byte; |
| 472 | } |
| 473 | |
| 474 | size_t utf8_to_utf32_length(const char *src, size_t src_len) |
| 475 | { |
| 476 | if (src == NULL || src_len == 0) { |
| 477 | return 0; |
| 478 | } |
| 479 | size_t ret = 0; |
| 480 | const char* cur; |
| 481 | const char* end; |
| 482 | size_t num_to_skip; |
| 483 | for (cur = src, end = src + src_len, num_to_skip = 1; |
| 484 | cur < end; |
| 485 | cur += num_to_skip, ret++) { |
| 486 | const char first_char = *cur; |
| 487 | num_to_skip = 1; |
| 488 | if ((first_char & 0x80) == 0) { // ASCII |
| 489 | continue; |
| 490 | } |
| 491 | int32_t mask; |
| 492 | |
| 493 | for (mask = 0x40; (first_char & mask); num_to_skip++, mask >>= 1) { |
| 494 | } |
| 495 | } |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst) |
| 500 | { |
| 501 | if (src == NULL || src_len == 0 || dst == NULL) { |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | const char* cur = src; |
| 506 | const char* const end = src + src_len; |
| 507 | char32_t* cur_utf32 = dst; |
| 508 | while (cur < end) { |
| 509 | size_t num_read; |
| 510 | *cur_utf32++ = static_cast<char32_t>(utf32_at_internal(cur, &num_read)); |
| 511 | cur += num_read; |
| 512 | } |
| 513 | *cur_utf32 = 0; |
| 514 | } |
| 515 | |
| 516 | static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src, size_t length) |
| 517 | { |
| 518 | uint32_t unicode; |
| 519 | |
| 520 | switch (length) |
| 521 | { |
| 522 | case 1: |
| 523 | return src[0]; |
| 524 | case 2: |
| 525 | unicode = src[0] & 0x1f; |
| 526 | utf8_shift_and_mask(&unicode, src[1]); |
| 527 | return unicode; |
| 528 | case 3: |
| 529 | unicode = src[0] & 0x0f; |
| 530 | utf8_shift_and_mask(&unicode, src[1]); |
| 531 | utf8_shift_and_mask(&unicode, src[2]); |
| 532 | return unicode; |
| 533 | case 4: |
| 534 | unicode = src[0] & 0x07; |
| 535 | utf8_shift_and_mask(&unicode, src[1]); |
| 536 | utf8_shift_and_mask(&unicode, src[2]); |
| 537 | utf8_shift_and_mask(&unicode, src[3]); |
| 538 | return unicode; |
| 539 | default: |
| 540 | return 0xffff; |
| 541 | } |
| 542 | |
| 543 | //printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result); |
| 544 | } |
| 545 | |
Sergio Giro | 9de6776 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 546 | ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len, bool overreadIsFatal) |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 547 | { |
| 548 | const uint8_t* const u8end = u8str + u8len; |
| 549 | const uint8_t* u8cur = u8str; |
| 550 | |
| 551 | /* Validate that the UTF-8 is the correct len */ |
| 552 | size_t u16measuredLen = 0; |
| 553 | while (u8cur < u8end) { |
| 554 | u16measuredLen++; |
| 555 | int u8charLen = utf8_codepoint_len(*u8cur); |
Sergio Giro | 9de6776 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 556 | // Malformed utf8, some characters are beyond the end. |
| 557 | // Cases: |
| 558 | // If u8charLen == 1, this becomes u8cur >= u8end, which cannot happen as u8cur < u8end, |
| 559 | // then this condition fail and we continue, as expected. |
| 560 | // If u8charLen == 2, this becomes u8cur + 1 >= u8end, which fails only if |
| 561 | // u8cur == u8end - 1, that is, there was only one remaining character to read but we need |
| 562 | // 2 of them. This condition holds and we return -1, as expected. |
| 563 | if (u8cur + u8charLen - 1 >= u8end) { |
| 564 | if (overreadIsFatal) { |
| 565 | LOG_ALWAYS_FATAL("Attempt to overread computing length of utf8 string"); |
| 566 | } else { |
| 567 | return -1; |
| 568 | } |
| 569 | } |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 570 | uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen); |
| 571 | if (codepoint > 0xFFFF) u16measuredLen++; // this will be a surrogate pair in utf16 |
| 572 | u8cur += u8charLen; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Make sure that we ended where we thought we would and the output UTF-16 |
| 577 | * will be exactly how long we were told it would be. |
| 578 | */ |
| 579 | if (u8cur != u8end) { |
| 580 | return -1; |
| 581 | } |
| 582 | |
| 583 | return u16measuredLen; |
| 584 | } |
| 585 | |
Sergio Giro | 9de6776 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 586 | char16_t* utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str, size_t u16len) { |
| 587 | // A value > SSIZE_MAX is probably a negative value returned as an error and casted. |
| 588 | LOG_ALWAYS_FATAL_IF(u16len == 0 || u16len > SSIZE_MAX, "u16len is %zu", u16len); |
| 589 | char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str, u16len - 1); |
Jeff Brown | aa983c9 | 2011-10-07 13:28:18 -0700 | [diff] [blame] | 590 | *end = 0; |
Sergio Giro | 9de6776 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 591 | return end; |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Sergio Giro | 9de6776 | 2016-07-20 20:01:33 +0100 | [diff] [blame] | 594 | char16_t* utf8_to_utf16_no_null_terminator( |
| 595 | const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) { |
| 596 | if (dstLen == 0) { |
| 597 | return dst; |
| 598 | } |
| 599 | // A value > SSIZE_MAX is probably a negative value returned as an error and casted. |
| 600 | LOG_ALWAYS_FATAL_IF(dstLen > SSIZE_MAX, "dstLen is %zu", dstLen); |
Dianne Hackborn | 0f10d0a | 2013-07-31 16:04:39 -0700 | [diff] [blame] | 601 | const uint8_t* const u8end = src + srcLen; |
| 602 | const uint8_t* u8cur = src; |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 603 | const char16_t* const u16end = dst + dstLen; |
Dianne Hackborn | 0f10d0a | 2013-07-31 16:04:39 -0700 | [diff] [blame] | 604 | char16_t* u16cur = dst; |
| 605 | |
| 606 | while (u8cur < u8end && u16cur < u16end) { |
| 607 | size_t u8len = utf8_codepoint_len(*u8cur); |
| 608 | uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len); |
| 609 | |
| 610 | // Convert the UTF32 codepoint to one or more UTF16 codepoints |
| 611 | if (codepoint <= 0xFFFF) { |
| 612 | // Single UTF16 character |
| 613 | *u16cur++ = (char16_t) codepoint; |
| 614 | } else { |
| 615 | // Multiple UTF16 characters with surrogates |
| 616 | codepoint = codepoint - 0x10000; |
| 617 | *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800); |
| 618 | if (u16cur >= u16end) { |
| 619 | // Ooops... not enough room for this surrogate pair. |
| 620 | return u16cur-1; |
| 621 | } |
| 622 | *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00); |
| 623 | } |
| 624 | |
| 625 | u8cur += u8len; |
| 626 | } |
| 627 | return u16cur; |
| 628 | } |
| 629 | |
Kenny Root | ba0165b | 2010-11-09 14:37:23 -0800 | [diff] [blame] | 630 | } |