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