1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBDEXFILE_DEX_UTF_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBDEXFILE_DEX_UTF_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <stddef.h>
21*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #include <string>
24*795d594fSAndroid Build Coastguard Worker #include <string_view>
25*795d594fSAndroid Build Coastguard Worker #include <type_traits>
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker /*
30*795d594fSAndroid Build Coastguard Worker * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction
31*795d594fSAndroid Build Coastguard Worker * doesn't matter.
32*795d594fSAndroid Build Coastguard Worker *
33*795d594fSAndroid Build Coastguard Worker * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details.
34*795d594fSAndroid Build Coastguard Worker */
35*795d594fSAndroid Build Coastguard Worker namespace art {
36*795d594fSAndroid Build Coastguard Worker
37*795d594fSAndroid Build Coastguard Worker /*
38*795d594fSAndroid Build Coastguard Worker * Returns the number of UTF-16 characters in the given modified UTF-8 string.
39*795d594fSAndroid Build Coastguard Worker */
40*795d594fSAndroid Build Coastguard Worker size_t CountModifiedUtf8Chars(const char* utf8);
41*795d594fSAndroid Build Coastguard Worker size_t CountModifiedUtf8Chars(const char* utf8, size_t byte_count);
42*795d594fSAndroid Build Coastguard Worker
43*795d594fSAndroid Build Coastguard Worker /*
44*795d594fSAndroid Build Coastguard Worker * Convert from Modified UTF-8 to UTF-16.
45*795d594fSAndroid Build Coastguard Worker */
46*795d594fSAndroid Build Coastguard Worker void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in);
47*795d594fSAndroid Build Coastguard Worker void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, size_t out_chars,
48*795d594fSAndroid Build Coastguard Worker const char* utf8_in, size_t in_bytes);
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker /*
51*795d594fSAndroid Build Coastguard Worker * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner
52*795d594fSAndroid Build Coastguard Worker */
53*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1,
54*795d594fSAndroid Build Coastguard Worker const char* utf8_2);
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Worker /*
57*795d594fSAndroid Build Coastguard Worker * Compare a null-terminated modified UTF-8 string with a UTF-16 string (not null-terminated)
58*795d594fSAndroid Build Coastguard Worker * as code point values in a non-locale sensitive manner.
59*795d594fSAndroid Build Coastguard Worker */
60*795d594fSAndroid Build Coastguard Worker int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8, const uint16_t* utf16,
61*795d594fSAndroid Build Coastguard Worker size_t utf16_length);
62*795d594fSAndroid Build Coastguard Worker
63*795d594fSAndroid Build Coastguard Worker /*
64*795d594fSAndroid Build Coastguard Worker * Helper template for converting UTF-16 to UTF-8 and similar encodings.
65*795d594fSAndroid Build Coastguard Worker *
66*795d594fSAndroid Build Coastguard Worker * Template arguments:
67*795d594fSAndroid Build Coastguard Worker * kUseShortZero: Encode U+0000 as a single byte with value 0 (otherwise emit 0xc0 0x80).
68*795d594fSAndroid Build Coastguard Worker * kUse4ByteSequence: Encode valid surrogate pairs as a 4-byte sequence.
69*795d594fSAndroid Build Coastguard Worker * kReplaceBadSurrogates: Replace unmatched surrogates with '?' (otherwise use 3-byte sequence).
70*795d594fSAndroid Build Coastguard Worker * Must be false if kUse4ByteSequence is false.
71*795d594fSAndroid Build Coastguard Worker * Append: The type of the `append` functor. Should be deduced automatically.
72*795d594fSAndroid Build Coastguard Worker *
73*795d594fSAndroid Build Coastguard Worker * Encoding kUseShortZero kUse4ByteSequence kReplaceBadSurrogates
74*795d594fSAndroid Build Coastguard Worker * UTF-8 true true true
75*795d594fSAndroid Build Coastguard Worker * Modified UTF8 false false n/a
76*795d594fSAndroid Build Coastguard Worker * JNI GetStringUTFChars false true false
77*795d594fSAndroid Build Coastguard Worker */
78*795d594fSAndroid Build Coastguard Worker template <bool kUseShortZero, bool kUse4ByteSequence, bool kReplaceBadSurrogates, typename Append>
79*795d594fSAndroid Build Coastguard Worker void ConvertUtf16ToUtf8(const uint16_t* utf16, size_t char_count, Append&& append);
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker /*
82*795d594fSAndroid Build Coastguard Worker * Returns the number of modified UTF-8 bytes needed to represent the given
83*795d594fSAndroid Build Coastguard Worker * UTF-16 string.
84*795d594fSAndroid Build Coastguard Worker */
85*795d594fSAndroid Build Coastguard Worker size_t CountModifiedUtf8BytesInUtf16(const uint16_t* chars, size_t char_count);
86*795d594fSAndroid Build Coastguard Worker
87*795d594fSAndroid Build Coastguard Worker /*
88*795d594fSAndroid Build Coastguard Worker * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_
89*795d594fSAndroid Build Coastguard Worker * NUL-terminated. You probably need to call CountModifiedUtf8BytesInUtf16 before calling
90*795d594fSAndroid Build Coastguard Worker * this anyway, so if you want a NUL-terminated string, you know where to
91*795d594fSAndroid Build Coastguard Worker * put the NUL byte.
92*795d594fSAndroid Build Coastguard Worker */
93*795d594fSAndroid Build Coastguard Worker void ConvertUtf16ToModifiedUtf8(char* utf8_out, size_t byte_count,
94*795d594fSAndroid Build Coastguard Worker const uint16_t* utf16_in, size_t char_count);
95*795d594fSAndroid Build Coastguard Worker
96*795d594fSAndroid Build Coastguard Worker /*
97*795d594fSAndroid Build Coastguard Worker * The java.lang.String hashCode() algorithm.
98*795d594fSAndroid Build Coastguard Worker */
99*795d594fSAndroid Build Coastguard Worker template<typename MemoryType>
ComputeUtf16Hash(const MemoryType * chars,size_t char_count)100*795d594fSAndroid Build Coastguard Worker int32_t ComputeUtf16Hash(const MemoryType* chars, size_t char_count) {
101*795d594fSAndroid Build Coastguard Worker static_assert(std::is_same_v<MemoryType, char> ||
102*795d594fSAndroid Build Coastguard Worker std::is_same_v<MemoryType, uint8_t> ||
103*795d594fSAndroid Build Coastguard Worker std::is_same_v<MemoryType, uint16_t>);
104*795d594fSAndroid Build Coastguard Worker using UnsignedMemoryType = std::make_unsigned_t<MemoryType>;
105*795d594fSAndroid Build Coastguard Worker uint32_t hash = 0;
106*795d594fSAndroid Build Coastguard Worker while (char_count--) {
107*795d594fSAndroid Build Coastguard Worker hash = hash * 31 + static_cast<UnsignedMemoryType>(*chars++);
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker return static_cast<int32_t>(hash);
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker int32_t ComputeUtf16HashFromModifiedUtf8(const char* utf8, size_t utf16_length);
113*795d594fSAndroid Build Coastguard Worker
114*795d594fSAndroid Build Coastguard Worker // Compute a hash code of a modified UTF-8 string. Not the standard java hash since it returns a
115*795d594fSAndroid Build Coastguard Worker // uint32_t and hashes individual chars instead of codepoint words.
116*795d594fSAndroid Build Coastguard Worker uint32_t ComputeModifiedUtf8Hash(const char* chars);
117*795d594fSAndroid Build Coastguard Worker uint32_t ComputeModifiedUtf8Hash(std::string_view chars);
118*795d594fSAndroid Build Coastguard Worker
119*795d594fSAndroid Build Coastguard Worker // The starting value of a modified UTF-8 hash.
StartModifiedUtf8Hash()120*795d594fSAndroid Build Coastguard Worker constexpr uint32_t StartModifiedUtf8Hash() {
121*795d594fSAndroid Build Coastguard Worker return 0u;
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker
124*795d594fSAndroid Build Coastguard Worker // Update a modified UTF-8 hash with one character.
125*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
UpdateModifiedUtf8Hash(uint32_t hash,char c)126*795d594fSAndroid Build Coastguard Worker inline uint32_t UpdateModifiedUtf8Hash(uint32_t hash, char c) {
127*795d594fSAndroid Build Coastguard Worker return hash * 31u + static_cast<uint8_t>(c);
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker
130*795d594fSAndroid Build Coastguard Worker // Update a modified UTF-8 hash with characters of a `std::string_view`.
131*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
UpdateModifiedUtf8Hash(uint32_t hash,std::string_view chars)132*795d594fSAndroid Build Coastguard Worker inline uint32_t UpdateModifiedUtf8Hash(uint32_t hash, std::string_view chars) {
133*795d594fSAndroid Build Coastguard Worker for (char c : chars) {
134*795d594fSAndroid Build Coastguard Worker hash = UpdateModifiedUtf8Hash(hash, c);
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker return hash;
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker
139*795d594fSAndroid Build Coastguard Worker /*
140*795d594fSAndroid Build Coastguard Worker * Retrieve the next UTF-16 character or surrogate pair from a UTF-8 string.
141*795d594fSAndroid Build Coastguard Worker * single byte, 2-byte and 3-byte UTF-8 sequences result in a single UTF-16
142*795d594fSAndroid Build Coastguard Worker * character (possibly one half of a surrogate) whereas 4-byte UTF-8 sequences
143*795d594fSAndroid Build Coastguard Worker * result in a surrogate pair. Use GetLeadingUtf16Char and GetTrailingUtf16Char
144*795d594fSAndroid Build Coastguard Worker * to process the return value of this function.
145*795d594fSAndroid Build Coastguard Worker *
146*795d594fSAndroid Build Coastguard Worker * Advances "*utf8_data_in" to the start of the next character.
147*795d594fSAndroid Build Coastguard Worker *
148*795d594fSAndroid Build Coastguard Worker * WARNING: If a string is corrupted by dropping a '\0' in the middle
149*795d594fSAndroid Build Coastguard Worker * of a multi byte sequence, you can end up overrunning the buffer with
150*795d594fSAndroid Build Coastguard Worker * reads (and possibly with the writes if the length was computed and
151*795d594fSAndroid Build Coastguard Worker * cached before the damage). For performance reasons, this function
152*795d594fSAndroid Build Coastguard Worker * assumes that the string being parsed is known to be valid (e.g., by
153*795d594fSAndroid Build Coastguard Worker * already being verified). Most strings we process here are coming
154*795d594fSAndroid Build Coastguard Worker * out of dex files or other internal translations, so the only real
155*795d594fSAndroid Build Coastguard Worker * risk comes from the JNI NewStringUTF call.
156*795d594fSAndroid Build Coastguard Worker */
157*795d594fSAndroid Build Coastguard Worker uint32_t GetUtf16FromUtf8(const char** utf8_data_in);
158*795d594fSAndroid Build Coastguard Worker
159*795d594fSAndroid Build Coastguard Worker /**
160*795d594fSAndroid Build Coastguard Worker * Gets the leading UTF-16 character from a surrogate pair, or the sole
161*795d594fSAndroid Build Coastguard Worker * UTF-16 character from the return value of GetUtf16FromUtf8.
162*795d594fSAndroid Build Coastguard Worker */
163*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE uint16_t GetLeadingUtf16Char(uint32_t maybe_pair);
164*795d594fSAndroid Build Coastguard Worker
165*795d594fSAndroid Build Coastguard Worker /**
166*795d594fSAndroid Build Coastguard Worker * Gets the trailing UTF-16 character from a surrogate pair, or 0 otherwise
167*795d594fSAndroid Build Coastguard Worker * from the return value of GetUtf16FromUtf8.
168*795d594fSAndroid Build Coastguard Worker */
169*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE uint16_t GetTrailingUtf16Char(uint32_t maybe_pair);
170*795d594fSAndroid Build Coastguard Worker
171*795d594fSAndroid Build Coastguard Worker // Returns a printable (escaped) version of a character.
172*795d594fSAndroid Build Coastguard Worker std::string PrintableChar(uint16_t ch);
173*795d594fSAndroid Build Coastguard Worker
174*795d594fSAndroid Build Coastguard Worker // Returns an ASCII string corresponding to the given UTF-8 string.
175*795d594fSAndroid Build Coastguard Worker // Java escapes are used for non-ASCII characters.
176*795d594fSAndroid Build Coastguard Worker std::string PrintableString(const char* utf8);
177*795d594fSAndroid Build Coastguard Worker
178*795d594fSAndroid Build Coastguard Worker } // namespace art
179*795d594fSAndroid Build Coastguard Worker
180*795d594fSAndroid Build Coastguard Worker #endif // ART_LIBDEXFILE_DEX_UTF_H_
181