blob: 41c7435b534d509e03d88a6a132ad28c48b6ac81 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 "ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Nicholas Lativy79f03962019-01-16 16:19:09 +000019#include <algorithm>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <sstream>
21
Adam Lesinski2eed52e2018-02-21 15:55:58 -080022#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "androidfw/ResourceTypes.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080024#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinskicacb28f2016-10-19 12:18:14 -070026#include "NameMangler.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070027#include "SdkConstants.h"
Adam Lesinski46708052017-09-29 14:49:15 -070028#include "format/binary/ResourceTypeExtensions.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Unicode.h"
30#include "text/Utf8Iterator.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080031#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032#include "util/Util.h"
33
Adam Lesinski2eed52e2018-02-21 15:55:58 -080034using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020035using ::android::ConfigDescription;
Adam Lesinski46708052017-09-29 14:49:15 -070036using ::android::StringPiece;
37using ::android::StringPiece16;
Adam Lesinski2eed52e2018-02-21 15:55:58 -080038using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040namespace aapt {
41namespace ResourceUtils {
42
Iurii Makhnoaeac0d02022-02-22 06:41:58 +000043static std::optional<ResourceNamedType> ToResourceNamedType(const char16_t* type16,
44 const char* type, size_t type_len) {
45 std::optional<ResourceNamedTypeRef> parsed_type;
Stephen Hineseb39a512022-10-04 02:36:07 -070046 std::string converted;
Iurii Makhnoaeac0d02022-02-22 06:41:58 +000047 if (type16) {
Stephen Hineseb39a512022-10-04 02:36:07 -070048 converted = android::util::Utf16ToUtf8(StringPiece16(type16, type_len));
Iurii Makhnoaeac0d02022-02-22 06:41:58 +000049 parsed_type = ParseResourceNamedType(converted);
50 } else if (type) {
51 parsed_type = ParseResourceNamedType(StringPiece(type, type_len));
52 } else {
53 return {};
54 }
55 if (!parsed_type) {
56 return {};
57 }
58 return parsed_type->ToResourceNamedType();
59}
60
Ryan Mitchell4382e442021-07-14 12:53:01 -070061std::optional<ResourceName> ToResourceName(const android::ResTable::resource_name& name_in) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080062 // TODO: Remove this when ResTable and AssetManager(1) are removed from AAPT2
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 ResourceName name_out;
64 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return {};
66 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070067
Jeremy Meyer56f36e82022-05-20 20:35:42 +000068 name_out.package = android::util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069
Iurii Makhnoaeac0d02022-02-22 06:41:58 +000070 std::optional<ResourceNamedType> type =
71 ToResourceNamedType(name_in.type, name_in.name8, name_in.typeLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 if (!type) {
73 return {};
74 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +000075 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070076
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 if (name_in.name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000078 name_out.entry = android::util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080080 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 } else {
82 return {};
83 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070085}
86
Ryan Mitchell4382e442021-07-14 12:53:01 -070087std::optional<ResourceName> ToResourceName(const android::AssetManager2::ResourceName& name_in) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080088 ResourceName name_out;
89 if (!name_in.package) {
90 return {};
91 }
92
93 name_out.package = std::string(name_in.package, name_in.package_len);
94
Iurii Makhnoaeac0d02022-02-22 06:41:58 +000095 std::optional<ResourceNamedType> type =
96 ToResourceNamedType(name_in.type16, name_in.type, name_in.type_len);
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080097 if (!type) {
98 return {};
99 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +0000100 name_out.type = *type;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800101
102 if (name_in.entry16) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000103 name_out.entry = android::util::Utf16ToUtf8(StringPiece16(name_in.entry16, name_in.entry_len));
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800104 } else if (name_in.entry) {
105 name_out.entry = std::string(name_in.entry, name_in.entry_len);
106 } else {
107 return {};
108 }
109 return name_out;
110}
111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
113 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 if (str.empty()) {
115 return false;
116 }
117
118 size_t offset = 0;
119 bool priv = false;
120 if (str.data()[0] == '*') {
121 priv = true;
122 offset = 1;
123 }
124
125 StringPiece package;
126 StringPiece type;
127 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800128 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
129 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 return false;
131 }
132
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000133 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 return false;
136 }
137
138 if (entry.empty()) {
139 return false;
140 }
141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 if (out_ref) {
143 out_ref->package = package;
144 out_ref->type = *parsed_type;
145 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 }
147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 if (out_private) {
149 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151 return true;
152}
153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
155 bool* out_create, bool* out_private) {
156 StringPiece trimmed_str(util::TrimWhitespace(str));
157 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 return false;
159 }
160
161 bool create = false;
162 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 create = true;
167 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800168 }
169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (!ParseResourceName(
172 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 &priv)) {
174 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800175 }
176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 if (create && priv) {
178 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800179 }
180
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000181 if (create && name.type.type != ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800183 }
184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 if (out_ref) {
186 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 }
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 if (out_create) {
190 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800191 }
192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 if (out_private) {
194 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800195 }
196 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 }
198 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199}
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201bool IsReference(const StringPiece& str) {
202 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800203}
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
206 StringPiece trimmed_str(util::TrimWhitespace(str));
207 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 }
210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 StringPiece package;
213 StringPiece type;
214 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800215 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
216 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 return false;
218 }
219
220 if (!type.empty() && type != "attr") {
221 return false;
222 }
223
224 if (entry.empty()) {
225 return false;
226 }
227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 if (out_ref) {
229 out_ref->package = package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000230 out_ref->type = ResourceNamedTypeWithDefaultName(ResourceType::kAttr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
233 return true;
234 }
235 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236}
237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238bool IsAttributeReference(const StringPiece& str) {
239 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800240}
241
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700242/*
243 * Style parent's are a bit different. We accept the following formats:
244 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800245 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800246 * ?[[*]package:]style/<entry>
247 * <[*]package>:[style/]<entry>
248 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700250std::optional<Reference> ParseStyleParentReference(const StringPiece& str, std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 if (str.empty()) {
252 return {};
253 }
254
255 StringPiece name = str;
256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 bool has_leading_identifiers = false;
258 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259
260 // Skip over these identifiers. A style's parent is a normal reference.
261 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 name = name.substr(1, name.size() - 1);
264 }
265
266 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 name = name.substr(1, name.size() - 1);
269 }
270
271 ResourceNameRef ref;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000272 ref.type = ResourceNamedTypeWithDefaultName(ResourceType::kStyle);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800275 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 const ResourceType* parsed_type = ParseResourceType(type_str);
279 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 err << "invalid resource type '" << type_str << "' for parent of style";
282 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 std::stringstream err;
289 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 return {};
292 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297}
298
Ryan Mitchell4382e442021-07-14 12:53:01 -0700299std::optional<Reference> ParseXmlAttributeName(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 StringPiece trimmed_str = util::TrimWhitespace(str);
301 const char* start = trimmed_str.data();
302 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700304
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 Reference ref;
306 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 start++;
309 p++;
310 }
311
312 StringPiece package;
313 StringPiece name;
314 while (p != end) {
315 if (*p == ':') {
316 package = StringPiece(start, p - start);
317 name = StringPiece(p + 1, end - (p + 1));
318 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700319 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 p++;
321 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700322
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000323 ref.name = ResourceName(package, ResourceNamedTypeWithDefaultName(ResourceType::kAttr),
324 name.empty() ? trimmed_str : name);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700325 return std::optional<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700326}
327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
329 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 bool private_ref = false;
332 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 return value;
336 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 if (ParseAttributeReference(str, &ref)) {
339 if (out_create) {
340 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700341 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
343 }
344 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700345}
346
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700347std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
348 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700350 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700352 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700354 return {};
355}
356
357std::unique_ptr<Reference> MakeNull() {
358 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
359 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
360 return util::make_unique<Reference>();
361}
362
363std::unique_ptr<BinaryPrimitive> MakeEmpty() {
364 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
365 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700366}
367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700369 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 StringPiece trimmed_str(util::TrimWhitespace(str));
371 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 // Enum symbols are stored as @package:id/symbol resources,
373 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
375 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 android::Res_value value = {};
Ryan Mitchellc1676802019-05-20 16:47:08 -0700377 value.dataType = symbol.type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 value.data = symbol.value;
379 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700380 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 }
382 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700383}
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700386 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 android::Res_value flags = {};
388 flags.dataType = android::Res_value::TYPE_INT_HEX;
389 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800390
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700393 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 }
395
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800396 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 bool flag_set = false;
400 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 // Flag symbols are stored as @package:id/symbol resources,
402 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 const ResourceName& flag_symbol_resource_name =
404 symbol.symbol.name.value();
405 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 break;
409 }
410 }
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 return {};
414 }
415 }
416 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417}
418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 if (c >= '0' && c <= '9') {
421 return c - '0';
422 } else if (c >= 'a' && c <= 'f') {
423 return c - 'a' + 0xa;
424 } else if (c >= 'A' && c <= 'F') {
425 return c - 'A' + 0xa;
426 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 return 0xffffffffu;
429 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700430}
431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
433 StringPiece color_str(util::TrimWhitespace(str));
434 const char* start = color_str.data();
435 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 if (len == 0 || start[0] != '#') {
437 return {};
438 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700439
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 android::Res_value value = {};
441 bool error = false;
442 if (len == 4) {
443 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
444 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 value.data |= ParseHex(start[1], &error) << 20;
446 value.data |= ParseHex(start[1], &error) << 16;
447 value.data |= ParseHex(start[2], &error) << 12;
448 value.data |= ParseHex(start[2], &error) << 8;
449 value.data |= ParseHex(start[3], &error) << 4;
450 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 } else if (len == 5) {
452 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 value.data |= ParseHex(start[1], &error) << 28;
454 value.data |= ParseHex(start[1], &error) << 24;
455 value.data |= ParseHex(start[2], &error) << 20;
456 value.data |= ParseHex(start[2], &error) << 16;
457 value.data |= ParseHex(start[3], &error) << 12;
458 value.data |= ParseHex(start[3], &error) << 8;
459 value.data |= ParseHex(start[4], &error) << 4;
460 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 } else if (len == 7) {
462 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
463 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 value.data |= ParseHex(start[1], &error) << 20;
465 value.data |= ParseHex(start[2], &error) << 16;
466 value.data |= ParseHex(start[3], &error) << 12;
467 value.data |= ParseHex(start[4], &error) << 8;
468 value.data |= ParseHex(start[5], &error) << 4;
469 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 } else if (len == 9) {
471 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 value.data |= ParseHex(start[1], &error) << 28;
473 value.data |= ParseHex(start[2], &error) << 24;
474 value.data |= ParseHex(start[3], &error) << 20;
475 value.data |= ParseHex(start[4], &error) << 16;
476 value.data |= ParseHex(start[5], &error) << 12;
477 value.data |= ParseHex(start[6], &error) << 8;
478 value.data |= ParseHex(start[7], &error) << 4;
479 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 } else {
481 return {};
482 }
483 return error ? std::unique_ptr<BinaryPrimitive>()
484 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700485}
486
Ryan Mitchell4382e442021-07-14 12:53:01 -0700487std::optional<bool> ParseBool(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 StringPiece trimmed_str(util::TrimWhitespace(str));
489 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700490 return std::optional<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
492 trimmed_str == "False") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700493 return std::optional<bool>(false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 }
495 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800496}
497
Ryan Mitchell4382e442021-07-14 12:53:01 -0700498std::optional<uint32_t> ParseInt(const StringPiece& str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000499 std::u16string str16 = android::util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 android::Res_value value;
501 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
502 return value.data;
503 }
504 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700505}
506
Ryan Mitchell4382e442021-07-14 12:53:01 -0700507std::optional<ResourceId> ParseResourceId(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700509
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000510 std::u16string str16 = android::util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 android::Res_value value;
512 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
513 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
514 ResourceId id(value.data);
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800515 if (id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 return id;
517 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700518 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 }
520 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700521}
522
Ryan Mitchell4382e442021-07-14 12:53:01 -0700523std::optional<int> ParseSdkVersion(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700525
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000526 std::u16string str16 = android::util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 android::Res_value value;
528 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
529 return static_cast<int>(value.data);
530 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700531
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 // Try parsing the code name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700533 std::optional<int> entry = GetDevelopmentSdkCodeNameVersion(trimmed_str);
Ryan Mitchell48002f62019-04-11 14:29:25 -0700534 if (entry) {
535 return entry.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 }
Nicholas Lativy79f03962019-01-16 16:19:09 +0000537
538 // Try parsing codename from "[codename].[preview_sdk_fingerprint]" value.
539 const StringPiece::const_iterator begin = std::begin(trimmed_str);
540 const StringPiece::const_iterator end = std::end(trimmed_str);
541 const StringPiece::const_iterator codename_end = std::find(begin, end, '.');
Ryan Mitchell48002f62019-04-11 14:29:25 -0700542 entry = GetDevelopmentSdkCodeNameVersion(trimmed_str.substr(begin, codename_end));
543 if (entry) {
544 return entry.value();
Nicholas Lativy79f03962019-01-16 16:19:09 +0000545 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700547}
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700550 if (std::optional<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700551 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
552 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700555}
556
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700557std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
558 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
559 val ? 0xffffffffu : 0u);
560}
561
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000563 std::u16string str16 = android::util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 android::Res_value value;
565 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
566 return {};
567 }
568 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700569}
570
Shane Farmerd05b9132018-02-14 15:40:35 -0800571std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
572 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
573}
574
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000576 std::u16string str16 = android::util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 android::Res_value value;
578 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
579 return {};
580 }
581 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582}
583
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700586 case android::Res_value::TYPE_NULL:
587 case android::Res_value::TYPE_REFERENCE:
588 case android::Res_value::TYPE_ATTRIBUTE:
589 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800590 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700592
593 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700595
596 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700598
599 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601
602 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700604
605 case android::Res_value::TYPE_INT_DEC:
606 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return android::ResTable_map::TYPE_INTEGER |
608 android::ResTable_map::TYPE_ENUM |
609 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610
611 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613
614 case android::Res_value::TYPE_INT_COLOR_ARGB8:
615 case android::Res_value::TYPE_INT_COLOR_RGB8:
616 case android::Res_value::TYPE_INT_COLOR_ARGB4:
617 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700619
620 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 return 0;
622 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623}
624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625std::unique_ptr<Item> TryParseItemForAttribute(
626 const StringPiece& value, uint32_t type_mask,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700627 const std::function<bool(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700628 using android::ResTable_map;
629
630 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700632 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700634
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700636 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 if (reference) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700638 reference->type_flags = type_mask;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 if (create && on_create_reference) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700640 if (!on_create_reference(reference->name.value())) {
641 return {};
642 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700643 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 return std::move(reference);
645 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700646
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700647 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700649 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 if (color) {
651 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700652 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700654
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700655 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700657 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 if (boolean) {
659 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700660 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700662
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700663 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700665 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 if (integer) {
667 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700668 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700670
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700671 const uint32_t float_mask =
672 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700675 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700677 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700680 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 }
682 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700683}
684
685/**
686 * We successively try to parse the string as a resource type that the Attribute
687 * allows.
688 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 const StringPiece& str, const Attribute* attr,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700691 const std::function<bool(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700692 using android::ResTable_map;
693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700695 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 if (value) {
697 return value;
698 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700699
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700700 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700702 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 if (enum_value) {
704 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700705 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700708 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700710 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 if (flag_value) {
712 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700713 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
715 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700716}
717
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700718std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 out << "res/" << res_file.name.type;
721 if (res_file.config != ConfigDescription{}) {
722 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 }
724 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
727 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800733}
734
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700735std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
736 const android::ResStringPool& src_pool,
737 const android::Res_value& res_value,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000738 android::StringPool* dst_pool) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700739 if (type == ResourceType::kId) {
Donald Chai4abc8282019-12-13 23:23:07 -0800740 if (res_value.dataType != android::Res_value::TYPE_REFERENCE &&
741 res_value.dataType != android::Res_value::TYPE_DYNAMIC_REFERENCE) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700742 // plain "id" resources are actually encoded as unused values (aapt1 uses an empty string,
Donald Chai4abc8282019-12-13 23:23:07 -0800743 // while aapt2 uses a false boolean).
744 return util::make_unique<Id>();
745 }
746 // fall through to regular reference deserialization logic
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700747 }
748
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000749 const uint32_t data = android::util::DeviceToHost32(res_value.data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700750 switch (res_value.dataType) {
751 case android::Res_value::TYPE_STRING: {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000752 const std::string str = android::util::GetString(src_pool, data);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000753 auto spans_result = src_pool.styleAt(data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700754
755 // Check if the string has a valid style associated with it.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000756 if (spans_result.has_value() &&
757 (*spans_result)->name.index != android::ResStringPool_span::END) {
758 const android::ResStringPool_span* spans = spans_result->unsafe_ptr();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000759 android::StyleString style_str = {str};
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700760 while (spans->name.index != android::ResStringPool_span::END) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000761 style_str.spans.push_back(
762 android::Span{android::util::GetString(src_pool, spans->name.index), spans->firstChar,
763 spans->lastChar});
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700764 spans++;
765 }
766 return util::make_unique<StyledString>(dst_pool->MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000767 style_str,
768 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700769 } else {
770 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
771 // This must be a FileReference.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000772 std::unique_ptr<FileReference> file_ref = util::make_unique<FileReference>(
773 dst_pool->MakeRef(str, android::StringPool::Context(
774 android::StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000775 if (type == ResourceType::kRaw) {
776 file_ref->type = ResourceFile::Type::kUnknown;
777 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700778 file_ref->type = ResourceFile::Type::kBinaryXml;
779 } else if (util::EndsWith(*file_ref->path, ".png")) {
780 file_ref->type = ResourceFile::Type::kPng;
781 }
782 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700783 }
784
785 // There are no styles associated with this string, so treat it as a simple string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000786 return util::make_unique<String>(
787 dst_pool->MakeRef(str, android::StringPool::Context(config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700788 }
789 } break;
790
791 case android::Res_value::TYPE_REFERENCE:
792 case android::Res_value::TYPE_ATTRIBUTE:
793 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
794 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
795 Reference::Type ref_type = Reference::Type::kResource;
796 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
797 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
798 ref_type = Reference::Type::kAttribute;
799 }
800
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700801 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700802 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700803 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700804 }
805
806 // This is a normal reference.
Clark DuValle9fedbe2020-01-09 11:52:52 -0800807 auto reference = util::make_unique<Reference>(data, ref_type);
808 if (res_value.dataType == android::Res_value::TYPE_DYNAMIC_REFERENCE ||
809 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
810 reference->is_dynamic = true;
811 }
812 return reference;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700813 } break;
814 }
815
816 // Treat this as a raw binary primitive.
817 return util::make_unique<BinaryPrimitive>(res_value);
818}
819
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800820// Converts the codepoint to UTF-8 and appends it to the string.
821static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
822 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
823 if (len < 0) {
824 return false;
825 }
826
827 const size_t start_append_pos = output->size();
828
829 // Make room for the next character.
830 output->resize(output->size() + len);
831
832 char* dst = &*(output->begin() + start_append_pos);
833 utf32_to_utf8(&codepoint, 1, dst, len + 1);
834 return true;
835}
836
837// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
838// Unicode codepoint represented by the escape sequence to the string.
839static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
840 char32_t code = 0;
841 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
842 char32_t codepoint = iter->Next();
843 char32_t a;
844 if (codepoint >= U'0' && codepoint <= U'9') {
845 a = codepoint - U'0';
846 } else if (codepoint >= U'a' && codepoint <= U'f') {
847 a = codepoint - U'a' + 10;
848 } else if (codepoint >= U'A' && codepoint <= U'F') {
849 a = codepoint - U'A' + 10;
850 } else {
851 return {};
852 }
853 code = (code << 4) | a;
854 }
855 return AppendCodepointToUtf8String(code, output);
856}
857
858StringBuilder::StringBuilder(bool preserve_spaces)
859 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
860}
861
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800862StringBuilder& StringBuilder::AppendText(const std::string& text) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800863 if (!error_.empty()) {
864 return *this;
865 }
866
867 const size_t previous_len = xml_string_.text.size();
868 Utf8Iterator iter(text);
869 while (iter.HasNext()) {
870 char32_t codepoint = iter.Next();
Ryan Mitchell0f326752019-03-11 11:00:25 -0700871 if (!preserve_spaces_ && !quote_ && (codepoint <= std::numeric_limits<char>::max())
872 && isspace(static_cast<char>(codepoint))) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800873 if (!last_codepoint_was_space_) {
874 // Emit a space if it's the first.
875 xml_string_.text += ' ';
876 last_codepoint_was_space_ = true;
877 }
878
879 // Keep eating spaces.
880 continue;
881 }
882
883 // This is not a space.
884 last_codepoint_was_space_ = false;
885
886 if (codepoint == U'\\') {
887 if (iter.HasNext()) {
888 codepoint = iter.Next();
889 switch (codepoint) {
890 case U't':
891 xml_string_.text += '\t';
892 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800893 case U'n':
894 xml_string_.text += '\n';
895 break;
896
897 case U'#':
898 case U'@':
899 case U'?':
900 case U'"':
901 case U'\'':
902 case U'\\':
903 xml_string_.text += static_cast<char>(codepoint);
904 break;
905
906 case U'u':
907 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
908 error_ =
909 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
910 return *this;
911 }
912 break;
913
914 default:
915 // Ignore the escape character and just include the codepoint.
916 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
917 break;
918 }
919 }
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800920 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800921 // Only toggle the quote state when we are not preserving spaces.
922 quote_ = !quote_;
923
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800924 } else if (!preserve_spaces_ && !quote_ && codepoint == U'\'') {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700925 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800926 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
927 return *this;
928
929 } else {
930 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
931 }
932 }
933
934 // Accumulate the added string's UTF-16 length.
935 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
936 const size_t utf8_length = xml_string_.text.size();
937 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
938 if (len < 0) {
939 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
940 return *this;
941 }
942
943 utf16_len_ += static_cast<uint32_t>(len);
944 return *this;
945}
946
947StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
948 if (!error_.empty()) {
949 return 0u;
950 }
951
952 // When we start a span, all state associated with whitespace truncation and quotation is ended.
953 ResetTextState();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000954 android::Span span;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800955 span.name = name;
956 span.first_char = span.last_char = utf16_len_;
957 xml_string_.spans.push_back(std::move(span));
958 return xml_string_.spans.size() - 1;
959}
960
961void StringBuilder::EndSpan(SpanHandle handle) {
962 if (!error_.empty()) {
963 return;
964 }
965
966 // When we end a span, all state associated with whitespace truncation and quotation is ended.
967 ResetTextState();
968 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
969}
970
971StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
972 if (!error_.empty()) {
973 return 0u;
974 }
975
976 UntranslatableSection section;
977 section.start = section.end = xml_string_.text.size();
978 xml_string_.untranslatable_sections.push_back(section);
979 return xml_string_.untranslatable_sections.size() - 1;
980}
981
982void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
983 if (!error_.empty()) {
984 return;
985 }
986 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
987}
988
989FlattenedXmlString StringBuilder::GetFlattenedString() const {
990 return xml_string_;
991}
992
993std::string StringBuilder::to_string() const {
994 return xml_string_.text;
995}
996
997StringBuilder::operator bool() const {
998 return error_.empty();
999}
1000
1001std::string StringBuilder::GetError() const {
1002 return error_;
1003}
1004
1005void StringBuilder::ResetTextState() {
1006 quote_ = preserve_spaces_;
1007 last_codepoint_was_space_ = false;
1008}
1009
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010} // namespace ResourceUtils
1011} // namespace aapt