blob: dd03c6ba127193b8503a86c04dcef892a8fa76ec [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
Sergio Girob0224472016-06-28 18:02:29 +010017#include <log/log.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080018#include <utils/Unicode.h>
19
20#include <stddef.h>
Adam Vartanianc17624d2017-08-14 15:51:29 +010021#include <limits.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080022
23#ifdef HAVE_WINSOCK
24# undef nhtol
25# undef htonl
26# undef nhtos
27# undef htons
28
29# ifdef HAVE_LITTLE_ENDIAN
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)
34# else
35# define ntohl(x) (x)
36# define htonl(x) (x)
37# define ntohs(x) (x)
38# define htons(x) (x)
39# endif
40#else
41# include <netinet/in.h>
42#endif
43
44extern "C" {
45
46static const char32_t kByteMask = 0x000000BF;
47static const char32_t kByteMark = 0x00000080;
48
49// Surrogates aren't valid for UTF-32 characters, so define some
50// constants that will let us screen them out.
51static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
52static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
53static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
54static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
55static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
56static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
57static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF;
58
59// Mask used to set appropriate bits in first byte of UTF-8 sequence,
60// indexed by number of bytes in the sequence.
61// 0xxxxxxx
62// -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000
63// 110yyyyx 10xxxxxx
64// -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0
65// 1110yyyy 10yxxxxx 10xxxxxx
66// -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0
67// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
68// -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0
69static const char32_t kFirstByteMark[] = {
70 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0
71};
72
73// --------------------------------------------------------------------------
74// UTF-32
75// --------------------------------------------------------------------------
76
77/**
78 * Return number of UTF-8 bytes required for the character. If the character
79 * is invalid, return size of 0.
80 */
81static inline size_t utf32_codepoint_utf8_length(char32_t srcChar)
82{
83 // Figure out how many bytes the result will require.
84 if (srcChar < 0x00000080) {
85 return 1;
86 } else if (srcChar < 0x00000800) {
87 return 2;
88 } else if (srcChar < 0x00010000) {
89 if ((srcChar < kUnicodeSurrogateStart) || (srcChar > kUnicodeSurrogateEnd)) {
90 return 3;
91 } else {
92 // Surrogates are invalid UTF-32 characters.
93 return 0;
94 }
95 }
96 // Max code point for Unicode is 0x0010FFFF.
97 else if (srcChar <= kUnicodeMaxCodepoint) {
98 return 4;
99 } else {
100 // Invalid UTF-32 character.
101 return 0;
102 }
103}
104
105// Write out the source character to <dstP>.
106
107static inline void utf32_codepoint_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes)
108{
109 dstP += bytes;
110 switch (bytes)
111 { /* note: everything falls through. */
112 case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
113 case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
114 case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
115 case 1: *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]);
116 }
117}
118
119size_t strlen32(const char32_t *s)
120{
121 const char32_t *ss = s;
122 while ( *ss )
123 ss++;
124 return ss-s;
125}
126
127size_t strnlen32(const char32_t *s, size_t maxlen)
128{
129 const char32_t *ss = s;
130 while ((maxlen > 0) && *ss) {
131 ss++;
132 maxlen--;
133 }
134 return ss-s;
135}
136
137static inline int32_t utf32_at_internal(const char* cur, size_t *num_read)
138{
139 const char first_char = *cur;
140 if ((first_char & 0x80) == 0) { // ASCII
141 *num_read = 1;
142 return *cur;
143 }
144 cur++;
145 char32_t mask, to_ignore_mask;
146 size_t num_to_read = 0;
147 char32_t utf32 = first_char;
148 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80;
149 (first_char & mask);
150 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
151 // 0x3F == 00111111
152 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
153 }
154 to_ignore_mask |= mask;
155 utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1)));
156
157 *num_read = num_to_read;
158 return static_cast<int32_t>(utf32);
159}
160
161int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index)
162{
163 if (index >= src_len) {
164 return -1;
165 }
166 size_t dummy_index;
167 if (next_index == NULL) {
168 next_index = &dummy_index;
169 }
170 size_t num_read;
171 int32_t ret = utf32_at_internal(src + index, &num_read);
172 if (ret >= 0) {
173 *next_index = index + num_read;
174 }
175
176 return ret;
177}
178
179ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len)
180{
181 if (src == NULL || src_len == 0) {
182 return -1;
183 }
184
185 size_t ret = 0;
186 const char32_t *end = src + src_len;
187 while (src < end) {
Adam Vartanianc17624d2017-08-14 15:51:29 +0100188 size_t char_len = utf32_codepoint_utf8_length(*src++);
189 if (SSIZE_MAX - char_len < ret) {
190 // If this happens, we would overflow the ssize_t type when
191 // returning from this function, so we cannot express how
192 // long this string is in an ssize_t.
193 return -1;
194 }
195 ret += char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800196 }
197 return ret;
198}
199
Sergio Girob0224472016-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{
202 if (src == NULL || src_len == 0 || dst == NULL) {
203 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 Girob0224472016-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 Girob0224472016-06-28 18:02:29 +0100214 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800215 }
Sergio Girob0224472016-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
243 while ( n-- ) {
244 d = (int)(ch = *s1++) - (int)*s2++;
245 if ( d || !ch )
246 break;
247 }
248
249 return d;
250}
251
252char16_t *strcpy16(char16_t *dst, const char16_t *src)
253{
254 char16_t *q = dst;
255 const char16_t *p = src;
256 char16_t ch;
257
258 do {
259 *q++ = ch = *p++;
260 } while ( ch );
261
262 return dst;
263}
264
265size_t strlen16(const char16_t *s)
266{
267 const char16_t *ss = s;
268 while ( *ss )
269 ss++;
270 return ss-s;
271}
272
273
274char16_t *strncpy16(char16_t *dst, const char16_t *src, size_t n)
275{
276 char16_t *q = dst;
277 const char16_t *p = src;
278 char ch;
279
280 while (n) {
281 n--;
282 *q++ = ch = *p++;
283 if ( !ch )
284 break;
285 }
286
287 *q = 0;
288
289 return dst;
290}
291
292size_t strnlen16(const char16_t *s, size_t maxlen)
293{
294 const char16_t *ss = s;
295
296 /* Important: the maxlen test must precede the reference through ss;
297 since the byte beyond the maximum may segfault */
298 while ((maxlen > 0) && *ss) {
299 ss++;
300 maxlen--;
301 }
302 return ss-s;
303}
304
305int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2)
306{
307 const char16_t* e1 = s1+n1;
308 const char16_t* e2 = s2+n2;
309
310 while (s1 < e1 && s2 < e2) {
311 const int d = (int)*s1++ - (int)*s2++;
312 if (d) {
313 return d;
314 }
315 }
316
317 return n1 < n2
318 ? (0 - (int)*s2)
319 : (n1 > n2
320 ? ((int)*s1 - 0)
321 : 0);
322}
323
324int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2)
325{
326 const char16_t* e1 = s1H+n1;
327 const char16_t* e2 = s2N+n2;
328
329 while (s1H < e1 && s2N < e2) {
330 const char16_t c2 = ntohs(*s2N);
331 const int d = (int)*s1H++ - (int)c2;
332 s2N++;
333 if (d) {
334 return d;
335 }
336 }
337
338 return n1 < n2
339 ? (0 - (int)ntohs(*s2N))
340 : (n1 > n2
341 ? ((int)*s1H - 0)
342 : 0);
343}
344
Sergio Girob0224472016-06-28 18:02:29 +0100345void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800346{
347 if (src == NULL || src_len == 0 || dst == NULL) {
348 return;
349 }
350
351 const char16_t* cur_utf16 = src;
352 const char16_t* const end_utf16 = src + src_len;
353 char *cur = dst;
354 while (cur_utf16 < end_utf16) {
355 char32_t utf32;
356 // surrogate pairs
Cylen Yao72299bf2014-06-04 19:11:27 +0800357 if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16
358 && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800359 utf32 = (*cur_utf16++ - 0xD800) << 10;
360 utf32 |= *cur_utf16++ - 0xDC00;
361 utf32 += 0x10000;
362 } else {
363 utf32 = (char32_t) *cur_utf16++;
364 }
365 const size_t len = utf32_codepoint_utf8_length(utf32);
Sergio Girob0224472016-06-28 18:02:29 +0100366 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800367 utf32_codepoint_to_utf8((uint8_t*)cur, utf32, len);
368 cur += len;
Sergio Girob0224472016-06-28 18:02:29 +0100369 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800370 }
Sergio Girob0224472016-06-28 18:02:29 +0100371 LOG_ALWAYS_FATAL_IF(dst_len < 1, "%zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800372 *cur = '\0';
373}
374
375// --------------------------------------------------------------------------
376// UTF-8
377// --------------------------------------------------------------------------
378
379ssize_t utf8_length(const char *src)
380{
381 const char *cur = src;
382 size_t ret = 0;
383 while (*cur != '\0') {
384 const char first_char = *cur++;
385 if ((first_char & 0x80) == 0) { // ASCII
386 ret += 1;
387 continue;
388 }
389 // (UTF-8's character must not be like 10xxxxxx,
390 // but 110xxxxx, 1110xxxx, ... or 1111110x)
391 if ((first_char & 0x40) == 0) {
392 return -1;
393 }
394
395 int32_t mask, to_ignore_mask;
396 size_t num_to_read = 0;
397 char32_t utf32 = 0;
398 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
399 num_to_read < 5 && (first_char & mask);
400 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
401 if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx
402 return -1;
403 }
404 // 0x3F == 00111111
405 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
406 }
407 // "first_char" must be (110xxxxx - 11110xxx)
408 if (num_to_read == 5) {
409 return -1;
410 }
411 to_ignore_mask |= mask;
412 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
413 if (utf32 > kUnicodeMaxCodepoint) {
414 return -1;
415 }
416
417 ret += num_to_read;
418 }
419 return ret;
420}
421
422ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
423{
424 if (src == NULL || src_len == 0) {
425 return -1;
426 }
427
428 size_t ret = 0;
429 const char16_t* const end = src + src_len;
430 while (src < end) {
Adam Vartanianc17624d2017-08-14 15:51:29 +0100431 size_t char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800432 if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
Sergio Girob0224472016-06-28 18:02:29 +0100433 && (*(src + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800434 // surrogate pairs are always 4 bytes.
Adam Vartanianc17624d2017-08-14 15:51:29 +0100435 char_len = 4;
Sergio Girob0224472016-06-28 18:02:29 +0100436 src += 2;
Kenny Rootba0165b2010-11-09 14:37:23 -0800437 } else {
Adam Vartanianc17624d2017-08-14 15:51:29 +0100438 char_len = utf32_codepoint_utf8_length((char32_t)*src++);
Kenny Rootba0165b2010-11-09 14:37:23 -0800439 }
Adam Vartanianc17624d2017-08-14 15:51:29 +0100440 if (SSIZE_MAX - char_len < ret) {
441 // If this happens, we would overflow the ssize_t type when
442 // returning from this function, so we cannot express how
443 // long this string is in an ssize_t.
444 return -1;
445 }
446 ret += char_len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800447 }
448 return ret;
449}
450
451/**
452 * Returns 1-4 based on the number of leading bits.
453 *
454 * 1111 -> 4
455 * 1110 -> 3
456 * 110x -> 2
457 * 10xx -> 1
458 * 0xxx -> 1
459 */
460static inline size_t utf8_codepoint_len(uint8_t ch)
461{
462 return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
463}
464
465static inline void utf8_shift_and_mask(uint32_t* codePoint, const uint8_t byte)
466{
467 *codePoint <<= 6;
468 *codePoint |= 0x3F & byte;
469}
470
471size_t utf8_to_utf32_length(const char *src, size_t src_len)
472{
473 if (src == NULL || src_len == 0) {
474 return 0;
475 }
476 size_t ret = 0;
477 const char* cur;
478 const char* end;
479 size_t num_to_skip;
480 for (cur = src, end = src + src_len, num_to_skip = 1;
481 cur < end;
482 cur += num_to_skip, ret++) {
483 const char first_char = *cur;
484 num_to_skip = 1;
485 if ((first_char & 0x80) == 0) { // ASCII
486 continue;
487 }
488 int32_t mask;
489
490 for (mask = 0x40; (first_char & mask); num_to_skip++, mask >>= 1) {
491 }
492 }
493 return ret;
494}
495
496void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst)
497{
498 if (src == NULL || src_len == 0 || dst == NULL) {
499 return;
500 }
501
502 const char* cur = src;
503 const char* const end = src + src_len;
504 char32_t* cur_utf32 = dst;
505 while (cur < end) {
506 size_t num_read;
507 *cur_utf32++ = static_cast<char32_t>(utf32_at_internal(cur, &num_read));
508 cur += num_read;
509 }
510 *cur_utf32 = 0;
511}
512
513static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src, size_t length)
514{
515 uint32_t unicode;
516
517 switch (length)
518 {
519 case 1:
520 return src[0];
521 case 2:
522 unicode = src[0] & 0x1f;
523 utf8_shift_and_mask(&unicode, src[1]);
524 return unicode;
525 case 3:
526 unicode = src[0] & 0x0f;
527 utf8_shift_and_mask(&unicode, src[1]);
528 utf8_shift_and_mask(&unicode, src[2]);
529 return unicode;
530 case 4:
531 unicode = src[0] & 0x07;
532 utf8_shift_and_mask(&unicode, src[1]);
533 utf8_shift_and_mask(&unicode, src[2]);
534 utf8_shift_and_mask(&unicode, src[3]);
535 return unicode;
536 default:
537 return 0xffff;
538 }
539
540 //printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result);
541}
542
543ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len)
544{
545 const uint8_t* const u8end = u8str + u8len;
546 const uint8_t* u8cur = u8str;
547
548 /* Validate that the UTF-8 is the correct len */
549 size_t u16measuredLen = 0;
550 while (u8cur < u8end) {
551 u16measuredLen++;
552 int u8charLen = utf8_codepoint_len(*u8cur);
553 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen);
554 if (codepoint > 0xFFFF) u16measuredLen++; // this will be a surrogate pair in utf16
555 u8cur += u8charLen;
556 }
557
558 /**
559 * Make sure that we ended where we thought we would and the output UTF-16
560 * will be exactly how long we were told it would be.
561 */
562 if (u8cur != u8end) {
563 return -1;
564 }
565
566 return u16measuredLen;
567}
568
Jeff Brownaa983c92011-10-07 13:28:18 -0700569char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, char16_t* u16str)
Kenny Rootba0165b2010-11-09 14:37:23 -0800570{
571 const uint8_t* const u8end = u8str + u8len;
572 const uint8_t* u8cur = u8str;
573 char16_t* u16cur = u16str;
574
575 while (u8cur < u8end) {
576 size_t u8len = utf8_codepoint_len(*u8cur);
577 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
578
579 // Convert the UTF32 codepoint to one or more UTF16 codepoints
580 if (codepoint <= 0xFFFF) {
581 // Single UTF16 character
582 *u16cur++ = (char16_t) codepoint;
583 } else {
584 // Multiple UTF16 characters with surrogates
585 codepoint = codepoint - 0x10000;
586 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
587 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
588 }
589
590 u8cur += u8len;
591 }
Jeff Brownaa983c92011-10-07 13:28:18 -0700592 return u16cur;
593}
594
595void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) {
596 char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str);
597 *end = 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800598}
599
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700600char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) {
601 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}