1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 * Copyright (C) 1999-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 ******************************************************************************
10 * file name: utf_impl.cpp
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 1999sep13
16 * created by: Markus W. Scherer
17 *
18 * This file provides implementation functions for macros in the utfXX.h
19 * that would otherwise be too long as macros.
20 */
21
22 #include "base/third_party/icu/icu_utf.h"
23
24 namespace base_icu {
25
26 // source/common/utf_impl.cpp
27
28 static const UChar32 utf8_errorValue[6] = {
29 // Same values as UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_2, UTF_ERROR_VALUE,
30 // but without relying on the obsolete unicode/utf_old.h.
31 0x15, 0x9f, 0xffff, 0x10ffff};
32
errorValue(int32_t count,int8_t strict)33 static UChar32 errorValue(int32_t count, int8_t strict) {
34 if (strict >= 0) {
35 return utf8_errorValue[count];
36 } else if (strict == -3) {
37 return 0xfffd;
38 } else {
39 return CBU_SENTINEL;
40 }
41 }
42
43 /*
44 * Handle the non-inline part of the U8_NEXT() and U8_NEXT_FFFD() macros
45 * and their obsolete sibling UTF8_NEXT_CHAR_SAFE().
46 *
47 * U8_NEXT() supports NUL-terminated strings indicated via length<0.
48 *
49 * The "strict" parameter controls the error behavior:
50 * <0 "Safe" behavior of U8_NEXT():
51 * -1: All illegal byte sequences yield U_SENTINEL=-1.
52 * -2: Same as -1, except for lenient treatment of surrogate code points as
53 * legal. Some implementations use this for roundtripping of Unicode 16-bit
54 * strings that are not well-formed UTF-16, that is, they contain
55 * unpaired surrogates. -3: All illegal byte sequences yield U+FFFD. 0
56 * Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., FALSE): All illegal
57 * byte sequences yield a positive code point such that this result code
58 * point would be encoded with the same number of bytes as the illegal
59 * sequence. >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(...,
60 * TRUE): Same as the obsolete "safe" behavior, but non-characters are also
61 * treated like illegal sequences.
62 *
63 * Note that a UBool is the same as an int8_t.
64 */
utf8_nextCharSafeBody(const uint8_t * s,int32_t * pi,int32_t length,UChar32 c,UBool strict)65 UChar32 utf8_nextCharSafeBody(const uint8_t* s,
66 int32_t* pi,
67 int32_t length,
68 UChar32 c,
69 UBool strict) {
70 // *pi is one after byte c.
71 int32_t i = *pi;
72 // length can be negative for NUL-terminated strings: Read and validate one
73 // byte at a time.
74 if (i == length || c > 0xf4) {
75 // end of string, or not a lead byte
76 } else if (c >= 0xf0) {
77 // Test for 4-byte sequences first because
78 // U8_NEXT() handles shorter valid sequences inline.
79 uint8_t t1 = s[i], t2, t3;
80 c &= 7;
81 if (CBU8_IS_VALID_LEAD4_AND_T1(c, t1) && ++i != length &&
82 (t2 = s[i] - 0x80) <= 0x3f && ++i != length &&
83 (t3 = s[i] - 0x80) <= 0x3f) {
84 ++i;
85 c = (c << 18) | ((t1 & 0x3f) << 12) | (t2 << 6) | t3;
86 // strict: forbid non-characters like U+fffe
87 if (strict <= 0 || !CBU_IS_UNICODE_NONCHAR(c)) {
88 *pi = i;
89 return c;
90 }
91 }
92 } else if (c >= 0xe0) {
93 c &= 0xf;
94 if (strict != -2) {
95 uint8_t t1 = s[i], t2;
96 if (CBU8_IS_VALID_LEAD3_AND_T1(c, t1) && ++i != length &&
97 (t2 = s[i] - 0x80) <= 0x3f) {
98 ++i;
99 c = (c << 12) | ((t1 & 0x3f) << 6) | t2;
100 // strict: forbid non-characters like U+fffe
101 if (strict <= 0 || !CBU_IS_UNICODE_NONCHAR(c)) {
102 *pi = i;
103 return c;
104 }
105 }
106 } else {
107 // strict=-2 -> lenient: allow surrogates
108 uint8_t t1 = s[i] - 0x80, t2;
109 if (t1 <= 0x3f && (c > 0 || t1 >= 0x20) && ++i != length &&
110 (t2 = s[i] - 0x80) <= 0x3f) {
111 *pi = i + 1;
112 return (c << 12) | (t1 << 6) | t2;
113 }
114 }
115 } else if (c >= 0xc2) {
116 uint8_t t1 = s[i] - 0x80;
117 if (t1 <= 0x3f) {
118 *pi = i + 1;
119 return ((c - 0xc0) << 6) | t1;
120 }
121 } // else 0x80<=c<0xc2 is not a lead byte
122
123 /* error handling */
124 c = errorValue(i - *pi, strict);
125 *pi = i;
126 return c;
127 }
128
129 } // namespace base_icu
130