1 /*
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
17 #include "util/Util.h"
18
19 #include <algorithm>
20 #include <ostream>
21 #include <string>
22 #include <vector>
23
24 #include "androidfw/StringPiece.h"
25 #include "utils/Unicode.h"
26
27 #include "text/Utf8Iterator.h"
28 #include "util/BigBuffer.h"
29 #include "util/Maybe.h"
30
31 using ::aapt::text::Utf8Iterator;
32 using ::android::StringPiece;
33 using ::android::StringPiece16;
34
35 namespace aapt {
36 namespace util {
37
SplitAndTransform(const StringPiece & str,char sep,const std::function<char (char)> & f)38 static std::vector<std::string> SplitAndTransform(
39 const StringPiece& str, char sep, const std::function<char(char)>& f) {
40 std::vector<std::string> parts;
41 const StringPiece::const_iterator end = std::end(str);
42 StringPiece::const_iterator start = std::begin(str);
43 StringPiece::const_iterator current;
44 do {
45 current = std::find(start, end, sep);
46 parts.emplace_back(str.substr(start, current).to_string());
47 if (f) {
48 std::string& part = parts.back();
49 std::transform(part.begin(), part.end(), part.begin(), f);
50 }
51 start = current + 1;
52 } while (current != end);
53 return parts;
54 }
55
Split(const StringPiece & str,char sep)56 std::vector<std::string> Split(const StringPiece& str, char sep) {
57 return SplitAndTransform(str, sep, nullptr);
58 }
59
SplitAndLowercase(const StringPiece & str,char sep)60 std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
61 return SplitAndTransform(str, sep, ::tolower);
62 }
63
StartsWith(const StringPiece & str,const StringPiece & prefix)64 bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
65 if (str.size() < prefix.size()) {
66 return false;
67 }
68 return str.substr(0, prefix.size()) == prefix;
69 }
70
EndsWith(const StringPiece & str,const StringPiece & suffix)71 bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
72 if (str.size() < suffix.size()) {
73 return false;
74 }
75 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
76 }
77
TrimWhitespace(const StringPiece & str)78 StringPiece TrimWhitespace(const StringPiece& str) {
79 if (str.size() == 0 || str.data() == nullptr) {
80 return str;
81 }
82
83 const char* start = str.data();
84 const char* end = str.data() + str.length();
85
86 while (start != end && isspace(*start)) {
87 start++;
88 }
89
90 while (end != start && isspace(*(end - 1))) {
91 end--;
92 }
93
94 return StringPiece(start, end - start);
95 }
96
FindNonAlphaNumericAndNotInSet(const StringPiece & str,const StringPiece & allowed_chars)97 StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
98 const StringPiece& str, const StringPiece& allowed_chars) {
99 const auto end_iter = str.end();
100 for (auto iter = str.begin(); iter != end_iter; ++iter) {
101 char c = *iter;
102 if ((c >= u'a' && c <= u'z') || (c >= u'A' && c <= u'Z') ||
103 (c >= u'0' && c <= u'9')) {
104 continue;
105 }
106
107 bool match = false;
108 for (char i : allowed_chars) {
109 if (c == i) {
110 match = true;
111 break;
112 }
113 }
114
115 if (!match) {
116 return iter;
117 }
118 }
119 return end_iter;
120 }
121
IsJavaClassName(const StringPiece & str)122 bool IsJavaClassName(const StringPiece& str) {
123 size_t pieces = 0;
124 for (const StringPiece& piece : Tokenize(str, '.')) {
125 pieces++;
126 if (piece.empty()) {
127 return false;
128 }
129
130 // Can't have starting or trailing $ character.
131 if (piece.data()[0] == '$' || piece.data()[piece.size() - 1] == '$') {
132 return false;
133 }
134
135 if (FindNonAlphaNumericAndNotInSet(piece, "$_") != piece.end()) {
136 return false;
137 }
138 }
139 return pieces >= 2;
140 }
141
IsJavaPackageName(const StringPiece & str)142 bool IsJavaPackageName(const StringPiece& str) {
143 if (str.empty()) {
144 return false;
145 }
146
147 size_t pieces = 0;
148 for (const StringPiece& piece : Tokenize(str, '.')) {
149 pieces++;
150 if (piece.empty()) {
151 return false;
152 }
153
154 if (piece.data()[0] == '_' || piece.data()[piece.size() - 1] == '_') {
155 return false;
156 }
157
158 if (FindNonAlphaNumericAndNotInSet(piece, "_") != piece.end()) {
159 return false;
160 }
161 }
162 return pieces >= 1;
163 }
164
GetFullyQualifiedClassName(const StringPiece & package,const StringPiece & classname)165 Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
166 const StringPiece& classname) {
167 if (classname.empty()) {
168 return {};
169 }
170
171 if (util::IsJavaClassName(classname)) {
172 return classname.to_string();
173 }
174
175 if (package.empty()) {
176 return {};
177 }
178
179 std::string result(package.data(), package.size());
180 if (classname.data()[0] != '.') {
181 result += '.';
182 }
183
184 result.append(classname.data(), classname.size());
185 if (!IsJavaClassName(result)) {
186 return {};
187 }
188 return result;
189 }
190
ConsumeDigits(const char * start,const char * end)191 static size_t ConsumeDigits(const char* start, const char* end) {
192 const char* c = start;
193 for (; c != end && *c >= '0' && *c <= '9'; c++) {
194 }
195 return static_cast<size_t>(c - start);
196 }
197
VerifyJavaStringFormat(const StringPiece & str)198 bool VerifyJavaStringFormat(const StringPiece& str) {
199 const char* c = str.begin();
200 const char* const end = str.end();
201
202 size_t arg_count = 0;
203 bool nonpositional = false;
204 while (c != end) {
205 if (*c == '%' && c + 1 < end) {
206 c++;
207
208 if (*c == '%' || *c == 'n') {
209 c++;
210 continue;
211 }
212
213 arg_count++;
214
215 size_t num_digits = ConsumeDigits(c, end);
216 if (num_digits > 0) {
217 c += num_digits;
218 if (c != end && *c != '$') {
219 // The digits were a size, but not a positional argument.
220 nonpositional = true;
221 }
222 } else if (*c == '<') {
223 // Reusing last argument, bad idea since positions can be moved around
224 // during translation.
225 nonpositional = true;
226
227 c++;
228
229 // Optionally we can have a $ after
230 if (c != end && *c == '$') {
231 c++;
232 }
233 } else {
234 nonpositional = true;
235 }
236
237 // Ignore size, width, flags, etc.
238 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
239 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
240 c++;
241 }
242
243 /*
244 * This is a shortcut to detect strings that are going to Time.format()
245 * instead of String.format()
246 *
247 * Comparison of String.format() and Time.format() args:
248 *
249 * String: ABC E GH ST X abcdefgh nost x
250 * Time: DEFGHKMS W Za d hkm s w yz
251 *
252 * Therefore we know it's definitely Time if we have:
253 * DFKMWZkmwyz
254 */
255 if (c != end) {
256 switch (*c) {
257 case 'D':
258 case 'F':
259 case 'K':
260 case 'M':
261 case 'W':
262 case 'Z':
263 case 'k':
264 case 'm':
265 case 'w':
266 case 'y':
267 case 'z':
268 return true;
269 }
270 }
271 }
272
273 if (c != end) {
274 c++;
275 }
276 }
277
278 if (arg_count > 1 && nonpositional) {
279 // Multiple arguments were specified, but some or all were non positional.
280 // Translated
281 // strings may rearrange the order of the arguments, which will break the
282 // string.
283 return false;
284 }
285 return true;
286 }
287
AppendCodepointToUtf8String(char32_t codepoint,std::string * output)288 static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
289 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
290 if (len < 0) {
291 return false;
292 }
293
294 const size_t start_append_pos = output->size();
295
296 // Make room for the next character.
297 output->resize(output->size() + len);
298
299 char* dst = &*(output->begin() + start_append_pos);
300 utf32_to_utf8(&codepoint, 1, dst, len + 1);
301 return true;
302 }
303
AppendUnicodeCodepoint(Utf8Iterator * iter,std::string * output)304 static bool AppendUnicodeCodepoint(Utf8Iterator* iter, std::string* output) {
305 char32_t code = 0;
306 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
307 char32_t codepoint = iter->Next();
308 char32_t a;
309 if (codepoint >= U'0' && codepoint <= U'9') {
310 a = codepoint - U'0';
311 } else if (codepoint >= U'a' && codepoint <= U'f') {
312 a = codepoint - U'a' + 10;
313 } else if (codepoint >= U'A' && codepoint <= U'F') {
314 a = codepoint - U'A' + 10;
315 } else {
316 return {};
317 }
318 code = (code << 4) | a;
319 }
320 return AppendCodepointToUtf8String(code, output);
321 }
322
IsCodepointSpace(char32_t codepoint)323 static bool IsCodepointSpace(char32_t codepoint) {
324 if (static_cast<uint32_t>(codepoint) & 0xffffff00u) {
325 return false;
326 }
327 return isspace(static_cast<char>(codepoint));
328 }
329
StringBuilder(bool preserve_spaces)330 StringBuilder::StringBuilder(bool preserve_spaces) : preserve_spaces_(preserve_spaces) {
331 }
332
Append(const StringPiece & str)333 StringBuilder& StringBuilder::Append(const StringPiece& str) {
334 if (!error_.empty()) {
335 return *this;
336 }
337
338 // Where the new data will be appended to.
339 const size_t new_data_index = str_.size();
340
341 Utf8Iterator iter(str);
342 while (iter.HasNext()) {
343 const char32_t codepoint = iter.Next();
344
345 if (last_char_was_escape_) {
346 switch (codepoint) {
347 case U't':
348 str_ += '\t';
349 break;
350
351 case U'n':
352 str_ += '\n';
353 break;
354
355 case U'#':
356 case U'@':
357 case U'?':
358 case U'"':
359 case U'\'':
360 case U'\\':
361 str_ += static_cast<char>(codepoint);
362 break;
363
364 case U'u':
365 if (!AppendUnicodeCodepoint(&iter, &str_)) {
366 error_ = "invalid unicode escape sequence";
367 return *this;
368 }
369 break;
370
371 default:
372 // Ignore the escape character and just include the codepoint.
373 AppendCodepointToUtf8String(codepoint, &str_);
374 break;
375 }
376 last_char_was_escape_ = false;
377
378 } else if (!preserve_spaces_ && codepoint == U'"') {
379 if (!quote_ && trailing_space_) {
380 // We found an opening quote, and we have trailing space, so we should append that
381 // space now.
382 if (trailing_space_) {
383 // We had trailing whitespace, so replace with a single space.
384 if (!str_.empty()) {
385 str_ += ' ';
386 }
387 trailing_space_ = false;
388 }
389 }
390 quote_ = !quote_;
391
392 } else if (!preserve_spaces_ && codepoint == U'\'' && !quote_) {
393 // This should be escaped.
394 error_ = "unescaped apostrophe";
395 return *this;
396
397 } else if (codepoint == U'\\') {
398 // This is an escape sequence, convert to the real value.
399 if (!quote_ && trailing_space_) {
400 // We had trailing whitespace, so
401 // replace with a single space.
402 if (!str_.empty()) {
403 str_ += ' ';
404 }
405 trailing_space_ = false;
406 }
407 last_char_was_escape_ = true;
408 } else {
409 if (preserve_spaces_ || quote_) {
410 // Quotes mean everything is taken, including whitespace.
411 AppendCodepointToUtf8String(codepoint, &str_);
412 } else {
413 // This is not quoted text, so we will accumulate whitespace and only emit a single
414 // character of whitespace if it is followed by a non-whitespace character.
415 if (IsCodepointSpace(codepoint)) {
416 // We found whitespace.
417 trailing_space_ = true;
418 } else {
419 if (trailing_space_) {
420 // We saw trailing space before, so replace all
421 // that trailing space with one space.
422 if (!str_.empty()) {
423 str_ += ' ';
424 }
425 trailing_space_ = false;
426 }
427 AppendCodepointToUtf8String(codepoint, &str_);
428 }
429 }
430 }
431 }
432
433 // Accumulate the added string's UTF-16 length.
434 ssize_t len = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str_.data()) + new_data_index,
435 str_.size() - new_data_index);
436 if (len < 0) {
437 error_ = "invalid unicode code point";
438 return *this;
439 }
440 utf16_len_ += len;
441 return *this;
442 }
443
Utf8ToUtf16(const StringPiece & utf8)444 std::u16string Utf8ToUtf16(const StringPiece& utf8) {
445 ssize_t utf16_length = utf8_to_utf16_length(
446 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
447 if (utf16_length <= 0) {
448 return {};
449 }
450
451 std::u16string utf16;
452 utf16.resize(utf16_length);
453 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
454 &*utf16.begin(), utf16_length + 1);
455 return utf16;
456 }
457
Utf16ToUtf8(const StringPiece16 & utf16)458 std::string Utf16ToUtf8(const StringPiece16& utf16) {
459 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
460 if (utf8_length <= 0) {
461 return {};
462 }
463
464 std::string utf8;
465 utf8.resize(utf8_length);
466 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
467 return utf8;
468 }
469
WriteAll(std::ostream & out,const BigBuffer & buffer)470 bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
471 for (const auto& b : buffer) {
472 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
473 return false;
474 }
475 }
476 return true;
477 }
478
Copy(const BigBuffer & buffer)479 std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
480 std::unique_ptr<uint8_t[]> data =
481 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
482 uint8_t* p = data.get();
483 for (const auto& block : buffer) {
484 memcpy(p, block.buffer.get(), block.size);
485 p += block.size;
486 }
487 return data;
488 }
489
operator ++()490 typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
491 const char* start = token_.end();
492 const char* end = str_.end();
493 if (start == end) {
494 end_ = true;
495 token_.assign(token_.end(), 0);
496 return *this;
497 }
498
499 start += 1;
500 const char* current = start;
501 while (current != end) {
502 if (*current == separator_) {
503 token_.assign(start, current - start);
504 return *this;
505 }
506 ++current;
507 }
508 token_.assign(start, end - start);
509 return *this;
510 }
511
operator ==(const iterator & rhs) const512 bool Tokenizer::iterator::operator==(const iterator& rhs) const {
513 // We check equality here a bit differently.
514 // We need to know that the addresses are the same.
515 return token_.begin() == rhs.token_.begin() &&
516 token_.end() == rhs.token_.end() && end_ == rhs.end_;
517 }
518
operator !=(const iterator & rhs) const519 bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
520 return !(*this == rhs);
521 }
522
iterator(StringPiece s,char sep,StringPiece tok,bool end)523 Tokenizer::iterator::iterator(StringPiece s, char sep, StringPiece tok,
524 bool end)
525 : str_(s), separator_(sep), token_(tok), end_(end) {}
526
Tokenizer(StringPiece str,char sep)527 Tokenizer::Tokenizer(StringPiece str, char sep)
528 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
529 end_(str, sep, StringPiece(str.end(), 0), true) {}
530
ExtractResFilePathParts(const StringPiece & path,StringPiece * out_prefix,StringPiece * out_entry,StringPiece * out_suffix)531 bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
532 StringPiece* out_entry, StringPiece* out_suffix) {
533 const StringPiece res_prefix("res/");
534 if (!StartsWith(path, res_prefix)) {
535 return false;
536 }
537
538 StringPiece::const_iterator last_occurence = path.end();
539 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
540 ++iter) {
541 if (*iter == '/') {
542 last_occurence = iter;
543 }
544 }
545
546 if (last_occurence == path.end()) {
547 return false;
548 }
549
550 auto iter = std::find(last_occurence, path.end(), '.');
551 *out_suffix = StringPiece(iter, path.end() - iter);
552 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
553 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
554 return true;
555 }
556
GetString16(const android::ResStringPool & pool,size_t idx)557 StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
558 size_t len;
559 const char16_t* str = pool.stringAt(idx, &len);
560 if (str != nullptr) {
561 return StringPiece16(str, len);
562 }
563 return StringPiece16();
564 }
565
GetString(const android::ResStringPool & pool,size_t idx)566 std::string GetString(const android::ResStringPool& pool, size_t idx) {
567 size_t len;
568 const char* str = pool.string8At(idx, &len);
569 if (str != nullptr) {
570 return std::string(str, len);
571 }
572 return Utf16ToUtf8(GetString16(pool, idx));
573 }
574
575 } // namespace util
576 } // namespace aapt
577