blob: 8d3b413154855f6d40068ca350f6c586ef434509 [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000026#include "androidfw/BigBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080028#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "utils/ByteOrder.h"
30
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#ifdef _WIN32
32// TODO(adamlesinski): remove once http://b/32447322 is resolved.
33// utils/ByteOrder.h includes winsock2.h on WIN32,
34// which will pull in the ERROR definition. This conflicts
35// with android-base/logging.h, which takes care of undefining
36// ERROR, but it gets included too early (before winsock2.h).
37#ifdef ERROR
38#undef ERROR
39#endif
40#endif
41
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042namespace aapt {
43namespace util {
44
Adam Lesinskic744ae82017-05-17 19:28:38 -070045template <typename T>
46struct Range {
47 T start;
48 T end;
49};
50
Adam Lesinskid5083f62017-01-16 15:07:21 -080051std::vector<std::string> Split(const android::StringPiece& str, char sep);
52std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinski96ea08f2017-11-06 10:44:46 -080054// Returns true if the string starts with prefix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080055bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070056
Adam Lesinski96ea08f2017-11-06 10:44:46 -080057// Returns true if the string ends with suffix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080058bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinski2eed52e2018-02-21 15:55:58 -080060// Creates a new StringPiece that points to a substring of the original string without leading
61// whitespace.
62android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str);
63
64// Creates a new StringPiece that points to a substring of the original string without trailing
65// whitespace.
66android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str);
67
68// Creates a new StringPiece that points to a substring of the original string without leading or
Adam Lesinski96ea08f2017-11-06 10:44:46 -080069// trailing whitespace.
Adam Lesinskid5083f62017-01-16 15:07:21 -080070android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070071
Adam Lesinski96ea08f2017-11-06 10:44:46 -080072// Tests that the string is a valid Java class name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080073bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070074
Adam Lesinski96ea08f2017-11-06 10:44:46 -080075// Tests that the string is a valid Java package name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080076bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinski96ea08f2017-11-06 10:44:46 -080078// Tests that the string is a valid Android package name. More strict than a Java package name.
79// - First character of each component (separated by '.') must be an ASCII letter.
80// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
81// - Package must contain at least two components, unless it is 'android'.
Rhed Jao2c434422020-11-12 10:48:03 +080082// - The maximum package name length is 223.
Adam Lesinski96ea08f2017-11-06 10:44:46 -080083bool IsAndroidPackageName(const android::StringPiece& str);
84
85// Tests that the string is a valid Android split name.
86// - First character of each component (separated by '.') must be an ASCII letter.
87// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
88bool IsAndroidSplitName(const android::StringPiece& str);
89
Rhed Jao2c434422020-11-12 10:48:03 +080090// Tests that the string is a valid Android shared user id.
91// - First character of each component (separated by '.') must be an ASCII letter.
92// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
93// - Must contain at least two components, unless package name is 'android'.
94// - The maximum shared user id length is 223.
95// - Treat empty string as valid, it's the case of no shared user id.
96bool IsAndroidSharedUserId(const android::StringPiece& package_name,
97 const android::StringPiece& shared_user_id);
98
Adam Lesinski96ea08f2017-11-06 10:44:46 -080099// Converts the class name to a fully qualified class name from the given
100// `package`. Ex:
101//
102// asdf --> package.asdf
103// .asdf --> package.asdf
104// .a.b --> package.a.b
105// asdf.adsf --> asdf.adsf
Ryan Mitchell4382e442021-07-14 12:53:01 -0700106std::optional<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
107 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700108
Ryan Mitchell34039b22019-03-18 08:57:47 -0700109// Retrieves the formatted name of aapt2.
110const char* GetToolName();
111
112// Retrieves the build fingerprint of aapt2.
113std::string GetToolFingerprint();
114
Adam Lesinski060b53d2017-07-28 17:10:35 -0700115template <typename T>
116typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
117 if (a < b) {
118 return -1;
119 } else if (a > b) {
120 return 1;
121 }
122 return 0;
123}
124
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800125// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
126// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127template <typename T, class... Args>
128std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130}
131
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800132// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700133template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800134::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
135 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 using std::begin;
137 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 const auto begin_iter = begin(container);
139 const auto end_iter = end(container);
140 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
141 for (auto iter = begin_iter; iter != end_iter; ++iter) {
142 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 out << sep;
144 }
145 out << *iter;
146 }
147 return out;
148 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149}
150
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800151// Checks that the Java string format contains no non-positional arguments (arguments without
152// explicitly specifying an index) when there are more than one argument. This is an error
153// because translations may rearrange the order of the arguments in the string, which will
154// break the string interpolation.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800155bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800156
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800157bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces,
158 std::string* out_str, std::string* out_error);
159
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 public:
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800162 StringBuilder() = default;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700163
Adam Lesinskid5083f62017-01-16 15:07:21 -0800164 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 const std::string& ToString() const;
166 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800167 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 // When building StyledStrings, we need UTF-16 indices into the string,
170 // which is what the Java layer expects when dealing with java
171 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::string str_;
178 size_t utf16_len_ = 0;
179 bool quote_ = false;
180 bool trailing_space_ = false;
181 bool last_char_was_escape_ = false;
182 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183};
184
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800185inline const std::string& StringBuilder::ToString() const {
186 return str_;
187}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800189inline const std::string& StringBuilder::Error() const {
190 return error_;
191}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800193inline bool StringBuilder::IsEmpty() const {
194 return str_.empty();
195}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800196
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800197inline size_t StringBuilder::Utf16Len() const {
198 return utf16_len_;
199}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700200
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800201inline StringBuilder::operator bool() const {
202 return error_.empty();
203}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800205// Writes the entire BigBuffer to the output stream.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000206bool WriteAll(std::ostream& out, const android::BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800208// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
209// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 public:
212 class iterator {
213 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700214 using reference = android::StringPiece&;
215 using value_type = android::StringPiece;
216 using difference_type = size_t;
217 using pointer = android::StringPiece*;
218 using iterator_category = std::forward_iterator_tag;
219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 iterator(const iterator&) = default;
221 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700224
Adam Lesinskid5083f62017-01-16 15:07:21 -0800225 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 bool operator==(const iterator& rhs) const;
227 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 private:
230 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700232 iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Adam Lesinskid5083f62017-01-16 15:07:21 -0800234 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800236 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700240 Tokenizer(const android::StringPiece& str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700241
Adam Lesinski448a15c2017-07-25 10:59:26 -0700242 iterator begin() const {
243 return begin_;
244 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700245
Adam Lesinski448a15c2017-07-25 10:59:26 -0700246 iterator end() const {
247 return end_;
248 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 const iterator begin_;
252 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253};
254
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800255inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
256 return Tokenizer(str, sep);
257}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800259// Given a path like: res/xml-sw600dp/foo.xml
260//
261// Extracts "res/xml-sw600dp/" into outPrefix.
262// Extracts "foo" into outEntry.
263// Extracts ".xml" into outSuffix.
264//
265// Returns true if successful.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800266bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
267 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000271} // namespace aapt
272
273namespace std {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800274// Stream operator for functions. Calls the function with the stream as an argument.
275// In the aapt namespace for lookup.
276inline ::std::ostream& operator<<(::std::ostream& out,
277 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279}
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000280} // namespace std
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282#endif // AAPT_UTIL_H