blob: ade896a02d91a388c8f1554232511f17602c828d [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
17#include <utils/Unicode.h>
18
19#include <stddef.h>
20
Elliott Hughesadbf4422015-07-29 17:45:24 -070021#if defined(_WIN32)
Kenny Rootba0165b2010-11-09 14:37:23 -080022# undef nhtol
23# undef htonl
24# undef nhtos
25# undef htons
26
Elliott Hughes97ac0e12014-11-21 23:01:59 -080027# define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
28# define htonl(x) ntohl(x)
29# define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
30# define htons(x) ntohs(x)
Kenny Rootba0165b2010-11-09 14:37:23 -080031#else
32# include <netinet/in.h>
33#endif
34
35extern "C" {
36
37static const char32_t kByteMask = 0x000000BF;
38static const char32_t kByteMark = 0x00000080;
39
40// Surrogates aren't valid for UTF-32 characters, so define some
41// constants that will let us screen them out.
42static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
Andreas Gampea53c8152014-11-24 09:42:07 -080043// Unused, here for completeness:
44// static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
45// static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
Kenny Rootba0165b2010-11-09 14:37:23 -080046static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
47static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
48static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
49static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF;
50
51// Mask used to set appropriate bits in first byte of UTF-8 sequence,
52// indexed by number of bytes in the sequence.
53// 0xxxxxxx
54// -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000
55// 110yyyyx 10xxxxxx
56// -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0
57// 1110yyyy 10yxxxxx 10xxxxxx
58// -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0
59// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
60// -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0
61static const char32_t kFirstByteMark[] = {
62 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0
63};
64
65// --------------------------------------------------------------------------
66// UTF-32
67// --------------------------------------------------------------------------
68
69/**
70 * Return number of UTF-8 bytes required for the character. If the character
71 * is invalid, return size of 0.
72 */
73static inline size_t utf32_codepoint_utf8_length(char32_t srcChar)
74{
75 // Figure out how many bytes the result will require.
76 if (srcChar < 0x00000080) {
77 return 1;
78 } else if (srcChar < 0x00000800) {
79 return 2;
80 } else if (srcChar < 0x00010000) {
81 if ((srcChar < kUnicodeSurrogateStart) || (srcChar > kUnicodeSurrogateEnd)) {
82 return 3;
83 } else {
84 // Surrogates are invalid UTF-32 characters.
85 return 0;
86 }
87 }
88 // Max code point for Unicode is 0x0010FFFF.
89 else if (srcChar <= kUnicodeMaxCodepoint) {
90 return 4;
91 } else {
92 // Invalid UTF-32 character.
93 return 0;
94 }
95}
96
97// Write out the source character to <dstP>.
98
99static inline void utf32_codepoint_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes)
100{
101 dstP += bytes;
102 switch (bytes)
103 { /* note: everything falls through. */
104 case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
105 case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
106 case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
107 case 1: *--dstP = (uint8_t)(srcChar | kFirstByteMark[bytes]);
108 }
109}
110
111size_t strlen32(const char32_t *s)
112{
113 const char32_t *ss = s;
114 while ( *ss )
115 ss++;
116 return ss-s;
117}
118
119size_t strnlen32(const char32_t *s, size_t maxlen)
120{
121 const char32_t *ss = s;
122 while ((maxlen > 0) && *ss) {
123 ss++;
124 maxlen--;
125 }
126 return ss-s;
127}
128
129static inline int32_t utf32_at_internal(const char* cur, size_t *num_read)
130{
131 const char first_char = *cur;
132 if ((first_char & 0x80) == 0) { // ASCII
133 *num_read = 1;
134 return *cur;
135 }
136 cur++;
137 char32_t mask, to_ignore_mask;
138 size_t num_to_read = 0;
139 char32_t utf32 = first_char;
140 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0xFFFFFF80;
141 (first_char & mask);
142 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
143 // 0x3F == 00111111
144 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
145 }
146 to_ignore_mask |= mask;
147 utf32 &= ~(to_ignore_mask << (6 * (num_to_read - 1)));
148
149 *num_read = num_to_read;
150 return static_cast<int32_t>(utf32);
151}
152
153int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index)
154{
155 if (index >= src_len) {
156 return -1;
157 }
158 size_t dummy_index;
159 if (next_index == NULL) {
160 next_index = &dummy_index;
161 }
162 size_t num_read;
163 int32_t ret = utf32_at_internal(src + index, &num_read);
164 if (ret >= 0) {
165 *next_index = index + num_read;
166 }
167
168 return ret;
169}
170
171ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len)
172{
173 if (src == NULL || src_len == 0) {
174 return -1;
175 }
176
177 size_t ret = 0;
178 const char32_t *end = src + src_len;
179 while (src < end) {
180 ret += utf32_codepoint_utf8_length(*src++);
181 }
182 return ret;
183}
184
185void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst)
186{
187 if (src == NULL || src_len == 0 || dst == NULL) {
188 return;
189 }
190
191 const char32_t *cur_utf32 = src;
192 const char32_t *end_utf32 = src + src_len;
193 char *cur = dst;
194 while (cur_utf32 < end_utf32) {
195 size_t len = utf32_codepoint_utf8_length(*cur_utf32);
196 utf32_codepoint_to_utf8((uint8_t *)cur, *cur_utf32++, len);
197 cur += len;
198 }
199 *cur = '\0';
200}
201
202// --------------------------------------------------------------------------
203// UTF-16
204// --------------------------------------------------------------------------
205
206int strcmp16(const char16_t *s1, const char16_t *s2)
207{
208 char16_t ch;
209 int d = 0;
210
211 while ( 1 ) {
212 d = (int)(ch = *s1++) - (int)*s2++;
213 if ( d || !ch )
214 break;
215 }
216
217 return d;
218}
219
220int strncmp16(const char16_t *s1, const char16_t *s2, size_t n)
221{
222 char16_t ch;
223 int d = 0;
224
Michael Wright5bacef32016-05-09 14:43:31 +0100225 if (n == 0) {
226 return 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800227 }
228
Michael Wright5bacef32016-05-09 14:43:31 +0100229 do {
230 d = (int)(ch = *s1++) - (int)*s2++;
231 if ( d || !ch ) {
232 break;
233 }
234 } while (--n);
235
Kenny Rootba0165b2010-11-09 14:37:23 -0800236 return d;
237}
238
239char16_t *strcpy16(char16_t *dst, const char16_t *src)
240{
241 char16_t *q = dst;
242 const char16_t *p = src;
243 char16_t ch;
244
245 do {
246 *q++ = ch = *p++;
247 } while ( ch );
248
249 return dst;
250}
251
252size_t strlen16(const char16_t *s)
253{
254 const char16_t *ss = s;
255 while ( *ss )
256 ss++;
257 return ss-s;
258}
259
260
261char16_t *strncpy16(char16_t *dst, const char16_t *src, size_t n)
262{
263 char16_t *q = dst;
264 const char16_t *p = src;
265 char ch;
266
267 while (n) {
268 n--;
269 *q++ = ch = *p++;
270 if ( !ch )
271 break;
272 }
273
274 *q = 0;
275
276 return dst;
277}
278
279size_t strnlen16(const char16_t *s, size_t maxlen)
280{
281 const char16_t *ss = s;
282
283 /* Important: the maxlen test must precede the reference through ss;
284 since the byte beyond the maximum may segfault */
285 while ((maxlen > 0) && *ss) {
286 ss++;
287 maxlen--;
288 }
289 return ss-s;
290}
291
Michael Wright5bacef32016-05-09 14:43:31 +0100292char16_t* strstr16(const char16_t* src, const char16_t* target)
293{
294 const char16_t needle = *target++;
295 if (needle != '\0') {
296 do {
297 do {
298 if (*src == '\0') {
299 return nullptr;
300 }
301 } while (*src++ != needle);
302 } while (strcmp16(src, target) != 0);
303 src--;
304 }
305
306 return (char16_t*)src;
307}
308
309
Kenny Rootba0165b2010-11-09 14:37:23 -0800310int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2)
311{
312 const char16_t* e1 = s1+n1;
313 const char16_t* e2 = s2+n2;
314
315 while (s1 < e1 && s2 < e2) {
316 const int d = (int)*s1++ - (int)*s2++;
317 if (d) {
318 return d;
319 }
320 }
321
322 return n1 < n2
323 ? (0 - (int)*s2)
324 : (n1 > n2
325 ? ((int)*s1 - 0)
326 : 0);
327}
328
329int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2)
330{
331 const char16_t* e1 = s1H+n1;
332 const char16_t* e2 = s2N+n2;
333
334 while (s1H < e1 && s2N < e2) {
335 const char16_t c2 = ntohs(*s2N);
336 const int d = (int)*s1H++ - (int)c2;
337 s2N++;
338 if (d) {
339 return d;
340 }
341 }
342
343 return n1 < n2
344 ? (0 - (int)ntohs(*s2N))
345 : (n1 > n2
346 ? ((int)*s1H - 0)
347 : 0);
348}
349
350void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst)
351{
352 if (src == NULL || src_len == 0 || dst == NULL) {
353 return;
354 }
355
356 const char16_t* cur_utf16 = src;
357 const char16_t* const end_utf16 = src + src_len;
358 char *cur = dst;
359 while (cur_utf16 < end_utf16) {
360 char32_t utf32;
361 // surrogate pairs
Cylen Yao72299bf2014-06-04 19:11:27 +0800362 if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16
363 && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800364 utf32 = (*cur_utf16++ - 0xD800) << 10;
365 utf32 |= *cur_utf16++ - 0xDC00;
366 utf32 += 0x10000;
367 } else {
368 utf32 = (char32_t) *cur_utf16++;
369 }
370 const size_t len = utf32_codepoint_utf8_length(utf32);
371 utf32_codepoint_to_utf8((uint8_t*)cur, utf32, len);
372 cur += len;
373 }
374 *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{
426 if (src == NULL || src_len == 0) {
427 return -1;
428 }
429
430 size_t ret = 0;
431 const char16_t* const end = src + src_len;
432 while (src < end) {
433 if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
434 && (*++src & 0xFC00) == 0xDC00) {
435 // surrogate pairs are always 4 bytes.
436 ret += 4;
437 src++;
438 } else {
439 ret += utf32_codepoint_utf8_length((char32_t) *src++);
440 }
441 }
442 return ret;
443}
444
445/**
446 * Returns 1-4 based on the number of leading bits.
447 *
448 * 1111 -> 4
449 * 1110 -> 3
450 * 110x -> 2
451 * 10xx -> 1
452 * 0xxx -> 1
453 */
454static inline size_t utf8_codepoint_len(uint8_t ch)
455{
456 return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
457}
458
459static inline void utf8_shift_and_mask(uint32_t* codePoint, const uint8_t byte)
460{
461 *codePoint <<= 6;
462 *codePoint |= 0x3F & byte;
463}
464
465size_t utf8_to_utf32_length(const char *src, size_t src_len)
466{
467 if (src == NULL || src_len == 0) {
468 return 0;
469 }
470 size_t ret = 0;
471 const char* cur;
472 const char* end;
473 size_t num_to_skip;
474 for (cur = src, end = src + src_len, num_to_skip = 1;
475 cur < end;
476 cur += num_to_skip, ret++) {
477 const char first_char = *cur;
478 num_to_skip = 1;
479 if ((first_char & 0x80) == 0) { // ASCII
480 continue;
481 }
482 int32_t mask;
483
484 for (mask = 0x40; (first_char & mask); num_to_skip++, mask >>= 1) {
485 }
486 }
487 return ret;
488}
489
490void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst)
491{
492 if (src == NULL || src_len == 0 || dst == NULL) {
493 return;
494 }
495
496 const char* cur = src;
497 const char* const end = src + src_len;
498 char32_t* cur_utf32 = dst;
499 while (cur < end) {
500 size_t num_read;
501 *cur_utf32++ = static_cast<char32_t>(utf32_at_internal(cur, &num_read));
502 cur += num_read;
503 }
504 *cur_utf32 = 0;
505}
506
507static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src, size_t length)
508{
509 uint32_t unicode;
510
511 switch (length)
512 {
513 case 1:
514 return src[0];
515 case 2:
516 unicode = src[0] & 0x1f;
517 utf8_shift_and_mask(&unicode, src[1]);
518 return unicode;
519 case 3:
520 unicode = src[0] & 0x0f;
521 utf8_shift_and_mask(&unicode, src[1]);
522 utf8_shift_and_mask(&unicode, src[2]);
523 return unicode;
524 case 4:
525 unicode = src[0] & 0x07;
526 utf8_shift_and_mask(&unicode, src[1]);
527 utf8_shift_and_mask(&unicode, src[2]);
528 utf8_shift_and_mask(&unicode, src[3]);
529 return unicode;
530 default:
531 return 0xffff;
532 }
533
534 //printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result);
535}
536
537ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len)
538{
539 const uint8_t* const u8end = u8str + u8len;
540 const uint8_t* u8cur = u8str;
541
542 /* Validate that the UTF-8 is the correct len */
543 size_t u16measuredLen = 0;
544 while (u8cur < u8end) {
545 u16measuredLen++;
546 int u8charLen = utf8_codepoint_len(*u8cur);
547 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen);
548 if (codepoint > 0xFFFF) u16measuredLen++; // this will be a surrogate pair in utf16
549 u8cur += u8charLen;
550 }
551
552 /**
553 * Make sure that we ended where we thought we would and the output UTF-16
554 * will be exactly how long we were told it would be.
555 */
556 if (u8cur != u8end) {
557 return -1;
558 }
559
560 return u16measuredLen;
561}
562
Jeff Brownaa983c92011-10-07 13:28:18 -0700563char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, char16_t* u16str)
Kenny Rootba0165b2010-11-09 14:37:23 -0800564{
565 const uint8_t* const u8end = u8str + u8len;
566 const uint8_t* u8cur = u8str;
567 char16_t* u16cur = u16str;
568
569 while (u8cur < u8end) {
570 size_t u8len = utf8_codepoint_len(*u8cur);
571 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
572
573 // Convert the UTF32 codepoint to one or more UTF16 codepoints
574 if (codepoint <= 0xFFFF) {
575 // Single UTF16 character
576 *u16cur++ = (char16_t) codepoint;
577 } else {
578 // Multiple UTF16 characters with surrogates
579 codepoint = codepoint - 0x10000;
580 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
581 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
582 }
583
584 u8cur += u8len;
585 }
Jeff Brownaa983c92011-10-07 13:28:18 -0700586 return u16cur;
587}
588
589void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) {
590 char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str);
591 *end = 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800592}
593
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700594char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) {
595 const uint8_t* const u8end = src + srcLen;
596 const uint8_t* u8cur = src;
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700597 const char16_t* const u16end = dst + dstLen;
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700598 char16_t* u16cur = dst;
599
600 while (u8cur < u8end && u16cur < u16end) {
601 size_t u8len = utf8_codepoint_len(*u8cur);
602 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
603
604 // Convert the UTF32 codepoint to one or more UTF16 codepoints
605 if (codepoint <= 0xFFFF) {
606 // Single UTF16 character
607 *u16cur++ = (char16_t) codepoint;
608 } else {
609 // Multiple UTF16 characters with surrogates
610 codepoint = codepoint - 0x10000;
611 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
612 if (u16cur >= u16end) {
613 // Ooops... not enough room for this surrogate pair.
614 return u16cur-1;
615 }
616 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
617 }
618
619 u8cur += u8len;
620 }
621 return u16cur;
622}
623
Kenny Rootba0165b2010-11-09 14:37:23 -0800624}