blob: f5e28d4cc496eb7bef6ac29e17f6eda3f908ddf6 [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>
21
22#ifdef HAVE_WINSOCK
23# undef nhtol
24# undef htonl
25# undef nhtos
26# undef htons
27
28# ifdef HAVE_LITTLE_ENDIAN
29# define ntohl(x) ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
30# define htonl(x) ntohl(x)
31# define ntohs(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
32# define htons(x) ntohs(x)
33# else
34# define ntohl(x) (x)
35# define htonl(x) (x)
36# define ntohs(x) (x)
37# define htons(x) (x)
38# endif
39#else
40# include <netinet/in.h>
41#endif
42
43extern "C" {
44
45static const char32_t kByteMask = 0x000000BF;
46static const char32_t kByteMark = 0x00000080;
47
48// Surrogates aren't valid for UTF-32 characters, so define some
49// constants that will let us screen them out.
50static const char32_t kUnicodeSurrogateHighStart = 0x0000D800;
51static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
52static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
53static const char32_t kUnicodeSurrogateLowEnd = 0x0000DFFF;
54static const char32_t kUnicodeSurrogateStart = kUnicodeSurrogateHighStart;
55static const char32_t kUnicodeSurrogateEnd = kUnicodeSurrogateLowEnd;
56static const char32_t kUnicodeMaxCodepoint = 0x0010FFFF;
57
58// Mask used to set appropriate bits in first byte of UTF-8 sequence,
59// indexed by number of bytes in the sequence.
60// 0xxxxxxx
61// -> (00-7f) 7bit. Bit mask for the first byte is 0x00000000
62// 110yyyyx 10xxxxxx
63// -> (c0-df)(80-bf) 11bit. Bit mask is 0x000000C0
64// 1110yyyy 10yxxxxx 10xxxxxx
65// -> (e0-ef)(80-bf)(80-bf) 16bit. Bit mask is 0x000000E0
66// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
67// -> (f0-f7)(80-bf)(80-bf)(80-bf) 21bit. Bit mask is 0x000000F0
68static const char32_t kFirstByteMark[] = {
69 0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0
70};
71
72// --------------------------------------------------------------------------
73// UTF-32
74// --------------------------------------------------------------------------
75
76/**
77 * Return number of UTF-8 bytes required for the character. If the character
78 * is invalid, return size of 0.
79 */
80static inline size_t utf32_codepoint_utf8_length(char32_t srcChar)
81{
82 // Figure out how many bytes the result will require.
83 if (srcChar < 0x00000080) {
84 return 1;
85 } else if (srcChar < 0x00000800) {
86 return 2;
87 } else if (srcChar < 0x00010000) {
88 if ((srcChar < kUnicodeSurrogateStart) || (srcChar > kUnicodeSurrogateEnd)) {
89 return 3;
90 } else {
91 // Surrogates are invalid UTF-32 characters.
92 return 0;
93 }
94 }
95 // Max code point for Unicode is 0x0010FFFF.
96 else if (srcChar <= kUnicodeMaxCodepoint) {
97 return 4;
98 } else {
99 // Invalid UTF-32 character.
100 return 0;
101 }
102}
103
104// Write out the source character to <dstP>.
105
106static inline void utf32_codepoint_to_utf8(uint8_t* dstP, char32_t srcChar, size_t bytes)
107{
108 dstP += bytes;
109 switch (bytes)
110 { /* note: everything falls through. */
111 case 4: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
112 case 3: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
113 case 2: *--dstP = (uint8_t)((srcChar | kByteMark) & kByteMask); srcChar >>= 6;
114 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;
166 if (next_index == NULL) {
167 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{
180 if (src == NULL || src_len == 0) {
181 return -1;
182 }
183
184 size_t ret = 0;
185 const char32_t *end = src + src_len;
186 while (src < end) {
187 ret += utf32_codepoint_utf8_length(*src++);
188 }
189 return ret;
190}
191
Sergio Girob0224472016-06-28 18:02:29 +0100192void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800193{
194 if (src == NULL || src_len == 0 || dst == NULL) {
195 return;
196 }
197
198 const char32_t *cur_utf32 = src;
199 const char32_t *end_utf32 = src + src_len;
200 char *cur = dst;
201 while (cur_utf32 < end_utf32) {
202 size_t len = utf32_codepoint_utf8_length(*cur_utf32);
Sergio Girob0224472016-06-28 18:02:29 +0100203 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800204 utf32_codepoint_to_utf8((uint8_t *)cur, *cur_utf32++, len);
205 cur += len;
Sergio Girob0224472016-06-28 18:02:29 +0100206 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800207 }
Sergio Girob0224472016-06-28 18:02:29 +0100208 LOG_ALWAYS_FATAL_IF(dst_len < 1, "dst_len < 1: %zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800209 *cur = '\0';
210}
211
212// --------------------------------------------------------------------------
213// UTF-16
214// --------------------------------------------------------------------------
215
216int strcmp16(const char16_t *s1, const char16_t *s2)
217{
218 char16_t ch;
219 int d = 0;
220
221 while ( 1 ) {
222 d = (int)(ch = *s1++) - (int)*s2++;
223 if ( d || !ch )
224 break;
225 }
226
227 return d;
228}
229
230int strncmp16(const char16_t *s1, const char16_t *s2, size_t n)
231{
232 char16_t ch;
233 int d = 0;
234
235 while ( n-- ) {
236 d = (int)(ch = *s1++) - (int)*s2++;
237 if ( d || !ch )
238 break;
239 }
240
241 return d;
242}
243
244char16_t *strcpy16(char16_t *dst, const char16_t *src)
245{
246 char16_t *q = dst;
247 const char16_t *p = src;
248 char16_t ch;
249
250 do {
251 *q++ = ch = *p++;
252 } while ( ch );
253
254 return dst;
255}
256
257size_t strlen16(const char16_t *s)
258{
259 const char16_t *ss = s;
260 while ( *ss )
261 ss++;
262 return ss-s;
263}
264
265
266char16_t *strncpy16(char16_t *dst, const char16_t *src, size_t n)
267{
268 char16_t *q = dst;
269 const char16_t *p = src;
270 char ch;
271
272 while (n) {
273 n--;
274 *q++ = ch = *p++;
275 if ( !ch )
276 break;
277 }
278
279 *q = 0;
280
281 return dst;
282}
283
284size_t strnlen16(const char16_t *s, size_t maxlen)
285{
286 const char16_t *ss = s;
287
288 /* Important: the maxlen test must precede the reference through ss;
289 since the byte beyond the maximum may segfault */
290 while ((maxlen > 0) && *ss) {
291 ss++;
292 maxlen--;
293 }
294 return ss-s;
295}
296
297int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2)
298{
299 const char16_t* e1 = s1+n1;
300 const char16_t* e2 = s2+n2;
301
302 while (s1 < e1 && s2 < e2) {
303 const int d = (int)*s1++ - (int)*s2++;
304 if (d) {
305 return d;
306 }
307 }
308
309 return n1 < n2
310 ? (0 - (int)*s2)
311 : (n1 > n2
312 ? ((int)*s1 - 0)
313 : 0);
314}
315
316int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2)
317{
318 const char16_t* e1 = s1H+n1;
319 const char16_t* e2 = s2N+n2;
320
321 while (s1H < e1 && s2N < e2) {
322 const char16_t c2 = ntohs(*s2N);
323 const int d = (int)*s1H++ - (int)c2;
324 s2N++;
325 if (d) {
326 return d;
327 }
328 }
329
330 return n1 < n2
331 ? (0 - (int)ntohs(*s2N))
332 : (n1 > n2
333 ? ((int)*s1H - 0)
334 : 0);
335}
336
Sergio Girob0224472016-06-28 18:02:29 +0100337void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len)
Kenny Rootba0165b2010-11-09 14:37:23 -0800338{
339 if (src == NULL || src_len == 0 || dst == NULL) {
340 return;
341 }
342
343 const char16_t* cur_utf16 = src;
344 const char16_t* const end_utf16 = src + src_len;
345 char *cur = dst;
346 while (cur_utf16 < end_utf16) {
347 char32_t utf32;
348 // surrogate pairs
Cylen Yao72299bf2014-06-04 19:11:27 +0800349 if((*cur_utf16 & 0xFC00) == 0xD800 && (cur_utf16 + 1) < end_utf16
350 && (*(cur_utf16 + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800351 utf32 = (*cur_utf16++ - 0xD800) << 10;
352 utf32 |= *cur_utf16++ - 0xDC00;
353 utf32 += 0x10000;
354 } else {
355 utf32 = (char32_t) *cur_utf16++;
356 }
357 const size_t len = utf32_codepoint_utf8_length(utf32);
Sergio Girob0224472016-06-28 18:02:29 +0100358 LOG_ALWAYS_FATAL_IF(dst_len < len, "%zu < %zu", dst_len, len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800359 utf32_codepoint_to_utf8((uint8_t*)cur, utf32, len);
360 cur += len;
Sergio Girob0224472016-06-28 18:02:29 +0100361 dst_len -= len;
Kenny Rootba0165b2010-11-09 14:37:23 -0800362 }
Sergio Girob0224472016-06-28 18:02:29 +0100363 LOG_ALWAYS_FATAL_IF(dst_len < 1, "%zu < 1", dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800364 *cur = '\0';
365}
366
367// --------------------------------------------------------------------------
368// UTF-8
369// --------------------------------------------------------------------------
370
371ssize_t utf8_length(const char *src)
372{
373 const char *cur = src;
374 size_t ret = 0;
375 while (*cur != '\0') {
376 const char first_char = *cur++;
377 if ((first_char & 0x80) == 0) { // ASCII
378 ret += 1;
379 continue;
380 }
381 // (UTF-8's character must not be like 10xxxxxx,
382 // but 110xxxxx, 1110xxxx, ... or 1111110x)
383 if ((first_char & 0x40) == 0) {
384 return -1;
385 }
386
387 int32_t mask, to_ignore_mask;
388 size_t num_to_read = 0;
389 char32_t utf32 = 0;
390 for (num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
391 num_to_read < 5 && (first_char & mask);
392 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
393 if ((*cur & 0xC0) != 0x80) { // must be 10xxxxxx
394 return -1;
395 }
396 // 0x3F == 00111111
397 utf32 = (utf32 << 6) + (*cur++ & 0x3F);
398 }
399 // "first_char" must be (110xxxxx - 11110xxx)
400 if (num_to_read == 5) {
401 return -1;
402 }
403 to_ignore_mask |= mask;
404 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
405 if (utf32 > kUnicodeMaxCodepoint) {
406 return -1;
407 }
408
409 ret += num_to_read;
410 }
411 return ret;
412}
413
414ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
415{
416 if (src == NULL || src_len == 0) {
417 return -1;
418 }
419
420 size_t ret = 0;
421 const char16_t* const end = src + src_len;
422 while (src < end) {
423 if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
Sergio Girob0224472016-06-28 18:02:29 +0100424 && (*(src + 1) & 0xFC00) == 0xDC00) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800425 // surrogate pairs are always 4 bytes.
426 ret += 4;
Sergio Girob0224472016-06-28 18:02:29 +0100427 src += 2;
Kenny Rootba0165b2010-11-09 14:37:23 -0800428 } else {
429 ret += utf32_codepoint_utf8_length((char32_t) *src++);
430 }
431 }
432 return ret;
433}
434
435/**
436 * Returns 1-4 based on the number of leading bits.
437 *
438 * 1111 -> 4
439 * 1110 -> 3
440 * 110x -> 2
441 * 10xx -> 1
442 * 0xxx -> 1
443 */
444static inline size_t utf8_codepoint_len(uint8_t ch)
445{
446 return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
447}
448
449static inline void utf8_shift_and_mask(uint32_t* codePoint, const uint8_t byte)
450{
451 *codePoint <<= 6;
452 *codePoint |= 0x3F & byte;
453}
454
455size_t utf8_to_utf32_length(const char *src, size_t src_len)
456{
457 if (src == NULL || src_len == 0) {
458 return 0;
459 }
460 size_t ret = 0;
461 const char* cur;
462 const char* end;
463 size_t num_to_skip;
464 for (cur = src, end = src + src_len, num_to_skip = 1;
465 cur < end;
466 cur += num_to_skip, ret++) {
467 const char first_char = *cur;
468 num_to_skip = 1;
469 if ((first_char & 0x80) == 0) { // ASCII
470 continue;
471 }
472 int32_t mask;
473
474 for (mask = 0x40; (first_char & mask); num_to_skip++, mask >>= 1) {
475 }
476 }
477 return ret;
478}
479
480void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst)
481{
482 if (src == NULL || src_len == 0 || dst == NULL) {
483 return;
484 }
485
486 const char* cur = src;
487 const char* const end = src + src_len;
488 char32_t* cur_utf32 = dst;
489 while (cur < end) {
490 size_t num_read;
491 *cur_utf32++ = static_cast<char32_t>(utf32_at_internal(cur, &num_read));
492 cur += num_read;
493 }
494 *cur_utf32 = 0;
495}
496
497static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src, size_t length)
498{
499 uint32_t unicode;
500
501 switch (length)
502 {
503 case 1:
504 return src[0];
505 case 2:
506 unicode = src[0] & 0x1f;
507 utf8_shift_and_mask(&unicode, src[1]);
508 return unicode;
509 case 3:
510 unicode = src[0] & 0x0f;
511 utf8_shift_and_mask(&unicode, src[1]);
512 utf8_shift_and_mask(&unicode, src[2]);
513 return unicode;
514 case 4:
515 unicode = src[0] & 0x07;
516 utf8_shift_and_mask(&unicode, src[1]);
517 utf8_shift_and_mask(&unicode, src[2]);
518 utf8_shift_and_mask(&unicode, src[3]);
519 return unicode;
520 default:
521 return 0xffff;
522 }
523
524 //printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result);
525}
526
527ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len)
528{
529 const uint8_t* const u8end = u8str + u8len;
530 const uint8_t* u8cur = u8str;
531
532 /* Validate that the UTF-8 is the correct len */
533 size_t u16measuredLen = 0;
534 while (u8cur < u8end) {
535 u16measuredLen++;
536 int u8charLen = utf8_codepoint_len(*u8cur);
537 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen);
538 if (codepoint > 0xFFFF) u16measuredLen++; // this will be a surrogate pair in utf16
539 u8cur += u8charLen;
540 }
541
542 /**
543 * Make sure that we ended where we thought we would and the output UTF-16
544 * will be exactly how long we were told it would be.
545 */
546 if (u8cur != u8end) {
547 return -1;
548 }
549
550 return u16measuredLen;
551}
552
Jeff Brownaa983c92011-10-07 13:28:18 -0700553char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, char16_t* u16str)
Kenny Rootba0165b2010-11-09 14:37:23 -0800554{
555 const uint8_t* const u8end = u8str + u8len;
556 const uint8_t* u8cur = u8str;
557 char16_t* u16cur = u16str;
558
559 while (u8cur < u8end) {
560 size_t u8len = utf8_codepoint_len(*u8cur);
561 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
562
563 // Convert the UTF32 codepoint to one or more UTF16 codepoints
564 if (codepoint <= 0xFFFF) {
565 // Single UTF16 character
566 *u16cur++ = (char16_t) codepoint;
567 } else {
568 // Multiple UTF16 characters with surrogates
569 codepoint = codepoint - 0x10000;
570 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
571 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
572 }
573
574 u8cur += u8len;
575 }
Jeff Brownaa983c92011-10-07 13:28:18 -0700576 return u16cur;
577}
578
579void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) {
580 char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str);
581 *end = 0;
Kenny Rootba0165b2010-11-09 14:37:23 -0800582}
583
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700584char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) {
585 const uint8_t* const u8end = src + srcLen;
586 const uint8_t* u8cur = src;
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700587 const char16_t* const u16end = dst + dstLen;
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700588 char16_t* u16cur = dst;
589
590 while (u8cur < u8end && u16cur < u16end) {
591 size_t u8len = utf8_codepoint_len(*u8cur);
592 uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len);
593
594 // Convert the UTF32 codepoint to one or more UTF16 codepoints
595 if (codepoint <= 0xFFFF) {
596 // Single UTF16 character
597 *u16cur++ = (char16_t) codepoint;
598 } else {
599 // Multiple UTF16 characters with surrogates
600 codepoint = codepoint - 0x10000;
601 *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800);
602 if (u16cur >= u16end) {
603 // Ooops... not enough room for this surrogate pair.
604 return u16cur-1;
605 }
606 *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
607 }
608
609 u8cur += u8len;
610 }
611 return u16cur;
612}
613
Kenny Rootba0165b2010-11-09 14:37:23 -0800614}