blob: 9b7ebdd690ac959f9066a58d6dfc0e626e61d010 [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"
Jeremy Meyer7f592a82021-11-01 21:06:20 +000025#include "android-base/strings.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000026#include "androidfw/BigBuffer.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000028#include "androidfw/Util.h"
Ryan Mitchell34039b22019-03-18 08:57:47 -070029#include "build/version.h"
Adam Lesinski96ea08f2017-11-06 10:44:46 -080030#include "text/Unicode.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070031#include "text/Utf8Iterator.h"
Ryan Mitchell34039b22019-03-18 08:57:47 -070032#include "utils/Unicode.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080033
Adam Lesinski66ea8402017-06-28 11:44:11 -070034using ::aapt::text::Utf8Iterator;
Adam Lesinski549e4372017-06-27 18:39:07 -070035using ::android::StringPiece;
36using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080037
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038namespace aapt {
39namespace util {
40
Rhed Jao2c434422020-11-12 10:48:03 +080041// Package name and shared user id would be used as a part of the file name.
42// Limits size to 223 and reserves 32 for the OS.
43// See frameworks/base/core/java/android/content/pm/parsing/ParsingPackageUtils.java
44constexpr static const size_t kMaxPackageNameSize = 223;
45
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046static std::vector<std::string> SplitAndTransform(
47 const StringPiece& str, char sep, const std::function<char(char)>& f) {
48 std::vector<std::string> parts;
49 const StringPiece::const_iterator end = std::end(str);
50 StringPiece::const_iterator start = std::begin(str);
51 StringPiece::const_iterator current;
52 do {
53 current = std::find(start, end, sep);
Adam Lesinskid5083f62017-01-16 15:07:21 -080054 parts.emplace_back(str.substr(start, current).to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 if (f) {
56 std::string& part = parts.back();
57 std::transform(part.begin(), part.end(), part.begin(), f);
58 }
59 start = current + 1;
60 } while (current != end);
61 return parts;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062}
63
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064std::vector<std::string> Split(const StringPiece& str, char sep) {
65 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066}
67
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
69 return SplitAndTransform(str, sep, ::tolower);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070}
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
73 if (str.size() < prefix.size()) {
74 return false;
75 }
76 return str.substr(0, prefix.size()) == prefix;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070077}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
80 if (str.size() < suffix.size()) {
81 return false;
82 }
83 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
Adam Lesinski2eed52e2018-02-21 15:55:58 -080086StringPiece TrimLeadingWhitespace(const StringPiece& str) {
87 if (str.size() == 0 || str.data() == nullptr) {
88 return str;
89 }
90
91 const char* start = str.data();
92 const char* end = start + str.length();
93
94 while (start != end && isspace(*start)) {
95 start++;
96 }
97 return StringPiece(start, end - start);
98}
99
100StringPiece TrimTrailingWhitespace(const StringPiece& str) {
101 if (str.size() == 0 || str.data() == nullptr) {
102 return str;
103 }
104
105 const char* start = str.data();
106 const char* end = start + str.length();
107
108 while (end != start && isspace(*(end - 1))) {
109 end--;
110 }
111 return StringPiece(start, end - start);
112}
113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114StringPiece TrimWhitespace(const StringPiece& str) {
115 if (str.size() == 0 || str.data() == nullptr) {
116 return str;
117 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 const char* start = str.data();
120 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 while (start != end && isspace(*start)) {
123 start++;
124 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 while (end != start && isspace(*(end - 1))) {
127 end--;
128 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700131}
132
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800133static int IsJavaNameImpl(const StringPiece& str) {
134 int pieces = 0;
135 for (const StringPiece& piece : Tokenize(str, '.')) {
136 pieces++;
137 if (!text::IsJavaIdentifier(piece)) {
138 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 }
140 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800141 return pieces;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142}
143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144bool IsJavaClassName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800145 return IsJavaNameImpl(str) >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700146}
147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148bool IsJavaPackageName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800149 return IsJavaNameImpl(str) >= 1;
150}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800152static int IsAndroidNameImpl(const StringPiece& str) {
153 int pieces = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 for (const StringPiece& piece : Tokenize(str, '.')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 if (piece.empty()) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800156 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157 }
158
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800159 const char first_character = piece.data()[0];
160 if (!::isalpha(first_character)) {
161 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800164 bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool {
165 return ::isalnum(c) || c == '_';
166 });
167
168 if (!valid) {
169 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800171 pieces++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800173 return pieces;
174}
175
176bool IsAndroidPackageName(const StringPiece& str) {
Rhed Jao2c434422020-11-12 10:48:03 +0800177 if (str.size() > kMaxPackageNameSize) {
178 return false;
179 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800180 return IsAndroidNameImpl(str) > 1 || str == "android";
181}
182
Rhed Jao2c434422020-11-12 10:48:03 +0800183bool IsAndroidSharedUserId(const android::StringPiece& package_name,
184 const android::StringPiece& shared_user_id) {
185 if (shared_user_id.size() > kMaxPackageNameSize) {
186 return false;
187 }
188 return shared_user_id.empty() || IsAndroidNameImpl(shared_user_id) > 1 ||
189 package_name == "android";
190}
191
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800192bool IsAndroidSplitName(const StringPiece& str) {
193 return IsAndroidNameImpl(str) > 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194}
195
Ryan Mitchell4382e442021-07-14 12:53:01 -0700196std::optional<std::string> GetFullyQualifiedClassName(const StringPiece& package,
197 const StringPiece& classname) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 if (classname.empty()) {
199 return {};
200 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800203 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 if (package.empty()) {
207 return {};
208 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700209
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800210 std::string result = package.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 if (classname.data()[0] != '.') {
212 result += '.';
213 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 result.append(classname.data(), classname.size());
216 if (!IsJavaClassName(result)) {
217 return {};
218 }
219 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700220}
221
Ryan Mitchell34039b22019-03-18 08:57:47 -0700222const char* GetToolName() {
223 static const char* const sToolName = "Android Asset Packaging Tool (aapt)";
224 return sToolName;
225}
226
227std::string GetToolFingerprint() {
228 // DO NOT UPDATE, this is more of a marketing version.
229 static const char* const sMajorVersion = "2";
230
231 // Update minor version whenever a feature or flag is added.
232 static const char* const sMinorVersion = "19";
233
234 // The build id of aapt2 binary.
Jeremy Meyer7f592a82021-11-01 21:06:20 +0000235 static std::string sBuildId = android::build::GetBuildNumber();
236
237 if (android::base::StartsWith(sBuildId, "eng.")) {
238 time_t now = time(0);
239 tm* ltm = localtime(&now);
240
241 sBuildId = android::base::StringPrintf("eng.%d%d", 1900 + ltm->tm_year, 1 + ltm->tm_mon);
242 }
Ryan Mitchell34039b22019-03-18 08:57:47 -0700243
244 return android::base::StringPrintf("%s.%s-%s", sMajorVersion, sMinorVersion, sBuildId.c_str());
245}
246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247static size_t ConsumeDigits(const char* start, const char* end) {
248 const char* c = start;
249 for (; c != end && *c >= '0' && *c <= '9'; c++) {
250 }
251 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800252}
253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254bool VerifyJavaStringFormat(const StringPiece& str) {
255 const char* c = str.begin();
256 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 size_t arg_count = 0;
259 bool nonpositional = false;
260 while (c != end) {
261 if (*c == '%' && c + 1 < end) {
262 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800263
Adam Lesinskib9f05482017-06-02 16:32:37 -0700264 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 c++;
266 continue;
267 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 size_t num_digits = ConsumeDigits(c, end);
272 if (num_digits > 0) {
273 c += num_digits;
274 if (c != end && *c != '$') {
275 // The digits were a size, but not a positional argument.
276 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800277 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 } else if (*c == '<') {
279 // Reusing last argument, bad idea since positions can be moved around
280 // during translation.
281 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 c++;
284
285 // Optionally we can have a $ after
286 if (c != end && *c == '$') {
287 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800288 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 } else {
290 nonpositional = true;
291 }
292
293 // Ignore size, width, flags, etc.
294 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
295 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
296 c++;
297 }
298
299 /*
300 * This is a shortcut to detect strings that are going to Time.format()
301 * instead of String.format()
302 *
303 * Comparison of String.format() and Time.format() args:
304 *
305 * String: ABC E GH ST X abcdefgh nost x
306 * Time: DEFGHKMS W Za d hkm s w yz
307 *
308 * Therefore we know it's definitely Time if we have:
309 * DFKMWZkmwyz
310 */
311 if (c != end) {
312 switch (*c) {
313 case 'D':
314 case 'F':
315 case 'K':
316 case 'M':
317 case 'W':
318 case 'Z':
319 case 'k':
320 case 'm':
321 case 'w':
322 case 'y':
323 case 'z':
324 return true;
325 }
326 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800327 }
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 if (c != end) {
330 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800331 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 }
333
334 if (arg_count > 1 && nonpositional) {
335 // Multiple arguments were specified, but some or all were non positional.
336 // Translated
337 // strings may rearrange the order of the arguments, which will break the
338 // string.
339 return false;
340 }
341 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800342}
343
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344std::u16string Utf8ToUtf16(const StringPiece& utf8) {
345 ssize_t utf16_length = utf8_to_utf16_length(
346 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
347 if (utf16_length <= 0) {
348 return {};
349 }
350
351 std::u16string utf16;
352 utf16.resize(utf16_length);
353 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
354 &*utf16.begin(), utf16_length + 1);
355 return utf16;
356}
357
358std::string Utf16ToUtf8(const StringPiece16& utf16) {
359 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
360 if (utf8_length <= 0) {
361 return {};
362 }
363
364 std::string utf8;
365 utf8.resize(utf8_length);
366 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
367 return utf8;
368}
369
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000370bool WriteAll(std::ostream& out, const android::BigBuffer& buffer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 for (const auto& b : buffer) {
372 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
373 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 }
376 return true;
377}
378
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700379typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 const char* start = token_.end();
381 const char* end = str_.end();
382 if (start == end) {
383 end_ = true;
384 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700385 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 }
387
388 start += 1;
389 const char* current = start;
390 while (current != end) {
391 if (*current == separator_) {
392 token_.assign(start, current - start);
393 return *this;
394 }
395 ++current;
396 }
397 token_.assign(start, end - start);
398 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700399}
400
401bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 // We check equality here a bit differently.
403 // We need to know that the addresses are the same.
404 return token_.begin() == rhs.token_.begin() &&
405 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700406}
407
408bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700410}
411
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700412Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700414
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700415Tokenizer::Tokenizer(const StringPiece& str, char sep)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
417 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
420 StringPiece* out_entry, StringPiece* out_suffix) {
421 const StringPiece res_prefix("res/");
422 if (!StartsWith(path, res_prefix)) {
423 return false;
424 }
425
426 StringPiece::const_iterator last_occurence = path.end();
427 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
428 ++iter) {
429 if (*iter == '/') {
430 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700431 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (last_occurence == path.end()) {
435 return false;
436 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 auto iter = std::find(last_occurence, path.end(), '.');
439 *out_suffix = StringPiece(iter, path.end() - iter);
440 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
441 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
442 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700443}
444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445} // namespace util
446} // namespace aapt