blob: c77aca31a810e8d74fe4fa1d13abbacbb6bb9ea5 [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'.
Rhed Jao2c434422020-11-12 10:48:03 +080084// - The maximum package name length is 223.
Adam Lesinski96ea08f2017-11-06 10:44:46 -080085bool IsAndroidPackageName(const android::StringPiece& str);
86
87// Tests that the string is a valid Android split name.
88// - First character of each component (separated by '.') must be an ASCII letter.
89// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
90bool IsAndroidSplitName(const android::StringPiece& str);
91
Rhed Jao2c434422020-11-12 10:48:03 +080092// Tests that the string is a valid Android shared user id.
93// - First character of each component (separated by '.') must be an ASCII letter.
94// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
95// - Must contain at least two components, unless package name is 'android'.
96// - The maximum shared user id length is 223.
97// - Treat empty string as valid, it's the case of no shared user id.
98bool IsAndroidSharedUserId(const android::StringPiece& package_name,
99 const android::StringPiece& shared_user_id);
100
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800101// Converts the class name to a fully qualified class name from the given
102// `package`. Ex:
103//
104// asdf --> package.asdf
105// .asdf --> package.asdf
106// .a.b --> package.a.b
107// asdf.adsf --> asdf.adsf
Adam Lesinskid5083f62017-01-16 15:07:21 -0800108Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
109 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700110
Ryan Mitchell34039b22019-03-18 08:57:47 -0700111// Retrieves the formatted name of aapt2.
112const char* GetToolName();
113
114// Retrieves the build fingerprint of aapt2.
115std::string GetToolFingerprint();
116
Adam Lesinski060b53d2017-07-28 17:10:35 -0700117template <typename T>
118typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
119 if (a < b) {
120 return -1;
121 } else if (a > b) {
122 return 1;
123 }
124 return 0;
125}
126
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800127// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
128// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129template <typename T, class... Args>
130std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132}
133
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800134// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700135template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800136::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
137 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 using std::begin;
139 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 const auto begin_iter = begin(container);
141 const auto end_iter = end(container);
142 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
143 for (auto iter = begin_iter; iter != end_iter; ++iter) {
144 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 out << sep;
146 }
147 out << *iter;
148 }
149 return out;
150 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151}
152
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800153// Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
154// the conversion to UTF-16 happens within ResStringPool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800155android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800157// Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
158// the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
159// which maintains no state or cache. This means we must return an std::string copy.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800161
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800162// Checks that the Java string format contains no non-positional arguments (arguments without
163// explicitly specifying an index) when there are more than one argument. This is an error
164// because translations may rearrange the order of the arguments in the string, which will
165// break the string interpolation.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800166bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800167
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800168bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces,
169 std::string* out_str, std::string* out_error);
170
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 public:
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800173 StringBuilder() = default;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700174
Adam Lesinskid5083f62017-01-16 15:07:21 -0800175 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 const std::string& ToString() const;
177 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800178 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 // When building StyledStrings, we need UTF-16 indices into the string,
181 // which is what the Java layer expects when dealing with java
182 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 std::string str_;
189 size_t utf16_len_ = 0;
190 bool quote_ = false;
191 bool trailing_space_ = false;
192 bool last_char_was_escape_ = false;
193 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194};
195
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800196inline const std::string& StringBuilder::ToString() const {
197 return str_;
198}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800200inline const std::string& StringBuilder::Error() const {
201 return error_;
202}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800204inline bool StringBuilder::IsEmpty() const {
205 return str_.empty();
206}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800207
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800208inline size_t StringBuilder::Utf16Len() const {
209 return utf16_len_;
210}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700211
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800212inline StringBuilder::operator bool() const {
213 return error_.empty();
214}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700216// Converts a UTF8 string into Modified UTF8
217std::string Utf8ToModifiedUtf8(const std::string& utf8);
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700218std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8);
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700219
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800220// Converts a UTF8 string to a UTF16 string.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800221std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
222std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800224// Writes the entire BigBuffer to the output stream.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800227// Copies the entire BigBuffer into a single buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800230// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
231// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 public:
234 class iterator {
235 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700236 using reference = android::StringPiece&;
237 using value_type = android::StringPiece;
238 using difference_type = size_t;
239 using pointer = android::StringPiece*;
240 using iterator_category = std::forward_iterator_tag;
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 iterator(const iterator&) = default;
243 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700246
Adam Lesinskid5083f62017-01-16 15:07:21 -0800247 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 bool operator==(const iterator& rhs) const;
249 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 private:
252 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700254 iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255
Adam Lesinskid5083f62017-01-16 15:07:21 -0800256 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800258 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700262 Tokenizer(const android::StringPiece& str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700263
Adam Lesinski448a15c2017-07-25 10:59:26 -0700264 iterator begin() const {
265 return begin_;
266 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700267
Adam Lesinski448a15c2017-07-25 10:59:26 -0700268 iterator end() const {
269 return end_;
270 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 const iterator begin_;
274 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275};
276
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800277inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
278 return Tokenizer(str, sep);
279}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800281inline uint16_t HostToDevice16(uint16_t value) {
282 return htods(value);
283}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800285inline uint32_t HostToDevice32(uint32_t value) {
286 return htodl(value);
287}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700288
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800289inline uint16_t DeviceToHost16(uint16_t value) {
290 return dtohs(value);
291}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800293inline uint32_t DeviceToHost32(uint32_t value) {
294 return dtohl(value);
295}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800297// Given a path like: res/xml-sw600dp/foo.xml
298//
299// Extracts "res/xml-sw600dp/" into outPrefix.
300// Extracts "foo" into outEntry.
301// Extracts ".xml" into outSuffix.
302//
303// Returns true if successful.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800304bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
305 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800309// Stream operator for functions. Calls the function with the stream as an argument.
310// In the aapt namespace for lookup.
311inline ::std::ostream& operator<<(::std::ostream& out,
312 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314}
315
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318#endif // AAPT_UTIL_H