blob: 3787f3b96f0894fc63ec2ed3cdf9c15e69c46a8a [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
Ryan Mitchell4382e442021-07-14 12:53:01 -070043std::optional<ResourceName> ToResourceName(const android::ResTable::resource_name& name_in) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080044 // TODO: Remove this when ResTable and AssetManager(1) are removed from AAPT2
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 ResourceName name_out;
46 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 return {};
48 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070049
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 name_out.package =
51 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070052
Iurii Makhnocff10ce2022-02-15 19:33:50 +000053 std::optional<ResourceNamedTypeRef> type;
Stephen Hinesd0ac4e12022-10-05 18:23:46 -070054 std::string converted;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 if (name_in.type) {
Stephen Hinesd0ac4e12022-10-05 18:23:46 -070056 converted = util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen));
57 type = ParseResourceNamedType(converted);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 } else if (name_in.type8) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +000059 type = ParseResourceNamedType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 } else {
61 return {};
62 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 if (!type) {
65 return {};
66 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070067
Iurii Makhnocff10ce2022-02-15 19:33:50 +000068 name_out.type = type->ToResourceNamedType();
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 if (name_in.name) {
71 name_out.entry =
72 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
73 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080074 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 } else {
76 return {};
77 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070079}
80
Ryan Mitchell4382e442021-07-14 12:53:01 -070081std::optional<ResourceName> ToResourceName(const android::AssetManager2::ResourceName& name_in) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080082 ResourceName name_out;
83 if (!name_in.package) {
84 return {};
85 }
86
87 name_out.package = std::string(name_in.package, name_in.package_len);
88
Iurii Makhnocff10ce2022-02-15 19:33:50 +000089 std::optional<ResourceNamedTypeRef> type;
Stephen Hinesd0ac4e12022-10-05 18:23:46 -070090 std::string converted;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080091 if (name_in.type16) {
Stephen Hinesd0ac4e12022-10-05 18:23:46 -070092 converted = util::Utf16ToUtf8(StringPiece16(name_in.type16, name_in.type_len));
93 type = ParseResourceNamedType(converted);
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080094 } else if (name_in.type) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +000095 type = ParseResourceNamedType(StringPiece(name_in.type, name_in.type_len));
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080096 } else {
97 return {};
98 }
99
100 if (!type) {
101 return {};
102 }
103
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000104 name_out.type = type->ToResourceNamedType();
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800105
106 if (name_in.entry16) {
107 name_out.entry =
108 util::Utf16ToUtf8(StringPiece16(name_in.entry16, name_in.entry_len));
109 } else if (name_in.entry) {
110 name_out.entry = std::string(name_in.entry, name_in.entry_len);
111 } else {
112 return {};
113 }
114 return name_out;
115}
116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
118 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 if (str.empty()) {
120 return false;
121 }
122
123 size_t offset = 0;
124 bool priv = false;
125 if (str.data()[0] == '*') {
126 priv = true;
127 offset = 1;
128 }
129
130 StringPiece package;
131 StringPiece type;
132 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800133 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
134 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 return false;
136 }
137
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000138 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 return false;
141 }
142
143 if (entry.empty()) {
144 return false;
145 }
146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 if (out_ref) {
148 out_ref->package = package;
149 out_ref->type = *parsed_type;
150 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 if (out_private) {
154 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
156 return true;
157}
158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
160 bool* out_create, bool* out_private) {
161 StringPiece trimmed_str(util::TrimWhitespace(str));
162 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 return false;
164 }
165
166 bool create = false;
167 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 create = true;
172 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800173 }
174
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 if (!ParseResourceName(
177 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 &priv)) {
179 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800180 }
181
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 if (create && priv) {
183 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800184 }
185
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000186 if (create && name.type.type != ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800188 }
189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 if (out_ref) {
191 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 }
193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 if (out_create) {
195 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800196 }
197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 if (out_private) {
199 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800200 }
201 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
203 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204}
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206bool IsReference(const StringPiece& str) {
207 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800208}
209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
211 StringPiece trimmed_str(util::TrimWhitespace(str));
212 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 }
215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 StringPiece package;
218 StringPiece type;
219 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800220 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
221 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 return false;
223 }
224
225 if (!type.empty() && type != "attr") {
226 return false;
227 }
228
229 if (entry.empty()) {
230 return false;
231 }
232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 if (out_ref) {
234 out_ref->package = package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000235 out_ref->type = ResourceNamedTypeWithDefaultName(ResourceType::kAttr);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 }
238 return true;
239 }
240 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241}
242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243bool IsAttributeReference(const StringPiece& str) {
244 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800245}
246
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247/*
248 * Style parent's are a bit different. We accept the following formats:
249 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800250 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff0f2015-12-16 14:01:57 -0800251 * ?[[*]package:]style/<entry>
252 * <[*]package>:[style/]<entry>
253 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700255std::optional<Reference> ParseStyleParentReference(const StringPiece& str, std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 if (str.empty()) {
257 return {};
258 }
259
260 StringPiece name = str;
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 bool has_leading_identifiers = false;
263 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264
265 // Skip over these identifiers. A style's parent is a normal reference.
266 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 name = name.substr(1, name.size() - 1);
269 }
270
271 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 name = name.substr(1, name.size() - 1);
274 }
275
276 ResourceNameRef ref;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000277 ref.type = ResourceNamedTypeWithDefaultName(ResourceType::kStyle);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800280 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 const ResourceType* parsed_type = ParseResourceType(type_str);
284 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 err << "invalid resource type '" << type_str << "' for parent of style";
287 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 std::stringstream err;
294 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return {};
297 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700302}
303
Ryan Mitchell4382e442021-07-14 12:53:01 -0700304std::optional<Reference> ParseXmlAttributeName(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 StringPiece trimmed_str = util::TrimWhitespace(str);
306 const char* start = trimmed_str.data();
307 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700309
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 Reference ref;
311 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 start++;
314 p++;
315 }
316
317 StringPiece package;
318 StringPiece name;
319 while (p != end) {
320 if (*p == ':') {
321 package = StringPiece(start, p - start);
322 name = StringPiece(p + 1, end - (p + 1));
323 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700324 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325 p++;
326 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700327
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000328 ref.name = ResourceName(package, ResourceNamedTypeWithDefaultName(ResourceType::kAttr),
329 name.empty() ? trimmed_str : name);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700330 return std::optional<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700331}
332
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
334 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 bool private_ref = false;
337 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 return value;
341 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 if (ParseAttributeReference(str, &ref)) {
344 if (out_create) {
345 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
348 }
349 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350}
351
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700352std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
353 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700355 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700357 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700359 return {};
360}
361
362std::unique_ptr<Reference> MakeNull() {
363 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
364 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
365 return util::make_unique<Reference>();
366}
367
368std::unique_ptr<BinaryPrimitive> MakeEmpty() {
369 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
370 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700371}
372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700374 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 StringPiece trimmed_str(util::TrimWhitespace(str));
376 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 // Enum symbols are stored as @package:id/symbol resources,
378 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
380 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 android::Res_value value = {};
Ryan Mitchellc1676802019-05-20 16:47:08 -0700382 value.dataType = symbol.type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 value.data = symbol.value;
384 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700385 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 }
387 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700388}
389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700391 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 android::Res_value flags = {};
393 flags.dataType = android::Res_value::TYPE_INT_HEX;
394 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800395
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 }
400
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800401 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 bool flag_set = false;
405 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 // Flag symbols are stored as @package:id/symbol resources,
407 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 const ResourceName& flag_symbol_resource_name =
409 symbol.symbol.name.value();
410 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 break;
414 }
415 }
416
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 return {};
419 }
420 }
421 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700422}
423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 if (c >= '0' && c <= '9') {
426 return c - '0';
427 } else if (c >= 'a' && c <= 'f') {
428 return c - 'a' + 0xa;
429 } else if (c >= 'A' && c <= 'F') {
430 return c - 'A' + 0xa;
431 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 return 0xffffffffu;
434 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700435}
436
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
438 StringPiece color_str(util::TrimWhitespace(str));
439 const char* start = color_str.data();
440 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 if (len == 0 || start[0] != '#') {
442 return {};
443 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700444
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 android::Res_value value = {};
446 bool error = false;
447 if (len == 4) {
448 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
449 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 value.data |= ParseHex(start[1], &error) << 20;
451 value.data |= ParseHex(start[1], &error) << 16;
452 value.data |= ParseHex(start[2], &error) << 12;
453 value.data |= ParseHex(start[2], &error) << 8;
454 value.data |= ParseHex(start[3], &error) << 4;
455 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 } else if (len == 5) {
457 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 value.data |= ParseHex(start[1], &error) << 28;
459 value.data |= ParseHex(start[1], &error) << 24;
460 value.data |= ParseHex(start[2], &error) << 20;
461 value.data |= ParseHex(start[2], &error) << 16;
462 value.data |= ParseHex(start[3], &error) << 12;
463 value.data |= ParseHex(start[3], &error) << 8;
464 value.data |= ParseHex(start[4], &error) << 4;
465 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 } else if (len == 7) {
467 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
468 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 value.data |= ParseHex(start[1], &error) << 20;
470 value.data |= ParseHex(start[2], &error) << 16;
471 value.data |= ParseHex(start[3], &error) << 12;
472 value.data |= ParseHex(start[4], &error) << 8;
473 value.data |= ParseHex(start[5], &error) << 4;
474 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 } else if (len == 9) {
476 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 value.data |= ParseHex(start[1], &error) << 28;
478 value.data |= ParseHex(start[2], &error) << 24;
479 value.data |= ParseHex(start[3], &error) << 20;
480 value.data |= ParseHex(start[4], &error) << 16;
481 value.data |= ParseHex(start[5], &error) << 12;
482 value.data |= ParseHex(start[6], &error) << 8;
483 value.data |= ParseHex(start[7], &error) << 4;
484 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 } else {
486 return {};
487 }
488 return error ? std::unique_ptr<BinaryPrimitive>()
489 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700490}
491
Ryan Mitchell4382e442021-07-14 12:53:01 -0700492std::optional<bool> ParseBool(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 StringPiece trimmed_str(util::TrimWhitespace(str));
494 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700495 return std::optional<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
497 trimmed_str == "False") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700498 return std::optional<bool>(false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 }
500 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800501}
502
Ryan Mitchell4382e442021-07-14 12:53:01 -0700503std::optional<uint32_t> ParseInt(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 android::Res_value value;
506 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
507 return value.data;
508 }
509 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700510}
511
Ryan Mitchell4382e442021-07-14 12:53:01 -0700512std::optional<ResourceId> ParseResourceId(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 android::Res_value value;
517 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
518 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
519 ResourceId id(value.data);
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800520 if (id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 return id;
522 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700523 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 }
525 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700526}
527
Ryan Mitchell4382e442021-07-14 12:53:01 -0700528std::optional<int> ParseSdkVersion(const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 android::Res_value value;
533 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
534 return static_cast<int>(value.data);
535 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700536
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 // Try parsing the code name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700538 std::optional<int> entry = GetDevelopmentSdkCodeNameVersion(trimmed_str);
Ryan Mitchell48002f62019-04-11 14:29:25 -0700539 if (entry) {
540 return entry.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
Nicholas Lativy79f03962019-01-16 16:19:09 +0000542
543 // Try parsing codename from "[codename].[preview_sdk_fingerprint]" value.
544 const StringPiece::const_iterator begin = std::begin(trimmed_str);
545 const StringPiece::const_iterator end = std::end(trimmed_str);
546 const StringPiece::const_iterator codename_end = std::find(begin, end, '.');
Ryan Mitchell48002f62019-04-11 14:29:25 -0700547 entry = GetDevelopmentSdkCodeNameVersion(trimmed_str.substr(begin, codename_end));
548 if (entry) {
549 return entry.value();
Nicholas Lativy79f03962019-01-16 16:19:09 +0000550 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700552}
553
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700555 if (std::optional<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700556 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
557 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 }
559 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700560}
561
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700562std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
563 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
564 val ? 0xffffffffu : 0u);
565}
566
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700568 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 android::Res_value value;
570 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
571 return {};
572 }
573 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700574}
575
Shane Farmerd05b9132018-02-14 15:40:35 -0800576std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
577 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
578}
579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700581 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 android::Res_value value;
583 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
584 return {};
585 }
586 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700587}
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591 case android::Res_value::TYPE_NULL:
592 case android::Res_value::TYPE_REFERENCE:
593 case android::Res_value::TYPE_ATTRIBUTE:
594 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800595 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597
598 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600
601 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603
604 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
607 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609
610 case android::Res_value::TYPE_INT_DEC:
611 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return android::ResTable_map::TYPE_INTEGER |
613 android::ResTable_map::TYPE_ENUM |
614 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700615
616 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700618
619 case android::Res_value::TYPE_INT_COLOR_ARGB8:
620 case android::Res_value::TYPE_INT_COLOR_RGB8:
621 case android::Res_value::TYPE_INT_COLOR_ARGB4:
622 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624
625 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 return 0;
627 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700628}
629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630std::unique_ptr<Item> TryParseItemForAttribute(
631 const StringPiece& value, uint32_t type_mask,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700632 const std::function<bool(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700633 using android::ResTable_map;
634
635 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700637 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700639
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700641 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 if (reference) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700643 reference->type_flags = type_mask;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 if (create && on_create_reference) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700645 if (!on_create_reference(reference->name.value())) {
646 return {};
647 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700648 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 return std::move(reference);
650 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700652 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700654 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 if (color) {
656 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700657 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700659
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700660 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700662 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 if (boolean) {
664 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700665 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700667
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700668 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700670 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 if (integer) {
672 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700673 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700675
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700676 const uint32_t float_mask =
677 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700680 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700682 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700685 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 }
687 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700688}
689
690/**
691 * We successively try to parse the string as a resource type that the Attribute
692 * allows.
693 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 const StringPiece& str, const Attribute* attr,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700696 const std::function<bool(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700697 using android::ResTable_map;
698
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700699 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700700 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 if (value) {
702 return value;
703 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700704
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700705 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700707 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 if (enum_value) {
709 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700710 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700712
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700713 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700715 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 if (flag_value) {
717 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700718 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 }
720 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700721}
722
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700723std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 out << "res/" << res_file.name.type;
726 if (res_file.config != ConfigDescription{}) {
727 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 }
729 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800730
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
732 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800738}
739
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700740std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
741 const android::ResStringPool& src_pool,
742 const android::Res_value& res_value,
743 StringPool* dst_pool) {
744 if (type == ResourceType::kId) {
Donald Chai4abc8282019-12-13 23:23:07 -0800745 if (res_value.dataType != android::Res_value::TYPE_REFERENCE &&
746 res_value.dataType != android::Res_value::TYPE_DYNAMIC_REFERENCE) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700747 // plain "id" resources are actually encoded as unused values (aapt1 uses an empty string,
Donald Chai4abc8282019-12-13 23:23:07 -0800748 // while aapt2 uses a false boolean).
749 return util::make_unique<Id>();
750 }
751 // fall through to regular reference deserialization logic
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700752 }
753
754 const uint32_t data = util::DeviceToHost32(res_value.data);
755 switch (res_value.dataType) {
756 case android::Res_value::TYPE_STRING: {
757 const std::string str = util::GetString(src_pool, data);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000758 auto spans_result = src_pool.styleAt(data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700759
760 // Check if the string has a valid style associated with it.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000761 if (spans_result.has_value() &&
762 (*spans_result)->name.index != android::ResStringPool_span::END) {
763 const android::ResStringPool_span* spans = spans_result->unsafe_ptr();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700764 StyleString style_str = {str};
765 while (spans->name.index != android::ResStringPool_span::END) {
766 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
767 spans->firstChar, spans->lastChar});
768 spans++;
769 }
770 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700771 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700772 } else {
773 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
774 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700775 std::unique_ptr<FileReference> file_ref =
776 util::make_unique<FileReference>(dst_pool->MakeRef(
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000777 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000778 if (type == ResourceType::kRaw) {
779 file_ref->type = ResourceFile::Type::kUnknown;
780 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700781 file_ref->type = ResourceFile::Type::kBinaryXml;
782 } else if (util::EndsWith(*file_ref->path, ".png")) {
783 file_ref->type = ResourceFile::Type::kPng;
784 }
785 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700786 }
787
788 // There are no styles associated with this string, so treat it as a simple string.
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000789 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700790 }
791 } break;
792
793 case android::Res_value::TYPE_REFERENCE:
794 case android::Res_value::TYPE_ATTRIBUTE:
795 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
796 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
797 Reference::Type ref_type = Reference::Type::kResource;
798 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
799 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
800 ref_type = Reference::Type::kAttribute;
801 }
802
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700803 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700804 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700805 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700806 }
807
808 // This is a normal reference.
Clark DuValle9fedbe2020-01-09 11:52:52 -0800809 auto reference = util::make_unique<Reference>(data, ref_type);
810 if (res_value.dataType == android::Res_value::TYPE_DYNAMIC_REFERENCE ||
811 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
812 reference->is_dynamic = true;
813 }
814 return reference;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700815 } break;
816 }
817
818 // Treat this as a raw binary primitive.
819 return util::make_unique<BinaryPrimitive>(res_value);
820}
821
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800822// Converts the codepoint to UTF-8 and appends it to the string.
823static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
824 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
825 if (len < 0) {
826 return false;
827 }
828
829 const size_t start_append_pos = output->size();
830
831 // Make room for the next character.
832 output->resize(output->size() + len);
833
834 char* dst = &*(output->begin() + start_append_pos);
835 utf32_to_utf8(&codepoint, 1, dst, len + 1);
836 return true;
837}
838
839// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
840// Unicode codepoint represented by the escape sequence to the string.
841static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
842 char32_t code = 0;
843 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
844 char32_t codepoint = iter->Next();
845 char32_t a;
846 if (codepoint >= U'0' && codepoint <= U'9') {
847 a = codepoint - U'0';
848 } else if (codepoint >= U'a' && codepoint <= U'f') {
849 a = codepoint - U'a' + 10;
850 } else if (codepoint >= U'A' && codepoint <= U'F') {
851 a = codepoint - U'A' + 10;
852 } else {
853 return {};
854 }
855 code = (code << 4) | a;
856 }
857 return AppendCodepointToUtf8String(code, output);
858}
859
860StringBuilder::StringBuilder(bool preserve_spaces)
861 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
862}
863
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800864StringBuilder& StringBuilder::AppendText(const std::string& text) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800865 if (!error_.empty()) {
866 return *this;
867 }
868
869 const size_t previous_len = xml_string_.text.size();
870 Utf8Iterator iter(text);
871 while (iter.HasNext()) {
872 char32_t codepoint = iter.Next();
Ryan Mitchell0f326752019-03-11 11:00:25 -0700873 if (!preserve_spaces_ && !quote_ && (codepoint <= std::numeric_limits<char>::max())
874 && isspace(static_cast<char>(codepoint))) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800875 if (!last_codepoint_was_space_) {
876 // Emit a space if it's the first.
877 xml_string_.text += ' ';
878 last_codepoint_was_space_ = true;
879 }
880
881 // Keep eating spaces.
882 continue;
883 }
884
885 // This is not a space.
886 last_codepoint_was_space_ = false;
887
888 if (codepoint == U'\\') {
889 if (iter.HasNext()) {
890 codepoint = iter.Next();
891 switch (codepoint) {
892 case U't':
893 xml_string_.text += '\t';
894 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800895 case U'n':
896 xml_string_.text += '\n';
897 break;
898
899 case U'#':
900 case U'@':
901 case U'?':
902 case U'"':
903 case U'\'':
904 case U'\\':
905 xml_string_.text += static_cast<char>(codepoint);
906 break;
907
908 case U'u':
909 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
910 error_ =
911 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
912 return *this;
913 }
914 break;
915
916 default:
917 // Ignore the escape character and just include the codepoint.
918 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
919 break;
920 }
921 }
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800922 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800923 // Only toggle the quote state when we are not preserving spaces.
924 quote_ = !quote_;
925
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800926 } else if (!preserve_spaces_ && !quote_ && codepoint == U'\'') {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700927 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800928 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
929 return *this;
930
931 } else {
932 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
933 }
934 }
935
936 // Accumulate the added string's UTF-16 length.
937 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
938 const size_t utf8_length = xml_string_.text.size();
939 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
940 if (len < 0) {
941 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
942 return *this;
943 }
944
945 utf16_len_ += static_cast<uint32_t>(len);
946 return *this;
947}
948
949StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
950 if (!error_.empty()) {
951 return 0u;
952 }
953
954 // When we start a span, all state associated with whitespace truncation and quotation is ended.
955 ResetTextState();
956 Span span;
957 span.name = name;
958 span.first_char = span.last_char = utf16_len_;
959 xml_string_.spans.push_back(std::move(span));
960 return xml_string_.spans.size() - 1;
961}
962
963void StringBuilder::EndSpan(SpanHandle handle) {
964 if (!error_.empty()) {
965 return;
966 }
967
968 // When we end a span, all state associated with whitespace truncation and quotation is ended.
969 ResetTextState();
970 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
971}
972
973StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
974 if (!error_.empty()) {
975 return 0u;
976 }
977
978 UntranslatableSection section;
979 section.start = section.end = xml_string_.text.size();
980 xml_string_.untranslatable_sections.push_back(section);
981 return xml_string_.untranslatable_sections.size() - 1;
982}
983
984void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
985 if (!error_.empty()) {
986 return;
987 }
988 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
989}
990
991FlattenedXmlString StringBuilder::GetFlattenedString() const {
992 return xml_string_;
993}
994
995std::string StringBuilder::to_string() const {
996 return xml_string_.text;
997}
998
999StringBuilder::operator bool() const {
1000 return error_.empty();
1001}
1002
1003std::string StringBuilder::GetError() const {
1004 return error_;
1005}
1006
1007void StringBuilder::ResetTextState() {
1008 quote_ = preserve_spaces_;
1009 last_codepoint_was_space_ = false;
1010}
1011
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012} // namespace ResourceUtils
1013} // namespace aapt