blob: 86c459fb4647ed62b0a644fc63ff0f41678fa3c7 [file] [log] [blame]
Adam Lesinskida431a22016-12-29 16:08:16 -05001/*
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
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020019#include <algorithm>
Adam Lesinskida431a22016-12-29 16:08:16 -050020#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
31namespace android {
32namespace util {
33
34void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out) {
Yurii Zubrytskyiadac7b02025-02-06 15:41:54 -080035 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 }
Adam Lesinskida431a22016-12-29 16:08:16 -050047 }
48}
49
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070050std::u16string Utf8ToUtf16(StringPiece utf8) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080051 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070064std::string Utf16ToUtf8(StringPiece16 utf16) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080065 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;
Yurii Zubrytskyiadac7b02025-02-06 15:41:54 -080071 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 });
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080075 return utf8;
76}
77
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070078std::string Utf8ToModifiedUtf8(std::string_view utf8) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000079 // 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) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070096 return std::string(utf8);
Jeremy Meyer56f36e82022-05-20 20:35:42 +000097 }
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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700125std::string ModifiedUtf8ToUtf8(std::string_view modified_utf8) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000126 // 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
Greg Kaiser851c62d2022-06-02 05:30:32 -0700161 const size_t start_index = output.size();
162 output.resize(start_index + utf8_length);
163 char* start = &output[start_index];
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000164 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700180template <class Func>
181static std::vector<std::string> SplitAndTransform(StringPiece str, char sep, Func&& f) {
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200182 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);
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700188 parts.emplace_back(StringPiece(start, current - start));
189 std::string& part = parts.back();
190 std::transform(part.begin(), part.end(), part.begin(), f);
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200191 start = current + 1;
192 } while (current != end);
193 return parts;
194}
195
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700196std::vector<std::string> SplitAndLowercase(StringPiece str, char sep) {
197 return SplitAndTransform(str, sep, [](char c) { return ::tolower(c); });
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200198}
199
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000200std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700201 auto data = std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000202 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
210StringPiece16 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
217std::string GetString(const android::ResStringPool& pool, size_t idx) {
218 if (auto str = pool.string8At(idx); str.ok()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700219 return ModifiedUtf8ToUtf8(*str);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000220 }
221 return Utf16ToUtf8(GetString16(pool, idx));
222}
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200223
Adam Lesinskida431a22016-12-29 16:08:16 -0500224} // namespace util
225} // namespace android