1 /*
2 * Copyright (C) 2016 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 "androidfw/Util.h"
18
19 #include <algorithm>
20 #include <string>
21
22 #include "utils/ByteOrder.h"
23 #include "utils/Unicode.h"
24
25 #ifdef _WIN32
26 #ifdef ERROR
27 #undef ERROR
28 #endif
29 #endif
30
31 namespace android {
32 namespace util {
33
ReadUtf16StringFromDevice(const uint16_t * src,size_t len,std::string * out)34 void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out) {
35 static constexpr bool kDeviceEndiannessSame = dtohs(0x1001) == 0x1001;
36 if constexpr (kDeviceEndiannessSame) {
37 *out = Utf16ToUtf8({(const char16_t*)src, strnlen16((const char16_t*)src, len)});
38 } else {
39 char buf[5];
40 while (*src && len != 0) {
41 char16_t c = static_cast<char16_t>(dtohs(*src));
42 utf16_to_utf8(&c, 1, buf, sizeof(buf));
43 out->append(buf, strlen(buf));
44 ++src;
45 --len;
46 }
47 }
48 }
49
Utf8ToUtf16(StringPiece utf8)50 std::u16string Utf8ToUtf16(StringPiece utf8) {
51 ssize_t utf16_length =
52 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
53 if (utf16_length <= 0) {
54 return {};
55 }
56
57 std::u16string utf16;
58 utf16.resize(utf16_length);
59 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), &*utf16.begin(),
60 utf16_length + 1);
61 return utf16;
62 }
63
Utf16ToUtf8(StringPiece16 utf16)64 std::string Utf16ToUtf8(StringPiece16 utf16) {
65 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
66 if (utf8_length <= 0) {
67 return {};
68 }
69
70 std::string utf8;
71 utf8.resize_and_overwrite(utf8_length, [&utf16](char* data, size_t size) {
72 utf16_to_utf8(utf16.data(), utf16.length(), data, size + 1);
73 return size;
74 });
75 return utf8;
76 }
77
Utf8ToModifiedUtf8(std::string_view utf8)78 std::string Utf8ToModifiedUtf8(std::string_view utf8) {
79 // Java uses Modified UTF-8 which only supports the 1, 2, and 3 byte formats of UTF-8. To encode
80 // 4 byte UTF-8 codepoints, Modified UTF-8 allows the use of surrogate pairs in the same format
81 // of CESU-8 surrogate pairs. Calculate the size of the utf8 string with all 4 byte UTF-8
82 // codepoints replaced with 2 3 byte surrogate pairs
83 size_t modified_size = 0;
84 const size_t size = utf8.size();
85 for (size_t i = 0; i < size; i++) {
86 if (((uint8_t)utf8[i] >> 4) == 0xF) {
87 modified_size += 6;
88 i += 3;
89 } else {
90 modified_size++;
91 }
92 }
93
94 // Early out if no 4 byte codepoints are found
95 if (size == modified_size) {
96 return std::string(utf8);
97 }
98
99 std::string output;
100 output.reserve(modified_size);
101 for (size_t i = 0; i < size; i++) {
102 if (((uint8_t)utf8[i] >> 4) == 0xF) {
103 int32_t codepoint = utf32_from_utf8_at(utf8.data(), size, i, nullptr);
104
105 // Calculate the high and low surrogates as UTF-16 would
106 int32_t high = ((codepoint - 0x10000) / 0x400) + 0xD800;
107 int32_t low = ((codepoint - 0x10000) % 0x400) + 0xDC00;
108
109 // Encode each surrogate in UTF-8
110 output.push_back((char)(0xE4 | ((high >> 12) & 0xF)));
111 output.push_back((char)(0x80 | ((high >> 6) & 0x3F)));
112 output.push_back((char)(0x80 | (high & 0x3F)));
113 output.push_back((char)(0xE4 | ((low >> 12) & 0xF)));
114 output.push_back((char)(0x80 | ((low >> 6) & 0x3F)));
115 output.push_back((char)(0x80 | (low & 0x3F)));
116 i += 3;
117 } else {
118 output.push_back(utf8[i]);
119 }
120 }
121
122 return output;
123 }
124
ModifiedUtf8ToUtf8(std::string_view modified_utf8)125 std::string ModifiedUtf8ToUtf8(std::string_view modified_utf8) {
126 // The UTF-8 representation will have a byte length less than or equal to the Modified UTF-8
127 // representation.
128 std::string output;
129 output.reserve(modified_utf8.size());
130
131 size_t index = 0;
132 const size_t modified_size = modified_utf8.size();
133 while (index < modified_size) {
134 size_t next_index;
135 int32_t high_surrogate =
136 utf32_from_utf8_at(modified_utf8.data(), modified_size, index, &next_index);
137 if (high_surrogate < 0) {
138 return {};
139 }
140
141 // Check that the first codepoint is within the high surrogate range
142 if (high_surrogate >= 0xD800 && high_surrogate <= 0xDB7F) {
143 int32_t low_surrogate =
144 utf32_from_utf8_at(modified_utf8.data(), modified_size, next_index, &next_index);
145 if (low_surrogate < 0) {
146 return {};
147 }
148
149 // Check that the second codepoint is within the low surrogate range
150 if (low_surrogate >= 0xDC00 && low_surrogate <= 0xDFFF) {
151 const char32_t codepoint =
152 (char32_t)(((high_surrogate - 0xD800) * 0x400) + (low_surrogate - 0xDC00) + 0x10000);
153
154 // The decoded codepoint should represent a 4 byte, UTF-8 character
155 const size_t utf8_length = (size_t)utf32_to_utf8_length(&codepoint, 1);
156 if (utf8_length != 4) {
157 return {};
158 }
159
160 // Encode the UTF-8 representation of the codepoint into the string
161 const size_t start_index = output.size();
162 output.resize(start_index + utf8_length);
163 char* start = &output[start_index];
164 utf32_to_utf8((char32_t*)&codepoint, 1, start, utf8_length + 1);
165
166 index = next_index;
167 continue;
168 }
169 }
170
171 // Append non-surrogate pairs to the output string
172 for (size_t i = index; i < next_index; i++) {
173 output.push_back(modified_utf8[i]);
174 }
175 index = next_index;
176 }
177 return output;
178 }
179
180 template <class Func>
SplitAndTransform(StringPiece str,char sep,Func && f)181 static std::vector<std::string> SplitAndTransform(StringPiece str, char sep, Func&& f) {
182 std::vector<std::string> parts;
183 const StringPiece::const_iterator end = std::end(str);
184 StringPiece::const_iterator start = std::begin(str);
185 StringPiece::const_iterator current;
186 do {
187 current = std::find(start, end, sep);
188 parts.emplace_back(StringPiece(start, current - start));
189 std::string& part = parts.back();
190 std::transform(part.begin(), part.end(), part.begin(), f);
191 start = current + 1;
192 } while (current != end);
193 return parts;
194 }
195
SplitAndLowercase(StringPiece str,char sep)196 std::vector<std::string> SplitAndLowercase(StringPiece str, char sep) {
197 return SplitAndTransform(str, sep, [](char c) { return ::tolower(c); });
198 }
199
Copy(const BigBuffer & buffer)200 std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
201 auto data = std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
202 uint8_t* p = data.get();
203 for (const auto& block : buffer) {
204 memcpy(p, block.buffer.get(), block.size);
205 p += block.size;
206 }
207 return data;
208 }
209
GetString16(const android::ResStringPool & pool,size_t idx)210 StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
211 if (auto str = pool.stringAt(idx); str.ok()) {
212 return *str;
213 }
214 return StringPiece16();
215 }
216
GetString(const android::ResStringPool & pool,size_t idx)217 std::string GetString(const android::ResStringPool& pool, size_t idx) {
218 if (auto str = pool.string8At(idx); str.ok()) {
219 return ModifiedUtf8ToUtf8(*str);
220 }
221 return Utf16ToUtf8(GetString16(pool, idx));
222 }
223
224 } // namespace util
225 } // namespace android
226