blob: 44b4ec13b2f1aecf9a635472821e6ea31b0e63b4 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
19#include <algorithm>
20#include <ostream>
21#include <string>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <vector>
23
Ryan Mitchell34039b22019-03-18 08:57:47 -070024#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Ryan Mitchell34039b22019-03-18 08:57:47 -070026#include "build/version.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027
Adam Lesinski96ea08f2017-11-06 10:44:46 -080028#include "text/Unicode.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070029#include "text/Utf8Iterator.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080030#include "util/BigBuffer.h"
Ryan Mitchell34039b22019-03-18 08:57:47 -070031#include "utils/Unicode.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080032
Adam Lesinski66ea8402017-06-28 11:44:11 -070033using ::aapt::text::Utf8Iterator;
Adam Lesinski549e4372017-06-27 18:39:07 -070034using ::android::StringPiece;
35using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080036
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037namespace aapt {
38namespace util {
39
Rhed Jao2c434422020-11-12 10:48:03 +080040// Package name and shared user id would be used as a part of the file name.
41// Limits size to 223 and reserves 32 for the OS.
42// See frameworks/base/core/java/android/content/pm/parsing/ParsingPackageUtils.java
43constexpr static const size_t kMaxPackageNameSize = 223;
44
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045static std::vector<std::string> SplitAndTransform(
46 const StringPiece& str, char sep, const std::function<char(char)>& f) {
47 std::vector<std::string> parts;
48 const StringPiece::const_iterator end = std::end(str);
49 StringPiece::const_iterator start = std::begin(str);
50 StringPiece::const_iterator current;
51 do {
52 current = std::find(start, end, sep);
Adam Lesinskid5083f62017-01-16 15:07:21 -080053 parts.emplace_back(str.substr(start, current).to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 if (f) {
55 std::string& part = parts.back();
56 std::transform(part.begin(), part.end(), part.begin(), f);
57 }
58 start = current + 1;
59 } while (current != end);
60 return parts;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061}
62
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063std::vector<std::string> Split(const StringPiece& str, char sep) {
64 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065}
66
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
68 return SplitAndTransform(str, sep, ::tolower);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069}
70
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
72 if (str.size() < prefix.size()) {
73 return false;
74 }
75 return str.substr(0, prefix.size()) == prefix;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070076}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
79 if (str.size() < suffix.size()) {
80 return false;
81 }
82 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083}
84
Adam Lesinski2eed52e2018-02-21 15:55:58 -080085StringPiece TrimLeadingWhitespace(const StringPiece& str) {
86 if (str.size() == 0 || str.data() == nullptr) {
87 return str;
88 }
89
90 const char* start = str.data();
91 const char* end = start + str.length();
92
93 while (start != end && isspace(*start)) {
94 start++;
95 }
96 return StringPiece(start, end - start);
97}
98
99StringPiece TrimTrailingWhitespace(const StringPiece& str) {
100 if (str.size() == 0 || str.data() == nullptr) {
101 return str;
102 }
103
104 const char* start = str.data();
105 const char* end = start + str.length();
106
107 while (end != start && isspace(*(end - 1))) {
108 end--;
109 }
110 return StringPiece(start, end - start);
111}
112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113StringPiece TrimWhitespace(const StringPiece& str) {
114 if (str.size() == 0 || str.data() == nullptr) {
115 return str;
116 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 const char* start = str.data();
119 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 while (start != end && isspace(*start)) {
122 start++;
123 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 while (end != start && isspace(*(end - 1))) {
126 end--;
127 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700130}
131
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800132static int IsJavaNameImpl(const StringPiece& str) {
133 int pieces = 0;
134 for (const StringPiece& piece : Tokenize(str, '.')) {
135 pieces++;
136 if (!text::IsJavaIdentifier(piece)) {
137 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 }
139 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800140 return pieces;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141}
142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143bool IsJavaClassName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800144 return IsJavaNameImpl(str) >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700145}
146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147bool IsJavaPackageName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800148 return IsJavaNameImpl(str) >= 1;
149}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800151static int IsAndroidNameImpl(const StringPiece& str) {
152 int pieces = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 for (const StringPiece& piece : Tokenize(str, '.')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (piece.empty()) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800155 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156 }
157
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800158 const char first_character = piece.data()[0];
159 if (!::isalpha(first_character)) {
160 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800163 bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool {
164 return ::isalnum(c) || c == '_';
165 });
166
167 if (!valid) {
168 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800170 pieces++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800172 return pieces;
173}
174
175bool IsAndroidPackageName(const StringPiece& str) {
Rhed Jao2c434422020-11-12 10:48:03 +0800176 if (str.size() > kMaxPackageNameSize) {
177 return false;
178 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800179 return IsAndroidNameImpl(str) > 1 || str == "android";
180}
181
Rhed Jao2c434422020-11-12 10:48:03 +0800182bool IsAndroidSharedUserId(const android::StringPiece& package_name,
183 const android::StringPiece& shared_user_id) {
184 if (shared_user_id.size() > kMaxPackageNameSize) {
185 return false;
186 }
187 return shared_user_id.empty() || IsAndroidNameImpl(shared_user_id) > 1 ||
188 package_name == "android";
189}
190
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800191bool IsAndroidSplitName(const StringPiece& str) {
192 return IsAndroidNameImpl(str) > 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700193}
194
Ryan Mitchell4382e442021-07-14 12:53:01 -0700195std::optional<std::string> GetFullyQualifiedClassName(const StringPiece& package,
196 const StringPiece& classname) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (classname.empty()) {
198 return {};
199 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800202 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 if (package.empty()) {
206 return {};
207 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700208
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800209 std::string result = package.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 if (classname.data()[0] != '.') {
211 result += '.';
212 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 result.append(classname.data(), classname.size());
215 if (!IsJavaClassName(result)) {
216 return {};
217 }
218 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700219}
220
Ryan Mitchell34039b22019-03-18 08:57:47 -0700221const char* GetToolName() {
222 static const char* const sToolName = "Android Asset Packaging Tool (aapt)";
223 return sToolName;
224}
225
226std::string GetToolFingerprint() {
227 // DO NOT UPDATE, this is more of a marketing version.
228 static const char* const sMajorVersion = "2";
229
230 // Update minor version whenever a feature or flag is added.
231 static const char* const sMinorVersion = "19";
232
233 // The build id of aapt2 binary.
234 static const std::string sBuildId = android::build::GetBuildNumber();
235
236 return android::base::StringPrintf("%s.%s-%s", sMajorVersion, sMinorVersion, sBuildId.c_str());
237}
238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239static size_t ConsumeDigits(const char* start, const char* end) {
240 const char* c = start;
241 for (; c != end && *c >= '0' && *c <= '9'; c++) {
242 }
243 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800244}
245
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246bool VerifyJavaStringFormat(const StringPiece& str) {
247 const char* c = str.begin();
248 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800249
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 size_t arg_count = 0;
251 bool nonpositional = false;
252 while (c != end) {
253 if (*c == '%' && c + 1 < end) {
254 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800255
Adam Lesinskib9f05482017-06-02 16:32:37 -0700256 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 c++;
258 continue;
259 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 size_t num_digits = ConsumeDigits(c, end);
264 if (num_digits > 0) {
265 c += num_digits;
266 if (c != end && *c != '$') {
267 // The digits were a size, but not a positional argument.
268 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800269 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 } else if (*c == '<') {
271 // Reusing last argument, bad idea since positions can be moved around
272 // during translation.
273 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 c++;
276
277 // Optionally we can have a $ after
278 if (c != end && *c == '$') {
279 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800280 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 } else {
282 nonpositional = true;
283 }
284
285 // Ignore size, width, flags, etc.
286 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
287 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
288 c++;
289 }
290
291 /*
292 * This is a shortcut to detect strings that are going to Time.format()
293 * instead of String.format()
294 *
295 * Comparison of String.format() and Time.format() args:
296 *
297 * String: ABC E GH ST X abcdefgh nost x
298 * Time: DEFGHKMS W Za d hkm s w yz
299 *
300 * Therefore we know it's definitely Time if we have:
301 * DFKMWZkmwyz
302 */
303 if (c != end) {
304 switch (*c) {
305 case 'D':
306 case 'F':
307 case 'K':
308 case 'M':
309 case 'W':
310 case 'Z':
311 case 'k':
312 case 'm':
313 case 'w':
314 case 'y':
315 case 'z':
316 return true;
317 }
318 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800319 }
320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 if (c != end) {
322 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800323 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 }
325
326 if (arg_count > 1 && nonpositional) {
327 // Multiple arguments were specified, but some or all were non positional.
328 // Translated
329 // strings may rearrange the order of the arguments, which will break the
330 // string.
331 return false;
332 }
333 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800334}
335
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700336std::string Utf8ToModifiedUtf8(const std::string& utf8) {
337 // Java uses Modified UTF-8 which only supports the 1, 2, and 3 byte formats of UTF-8. To encode
338 // 4 byte UTF-8 codepoints, Modified UTF-8 allows the use of surrogate pairs in the same format
339 // of CESU-8 surrogate pairs. Calculate the size of the utf8 string with all 4 byte UTF-8
340 // codepoints replaced with 2 3 byte surrogate pairs
341 size_t modified_size = 0;
342 const size_t size = utf8.size();
343 for (size_t i = 0; i < size; i++) {
344 if (((uint8_t) utf8[i] >> 4) == 0xF) {
345 modified_size += 6;
346 i += 3;
347 } else {
348 modified_size++;
349 }
350 }
351
352 // Early out if no 4 byte codepoints are found
353 if (size == modified_size) {
354 return utf8;
355 }
356
357 std::string output;
358 output.reserve(modified_size);
359 for (size_t i = 0; i < size; i++) {
360 if (((uint8_t) utf8[i] >> 4) == 0xF) {
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700361 int32_t codepoint = utf32_from_utf8_at(utf8.data(), size, i, nullptr);
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700362
363 // Calculate the high and low surrogates as UTF-16 would
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700364 int32_t high = ((codepoint - 0x10000) / 0x400) + 0xD800;
365 int32_t low = ((codepoint - 0x10000) % 0x400) + 0xDC00;
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700366
367 // Encode each surrogate in UTF-8
368 output.push_back((char) (0xE4 | ((high >> 12) & 0xF)));
369 output.push_back((char) (0x80 | ((high >> 6) & 0x3F)));
370 output.push_back((char) (0x80 | (high & 0x3F)));
371 output.push_back((char) (0xE4 | ((low >> 12) & 0xF)));
372 output.push_back((char) (0x80 | ((low >> 6) & 0x3F)));
373 output.push_back((char) (0x80 | (low & 0x3F)));
374 i += 3;
375 } else {
376 output.push_back(utf8[i]);
377 }
378 }
379
380 return output;
381}
382
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700383std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8) {
384 // The UTF-8 representation will have a byte length less than or equal to the Modified UTF-8
385 // representation.
386 std::string output;
387 output.reserve(modified_utf8.size());
388
389 size_t index = 0;
390 const size_t modified_size = modified_utf8.size();
391 while (index < modified_size) {
392 size_t next_index;
393 int32_t high_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, index,
394 &next_index);
395 if (high_surrogate < 0) {
396 return {};
397 }
398
399 // Check that the first codepoint is within the high surrogate range
400 if (high_surrogate >= 0xD800 && high_surrogate <= 0xDB7F) {
401 int32_t low_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, next_index,
402 &next_index);
403 if (low_surrogate < 0) {
404 return {};
405 }
406
407 // Check that the second codepoint is within the low surrogate range
408 if (low_surrogate >= 0xDC00 && low_surrogate <= 0xDFFF) {
409 const char32_t codepoint = (char32_t) (((high_surrogate - 0xD800) * 0x400)
410 + (low_surrogate - 0xDC00) + 0x10000);
411
412 // The decoded codepoint should represent a 4 byte, UTF-8 character
413 const size_t utf8_length = (size_t) utf32_to_utf8_length(&codepoint, 1);
414 if (utf8_length != 4) {
415 return {};
416 }
417
418 // Encode the UTF-8 representation of the codepoint into the string
419 char* start = &output[output.size()];
420 output.resize(output.size() + utf8_length);
421 utf32_to_utf8((char32_t*) &codepoint, 1, start, utf8_length + 1);
422
423 index = next_index;
424 continue;
425 }
426 }
427
428 // Append non-surrogate pairs to the output string
429 for (size_t i = index; i < next_index; i++) {
430 output.push_back(modified_utf8[i]);
431 }
432 index = next_index;
433 }
434 return output;
435}
436
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437std::u16string Utf8ToUtf16(const StringPiece& utf8) {
438 ssize_t utf16_length = utf8_to_utf16_length(
439 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
440 if (utf16_length <= 0) {
441 return {};
442 }
443
444 std::u16string utf16;
445 utf16.resize(utf16_length);
446 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
447 &*utf16.begin(), utf16_length + 1);
448 return utf16;
449}
450
451std::string Utf16ToUtf8(const StringPiece16& utf16) {
452 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
453 if (utf8_length <= 0) {
454 return {};
455 }
456
457 std::string utf8;
458 utf8.resize(utf8_length);
459 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
460 return utf8;
461}
462
463bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
464 for (const auto& b : buffer) {
465 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
466 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800467 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 }
469 return true;
470}
471
472std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
473 std::unique_ptr<uint8_t[]> data =
474 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
475 uint8_t* p = data.get();
476 for (const auto& block : buffer) {
477 memcpy(p, block.buffer.get(), block.size);
478 p += block.size;
479 }
480 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481}
482
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700483typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 const char* start = token_.end();
485 const char* end = str_.end();
486 if (start == end) {
487 end_ = true;
488 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700489 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 }
491
492 start += 1;
493 const char* current = start;
494 while (current != end) {
495 if (*current == separator_) {
496 token_.assign(start, current - start);
497 return *this;
498 }
499 ++current;
500 }
501 token_.assign(start, end - start);
502 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700503}
504
505bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 // We check equality here a bit differently.
507 // We need to know that the addresses are the same.
508 return token_.begin() == rhs.token_.begin() &&
509 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700510}
511
512bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700514}
515
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700516Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700518
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700519Tokenizer::Tokenizer(const StringPiece& str, char sep)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
521 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
524 StringPiece* out_entry, StringPiece* out_suffix) {
525 const StringPiece res_prefix("res/");
526 if (!StartsWith(path, res_prefix)) {
527 return false;
528 }
529
530 StringPiece::const_iterator last_occurence = path.end();
531 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
532 ++iter) {
533 if (*iter == '/') {
534 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700535 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 if (last_occurence == path.end()) {
539 return false;
540 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 auto iter = std::find(last_occurence, path.end(), '.');
543 *out_suffix = StringPiece(iter, path.end() - iter);
544 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
545 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
546 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700547}
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
Bernie Innocenti58cf8e32020-12-19 15:31:52 +0900550 if (auto str = pool.stringAt(idx); str.ok()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000551 return *str;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 }
553 return StringPiece16();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700554}
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556std::string GetString(const android::ResStringPool& pool, size_t idx) {
Bernie Innocenti58cf8e32020-12-19 15:31:52 +0900557 if (auto str = pool.string8At(idx); str.ok()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000558 return ModifiedUtf8ToUtf8(str->to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 }
560 return Utf16ToUtf8(GetString16(pool, idx));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700561}
562
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700563} // namespace util
564} // namespace aapt