blob: a956957eace867c124be1132b5a65725d5063dee [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
17#ifndef AAPT_UTIL_H
18#define AAPT_UTIL_H
19
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <functional>
21#include <memory>
22#include <ostream>
23#include <string>
24#include <vector>
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "utils/ByteOrder.h"
29
30#include "util/BigBuffer.h"
31#include "util/Maybe.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
33#ifdef _WIN32
34// TODO(adamlesinski): remove once http://b/32447322 is resolved.
35// utils/ByteOrder.h includes winsock2.h on WIN32,
36// which will pull in the ERROR definition. This conflicts
37// with android-base/logging.h, which takes care of undefining
38// ERROR, but it gets included too early (before winsock2.h).
39#ifdef ERROR
40#undef ERROR
41#endif
42#endif
43
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
45namespace util {
46
Adam Lesinskic744ae82017-05-17 19:28:38 -070047template <typename T>
48struct Range {
49 T start;
50 T end;
51};
52
Adam Lesinskid5083f62017-01-16 15:07:21 -080053std::vector<std::string> Split(const android::StringPiece& str, char sep);
54std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinski96ea08f2017-11-06 10:44:46 -080056// Returns true if the string starts with prefix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080057bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070058
Adam Lesinski96ea08f2017-11-06 10:44:46 -080059// Returns true if the string ends with suffix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080060bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinski2eed52e2018-02-21 15:55:58 -080062// Creates a new StringPiece that points to a substring of the original string without leading
63// whitespace.
64android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str);
65
66// Creates a new StringPiece that points to a substring of the original string without trailing
67// whitespace.
68android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str);
69
70// Creates a new StringPiece that points to a substring of the original string without leading or
Adam Lesinski96ea08f2017-11-06 10:44:46 -080071// trailing whitespace.
Adam Lesinskid5083f62017-01-16 15:07:21 -080072android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070073
Adam Lesinski96ea08f2017-11-06 10:44:46 -080074// Tests that the string is a valid Java class name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080075bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070076
Adam Lesinski96ea08f2017-11-06 10:44:46 -080077// Tests that the string is a valid Java package name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080078bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079
Adam Lesinski96ea08f2017-11-06 10:44:46 -080080// Tests that the string is a valid Android package name. More strict than a Java package name.
81// - First character of each component (separated by '.') must be an ASCII letter.
82// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
83// - Package must contain at least two components, unless it is 'android'.
84bool IsAndroidPackageName(const android::StringPiece& str);
85
86// Tests that the string is a valid Android split name.
87// - First character of each component (separated by '.') must be an ASCII letter.
88// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
89bool IsAndroidSplitName(const android::StringPiece& str);
90
91// Converts the class name to a fully qualified class name from the given
92// `package`. Ex:
93//
94// asdf --> package.asdf
95// .asdf --> package.asdf
96// .a.b --> package.a.b
97// asdf.adsf --> asdf.adsf
Adam Lesinskid5083f62017-01-16 15:07:21 -080098Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
99 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700100
Ryan Mitchell34039b22019-03-18 08:57:47 -0700101// Retrieves the formatted name of aapt2.
102const char* GetToolName();
103
104// Retrieves the build fingerprint of aapt2.
105std::string GetToolFingerprint();
106
Adam Lesinski060b53d2017-07-28 17:10:35 -0700107template <typename T>
108typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
109 if (a < b) {
110 return -1;
111 } else if (a > b) {
112 return 1;
113 }
114 return 0;
115}
116
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800117// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
118// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119template <typename T, class... Args>
120std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800124// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700125template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800126::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
127 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 using std::begin;
129 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 const auto begin_iter = begin(container);
131 const auto end_iter = end(container);
132 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
133 for (auto iter = begin_iter; iter != end_iter; ++iter) {
134 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 out << sep;
136 }
137 out << *iter;
138 }
139 return out;
140 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141}
142
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800143// Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
144// the conversion to UTF-16 happens within ResStringPool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800145android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800147// Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
148// the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
149// which maintains no state or cache. This means we must return an std::string copy.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800151
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800152// Checks that the Java string format contains no non-positional arguments (arguments without
153// explicitly specifying an index) when there are more than one argument. This is an error
154// because translations may rearrange the order of the arguments in the string, which will
155// break the string interpolation.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800156bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800157
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800158bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces,
159 std::string* out_str, std::string* out_error);
160
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 public:
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800163 StringBuilder() = default;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700164
Adam Lesinskid5083f62017-01-16 15:07:21 -0800165 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 const std::string& ToString() const;
167 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800168 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 // When building StyledStrings, we need UTF-16 indices into the string,
171 // which is what the Java layer expects when dealing with java
172 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 std::string str_;
179 size_t utf16_len_ = 0;
180 bool quote_ = false;
181 bool trailing_space_ = false;
182 bool last_char_was_escape_ = false;
183 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184};
185
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800186inline const std::string& StringBuilder::ToString() const {
187 return str_;
188}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800190inline const std::string& StringBuilder::Error() const {
191 return error_;
192}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800194inline bool StringBuilder::IsEmpty() const {
195 return str_.empty();
196}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800197
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800198inline size_t StringBuilder::Utf16Len() const {
199 return utf16_len_;
200}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700201
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800202inline StringBuilder::operator bool() const {
203 return error_.empty();
204}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700206// Converts a UTF8 string into Modified UTF8
207std::string Utf8ToModifiedUtf8(const std::string& utf8);
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700208std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8);
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700209
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800210// Converts a UTF8 string to a UTF16 string.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800211std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
212std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800214// Writes the entire BigBuffer to the output stream.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800217// Copies the entire BigBuffer into a single buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800220// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
221// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 public:
224 class iterator {
225 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700226 using reference = android::StringPiece&;
227 using value_type = android::StringPiece;
228 using difference_type = size_t;
229 using pointer = android::StringPiece*;
230 using iterator_category = std::forward_iterator_tag;
231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 iterator(const iterator&) = default;
233 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700236
Adam Lesinskid5083f62017-01-16 15:07:21 -0800237 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 bool operator==(const iterator& rhs) const;
239 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 private:
242 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700244 iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245
Adam Lesinskid5083f62017-01-16 15:07:21 -0800246 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800248 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700252 Tokenizer(const android::StringPiece& str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700253
Adam Lesinski448a15c2017-07-25 10:59:26 -0700254 iterator begin() const {
255 return begin_;
256 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700257
Adam Lesinski448a15c2017-07-25 10:59:26 -0700258 iterator end() const {
259 return end_;
260 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 const iterator begin_;
264 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265};
266
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800267inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
268 return Tokenizer(str, sep);
269}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800271inline uint16_t HostToDevice16(uint16_t value) {
272 return htods(value);
273}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800275inline uint32_t HostToDevice32(uint32_t value) {
276 return htodl(value);
277}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800279inline uint16_t DeviceToHost16(uint16_t value) {
280 return dtohs(value);
281}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700282
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800283inline uint32_t DeviceToHost32(uint32_t value) {
284 return dtohl(value);
285}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800287// Given a path like: res/xml-sw600dp/foo.xml
288//
289// Extracts "res/xml-sw600dp/" into outPrefix.
290// Extracts "foo" into outEntry.
291// Extracts ".xml" into outSuffix.
292//
293// Returns true if successful.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800294bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
295 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800299// Stream operator for functions. Calls the function with the stream as an argument.
300// In the aapt namespace for lookup.
301inline ::std::ostream& operator<<(::std::ostream& out,
302 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304}
305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308#endif // AAPT_UTIL_H