blob: be877660ef72fc998b2c37eb1b7c4fc062894924 [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 Lesinskice5e56e22016-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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070046static std::vector<std::string> SplitAndTransform(StringPiece str, char sep, char (*f)(char)) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070047 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);
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070053 parts.emplace_back(start, current);
Adam Lesinskice5e56e22016-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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070063std::vector<std::string> Split(StringPiece str, char sep) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070064 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065}
66
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070067std::vector<std::string> SplitAndLowercase(StringPiece str, char sep) {
68 return SplitAndTransform(str, sep, [](char c) -> char { return ::tolower(c); });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069}
70
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070071bool StartsWith(StringPiece str, StringPiece prefix) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070072 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070078bool EndsWith(StringPiece str, StringPiece suffix) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070079 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070085StringPiece TrimLeadingWhitespace(StringPiece str) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -080086 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070099StringPiece TrimTrailingWhitespace(StringPiece str) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800100 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700113StringPiece TrimWhitespace(StringPiece str) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700114 if (str.size() == 0 || str.data() == nullptr) {
115 return str;
116 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700117
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700121 while (start != end && isspace(*start)) {
122 start++;
123 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700124
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700125 while (end != start && isspace(*(end - 1))) {
126 end--;
127 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700128
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700129 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700130}
131
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700132static int IsJavaNameImpl(StringPiece str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800133 int pieces = 0;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700134 for (StringPiece piece : Tokenize(str, '.')) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800135 pieces++;
136 if (!text::IsJavaIdentifier(piece)) {
137 return -1;
Adam Lesinskice5e56e22016-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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700143bool IsJavaClassName(StringPiece str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800144 return IsJavaNameImpl(str) >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700145}
146
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700147bool IsJavaPackageName(StringPiece str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800148 return IsJavaNameImpl(str) >= 1;
149}
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700150
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700151static int IsAndroidNameImpl(StringPiece str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800152 int pieces = 0;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700153 for (StringPiece piece : Tokenize(str, '.')) {
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700169 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800170 pieces++;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700171 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800172 return pieces;
173}
174
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700175bool IsAndroidPackageName(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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700182bool IsAndroidSharedUserId(android::StringPiece package_name, android::StringPiece shared_user_id) {
Rhed Jao2c434422020-11-12 10:48:03 +0800183 if (shared_user_id.size() > kMaxPackageNameSize) {
184 return false;
185 }
186 return shared_user_id.empty() || IsAndroidNameImpl(shared_user_id) > 1 ||
187 package_name == "android";
188}
189
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700190bool IsAndroidSplitName(StringPiece str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800191 return IsAndroidNameImpl(str) > 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192}
193
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700194std::optional<std::string> GetFullyQualifiedClassName(StringPiece package, StringPiece classname) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700195 if (classname.empty()) {
196 return {};
197 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700198
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700199 if (util::IsJavaClassName(classname)) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700200 return std::string(classname);
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700201 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700202
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700203 if (package.empty()) {
204 return {};
205 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700206
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700207 std::string result{package};
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700208 if (classname.data()[0] != '.') {
209 result += '.';
210 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800211
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700212 result.append(classname.data(), classname.size());
213 if (!IsJavaClassName(result)) {
214 return {};
215 }
216 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700217}
218
Ryan Mitchell34039b22019-03-18 08:57:47 -0700219const char* GetToolName() {
220 static const char* const sToolName = "Android Asset Packaging Tool (aapt)";
221 return sToolName;
222}
223
224std::string GetToolFingerprint() {
225 // DO NOT UPDATE, this is more of a marketing version.
226 static const char* const sMajorVersion = "2";
227
228 // Update minor version whenever a feature or flag is added.
229 static const char* const sMinorVersion = "19";
230
231 // The build id of aapt2 binary.
Jeremy Meyer7f592a82021-11-01 21:06:20 +0000232 static std::string sBuildId = android::build::GetBuildNumber();
233
234 if (android::base::StartsWith(sBuildId, "eng.")) {
235 time_t now = time(0);
236 tm* ltm = localtime(&now);
237
238 sBuildId = android::base::StringPrintf("eng.%d%d", 1900 + ltm->tm_year, 1 + ltm->tm_mon);
239 }
Ryan Mitchell34039b22019-03-18 08:57:47 -0700240
241 return android::base::StringPrintf("%s.%s-%s", sMajorVersion, sMinorVersion, sBuildId.c_str());
242}
243
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700244static size_t ConsumeDigits(const char* start, const char* end) {
245 const char* c = start;
246 for (; c != end && *c >= '0' && *c <= '9'; c++) {
247 }
248 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800249}
250
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700251bool VerifyJavaStringFormat(StringPiece str) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700252 const char* c = str.begin();
253 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800254
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700255 size_t arg_count = 0;
256 bool nonpositional = false;
257 while (c != end) {
258 if (*c == '%' && c + 1 < end) {
259 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800260
Adam Lesinskib9f05482017-06-02 16:32:37 -0700261 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700262 c++;
263 continue;
264 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800265
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700266 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800267
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700268 size_t num_digits = ConsumeDigits(c, end);
269 if (num_digits > 0) {
270 c += num_digits;
271 if (c != end && *c != '$') {
272 // The digits were a size, but not a positional argument.
273 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800274 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700275 } else if (*c == '<') {
276 // Reusing last argument, bad idea since positions can be moved around
277 // during translation.
278 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800279
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700280 c++;
281
282 // Optionally we can have a $ after
283 if (c != end && *c == '$') {
284 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800285 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700286 } else {
287 nonpositional = true;
288 }
289
290 // Ignore size, width, flags, etc.
291 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
292 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
293 c++;
294 }
295
296 /*
297 * This is a shortcut to detect strings that are going to Time.format()
298 * instead of String.format()
299 *
300 * Comparison of String.format() and Time.format() args:
301 *
302 * String: ABC E GH ST X abcdefgh nost x
303 * Time: DEFGHKMS W Za d hkm s w yz
304 *
305 * Therefore we know it's definitely Time if we have:
306 * DFKMWZkmwyz
307 */
308 if (c != end) {
309 switch (*c) {
310 case 'D':
311 case 'F':
312 case 'K':
313 case 'M':
314 case 'W':
315 case 'Z':
316 case 'k':
317 case 'm':
318 case 'w':
319 case 'y':
320 case 'z':
321 return true;
322 }
323 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800324 }
325
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700326 if (c != end) {
327 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800328 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700329 }
330
331 if (arg_count > 1 && nonpositional) {
332 // Multiple arguments were specified, but some or all were non positional.
333 // Translated
334 // strings may rearrange the order of the arguments, which will break the
335 // string.
336 return false;
337 }
338 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800339}
340
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700341std::u16string Utf8ToUtf16(StringPiece utf8) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700342 ssize_t utf16_length = utf8_to_utf16_length(
343 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
344 if (utf16_length <= 0) {
345 return {};
346 }
347
348 std::u16string utf16;
349 utf16.resize(utf16_length);
350 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
351 &*utf16.begin(), utf16_length + 1);
352 return utf16;
353}
354
355std::string Utf16ToUtf8(const StringPiece16& utf16) {
356 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
357 if (utf8_length <= 0) {
358 return {};
359 }
360
361 std::string utf8;
362 utf8.resize(utf8_length);
363 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
364 return utf8;
365}
366
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000367bool WriteAll(std::ostream& out, const android::BigBuffer& buffer) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700368 for (const auto& b : buffer) {
369 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
370 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800371 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700372 }
373 return true;
374}
375
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700376typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700377 const char* start = token_.end();
378 const char* end = str_.end();
379 if (start == end) {
380 end_ = true;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700381 token_ = StringPiece(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700382 return *this;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700383 }
384
385 start += 1;
386 const char* current = start;
387 while (current != end) {
388 if (*current == separator_) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700389 token_ = StringPiece(start, current - start);
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700390 return *this;
391 }
392 ++current;
393 }
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700394 token_ = StringPiece(start, end - start);
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700395 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700396}
397
398bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700399 // We check equality here a bit differently.
400 // We need to know that the addresses are the same.
401 return token_.begin() == rhs.token_.begin() &&
402 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700403}
404
405bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700406 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700407}
408
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700409Tokenizer::iterator::iterator(StringPiece s, char sep, StringPiece tok, bool end)
410 : str_(s), separator_(sep), token_(tok), end_(end) {
411}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700412
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700413Tokenizer::Tokenizer(StringPiece str, char sep)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700414 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700415 end_(str, sep, StringPiece(str.end(), 0), true) {
416}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700417
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700418bool ExtractResFilePathParts(StringPiece path, StringPiece* out_prefix, StringPiece* out_entry,
419 StringPiece* out_suffix) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700420 const StringPiece res_prefix("res/");
421 if (!StartsWith(path, res_prefix)) {
422 return false;
423 }
424
425 StringPiece::const_iterator last_occurence = path.end();
426 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
427 ++iter) {
428 if (*iter == '/') {
429 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700430 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700431 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700432
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700433 if (last_occurence == path.end()) {
434 return false;
435 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700437 auto iter = std::find(last_occurence, path.end(), '.');
438 *out_suffix = StringPiece(iter, path.end() - iter);
439 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
440 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
441 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700442}
443
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700444} // namespace util
445} // namespace aapt