blob: c3efe6a63feb8361cf3a48e72b44f2baa207d413 [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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031
32#ifdef _WIN32
33// TODO(adamlesinski): remove once http://b/32447322 is resolved.
34// utils/ByteOrder.h includes winsock2.h on WIN32,
35// which will pull in the ERROR definition. This conflicts
36// with android-base/logging.h, which takes care of undefining
37// ERROR, but it gets included too early (before winsock2.h).
38#ifdef ERROR
39#undef ERROR
40#endif
41#endif
42
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043namespace aapt {
44namespace util {
45
Adam Lesinskic744ae82017-05-17 19:28:38 -070046template <typename T>
47struct Range {
48 T start;
49 T end;
50};
51
Adam Lesinskid5083f62017-01-16 15:07:21 -080052std::vector<std::string> Split(const android::StringPiece& str, char sep);
53std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054
Adam Lesinski96ea08f2017-11-06 10:44:46 -080055// Returns true if the string starts with prefix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080056bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070057
Adam Lesinski96ea08f2017-11-06 10:44:46 -080058// Returns true if the string ends with suffix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080059bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060
Adam Lesinski2eed52e2018-02-21 15:55:58 -080061// Creates a new StringPiece that points to a substring of the original string without leading
62// whitespace.
63android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str);
64
65// Creates a new StringPiece that points to a substring of the original string without trailing
66// whitespace.
67android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str);
68
69// Creates a new StringPiece that points to a substring of the original string without leading or
Adam Lesinski96ea08f2017-11-06 10:44:46 -080070// trailing whitespace.
Adam Lesinskid5083f62017-01-16 15:07:21 -080071android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070072
Adam Lesinski96ea08f2017-11-06 10:44:46 -080073// Tests that the string is a valid Java class name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080074bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070075
Adam Lesinski96ea08f2017-11-06 10:44:46 -080076// Tests that the string is a valid Java package name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080077bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078
Adam Lesinski96ea08f2017-11-06 10:44:46 -080079// Tests that the string is a valid Android package name. More strict than a Java package name.
80// - First character of each component (separated by '.') must be an ASCII letter.
81// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
82// - Package must contain at least two components, unless it is 'android'.
Rhed Jao2c434422020-11-12 10:48:03 +080083// - The maximum package name length is 223.
Adam Lesinski96ea08f2017-11-06 10:44:46 -080084bool 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
Rhed Jao2c434422020-11-12 10:48:03 +080091// Tests that the string is a valid Android shared user id.
92// - First character of each component (separated by '.') must be an ASCII letter.
93// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
94// - Must contain at least two components, unless package name is 'android'.
95// - The maximum shared user id length is 223.
96// - Treat empty string as valid, it's the case of no shared user id.
97bool IsAndroidSharedUserId(const android::StringPiece& package_name,
98 const android::StringPiece& shared_user_id);
99
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800100// Converts the class name to a fully qualified class name from the given
101// `package`. Ex:
102//
103// asdf --> package.asdf
104// .asdf --> package.asdf
105// .a.b --> package.a.b
106// asdf.adsf --> asdf.adsf
Ryan Mitchell4382e442021-07-14 12:53:01 -0700107std::optional<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
108 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700109
Ryan Mitchell34039b22019-03-18 08:57:47 -0700110// Retrieves the formatted name of aapt2.
111const char* GetToolName();
112
113// Retrieves the build fingerprint of aapt2.
114std::string GetToolFingerprint();
115
Adam Lesinski060b53d2017-07-28 17:10:35 -0700116template <typename T>
117typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
118 if (a < b) {
119 return -1;
120 } else if (a > b) {
121 return 1;
122 }
123 return 0;
124}
125
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800126// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
127// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128template <typename T, class... Args>
129std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131}
132
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800133// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700134template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800135::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
136 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 using std::begin;
138 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 const auto begin_iter = begin(container);
140 const auto end_iter = end(container);
141 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
142 for (auto iter = begin_iter; iter != end_iter; ++iter) {
143 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 out << sep;
145 }
146 out << *iter;
147 }
148 return out;
149 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150}
151
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800152// Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
153// the conversion to UTF-16 happens within ResStringPool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800154android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800156// Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
157// the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
158// which maintains no state or cache. This means we must return an std::string copy.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800160
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800161// Checks that the Java string format contains no non-positional arguments (arguments without
162// explicitly specifying an index) when there are more than one argument. This is an error
163// because translations may rearrange the order of the arguments in the string, which will
164// break the string interpolation.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800165bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800166
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800167bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces,
168 std::string* out_str, std::string* out_error);
169
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 public:
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800172 StringBuilder() = default;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700173
Adam Lesinskid5083f62017-01-16 15:07:21 -0800174 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 const std::string& ToString() const;
176 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800177 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 // When building StyledStrings, we need UTF-16 indices into the string,
180 // which is what the Java layer expects when dealing with java
181 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 std::string str_;
188 size_t utf16_len_ = 0;
189 bool quote_ = false;
190 bool trailing_space_ = false;
191 bool last_char_was_escape_ = false;
192 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193};
194
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800195inline const std::string& StringBuilder::ToString() const {
196 return str_;
197}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800199inline const std::string& StringBuilder::Error() const {
200 return error_;
201}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800203inline bool StringBuilder::IsEmpty() const {
204 return str_.empty();
205}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800206
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800207inline size_t StringBuilder::Utf16Len() const {
208 return utf16_len_;
209}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700210
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800211inline StringBuilder::operator bool() const {
212 return error_.empty();
213}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700215// Converts a UTF8 string into Modified UTF8
216std::string Utf8ToModifiedUtf8(const std::string& utf8);
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700217std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8);
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700218
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800219// Converts a UTF8 string to a UTF16 string.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800220std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
221std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800223// Writes the entire BigBuffer to the output stream.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800226// Copies the entire BigBuffer into a single buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800229// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
230// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 public:
233 class iterator {
234 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700235 using reference = android::StringPiece&;
236 using value_type = android::StringPiece;
237 using difference_type = size_t;
238 using pointer = android::StringPiece*;
239 using iterator_category = std::forward_iterator_tag;
240
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 iterator(const iterator&) = default;
242 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700245
Adam Lesinskid5083f62017-01-16 15:07:21 -0800246 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 bool operator==(const iterator& rhs) const;
248 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 private:
251 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700253 iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254
Adam Lesinskid5083f62017-01-16 15:07:21 -0800255 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800257 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700261 Tokenizer(const android::StringPiece& str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700262
Adam Lesinski448a15c2017-07-25 10:59:26 -0700263 iterator begin() const {
264 return begin_;
265 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700266
Adam Lesinski448a15c2017-07-25 10:59:26 -0700267 iterator end() const {
268 return end_;
269 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 const iterator begin_;
273 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274};
275
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800276inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
277 return Tokenizer(str, sep);
278}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800280inline uint16_t HostToDevice16(uint16_t value) {
281 return htods(value);
282}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800284inline uint32_t HostToDevice32(uint32_t value) {
285 return htodl(value);
286}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700287
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800288inline uint16_t DeviceToHost16(uint16_t value) {
289 return dtohs(value);
290}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700291
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800292inline uint32_t DeviceToHost32(uint32_t value) {
293 return dtohl(value);
294}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800296// Given a path like: res/xml-sw600dp/foo.xml
297//
298// Extracts "res/xml-sw600dp/" into outPrefix.
299// Extracts "foo" into outEntry.
300// Extracts ".xml" into outSuffix.
301//
302// Returns true if successful.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800303bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
304 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800308// Stream operator for functions. Calls the function with the stream as an argument.
309// In the aapt namespace for lookup.
310inline ::std::ostream& operator<<(::std::ostream& out,
311 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313}
314
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317#endif // AAPT_UTIL_H