blob: 5f0a51f1c5be68a835d65178dfc82d02a19b890b [file] [log] [blame]
Kenny Rootba0165b2010-11-09 14:37:23 -08001/*
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 Salyzyncfd5b082016-10-17 14:28:00 -070017#define LOG_TAG "unicode"
18
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -070019#include <android-base/macros.h>
Sergio Giro9de67762016-07-20 20:01:33 +010020#include <limits.h>
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -070021#include <utils/Unicode.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080022
Mark Salyzyn30f991f2017-01-10 13:19:54 -080023#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024
Elliott Hughesadbf4422015-07-29 17:45:24 -070025#if defined(_WIN32)
Kenny Rootba0165b2010-11-09 14:37:23 -080026# undef nhtol
27# undef htonl
28# undef nhtos
29# undef htons
30
Elliott Hughes97ac0e12014-11-21 23:01:59 -080031# define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
32# define htonl(x) ntohl(x)
33# define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
34# define htons(x) ntohs(x)
Kenny Rootba0165b2010-11-09 14:37:23 -080035#else
36# include <netinet/in.h>
37#endif
38
39extern "C" {
40
41static const char32_t kByteMask = 0x000000BF;
42static const char32_t kByteMark = 0x00000080;
43
44// Surrogates aren't valid for UTF-32 characters, so define some
45// constants that will let us screen them out.
46static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
Andreas Gampea53c8152014-11-24 09:42:07 -080047// Unused, here for completeness:
48// static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
49// static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
Kenny Rootba0165b2010-11-09 14:37:23 -080050static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
51static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
52static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
53static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF;
54
55// Mask used to set appropriate bits in first byte of UTF-8 sequence,
56// indexed by number of bytes in the sequence.
57// 0xxxxxxx
58// -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000
59// 110yyyyx 10xxxxxx
60// -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0
61// 1110yyyy 10yxxxxx 10xxxxxx
62// -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0
63// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
64// -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0
65static const char32_t kFirstByteMark[] = {
66 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0
67};
68
69// --------------------------------------------------------------------------
70// UTF-32
71// --------------------------------------------------------------------------
72
73/**
74 * Return number of UTF-8 bytes required for the character. If the character
75 * is invalid, return size of 0.
76 */
77static inline size_t utf32_codepoint_utf8_length(char32_t srcChar)
78{
79 // Figure out how many bytes the result will require.
80 if (srcChar < 0x00000080) {
81 return 1;
82 } else if (srcChar < 0x00000800) {
83 return 2;
84 } else if (srcChar < 0x00010000) {
85 if ((srcChar < kUnicodeSurrogateStart) || (srcChar > kUnicodeSurrogateEnd)) {
86 return 3;
87 } else {
88 // Surrogates are invalid UTF-32 characters.
89 return 0;
90 }
91 }
92 // Max code point for Unicode is 0x0010FFFF.
93 else if (srcChar <= kUnicodeMaxCodepoint) {
94 return 4;
95 } else {
96 // Invalid UTF-32 character.
97 return 0;
98 }
99}
100
101// Write out the source character to <dstP>.
102
103static inline void utf32_codepoint_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes)
104{
105 dstP += bytes;
106 switch (bytes)
107 { /* note: everything falls through. */
108 case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700109 FALLTHROUGH_INTENDED;
Kenny Rootba0165b2010-11-09 14:37:23 -0800110 case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700111 FALLTHROUGH_INTENDED;
Kenny Rootba0165b2010-11-09 14:37:23 -0800112 case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700113 FALLTHROUGH_INTENDED;
Kenny Rootba0165b2010-11-09 14:37:23 -0800114 case 1: *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]);
115 }
116}
117
118size_t strlen32(const char32_t *s)
119{
120 const char32_t *ss = s;
121 while ( *ss )
122 ss++;
123 return ss-s;
124}
125
126size_t strnlen32(const char32_t *s, size_t maxlen)
127{
128 const char32_t *ss = s;
129 while ((maxlen > 0) && *ss) {
130 ss++;
131 maxlen--;
132 }
133 return ss-s;
134}
135
136static inline int32_t utf32_at_internal(const char* cur, size_t *num_read)
137{
138 const char first_char = *cur;
139 if ((first_char & 0x80) == 0) { // ASCII
140 *num_read = 1;
141 return *cur;
142 }
143 cur++;
144 char32_t mask, to_ignore_mask;
145 size_t num_to_read = 0;
146 char32_t utf32 = first_char;
147 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80;
148 (first_char & mask);
149 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
150 // 0x3F == 00111111
151 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
152 }
153 to_ignore_mask |= mask;
154 utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1)));
155
156 *num_read = num_to_read;
157 return static_cast<int32_t>(utf32);
158}
159
160int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index)
161{
162 if (index >= src_len) {
163 return -1;
164 }
165 size_t dummy_index;
Yi Konge1731a42018-07-16 18:11:34 -0700166 if (next_index == nullptr) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800167 next_index = &dummy_index;
168 }
169 size_t num_read;
170 int32_t ret = utf32_at_internal(src + index, &num_read);
171 if (ret >= 0) {
172 *next_index = index + num_read;
173 }
174
175 return ret;
176}
177
178ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len)
179{
Yi Konge1731a42018-07-16 18:11:34 -0700180 if (src == nullptr || src_len == 0) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800181 return -1;
182 }
183
184 size_t ret = 0;
185 const char32_t *end = src + src_len;
186 while (src < end) {
Adam Vartanian47efc672017-08-14 15:51:29 +0100187 size_t char_len = utf32_codepoint_utf8_length(*src++);
188 if (SSIZE_MAX - char_len < ret) {
189 // If this happens, we would overflow the ssize_t type when
190 // returning from this function, so we cannot express how
191 // long this string is in an ssize_t.
192 android_errorWriteLog(0x534e4554, "37723026");
193 return -1;
194 }
195 ret += char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800196 }
197 return ret;
198}
199
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100200void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800201{
Yi Konge1731a42018-07-16 18:11:34 -0700202 if (src == nullptr || src_len == 0 || dst == nullptr) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800203 return;
204 }
205
206 const char32_t *cur_utf32 = src;
207 const char32_t *end_utf32 = src + src_len;
208 char *cur = dst;
209 while (cur_utf32 < end_utf32) {
210 size_t len = utf32_codepoint_utf8_length(*cur_utf32);
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100211 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800212 utf32_codepoint_to_utf8((uint8_t *)cur, *cur_utf32++, len);
213 cur += len;
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100214 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800215 }
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100216 LOG_ALWAYS_FATAL_IF(dst_len < 1, "dst_len < 1: %zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800217 *cur = '\0';
218}
219
220// --------------------------------------------------------------------------
221// UTF-16
222// --------------------------------------------------------------------------
223
224int strcmp16(const char16_t *s1, const char16_t *s2)
225{
226 char16_t ch;
227 int d = 0;
228
229 while ( 1 ) {
230 d = (int)(ch = *s1++) - (int)*s2++;
231 if ( d || !ch )
232 break;
233 }
234
235 return d;
236}
237
238int strncmp16(const char16_t *s1, const char16_t *s2, size_t n)
239{
240 char16_t ch;
241 int d = 0;
242
Michael Wright5bacef32016-05-09 14:43:31 +0100243 if (n == 0) {
244 return 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800245 }
246
Michael Wright5bacef32016-05-09 14:43:31 +0100247 do {
248 d = (int)(ch = *s1++) - (int)*s2++;
249 if ( d || !ch ) {
250 break;
251 }
252 } while (--n);
253
Kenny Rootba0165b2010-11-09 14:37:23 -0800254 return d;
255}
256
257char16_t *strcpy16(char16_t *dst, const char16_t *src)
258{
259 char16_t *q = dst;
260 const char16_t *p = src;
261 char16_t ch;
262
263 do {
264 *q++ = ch = *p++;
265 } while ( ch );
266
267 return dst;
268}
269
270size_t strlen16(const char16_t *s)
271{
272 const char16_t *ss = s;
273 while ( *ss )
274 ss++;
275 return ss-s;
276}
277
278
279char16_t *strncpy16(char16_t *dst, const char16_t *src, size_t n)
280{
281 char16_t *q = dst;
282 const char16_t *p = src;
283 char ch;
284
285 while (n) {
286 n--;
287 *q++ = ch = *p++;
288 if ( !ch )
289 break;
290 }
291
292 *q = 0;
293
294 return dst;
295}
296
297size_t strnlen16(const char16_t *s, size_t maxlen)
298{
299 const char16_t *ss = s;
300
301 /* Important: the maxlen test must precede the reference through ss;
302 since the byte beyond the maximum may segfault */
303 while ((maxlen > 0) && *ss) {
304 ss++;
305 maxlen--;
306 }
307 return ss-s;
308}
309
Michael Wright5bacef32016-05-09 14:43:31 +0100310char16_t* strstr16(const char16_t* src, const char16_t* target)
311{
Branislav Rankovbf3fff12017-10-12 15:08:42 +0200312 const char16_t needle = *target;
313 if (needle == '\0') return (char16_t*)src;
314
315 const size_t target_len = strlen16(++target);
316 do {
Michael Wright5bacef32016-05-09 14:43:31 +0100317 do {
Branislav Rankovbf3fff12017-10-12 15:08:42 +0200318 if (*src == '\0') {
319 return nullptr;
320 }
Michael Wright5bacef32016-05-09 14:43:31 +0100321 } while (*src++ != needle);
Branislav Rankovbf3fff12017-10-12 15:08:42 +0200322 } while (strncmp16(src, target, target_len) != 0);
323 src--;
Michael Wright5bacef32016-05-09 14:43:31 +0100324
325 return (char16_t*)src;
326}
327
Kenny Rootba0165b2010-11-09 14:37:23 -0800328int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2)
329{
330 const char16_t* e1 = s1+n1;
331 const char16_t* e2 = s2+n2;
332
333 while (s1 < e1 && s2 < e2) {
334 const int d = (int)*s1++ - (int)*s2++;
335 if (d) {
336 return d;
337 }
338 }
339
340 return n1 < n2
341 ? (0 - (int)*s2)
342 : (n1 > n2
343 ? ((int)*s1 - 0)
344 : 0);
345}
346
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100347void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800348{
Yi Konge1731a42018-07-16 18:11:34 -0700349 if (src == nullptr || src_len == 0 || dst == nullptr) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800350 return;
351 }
352
353 const char16_t* cur_utf16 = src;
354 const char16_t* const end_utf16 = src + src_len;
355 char *cur = dst;
356 while (cur_utf16 < end_utf16) {
357 char32_t utf32;
358 // surrogate pairs
Cylen Yao72299bf2014-06-04 19:11:27 +0800359 if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16
360 && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800361 utf32 = (*cur_utf16++ - 0xD800) << 10;
362 utf32 |= *cur_utf16++ - 0xDC00;
363 utf32 += 0x10000;
364 } else {
365 utf32 = (char32_t) *cur_utf16++;
366 }
367 const size_t len = utf32_codepoint_utf8_length(utf32);
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100368 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800369 utf32_codepoint_to_utf8((uint8_t*)cur, utf32, len);
370 cur += len;
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100371 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800372 }
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100373 LOG_ALWAYS_FATAL_IF(dst_len < 1, "%zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800374 *cur = '\0';
375}
376
377// --------------------------------------------------------------------------
378// UTF-8
379// --------------------------------------------------------------------------
380
381ssize_t utf8_length(const char *src)
382{
383 const char *cur = src;
384 size_t ret = 0;
385 while (*cur != '\0') {
386 const char first_char = *cur++;
387 if ((first_char & 0x80) == 0) { // ASCII
388 ret += 1;
389 continue;
390 }
391 // (UTF-8's character must not be like 10xxxxxx,
392 // but 110xxxxx, 1110xxxx, ... or 1111110x)
393 if ((first_char & 0x40) == 0) {
394 return -1;
395 }
396
397 int32_t mask, to_ignore_mask;
398 size_t num_to_read = 0;
399 char32_t utf32 = 0;
400 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
401 num_to_read < 5 && (first_char & mask);
402 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
403 if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx
404 return -1;
405 }
406 // 0x3F == 00111111
407 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
408 }
409 // "first_char" must be (110xxxxx - 11110xxx)
410 if (num_to_read == 5) {
411 return -1;
412 }
413 to_ignore_mask |= mask;
414 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
415 if (utf32 > kUnicodeMaxCodepoint) {
416 return -1;
417 }
418
419 ret += num_to_read;
420 }
421 return ret;
422}
423
424ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
425{
Yi Konge1731a42018-07-16 18:11:34 -0700426 if (src == nullptr || src_len == 0) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800427 return -1;
428 }
429
430 size_t ret = 0;
431 const char16_t* const end = src + src_len;
432 while (src < end) {
Adam Vartanian47efc672017-08-14 15:51:29 +0100433 size_t char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800434 if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100435 && (*(src + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800436 // surrogate pairs are always 4 bytes.
Adam Vartanian47efc672017-08-14 15:51:29 +0100437 char_len = 4;
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100438 src += 2;
Kenny Rootba0165b2010-11-09 14:37:23 -0800439 } else {
Adam Vartanian47efc672017-08-14 15:51:29 +0100440 char_len = utf32_codepoint_utf8_length((char32_t)*src++);
Kenny Rootba0165b2010-11-09 14:37:23 -0800441 }
Adam Vartanian47efc672017-08-14 15:51:29 +0100442 if (SSIZE_MAX - char_len < ret) {
443 // If this happens, we would overflow the ssize_t type when
444 // returning from this function, so we cannot express how
445 // long this string is in an ssize_t.
446 android_errorWriteLog(0x534e4554, "37723026");
447 return -1;
448 }
449 ret += char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800450 }
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 */
463static inline size_t utf8_codepoint_len(uint8_t ch)
464{
465 return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
466}
467
468static inline void utf8_shift_and_mask(uint32_t* codePoint, const uint8_t byte)
469{
470 *codePoint <<= 6;
471 *codePoint |= 0x3F & byte;
472}
473
474size_t utf8_to_utf32_length(const char *src, size_t src_len)
475{
Yi Konge1731a42018-07-16 18:11:34 -0700476 if (src == nullptr || src_len == 0) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800477 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
499void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst)
500{
Yi Konge1731a42018-07-16 18:11:34 -0700501 if (src == nullptr || src_len == 0 || dst == nullptr) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800502 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
516static 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 Giro9de67762016-07-20 20:01:33 +0100546ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len, bool overreadIsFatal)
Kenny Rootba0165b2010-11-09 14:37:23 -0800547{
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 Giro9de67762016-07-20 20:01:33 +0100556 // 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 Rootba0165b2010-11-09 14:37:23 -0800570 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 Giro9de67762016-07-20 20:01:33 +0100586char16_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 Brownaa983c92011-10-07 13:28:18 -0700590 *end = 0;
Sergio Giro9de67762016-07-20 20:01:33 +0100591 return end;
Kenny Rootba0165b2010-11-09 14:37:23 -0800592}
593
Sergio Giro9de67762016-07-20 20:01:33 +0100594char16_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 Hackborn0f10d0a2013-07-31 16:04:39 -0700601 const uint8_t* const u8end = src + srcLen;
602 const uint8_t* u8cur = src;
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700603 const char16_t* const u16end = dst + dstLen;
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700604 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 Rootba0165b2010-11-09 14:37:23 -0800630}