blob: d358df98ada64bbd98d0df7830d033383743d252 [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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700112bool ParseResourceName(StringPiece str, ResourceNameRef* out_ref, bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 if (str.empty()) {
114 return false;
115 }
116
117 size_t offset = 0;
118 bool priv = false;
119 if (str.data()[0] == '*') {
120 priv = true;
121 offset = 1;
122 }
123
124 StringPiece package;
125 StringPiece type;
126 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800127 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
128 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return false;
130 }
131
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000132 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 return false;
135 }
136
137 if (entry.empty()) {
138 return false;
139 }
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 if (out_ref) {
142 out_ref->package = package;
143 out_ref->type = *parsed_type;
144 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 }
146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 if (out_private) {
148 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
150 return true;
151}
152
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700153bool ParseReference(StringPiece str, ResourceNameRef* out_ref, bool* out_create,
154 bool* out_private) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 StringPiece trimmed_str(util::TrimWhitespace(str));
156 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 return false;
158 }
159
160 bool create = false;
161 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 create = true;
166 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800167 }
168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 if (!ParseResourceName(
171 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 &priv)) {
173 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800174 }
175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 if (create && priv) {
177 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800178 }
179
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000180 if (create && name.type.type != ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800182 }
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 if (out_ref) {
185 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 if (out_create) {
189 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800190 }
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 if (out_private) {
193 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800194 }
195 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
197 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198}
199
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700200bool IsReference(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800202}
203
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700204bool ParseAttributeReference(StringPiece str, ResourceNameRef* out_ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 StringPiece trimmed_str(util::TrimWhitespace(str));
206 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 }
209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 StringPiece package;
212 StringPiece type;
213 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800214 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
215 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 return false;
217 }
218
219 if (!type.empty() && type != "attr") {
220 return false;
221 }
222
223 if (entry.empty()) {
224 return false;
225 }
226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 if (out_ref) {
228 out_ref->package = package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000229 out_ref->type = ResourceNamedTypeWithDefaultName(ResourceType::kAttr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 }
232 return true;
233 }
234 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235}
236
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700237bool IsAttributeReference(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800239}
240
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241/*
242 * Style parent's are a bit different. We accept the following formats:
243 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800244 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800245 * ?[[*]package:]style/<entry>
246 * <[*]package>:[style/]<entry>
247 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700249std::optional<Reference> ParseStyleParentReference(StringPiece str, std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 if (str.empty()) {
251 return {};
252 }
253
254 StringPiece name = str;
255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 bool has_leading_identifiers = false;
257 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258
259 // Skip over these identifiers. A style's parent is a normal reference.
260 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 name = name.substr(1, name.size() - 1);
263 }
264
265 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 name = name.substr(1, name.size() - 1);
268 }
269
270 ResourceNameRef ref;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000271 ref.type = ResourceNamedTypeWithDefaultName(ResourceType::kStyle);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800274 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 const ResourceType* parsed_type = ParseResourceType(type_str);
278 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 err << "invalid resource type '" << type_str << "' for parent of style";
281 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 std::stringstream err;
288 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 return {};
291 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296}
297
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700298std::optional<Reference> ParseXmlAttributeName(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 StringPiece trimmed_str = util::TrimWhitespace(str);
300 const char* start = trimmed_str.data();
301 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700303
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 Reference ref;
305 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 start++;
308 p++;
309 }
310
311 StringPiece package;
312 StringPiece name;
313 while (p != end) {
314 if (*p == ':') {
315 package = StringPiece(start, p - start);
316 name = StringPiece(p + 1, end - (p + 1));
317 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700318 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 p++;
320 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700321
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000322 ref.name = ResourceName(package, ResourceNamedTypeWithDefaultName(ResourceType::kAttr),
323 name.empty() ? trimmed_str : name);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700324 return std::optional<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700325}
326
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700327std::unique_ptr<Reference> TryParseReference(StringPiece str, bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 bool private_ref = false;
330 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 return value;
334 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 if (ParseAttributeReference(str, &ref)) {
337 if (out_create) {
338 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
341 }
342 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700343}
344
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700345std::unique_ptr<Item> TryParseNullOrEmpty(StringPiece str) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700346 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700348 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700350 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700352 return {};
353}
354
355std::unique_ptr<Reference> MakeNull() {
356 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
357 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
358 return util::make_unique<Reference>();
359}
360
361std::unique_ptr<BinaryPrimitive> MakeEmpty() {
362 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
363 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364}
365
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700366std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr, StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 StringPiece trimmed_str(util::TrimWhitespace(str));
368 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 // Enum symbols are stored as @package:id/symbol resources,
370 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
372 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 android::Res_value value = {};
Ryan Mitchellc1676802019-05-20 16:47:08 -0700374 value.dataType = symbol.type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 value.data = symbol.value;
376 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700377 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 }
379 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700380}
381
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700382std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr, StringPiece str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 android::Res_value flags = {};
384 flags.dataType = android::Res_value::TYPE_INT_HEX;
385 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 }
391
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700392 for (StringPiece part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 bool flag_set = false;
396 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 // Flag symbols are stored as @package:id/symbol resources,
398 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 const ResourceName& flag_symbol_resource_name =
400 symbol.symbol.name.value();
401 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 break;
405 }
406 }
407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 return {};
410 }
411 }
412 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700413}
414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 if (c >= '0' && c <= '9') {
417 return c - '0';
418 } else if (c >= 'a' && c <= 'f') {
419 return c - 'a' + 0xa;
420 } else if (c >= 'A' && c <= 'F') {
421 return c - 'A' + 0xa;
422 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return 0xffffffffu;
425 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700426}
427
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700428std::unique_ptr<BinaryPrimitive> TryParseColor(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 StringPiece color_str(util::TrimWhitespace(str));
430 const char* start = color_str.data();
431 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 if (len == 0 || start[0] != '#') {
433 return {};
434 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700435
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 android::Res_value value = {};
437 bool error = false;
438 if (len == 4) {
439 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
440 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 value.data |= ParseHex(start[1], &error) << 20;
442 value.data |= ParseHex(start[1], &error) << 16;
443 value.data |= ParseHex(start[2], &error) << 12;
444 value.data |= ParseHex(start[2], &error) << 8;
445 value.data |= ParseHex(start[3], &error) << 4;
446 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 } else if (len == 5) {
448 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 value.data |= ParseHex(start[1], &error) << 28;
450 value.data |= ParseHex(start[1], &error) << 24;
451 value.data |= ParseHex(start[2], &error) << 20;
452 value.data |= ParseHex(start[2], &error) << 16;
453 value.data |= ParseHex(start[3], &error) << 12;
454 value.data |= ParseHex(start[3], &error) << 8;
455 value.data |= ParseHex(start[4], &error) << 4;
456 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 } else if (len == 7) {
458 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
459 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 value.data |= ParseHex(start[1], &error) << 20;
461 value.data |= ParseHex(start[2], &error) << 16;
462 value.data |= ParseHex(start[3], &error) << 12;
463 value.data |= ParseHex(start[4], &error) << 8;
464 value.data |= ParseHex(start[5], &error) << 4;
465 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 } else if (len == 9) {
467 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 value.data |= ParseHex(start[1], &error) << 28;
469 value.data |= ParseHex(start[2], &error) << 24;
470 value.data |= ParseHex(start[3], &error) << 20;
471 value.data |= ParseHex(start[4], &error) << 16;
472 value.data |= ParseHex(start[5], &error) << 12;
473 value.data |= ParseHex(start[6], &error) << 8;
474 value.data |= ParseHex(start[7], &error) << 4;
475 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 } else {
477 return {};
478 }
479 return error ? std::unique_ptr<BinaryPrimitive>()
480 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700481}
482
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700483std::optional<bool> ParseBool(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 StringPiece trimmed_str(util::TrimWhitespace(str));
485 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700486 return std::optional<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
488 trimmed_str == "False") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700489 return std::optional<bool>(false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 }
491 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800492}
493
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700494std::optional<uint32_t> ParseInt(StringPiece str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000495 std::u16string str16 = android::util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 android::Res_value value;
497 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
498 return value.data;
499 }
500 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700501}
502
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700503std::optional<ResourceId> ParseResourceId(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700505
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000506 std::u16string str16 = android::util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 android::Res_value value;
508 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
509 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
510 ResourceId id(value.data);
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800511 if (id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 return id;
513 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700514 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 }
516 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700517}
518
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700519std::optional<int> ParseSdkVersion(StringPiece str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700521
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000522 std::u16string str16 = android::util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 android::Res_value value;
524 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
525 return static_cast<int>(value.data);
526 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700527
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 // Try parsing the code name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700529 std::optional<int> entry = GetDevelopmentSdkCodeNameVersion(trimmed_str);
Ryan Mitchell48002f62019-04-11 14:29:25 -0700530 if (entry) {
531 return entry.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 }
Nicholas Lativy79f03962019-01-16 16:19:09 +0000533
534 // Try parsing codename from "[codename].[preview_sdk_fingerprint]" value.
535 const StringPiece::const_iterator begin = std::begin(trimmed_str);
536 const StringPiece::const_iterator end = std::end(trimmed_str);
537 const StringPiece::const_iterator codename_end = std::find(begin, end, '.');
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700538 entry = GetDevelopmentSdkCodeNameVersion(StringPiece(begin, codename_end - begin));
Ryan Mitchell48002f62019-04-11 14:29:25 -0700539 if (entry) {
540 return entry.value();
Nicholas Lativy79f03962019-01-16 16:19:09 +0000541 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700543}
544
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700545std::unique_ptr<BinaryPrimitive> TryParseBool(StringPiece str) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700546 if (std::optional<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700547 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
548 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 }
550 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700551}
552
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700553std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
554 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
555 val ? 0xffffffffu : 0u);
556}
557
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700558std::unique_ptr<BinaryPrimitive> TryParseInt(StringPiece str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000559 std::u16string str16 = android::util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 android::Res_value value;
561 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
562 return {};
563 }
564 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700565}
566
Shane Farmerd05b9132018-02-14 15:40:35 -0800567std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
568 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
569}
570
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700571std::unique_ptr<BinaryPrimitive> TryParseFloat(StringPiece str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000572 std::u16string str16 = android::util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 android::Res_value value;
574 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
575 return {};
576 }
577 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578}
579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582 case android::Res_value::TYPE_NULL:
583 case android::Res_value::TYPE_REFERENCE:
584 case android::Res_value::TYPE_ATTRIBUTE:
585 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800586 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700588
589 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591
592 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700594
595 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597
598 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600
601 case android::Res_value::TYPE_INT_DEC:
602 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 return android::ResTable_map::TYPE_INTEGER |
604 android::ResTable_map::TYPE_ENUM |
605 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
607 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609
610 case android::Res_value::TYPE_INT_COLOR_ARGB8:
611 case android::Res_value::TYPE_INT_COLOR_RGB8:
612 case android::Res_value::TYPE_INT_COLOR_ARGB4:
613 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700615
616 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 return 0;
618 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700619}
620
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621std::unique_ptr<Item> TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000622 android::IDiagnostics* diag, StringPiece value, uint32_t type_mask,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700623 const std::function<bool(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700624 using android::ResTable_map;
625
626 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700628 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700630
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700632 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 if (reference) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700634 reference->type_flags = type_mask;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 if (create && on_create_reference) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700636 if (!on_create_reference(reference->name.value())) {
637 return {};
638 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700639 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 return std::move(reference);
641 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700642
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700643 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700645 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 if (color) {
647 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700648 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700650
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700651 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700653 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 if (boolean) {
655 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700656 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700658
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700659 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700661 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 if (integer) {
663 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700664 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700666
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700667 const uint32_t float_mask =
668 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700671 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 if (floating_point) {
Brandon Liuc674d382023-03-31 22:37:42 +0000673 // Only check if the parsed result lost precision when the parsed item is
674 // android::Res_value::TYPE_FLOAT and there is other possible types saved in type_mask, like
675 // ResTable_map::TYPE_INTEGER.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700676 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Brandon Liuc674d382023-03-31 22:37:42 +0000677 const bool mayOnlyBeFloat = (type_mask & ~float_mask) == 0;
678 const bool parsedAsFloat = floating_point->value.dataType == android::Res_value::TYPE_FLOAT;
679 if (!mayOnlyBeFloat && parsedAsFloat) {
680 float f = reinterpret_cast<float&>(floating_point->value.data);
681 std::u16string str16 = android::util::Utf8ToUtf16(util::TrimWhitespace(value));
682 double d;
683 if (android::ResTable::stringToDouble(str16.data(), str16.size(), d)) {
684 // Parse as a float only if the difference between float and double parsed from the
685 // same string is smaller than 1, otherwise return as raw string.
686 if (fabs(f - d) < 1) {
687 return std::move(floating_point);
Brandon Liu48d229d2023-05-04 23:54:03 +0000688 } else {
689 if (diag->IsVerbose()) {
690 diag->Note(android::DiagMessage()
691 << "precision lost greater than 1 while parsing float " << value
692 << ", return a raw string");
693 }
Brandon Liuc674d382023-03-31 22:37:42 +0000694 }
695 }
696 } else {
697 return std::move(floating_point);
698 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700700 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 }
702 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700703}
704
705/**
706 * We successively try to parse the string as a resource type that the Attribute
707 * allows.
708 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709std::unique_ptr<Item> TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000710 android::IDiagnostics* diag, StringPiece str, const Attribute* attr,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700711 const std::function<bool(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700712 using android::ResTable_map;
713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 const uint32_t type_mask = attr->type_mask;
Brandon Liu48d229d2023-05-04 23:54:03 +0000715 auto value = TryParseItemForAttribute(diag, str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 if (value) {
717 return value;
718 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700719
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700720 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700722 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 if (enum_value) {
724 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700725 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700727
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700728 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700730 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 if (flag_value) {
732 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700733 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 }
735 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700736}
737
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700738std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 out << "res/" << res_file.name.type;
741 if (res_file.config != ConfigDescription{}) {
742 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 }
744 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800745
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
747 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800753}
754
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700755std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
756 const android::ResStringPool& src_pool,
757 const android::Res_value& res_value,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000758 android::StringPool* dst_pool) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700759 if (type == ResourceType::kId) {
Donald Chai4abc8282019-12-13 23:23:07 -0800760 if (res_value.dataType != android::Res_value::TYPE_REFERENCE &&
761 res_value.dataType != android::Res_value::TYPE_DYNAMIC_REFERENCE) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700762 // plain "id" resources are actually encoded as unused values (aapt1 uses an empty string,
Donald Chai4abc8282019-12-13 23:23:07 -0800763 // while aapt2 uses a false boolean).
764 return util::make_unique<Id>();
765 }
766 // fall through to regular reference deserialization logic
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700767 }
768
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000769 const uint32_t data = android::util::DeviceToHost32(res_value.data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700770 switch (res_value.dataType) {
771 case android::Res_value::TYPE_STRING: {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000772 const std::string str = android::util::GetString(src_pool, data);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000773 auto spans_result = src_pool.styleAt(data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700774
775 // Check if the string has a valid style associated with it.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000776 if (spans_result.has_value() &&
777 (*spans_result)->name.index != android::ResStringPool_span::END) {
778 const android::ResStringPool_span* spans = spans_result->unsafe_ptr();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000779 android::StyleString style_str = {str};
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700780 while (spans->name.index != android::ResStringPool_span::END) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000781 style_str.spans.push_back(
782 android::Span{android::util::GetString(src_pool, spans->name.index), spans->firstChar,
783 spans->lastChar});
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700784 spans++;
785 }
786 return util::make_unique<StyledString>(dst_pool->MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000787 style_str,
788 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700789 } else {
790 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
791 // This must be a FileReference.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000792 std::unique_ptr<FileReference> file_ref = util::make_unique<FileReference>(
793 dst_pool->MakeRef(str, android::StringPool::Context(
794 android::StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000795 if (type == ResourceType::kRaw) {
796 file_ref->type = ResourceFile::Type::kUnknown;
797 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700798 file_ref->type = ResourceFile::Type::kBinaryXml;
799 } else if (util::EndsWith(*file_ref->path, ".png")) {
800 file_ref->type = ResourceFile::Type::kPng;
801 }
802 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700803 }
804
805 // There are no styles associated with this string, so treat it as a simple string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000806 return util::make_unique<String>(
807 dst_pool->MakeRef(str, android::StringPool::Context(config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700808 }
809 } break;
810
811 case android::Res_value::TYPE_REFERENCE:
812 case android::Res_value::TYPE_ATTRIBUTE:
813 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
814 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
815 Reference::Type ref_type = Reference::Type::kResource;
816 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
817 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
818 ref_type = Reference::Type::kAttribute;
819 }
820
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700821 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700822 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700823 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700824 }
825
826 // This is a normal reference.
Clark DuValle9fedbe2020-01-09 11:52:52 -0800827 auto reference = util::make_unique<Reference>(data, ref_type);
828 if (res_value.dataType == android::Res_value::TYPE_DYNAMIC_REFERENCE ||
829 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
830 reference->is_dynamic = true;
831 }
832 return reference;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700833 } break;
834 }
835
836 // Treat this as a raw binary primitive.
837 return util::make_unique<BinaryPrimitive>(res_value);
838}
839
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800840// Converts the codepoint to UTF-8 and appends it to the string.
841static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
842 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
843 if (len < 0) {
844 return false;
845 }
846
847 const size_t start_append_pos = output->size();
848
849 // Make room for the next character.
850 output->resize(output->size() + len);
851
852 char* dst = &*(output->begin() + start_append_pos);
853 utf32_to_utf8(&codepoint, 1, dst, len + 1);
854 return true;
855}
856
857// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
858// Unicode codepoint represented by the escape sequence to the string.
859static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
860 char32_t code = 0;
861 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
862 char32_t codepoint = iter->Next();
863 char32_t a;
864 if (codepoint >= U'0' && codepoint <= U'9') {
865 a = codepoint - U'0';
866 } else if (codepoint >= U'a' && codepoint <= U'f') {
867 a = codepoint - U'a' + 10;
868 } else if (codepoint >= U'A' && codepoint <= U'F') {
869 a = codepoint - U'A' + 10;
870 } else {
871 return {};
872 }
873 code = (code << 4) | a;
874 }
875 return AppendCodepointToUtf8String(code, output);
876}
877
878StringBuilder::StringBuilder(bool preserve_spaces)
879 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
880}
881
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800882StringBuilder& StringBuilder::AppendText(const std::string& text) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800883 if (!error_.empty()) {
884 return *this;
885 }
886
887 const size_t previous_len = xml_string_.text.size();
888 Utf8Iterator iter(text);
889 while (iter.HasNext()) {
890 char32_t codepoint = iter.Next();
Ryan Mitchell0f326752019-03-11 11:00:25 -0700891 if (!preserve_spaces_ && !quote_ && (codepoint <= std::numeric_limits<char>::max())
892 && isspace(static_cast<char>(codepoint))) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800893 if (!last_codepoint_was_space_) {
894 // Emit a space if it's the first.
895 xml_string_.text += ' ';
896 last_codepoint_was_space_ = true;
897 }
898
899 // Keep eating spaces.
900 continue;
901 }
902
903 // This is not a space.
904 last_codepoint_was_space_ = false;
905
906 if (codepoint == U'\\') {
907 if (iter.HasNext()) {
908 codepoint = iter.Next();
909 switch (codepoint) {
910 case U't':
911 xml_string_.text += '\t';
912 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800913 case U'n':
914 xml_string_.text += '\n';
915 break;
916
917 case U'#':
918 case U'@':
919 case U'?':
920 case U'"':
921 case U'\'':
922 case U'\\':
923 xml_string_.text += static_cast<char>(codepoint);
924 break;
925
926 case U'u':
927 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
928 error_ =
929 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
930 return *this;
931 }
932 break;
933
934 default:
935 // Ignore the escape character and just include the codepoint.
936 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
937 break;
938 }
939 }
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800940 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800941 // Only toggle the quote state when we are not preserving spaces.
942 quote_ = !quote_;
943
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800944 } else if (!preserve_spaces_ && !quote_ && codepoint == U'\'') {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700945 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800946 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
947 return *this;
948
949 } else {
950 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
951 }
952 }
953
954 // Accumulate the added string's UTF-16 length.
955 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
956 const size_t utf8_length = xml_string_.text.size();
957 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
958 if (len < 0) {
959 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
960 return *this;
961 }
962
963 utf16_len_ += static_cast<uint32_t>(len);
964 return *this;
965}
966
967StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
968 if (!error_.empty()) {
969 return 0u;
970 }
971
972 // When we start a span, all state associated with whitespace truncation and quotation is ended.
973 ResetTextState();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000974 android::Span span;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800975 span.name = name;
976 span.first_char = span.last_char = utf16_len_;
977 xml_string_.spans.push_back(std::move(span));
978 return xml_string_.spans.size() - 1;
979}
980
981void StringBuilder::EndSpan(SpanHandle handle) {
982 if (!error_.empty()) {
983 return;
984 }
985
986 // When we end a span, all state associated with whitespace truncation and quotation is ended.
987 ResetTextState();
988 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
989}
990
991StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
992 if (!error_.empty()) {
993 return 0u;
994 }
995
996 UntranslatableSection section;
997 section.start = section.end = xml_string_.text.size();
998 xml_string_.untranslatable_sections.push_back(section);
999 return xml_string_.untranslatable_sections.size() - 1;
1000}
1001
1002void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
1003 if (!error_.empty()) {
1004 return;
1005 }
1006 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
1007}
1008
1009FlattenedXmlString StringBuilder::GetFlattenedString() const {
1010 return xml_string_;
1011}
1012
1013std::string StringBuilder::to_string() const {
1014 return xml_string_.text;
1015}
1016
1017StringBuilder::operator bool() const {
1018 return error_.empty();
1019}
1020
1021std::string StringBuilder::GetError() const {
1022 return error_;
1023}
1024
1025void StringBuilder::ResetTextState() {
1026 quote_ = preserve_spaces_;
1027 last_codepoint_was_space_ = false;
1028}
1029
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030} // namespace ResourceUtils
1031} // namespace aapt