blob: d1c9ca1644d99d75cff7b9780a842819ab77558b [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
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinski549e4372017-06-27 18:39:07 -070025#include "utils/Unicode.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026
Adam Lesinski96ea08f2017-11-06 10:44:46 -080027#include "text/Unicode.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070028#include "text/Utf8Iterator.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029#include "util/BigBuffer.h"
30#include "util/Maybe.h"
31
Adam Lesinski66ea8402017-06-28 11:44:11 -070032using ::aapt::text::Utf8Iterator;
Adam Lesinski549e4372017-06-27 18:39:07 -070033using ::android::StringPiece;
34using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080035
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036namespace aapt {
37namespace util {
38
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039static std::vector<std::string> SplitAndTransform(
40 const StringPiece& str, char sep, const std::function<char(char)>& f) {
41 std::vector<std::string> parts;
42 const StringPiece::const_iterator end = std::end(str);
43 StringPiece::const_iterator start = std::begin(str);
44 StringPiece::const_iterator current;
45 do {
46 current = std::find(start, end, sep);
Adam Lesinskid5083f62017-01-16 15:07:21 -080047 parts.emplace_back(str.substr(start, current).to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 if (f) {
49 std::string& part = parts.back();
50 std::transform(part.begin(), part.end(), part.begin(), f);
51 }
52 start = current + 1;
53 } while (current != end);
54 return parts;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057std::vector<std::string> Split(const StringPiece& str, char sep) {
58 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059}
60
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
62 return SplitAndTransform(str, sep, ::tolower);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
66 if (str.size() < prefix.size()) {
67 return false;
68 }
69 return str.substr(0, prefix.size()) == prefix;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070070}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
73 if (str.size() < suffix.size()) {
74 return false;
75 }
76 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077}
78
Adam Lesinski2eed52e2018-02-21 15:55:58 -080079StringPiece TrimLeadingWhitespace(const StringPiece& str) {
80 if (str.size() == 0 || str.data() == nullptr) {
81 return str;
82 }
83
84 const char* start = str.data();
85 const char* end = start + str.length();
86
87 while (start != end && isspace(*start)) {
88 start++;
89 }
90 return StringPiece(start, end - start);
91}
92
93StringPiece TrimTrailingWhitespace(const StringPiece& str) {
94 if (str.size() == 0 || str.data() == nullptr) {
95 return str;
96 }
97
98 const char* start = str.data();
99 const char* end = start + str.length();
100
101 while (end != start && isspace(*(end - 1))) {
102 end--;
103 }
104 return StringPiece(start, end - start);
105}
106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107StringPiece TrimWhitespace(const StringPiece& str) {
108 if (str.size() == 0 || str.data() == nullptr) {
109 return str;
110 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 const char* start = str.data();
113 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 while (start != end && isspace(*start)) {
116 start++;
117 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 while (end != start && isspace(*(end - 1))) {
120 end--;
121 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700124}
125
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800126static int IsJavaNameImpl(const StringPiece& str) {
127 int pieces = 0;
128 for (const StringPiece& piece : Tokenize(str, '.')) {
129 pieces++;
130 if (!text::IsJavaIdentifier(piece)) {
131 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 }
133 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800134 return pieces;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135}
136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137bool IsJavaClassName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800138 return IsJavaNameImpl(str) >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700139}
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141bool IsJavaPackageName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800142 return IsJavaNameImpl(str) >= 1;
143}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800145static int IsAndroidNameImpl(const StringPiece& str) {
146 int pieces = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 for (const StringPiece& piece : Tokenize(str, '.')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 if (piece.empty()) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800149 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150 }
151
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800152 const char first_character = piece.data()[0];
153 if (!::isalpha(first_character)) {
154 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800157 bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool {
158 return ::isalnum(c) || c == '_';
159 });
160
161 if (!valid) {
162 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800164 pieces++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800166 return pieces;
167}
168
169bool IsAndroidPackageName(const StringPiece& str) {
170 return IsAndroidNameImpl(str) > 1 || str == "android";
171}
172
173bool IsAndroidSplitName(const StringPiece& str) {
174 return IsAndroidNameImpl(str) > 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175}
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
178 const StringPiece& classname) {
179 if (classname.empty()) {
180 return {};
181 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800184 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 if (package.empty()) {
188 return {};
189 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700190
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800191 std::string result = package.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 if (classname.data()[0] != '.') {
193 result += '.';
194 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 result.append(classname.data(), classname.size());
197 if (!IsJavaClassName(result)) {
198 return {};
199 }
200 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700201}
202
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203static size_t ConsumeDigits(const char* start, const char* end) {
204 const char* c = start;
205 for (; c != end && *c >= '0' && *c <= '9'; c++) {
206 }
207 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800208}
209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210bool VerifyJavaStringFormat(const StringPiece& str) {
211 const char* c = str.begin();
212 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 size_t arg_count = 0;
215 bool nonpositional = false;
216 while (c != end) {
217 if (*c == '%' && c + 1 < end) {
218 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800219
Adam Lesinskib9f05482017-06-02 16:32:37 -0700220 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 c++;
222 continue;
223 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 size_t num_digits = ConsumeDigits(c, end);
228 if (num_digits > 0) {
229 c += num_digits;
230 if (c != end && *c != '$') {
231 // The digits were a size, but not a positional argument.
232 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800233 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 } else if (*c == '<') {
235 // Reusing last argument, bad idea since positions can be moved around
236 // during translation.
237 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 c++;
240
241 // Optionally we can have a $ after
242 if (c != end && *c == '$') {
243 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800244 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 } else {
246 nonpositional = true;
247 }
248
249 // Ignore size, width, flags, etc.
250 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
251 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
252 c++;
253 }
254
255 /*
256 * This is a shortcut to detect strings that are going to Time.format()
257 * instead of String.format()
258 *
259 * Comparison of String.format() and Time.format() args:
260 *
261 * String: ABC E GH ST X abcdefgh nost x
262 * Time: DEFGHKMS W Za d hkm s w yz
263 *
264 * Therefore we know it's definitely Time if we have:
265 * DFKMWZkmwyz
266 */
267 if (c != end) {
268 switch (*c) {
269 case 'D':
270 case 'F':
271 case 'K':
272 case 'M':
273 case 'W':
274 case 'Z':
275 case 'k':
276 case 'm':
277 case 'w':
278 case 'y':
279 case 'z':
280 return true;
281 }
282 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800283 }
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 if (c != end) {
286 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800287 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 }
289
290 if (arg_count > 1 && nonpositional) {
291 // Multiple arguments were specified, but some or all were non positional.
292 // Translated
293 // strings may rearrange the order of the arguments, which will break the
294 // string.
295 return false;
296 }
297 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800298}
299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300std::u16string Utf8ToUtf16(const StringPiece& utf8) {
301 ssize_t utf16_length = utf8_to_utf16_length(
302 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
303 if (utf16_length <= 0) {
304 return {};
305 }
306
307 std::u16string utf16;
308 utf16.resize(utf16_length);
309 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
310 &*utf16.begin(), utf16_length + 1);
311 return utf16;
312}
313
314std::string Utf16ToUtf8(const StringPiece16& utf16) {
315 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
316 if (utf8_length <= 0) {
317 return {};
318 }
319
320 std::string utf8;
321 utf8.resize(utf8_length);
322 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
323 return utf8;
324}
325
326bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
327 for (const auto& b : buffer) {
328 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
329 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800330 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 }
332 return true;
333}
334
335std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
336 std::unique_ptr<uint8_t[]> data =
337 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
338 uint8_t* p = data.get();
339 for (const auto& block : buffer) {
340 memcpy(p, block.buffer.get(), block.size);
341 p += block.size;
342 }
343 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800344}
345
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700346typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 const char* start = token_.end();
348 const char* end = str_.end();
349 if (start == end) {
350 end_ = true;
351 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700352 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 }
354
355 start += 1;
356 const char* current = start;
357 while (current != end) {
358 if (*current == separator_) {
359 token_.assign(start, current - start);
360 return *this;
361 }
362 ++current;
363 }
364 token_.assign(start, end - start);
365 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700366}
367
368bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 // We check equality here a bit differently.
370 // We need to know that the addresses are the same.
371 return token_.begin() == rhs.token_.begin() &&
372 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700373}
374
375bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700377}
378
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700379Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700381
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700382Tokenizer::Tokenizer(const StringPiece& str, char sep)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
384 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
387 StringPiece* out_entry, StringPiece* out_suffix) {
388 const StringPiece res_prefix("res/");
389 if (!StartsWith(path, res_prefix)) {
390 return false;
391 }
392
393 StringPiece::const_iterator last_occurence = path.end();
394 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
395 ++iter) {
396 if (*iter == '/') {
397 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 if (last_occurence == path.end()) {
402 return false;
403 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 auto iter = std::find(last_occurence, path.end(), '.');
406 *out_suffix = StringPiece(iter, path.end() - iter);
407 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
408 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
409 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700410}
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
413 size_t len;
414 const char16_t* str = pool.stringAt(idx, &len);
415 if (str != nullptr) {
416 return StringPiece16(str, len);
417 }
418 return StringPiece16();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700419}
420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421std::string GetString(const android::ResStringPool& pool, size_t idx) {
422 size_t len;
423 const char* str = pool.string8At(idx, &len);
424 if (str != nullptr) {
425 return std::string(str, len);
426 }
427 return Utf16ToUtf8(GetString16(pool, idx));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700428}
429
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430} // namespace util
431} // namespace aapt