xref: /aosp_15_r20/art/test/ti-agent/ti_utf.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2017 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 #ifndef ART_TEST_TI_AGENT_TI_UTF_H_
18 #define ART_TEST_TI_AGENT_TI_UTF_H_
19 
20 #include <inttypes.h>
21 #include <string.h>
22 
23 #include "android-base/logging.h"
24 #include "ti_macros.h"
25 
26 namespace art {
27 namespace ti {
28 
CountModifiedUtf8Chars(const char * utf8,size_t byte_count)29 inline size_t CountModifiedUtf8Chars(const char* utf8, size_t byte_count) {
30   DCHECK_LE(byte_count, strlen(utf8));
31   size_t len = 0;
32   const char* end = utf8 + byte_count;
33   for (; utf8 < end; ++utf8) {
34     int ic = *utf8;
35     len++;
36     if (LIKELY((ic & 0x80) == 0)) {
37       // One-byte encoding.
38       continue;
39     }
40     // Two- or three-byte encoding.
41     utf8++;
42     if ((ic & 0x20) == 0) {
43       // Two-byte encoding.
44       continue;
45     }
46     utf8++;
47     if ((ic & 0x10) == 0) {
48       // Three-byte encoding.
49       continue;
50     }
51 
52     // Four-byte encoding: needs to be converted into a surrogate
53     // pair.
54     utf8++;
55     len++;
56   }
57   return len;
58 }
59 
GetTrailingUtf16Char(uint32_t maybe_pair)60 inline uint16_t GetTrailingUtf16Char(uint32_t maybe_pair) {
61   return static_cast<uint16_t>(maybe_pair >> 16);
62 }
63 
GetLeadingUtf16Char(uint32_t maybe_pair)64 inline uint16_t GetLeadingUtf16Char(uint32_t maybe_pair) {
65   return static_cast<uint16_t>(maybe_pair & 0x0000FFFF);
66 }
67 
GetUtf16FromUtf8(const char ** utf8_data_in)68 inline uint32_t GetUtf16FromUtf8(const char** utf8_data_in) {
69   const uint8_t one = *(*utf8_data_in)++;
70   if ((one & 0x80) == 0) {
71     // one-byte encoding
72     return one;
73   }
74 
75   const uint8_t two = *(*utf8_data_in)++;
76   if ((one & 0x20) == 0) {
77     // two-byte encoding
78     return ((one & 0x1f) << 6) | (two & 0x3f);
79   }
80 
81   const uint8_t three = *(*utf8_data_in)++;
82   if ((one & 0x10) == 0) {
83     return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f);
84   }
85 
86   // Four byte encodings need special handling. We'll have
87   // to convert them into a surrogate pair.
88   const uint8_t four = *(*utf8_data_in)++;
89 
90   // Since this is a 4 byte UTF-8 sequence, it will lie between
91   // U+10000 and U+1FFFFF.
92   //
93   // TODO: What do we do about values in (U+10FFFF, U+1FFFFF) ? The
94   // spec says they're invalid but nobody appears to check for them.
95   const uint32_t code_point = ((one & 0x0f) << 18) | ((two & 0x3f) << 12)
96       | ((three & 0x3f) << 6) | (four & 0x3f);
97 
98   uint32_t surrogate_pair = 0;
99   // Step two: Write out the high (leading) surrogate to the bottom 16 bits
100   // of the of the 32 bit type.
101   surrogate_pair |= ((code_point >> 10) + 0xd7c0) & 0xffff;
102   // Step three : Write out the low (trailing) surrogate to the top 16 bits.
103   surrogate_pair |= ((code_point & 0x03ff) + 0xdc00) << 16;
104 
105   return surrogate_pair;
106 }
107 
108 // Note: This is a copy of the code in `libdexfile`.
109 template <bool kUseShortZero, bool kUse4ByteSequence, bool kReplaceBadSurrogates, typename Append>
ConvertUtf16ToUtf8(const uint16_t * utf16,size_t char_count,Append && append)110 inline void ConvertUtf16ToUtf8(const uint16_t* utf16, size_t char_count, Append&& append) {
111   static_assert(kUse4ByteSequence || !kReplaceBadSurrogates);
112 
113   // Use local helpers instead of macros from `libicu` to avoid the dependency on `libicu`.
114   auto is_lead = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0xfc00u) == 0xd800u; };
115   auto is_trail = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0xfc00u) == 0xdc00u; };
116   auto is_surrogate = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0xf800u) == 0xd800u; };
117   auto is_surrogate_lead = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0x0400u) == 0u; };
118   auto get_supplementary = [](uint16_t lead, uint16_t trail) ALWAYS_INLINE {
119     constexpr uint32_t offset = (0xd800u << 10) + 0xdc00u - 0x10000u;
120     return (static_cast<uint32_t>(lead) << 10) + static_cast<uint32_t>(trail) - offset;
121   };
122 
123   for (size_t i = 0u; i < char_count; ++i) {
124     auto has_trail = [&]() { return i + 1u != char_count && is_trail(utf16[i + 1u]); };
125 
126     uint16_t ch = utf16[i];
127     if (ch < 0x80u && (kUseShortZero || ch != 0u)) {
128       // One byte.
129       append(ch);
130     } else if (ch < 0x800u) {
131       // Two bytes.
132       append((ch >> 6) | 0xc0);
133       append((ch & 0x3f) | 0x80);
134     } else if (kReplaceBadSurrogates
135                    ? is_surrogate(ch)
136                    : kUse4ByteSequence && is_lead(ch) && has_trail()) {
137       if (kReplaceBadSurrogates && (!is_surrogate_lead(ch) || !has_trail())) {
138         append('?');
139       } else {
140         // We have a *valid* surrogate pair.
141         uint32_t code_point = get_supplementary(ch, utf16[i + 1u]);
142         ++i;  //  Consume the leading surrogate.
143         // Four bytes.
144         append((code_point >> 18) | 0xf0);
145         append(((code_point >> 12) & 0x3f) | 0x80);
146         append(((code_point >> 6) & 0x3f) | 0x80);
147         append((code_point & 0x3f) | 0x80);
148       }
149     } else {
150       // Three bytes.
151       append((ch >> 12) | 0xe0);
152       append(((ch >> 6) & 0x3f) | 0x80);
153       append((ch & 0x3f) | 0x80);
154     }
155   }
156 }
157 
ConvertUtf16ToModifiedUtf8(char * utf8_out,size_t byte_count,const uint16_t * utf16_in,size_t char_count)158 inline void ConvertUtf16ToModifiedUtf8(char* utf8_out,
159                                        size_t byte_count,
160                                        const uint16_t* utf16_in,
161                                        size_t char_count) {
162   if (LIKELY(byte_count == char_count)) {
163     // Common case where all characters are ASCII.
164     const uint16_t *utf16_end = utf16_in + char_count;
165     for (const uint16_t *p = utf16_in; p < utf16_end;) {
166       *utf8_out++ = static_cast<char>(*p++);
167     }
168     return;
169   }
170 
171   // String contains non-ASCII characters.
172   // FIXME: We should not emit 4-byte sequences. Bug: 192935764
173   auto append = [&](char c) { *utf8_out++ = c; };
174   ConvertUtf16ToUtf8</*kUseShortZero=*/ false,
175                      /*kUse4ByteSequence=*/ true,
176                      /*kReplaceBadSurrogates=*/ false>(utf16_in, char_count, append);
177 }
178 
CountModifiedUtf8BytesInUtf16(const uint16_t * chars,size_t char_count)179 inline size_t CountModifiedUtf8BytesInUtf16(const uint16_t* chars, size_t char_count) {
180   // FIXME: We should not emit 4-byte sequences. Bug: 192935764
181   size_t result = 0;
182   auto append = [&]([[maybe_unused]] char c) { ++result; };
183   ConvertUtf16ToUtf8</*kUseShortZero=*/ false,
184                      /*kUse4ByteSequence=*/ true,
185                      /*kReplaceBadSurrogates=*/ false>(chars, char_count, append);
186   return result;
187 }
188 
189 }  // namespace ti
190 }  // namespace art
191 
192 #endif  // ART_TEST_TI_AGENT_TI_UTF_H_
193