blob: 469128b1e50b1eb92471653e569bc7afa6b596f5 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043Maybe<ResourceName> ToResourceName(
44 const android::ResTable::resource_name& name_in) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080045 // TODO: Remove this when ResTable and AssetManager(1) are removed from AAPT2
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 ResourceName name_out;
47 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 return {};
49 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070050
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 name_out.package =
52 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 if (name_in.type) {
56 type = ParseResourceType(
57 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
58 } else if (name_in.type8) {
59 type = ParseResourceType(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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 name_out.type = *type;
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 Mitchella55dc2e2019-01-24 10:58:23 -080081Maybe<ResourceName> ToResourceName(const android::AssetManager2::ResourceName& name_in) {
82 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
89 const ResourceType* type;
90 if (name_in.type16) {
91 type = ParseResourceType(
92 util::Utf16ToUtf8(StringPiece16(name_in.type16, name_in.type_len)));
93 } else if (name_in.type) {
94 type = ParseResourceType(StringPiece(name_in.type, name_in.type_len));
95 } else {
96 return {};
97 }
98
99 if (!type) {
100 return {};
101 }
102
103 name_out.type = *type;
104
105 if (name_in.entry16) {
106 name_out.entry =
107 util::Utf16ToUtf8(StringPiece16(name_in.entry16, name_in.entry_len));
108 } else if (name_in.entry) {
109 name_out.entry = std::string(name_in.entry, name_in.entry_len);
110 } else {
111 return {};
112 }
113 return name_out;
114}
115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
117 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 if (str.empty()) {
119 return false;
120 }
121
122 size_t offset = 0;
123 bool priv = false;
124 if (str.data()[0] == '*') {
125 priv = true;
126 offset = 1;
127 }
128
129 StringPiece package;
130 StringPiece type;
131 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800132 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
133 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 return false;
135 }
136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 const ResourceType* parsed_type = ParseResourceType(type);
138 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 return false;
140 }
141
142 if (entry.empty()) {
143 return false;
144 }
145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 if (out_ref) {
147 out_ref->package = package;
148 out_ref->type = *parsed_type;
149 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 if (out_private) {
153 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
155 return true;
156}
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
159 bool* out_create, bool* out_private) {
160 StringPiece trimmed_str(util::TrimWhitespace(str));
161 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 return false;
163 }
164
165 bool create = false;
166 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 create = true;
171 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800172 }
173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 if (!ParseResourceName(
176 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 &priv)) {
178 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800179 }
180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 if (create && priv) {
182 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800183 }
184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 if (create && name.type != ResourceType::kId) {
186 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800187 }
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 if (out_ref) {
190 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 }
192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 if (out_create) {
194 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800195 }
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (out_private) {
198 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800199 }
200 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 }
202 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203}
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205bool IsReference(const StringPiece& str) {
206 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800207}
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
210 StringPiece trimmed_str(util::TrimWhitespace(str));
211 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 }
214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 StringPiece package;
217 StringPiece type;
218 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800219 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
220 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 return false;
222 }
223
224 if (!type.empty() && type != "attr") {
225 return false;
226 }
227
228 if (entry.empty()) {
229 return false;
230 }
231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 if (out_ref) {
233 out_ref->package = package;
234 out_ref->type = ResourceType::kAttr;
235 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
237 return true;
238 }
239 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240}
241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242bool IsAttributeReference(const StringPiece& str) {
243 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800244}
245
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246/*
247 * Style parent's are a bit different. We accept the following formats:
248 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800249 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800250 * ?[[*]package:]style/<entry>
251 * <[*]package>:[style/]<entry>
252 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
255 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;
277 ref.type = ResourceType::kStyle;
278
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
305 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
Adam Lesinskid5083f62017-01-16 15:07:21 -0800328 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700330}
331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
333 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 bool private_ref = false;
336 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 return value;
340 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 if (ParseAttributeReference(str, &ref)) {
343 if (out_create) {
344 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700345 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
347 }
348 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700349}
350
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700351std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
352 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700354 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700356 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700358 return {};
359}
360
361std::unique_ptr<Reference> MakeNull() {
362 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
363 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
364 return util::make_unique<Reference>();
365}
366
367std::unique_ptr<BinaryPrimitive> MakeEmpty() {
368 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
369 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700370}
371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700373 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 StringPiece trimmed_str(util::TrimWhitespace(str));
375 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 // Enum symbols are stored as @package:id/symbol resources,
377 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
379 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 android::Res_value value = {};
Ryan Mitchellc1676802019-05-20 16:47:08 -0700381 value.dataType = symbol.type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 value.data = symbol.value;
383 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700384 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 }
386 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700387}
388
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700390 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 android::Res_value flags = {};
392 flags.dataType = android::Res_value::TYPE_INT_HEX;
393 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700397 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 }
399
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800400 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 bool flag_set = false;
404 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 // Flag symbols are stored as @package:id/symbol resources,
406 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 const ResourceName& flag_symbol_resource_name =
408 symbol.symbol.name.value();
409 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 break;
413 }
414 }
415
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 return {};
418 }
419 }
420 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700421}
422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 if (c >= '0' && c <= '9') {
425 return c - '0';
426 } else if (c >= 'a' && c <= 'f') {
427 return c - 'a' + 0xa;
428 } else if (c >= 'A' && c <= 'F') {
429 return c - 'A' + 0xa;
430 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 return 0xffffffffu;
433 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700434}
435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
437 StringPiece color_str(util::TrimWhitespace(str));
438 const char* start = color_str.data();
439 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 if (len == 0 || start[0] != '#') {
441 return {};
442 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700443
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 android::Res_value value = {};
445 bool error = false;
446 if (len == 4) {
447 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
448 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 value.data |= ParseHex(start[1], &error) << 20;
450 value.data |= ParseHex(start[1], &error) << 16;
451 value.data |= ParseHex(start[2], &error) << 12;
452 value.data |= ParseHex(start[2], &error) << 8;
453 value.data |= ParseHex(start[3], &error) << 4;
454 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 } else if (len == 5) {
456 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 value.data |= ParseHex(start[1], &error) << 28;
458 value.data |= ParseHex(start[1], &error) << 24;
459 value.data |= ParseHex(start[2], &error) << 20;
460 value.data |= ParseHex(start[2], &error) << 16;
461 value.data |= ParseHex(start[3], &error) << 12;
462 value.data |= ParseHex(start[3], &error) << 8;
463 value.data |= ParseHex(start[4], &error) << 4;
464 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 } else if (len == 7) {
466 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
467 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 value.data |= ParseHex(start[1], &error) << 20;
469 value.data |= ParseHex(start[2], &error) << 16;
470 value.data |= ParseHex(start[3], &error) << 12;
471 value.data |= ParseHex(start[4], &error) << 8;
472 value.data |= ParseHex(start[5], &error) << 4;
473 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 } else if (len == 9) {
475 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 value.data |= ParseHex(start[1], &error) << 28;
477 value.data |= ParseHex(start[2], &error) << 24;
478 value.data |= ParseHex(start[3], &error) << 20;
479 value.data |= ParseHex(start[4], &error) << 16;
480 value.data |= ParseHex(start[5], &error) << 12;
481 value.data |= ParseHex(start[6], &error) << 8;
482 value.data |= ParseHex(start[7], &error) << 4;
483 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 } else {
485 return {};
486 }
487 return error ? std::unique_ptr<BinaryPrimitive>()
488 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700489}
490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491Maybe<bool> ParseBool(const StringPiece& str) {
492 StringPiece trimmed_str(util::TrimWhitespace(str));
493 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
496 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 return Maybe<bool>(false);
498 }
499 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800500}
501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502Maybe<uint32_t> ParseInt(const StringPiece& str) {
503 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 android::Res_value value;
505 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
506 return value.data;
507 }
508 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700509}
510
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
512 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 android::Res_value value;
516 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
517 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
518 ResourceId id(value.data);
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800519 if (id.is_valid()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 return id;
521 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700522 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 }
524 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700525}
526
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527Maybe<int> ParseSdkVersion(const StringPiece& str) {
528 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 android::Res_value value;
532 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
533 return static_cast<int>(value.data);
534 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700535
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 // Try parsing the code name.
Ryan Mitchell48002f62019-04-11 14:29:25 -0700537 Maybe<int> entry = GetDevelopmentSdkCodeNameVersion(trimmed_str);
538 if (entry) {
539 return entry.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 }
Nicholas Lativy79f03962019-01-16 16:19:09 +0000541
542 // Try parsing codename from "[codename].[preview_sdk_fingerprint]" value.
543 const StringPiece::const_iterator begin = std::begin(trimmed_str);
544 const StringPiece::const_iterator end = std::end(trimmed_str);
545 const StringPiece::const_iterator codename_end = std::find(begin, end, '.');
Ryan Mitchell48002f62019-04-11 14:29:25 -0700546 entry = GetDevelopmentSdkCodeNameVersion(trimmed_str.substr(begin, codename_end));
547 if (entry) {
548 return entry.value();
Nicholas Lativy79f03962019-01-16 16:19:09 +0000549 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700551}
552
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
554 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700555 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
556 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 }
558 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700559}
560
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700561std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
562 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
563 val ? 0xffffffffu : 0u);
564}
565
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700567 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 android::Res_value value;
569 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
570 return {};
571 }
572 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700573}
574
Shane Farmerd05b9132018-02-14 15:40:35 -0800575std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
576 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
577}
578
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700580 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 android::Res_value value;
582 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
583 return {};
584 }
585 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700586}
587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700590 case android::Res_value::TYPE_NULL:
591 case android::Res_value::TYPE_REFERENCE:
592 case android::Res_value::TYPE_ATTRIBUTE:
593 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800594 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700596
597 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599
600 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700602
603 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605
606 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700608
609 case android::Res_value::TYPE_INT_DEC:
610 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 return android::ResTable_map::TYPE_INTEGER |
612 android::ResTable_map::TYPE_ENUM |
613 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700614
615 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617
618 case android::Res_value::TYPE_INT_COLOR_ARGB8:
619 case android::Res_value::TYPE_INT_COLOR_RGB8:
620 case android::Res_value::TYPE_INT_COLOR_ARGB4:
621 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623
624 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 return 0;
626 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700627}
628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629std::unique_ptr<Item> TryParseItemForAttribute(
630 const StringPiece& value, uint32_t type_mask,
631 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700632 using android::ResTable_map;
633
634 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700636 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700640 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 if (create && on_create_reference) {
643 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 return std::move(reference);
646 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700647
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700648 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700650 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 if (color) {
652 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700653 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700655
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700656 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700658 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 if (boolean) {
660 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700661 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700663
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700664 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700666 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 if (integer) {
668 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700669 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700671
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700672 const uint32_t float_mask =
673 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700676 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700678 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700681 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 }
683 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700684}
685
686/**
687 * We successively try to parse the string as a resource type that the Attribute
688 * allows.
689 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700692 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700693 using android::ResTable_map;
694
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700696 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 if (value) {
698 return value;
699 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700700
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700701 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700703 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 if (enum_value) {
705 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700706 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700708
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700709 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700711 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 if (flag_value) {
713 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700714 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
716 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700717}
718
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700719std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 out << "res/" << res_file.name.type;
722 if (res_file.config != ConfigDescription{}) {
723 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 }
725 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800726
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
728 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800734}
735
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700736std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
737 const android::ResStringPool& src_pool,
738 const android::Res_value& res_value,
739 StringPool* dst_pool) {
740 if (type == ResourceType::kId) {
Donald Chai4abc8282019-12-13 23:23:07 -0800741 if (res_value.dataType != android::Res_value::TYPE_REFERENCE &&
742 res_value.dataType != android::Res_value::TYPE_DYNAMIC_REFERENCE) {
743 // plain "id" resources are actually encoded as dummy values (aapt1 uses an empty string,
744 // while aapt2 uses a false boolean).
745 return util::make_unique<Id>();
746 }
747 // fall through to regular reference deserialization logic
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700748 }
749
750 const uint32_t data = util::DeviceToHost32(res_value.data);
751 switch (res_value.dataType) {
752 case android::Res_value::TYPE_STRING: {
753 const std::string str = util::GetString(src_pool, data);
754 const android::ResStringPool_span* spans = src_pool.styleAt(data);
755
756 // Check if the string has a valid style associated with it.
757 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
758 StyleString style_str = {str};
759 while (spans->name.index != android::ResStringPool_span::END) {
760 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
761 spans->firstChar, spans->lastChar});
762 spans++;
763 }
764 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700765 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700766 } else {
767 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
768 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700769 std::unique_ptr<FileReference> file_ref =
770 util::make_unique<FileReference>(dst_pool->MakeRef(
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000771 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000772 if (type == ResourceType::kRaw) {
773 file_ref->type = ResourceFile::Type::kUnknown;
774 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700775 file_ref->type = ResourceFile::Type::kBinaryXml;
776 } else if (util::EndsWith(*file_ref->path, ".png")) {
777 file_ref->type = ResourceFile::Type::kPng;
778 }
779 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700780 }
781
782 // There are no styles associated with this string, so treat it as a simple string.
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000783 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700784 }
785 } break;
786
787 case android::Res_value::TYPE_REFERENCE:
788 case android::Res_value::TYPE_ATTRIBUTE:
789 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
790 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
791 Reference::Type ref_type = Reference::Type::kResource;
792 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
793 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
794 ref_type = Reference::Type::kAttribute;
795 }
796
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700797 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700798 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700799 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700800 }
801
802 // This is a normal reference.
Clark DuValle9fedbe2020-01-09 11:52:52 -0800803 auto reference = util::make_unique<Reference>(data, ref_type);
804 if (res_value.dataType == android::Res_value::TYPE_DYNAMIC_REFERENCE ||
805 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
806 reference->is_dynamic = true;
807 }
808 return reference;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700809 } break;
810 }
811
812 // Treat this as a raw binary primitive.
813 return util::make_unique<BinaryPrimitive>(res_value);
814}
815
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800816// Converts the codepoint to UTF-8 and appends it to the string.
817static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
818 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
819 if (len < 0) {
820 return false;
821 }
822
823 const size_t start_append_pos = output->size();
824
825 // Make room for the next character.
826 output->resize(output->size() + len);
827
828 char* dst = &*(output->begin() + start_append_pos);
829 utf32_to_utf8(&codepoint, 1, dst, len + 1);
830 return true;
831}
832
833// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
834// Unicode codepoint represented by the escape sequence to the string.
835static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
836 char32_t code = 0;
837 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
838 char32_t codepoint = iter->Next();
839 char32_t a;
840 if (codepoint >= U'0' && codepoint <= U'9') {
841 a = codepoint - U'0';
842 } else if (codepoint >= U'a' && codepoint <= U'f') {
843 a = codepoint - U'a' + 10;
844 } else if (codepoint >= U'A' && codepoint <= U'F') {
845 a = codepoint - U'A' + 10;
846 } else {
847 return {};
848 }
849 code = (code << 4) | a;
850 }
851 return AppendCodepointToUtf8String(code, output);
852}
853
854StringBuilder::StringBuilder(bool preserve_spaces)
855 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
856}
857
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800858StringBuilder& StringBuilder::AppendText(const std::string& text) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800859 if (!error_.empty()) {
860 return *this;
861 }
862
863 const size_t previous_len = xml_string_.text.size();
864 Utf8Iterator iter(text);
865 while (iter.HasNext()) {
866 char32_t codepoint = iter.Next();
Ryan Mitchell0f326752019-03-11 11:00:25 -0700867 if (!preserve_spaces_ && !quote_ && (codepoint <= std::numeric_limits<char>::max())
868 && isspace(static_cast<char>(codepoint))) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800869 if (!last_codepoint_was_space_) {
870 // Emit a space if it's the first.
871 xml_string_.text += ' ';
872 last_codepoint_was_space_ = true;
873 }
874
875 // Keep eating spaces.
876 continue;
877 }
878
879 // This is not a space.
880 last_codepoint_was_space_ = false;
881
882 if (codepoint == U'\\') {
883 if (iter.HasNext()) {
884 codepoint = iter.Next();
885 switch (codepoint) {
886 case U't':
887 xml_string_.text += '\t';
888 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800889 case U'n':
890 xml_string_.text += '\n';
891 break;
892
893 case U'#':
894 case U'@':
895 case U'?':
896 case U'"':
897 case U'\'':
898 case U'\\':
899 xml_string_.text += static_cast<char>(codepoint);
900 break;
901
902 case U'u':
903 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
904 error_ =
905 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
906 return *this;
907 }
908 break;
909
910 default:
911 // Ignore the escape character and just include the codepoint.
912 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
913 break;
914 }
915 }
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800916 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800917 // Only toggle the quote state when we are not preserving spaces.
918 quote_ = !quote_;
919
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800920 } else if (!preserve_spaces_ && !quote_ && codepoint == U'\'') {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700921 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800922 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
923 return *this;
924
925 } else {
926 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
927 }
928 }
929
930 // Accumulate the added string's UTF-16 length.
931 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
932 const size_t utf8_length = xml_string_.text.size();
933 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
934 if (len < 0) {
935 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
936 return *this;
937 }
938
939 utf16_len_ += static_cast<uint32_t>(len);
940 return *this;
941}
942
943StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
944 if (!error_.empty()) {
945 return 0u;
946 }
947
948 // When we start a span, all state associated with whitespace truncation and quotation is ended.
949 ResetTextState();
950 Span span;
951 span.name = name;
952 span.first_char = span.last_char = utf16_len_;
953 xml_string_.spans.push_back(std::move(span));
954 return xml_string_.spans.size() - 1;
955}
956
957void StringBuilder::EndSpan(SpanHandle handle) {
958 if (!error_.empty()) {
959 return;
960 }
961
962 // When we end a span, all state associated with whitespace truncation and quotation is ended.
963 ResetTextState();
964 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
965}
966
967StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
968 if (!error_.empty()) {
969 return 0u;
970 }
971
972 UntranslatableSection section;
973 section.start = section.end = xml_string_.text.size();
974 xml_string_.untranslatable_sections.push_back(section);
975 return xml_string_.untranslatable_sections.size() - 1;
976}
977
978void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
979 if (!error_.empty()) {
980 return;
981 }
982 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
983}
984
985FlattenedXmlString StringBuilder::GetFlattenedString() const {
986 return xml_string_;
987}
988
989std::string StringBuilder::to_string() const {
990 return xml_string_.text;
991}
992
993StringBuilder::operator bool() const {
994 return error_.empty();
995}
996
997std::string StringBuilder::GetError() const {
998 return error_;
999}
1000
1001void StringBuilder::ResetTextState() {
1002 quote_ = preserve_spaces_;
1003 last_codepoint_was_space_ = false;
1004}
1005
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006} // namespace ResourceUtils
1007} // namespace aapt