• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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_LIBDEXFILE_DEX_UTF_H_
18 #define ART_LIBDEXFILE_DEX_UTF_H_
19 
20 #include "base/macros.h"
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include <string>
26 
27 /*
28  * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction
29  * doesn't matter.
30  *
31  * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details.
32  */
33 namespace art {
34 
35 /*
36  * Returns the number of UTF-16 characters in the given modified UTF-8 string.
37  */
38 size_t CountModifiedUtf8Chars(const char* utf8);
39 size_t CountModifiedUtf8Chars(const char* utf8, size_t byte_count);
40 
41 /*
42  * Returns the number of modified UTF-8 bytes needed to represent the given
43  * UTF-16 string.
44  */
45 size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count);
46 
47 /*
48  * Convert from Modified UTF-8 to UTF-16.
49  */
50 void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in);
51 void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, size_t out_chars,
52                                 const char* utf8_in, size_t in_bytes);
53 
54 /*
55  * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner
56  */
57 ALWAYS_INLINE int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1,
58                                                                           const char* utf8_2);
59 
60 /*
61  * Compare a null-terminated modified UTF-8 string with a UTF-16 string (not null-terminated)
62  * as code point values in a non-locale sensitive manner.
63  */
64 int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8, const uint16_t* utf16,
65                                                 size_t utf16_length);
66 
67 /*
68  * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_
69  * NUL-terminated. You probably need to call CountUtf8Bytes before calling
70  * this anyway, so if you want a NUL-terminated string, you know where to
71  * put the NUL byte.
72  */
73 void ConvertUtf16ToModifiedUtf8(char* utf8_out, size_t byte_count,
74                                 const uint16_t* utf16_in, size_t char_count);
75 
76 /*
77  * The java.lang.String hashCode() algorithm.
78  */
79 template<typename MemoryType>
ComputeUtf16Hash(const MemoryType * chars,size_t char_count)80 int32_t ComputeUtf16Hash(const MemoryType* chars, size_t char_count) {
81   uint32_t hash = 0;
82   while (char_count--) {
83     hash = hash * 31 + *chars++;
84   }
85   return static_cast<int32_t>(hash);
86 }
87 
88 int32_t ComputeUtf16HashFromModifiedUtf8(const char* utf8, size_t utf16_length);
89 
90 // Compute a hash code of a modified UTF-8 string. Not the standard java hash since it returns a
91 // uint32_t and hashes individual chars instead of codepoint words.
92 uint32_t ComputeModifiedUtf8Hash(const char* chars);
93 
94 /*
95  * Retrieve the next UTF-16 character or surrogate pair from a UTF-8 string.
96  * single byte, 2-byte and 3-byte UTF-8 sequences result in a single UTF-16
97  * character (possibly one half of a surrogate) whereas 4-byte UTF-8 sequences
98  * result in a surrogate pair. Use GetLeadingUtf16Char and GetTrailingUtf16Char
99  * to process the return value of this function.
100  *
101  * Advances "*utf8_data_in" to the start of the next character.
102  *
103  * WARNING: If a string is corrupted by dropping a '\0' in the middle
104  * of a multi byte sequence, you can end up overrunning the buffer with
105  * reads (and possibly with the writes if the length was computed and
106  * cached before the damage). For performance reasons, this function
107  * assumes that the string being parsed is known to be valid (e.g., by
108  * already being verified). Most strings we process here are coming
109  * out of dex files or other internal translations, so the only real
110  * risk comes from the JNI NewStringUTF call.
111  */
112 uint32_t GetUtf16FromUtf8(const char** utf8_data_in);
113 
114 /**
115  * Gets the leading UTF-16 character from a surrogate pair, or the sole
116  * UTF-16 character from the return value of GetUtf16FromUtf8.
117  */
118 ALWAYS_INLINE uint16_t GetLeadingUtf16Char(uint32_t maybe_pair);
119 
120 /**
121  * Gets the trailing UTF-16 character from a surrogate pair, or 0 otherwise
122  * from the return value of GetUtf16FromUtf8.
123  */
124 ALWAYS_INLINE uint16_t GetTrailingUtf16Char(uint32_t maybe_pair);
125 
126 // Returns a printable (escaped) version of a character.
127 std::string PrintableChar(uint16_t ch);
128 
129 // Returns an ASCII string corresponding to the given UTF-8 string.
130 // Java escapes are used for non-ASCII characters.
131 std::string PrintableString(const char* utf8);
132 
133 }  // namespace art
134 
135 #endif  // ART_LIBDEXFILE_DEX_UTF_H_
136