blob: c48765b7b9476adfa419d12a4e4a9cb8a0a2f9a4 [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
19#include <sstream>
20
Adam Lesinski2eed52e2018-02-21 15:55:58 -080021#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "androidfw/ResourceTypes.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080023#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025#include "NameMangler.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070026#include "SdkConstants.h"
Adam Lesinski46708052017-09-29 14:49:15 -070027#include "format/binary/ResourceTypeExtensions.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080028#include "text/Unicode.h"
29#include "text/Utf8Iterator.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080030#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "util/Util.h"
32
Adam Lesinski2eed52e2018-02-21 15:55:58 -080033using ::aapt::text::Utf8Iterator;
Adam Lesinski46708052017-09-29 14:49:15 -070034using ::android::StringPiece;
35using ::android::StringPiece16;
Adam Lesinski2eed52e2018-02-21 15:55:58 -080036using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038namespace aapt {
39namespace ResourceUtils {
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041Maybe<ResourceName> ToResourceName(
42 const android::ResTable::resource_name& name_in) {
43 ResourceName name_out;
44 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 return {};
46 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 name_out.package =
49 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 if (name_in.type) {
53 type = ParseResourceType(
54 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
55 } else if (name_in.type8) {
56 type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 } else {
58 return {};
59 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 if (!type) {
62 return {};
63 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 if (name_in.name) {
68 name_out.entry =
69 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
70 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080071 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 } else {
73 return {};
74 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070076}
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
79 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 if (str.empty()) {
81 return false;
82 }
83
84 size_t offset = 0;
85 bool priv = false;
86 if (str.data()[0] == '*') {
87 priv = true;
88 offset = 1;
89 }
90
91 StringPiece package;
92 StringPiece type;
93 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -080094 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
95 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 return false;
97 }
98
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 const ResourceType* parsed_type = ParseResourceType(type);
100 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 return false;
102 }
103
104 if (entry.empty()) {
105 return false;
106 }
107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 if (out_ref) {
109 out_ref->package = package;
110 out_ref->type = *parsed_type;
111 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 }
113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 if (out_private) {
115 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 }
117 return true;
118}
119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
121 bool* out_create, bool* out_private) {
122 StringPiece trimmed_str(util::TrimWhitespace(str));
123 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 return false;
125 }
126
127 bool create = false;
128 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 create = true;
133 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800134 }
135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 if (!ParseResourceName(
138 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 &priv)) {
140 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800141 }
142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 if (create && priv) {
144 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800145 }
146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 if (create && name.type != ResourceType::kId) {
148 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800149 }
150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 if (out_ref) {
152 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 }
154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 if (out_create) {
156 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800157 }
158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 if (out_private) {
160 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800161 }
162 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 }
164 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165}
166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167bool IsReference(const StringPiece& str) {
168 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800169}
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
172 StringPiece trimmed_str(util::TrimWhitespace(str));
173 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 }
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 StringPiece package;
179 StringPiece type;
180 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800181 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
182 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 return false;
184 }
185
186 if (!type.empty() && type != "attr") {
187 return false;
188 }
189
190 if (entry.empty()) {
191 return false;
192 }
193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 if (out_ref) {
195 out_ref->package = package;
196 out_ref->type = ResourceType::kAttr;
197 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 }
199 return true;
200 }
201 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700202}
203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204bool IsAttributeReference(const StringPiece& str) {
205 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800206}
207
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208/*
209 * Style parent's are a bit different. We accept the following formats:
210 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800211 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800212 * ?[[*]package:]style/<entry>
213 * <[*]package>:[style/]<entry>
214 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
217 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 if (str.empty()) {
219 return {};
220 }
221
222 StringPiece name = str;
223
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 bool has_leading_identifiers = false;
225 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226
227 // Skip over these identifiers. A style's parent is a normal reference.
228 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 name = name.substr(1, name.size() - 1);
231 }
232
233 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 name = name.substr(1, name.size() - 1);
236 }
237
238 ResourceNameRef ref;
239 ref.type = ResourceType::kStyle;
240
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800242 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 const ResourceType* parsed_type = ParseResourceType(type_str);
246 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 err << "invalid resource type '" << type_str << "' for parent of style";
249 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 std::stringstream err;
256 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 return {};
259 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264}
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
267 StringPiece trimmed_str = util::TrimWhitespace(str);
268 const char* start = trimmed_str.data();
269 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 Reference ref;
273 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 start++;
276 p++;
277 }
278
279 StringPiece package;
280 StringPiece name;
281 while (p != end) {
282 if (*p == ':') {
283 package = StringPiece(start, p - start);
284 name = StringPiece(p + 1, end - (p + 1));
285 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700286 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 p++;
288 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700289
Adam Lesinskid5083f62017-01-16 15:07:21 -0800290 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700292}
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
295 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 bool private_ref = false;
298 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return value;
302 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 if (ParseAttributeReference(str, &ref)) {
305 if (out_create) {
306 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
309 }
310 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700311}
312
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700313std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
314 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700316 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700318 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700320 return {};
321}
322
323std::unique_ptr<Reference> MakeNull() {
324 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
325 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
326 return util::make_unique<Reference>();
327}
328
329std::unique_ptr<BinaryPrimitive> MakeEmpty() {
330 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
331 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700332}
333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700335 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 StringPiece trimmed_str(util::TrimWhitespace(str));
337 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 // Enum symbols are stored as @package:id/symbol resources,
339 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
341 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 android::Res_value value = {};
343 value.dataType = android::Res_value::TYPE_INT_DEC;
344 value.data = symbol.value;
345 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 }
348 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700349}
350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700352 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 android::Res_value flags = {};
354 flags.dataType = android::Res_value::TYPE_INT_HEX;
355 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700359 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 }
361
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 for (StringPiece part : util::Tokenize(str, '|')) {
363 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 bool flag_set = false;
366 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 // Flag symbols are stored as @package:id/symbol resources,
368 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 const ResourceName& flag_symbol_resource_name =
370 symbol.symbol.name.value();
371 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 break;
375 }
376 }
377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 return {};
380 }
381 }
382 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700383}
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 if (c >= '0' && c <= '9') {
387 return c - '0';
388 } else if (c >= 'a' && c <= 'f') {
389 return c - 'a' + 0xa;
390 } else if (c >= 'A' && c <= 'F') {
391 return c - 'A' + 0xa;
392 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 return 0xffffffffu;
395 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700396}
397
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
399 StringPiece color_str(util::TrimWhitespace(str));
400 const char* start = color_str.data();
401 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 if (len == 0 || start[0] != '#') {
403 return {};
404 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 android::Res_value value = {};
407 bool error = false;
408 if (len == 4) {
409 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
410 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 value.data |= ParseHex(start[1], &error) << 20;
412 value.data |= ParseHex(start[1], &error) << 16;
413 value.data |= ParseHex(start[2], &error) << 12;
414 value.data |= ParseHex(start[2], &error) << 8;
415 value.data |= ParseHex(start[3], &error) << 4;
416 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 } else if (len == 5) {
418 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 value.data |= ParseHex(start[1], &error) << 28;
420 value.data |= ParseHex(start[1], &error) << 24;
421 value.data |= ParseHex(start[2], &error) << 20;
422 value.data |= ParseHex(start[2], &error) << 16;
423 value.data |= ParseHex(start[3], &error) << 12;
424 value.data |= ParseHex(start[3], &error) << 8;
425 value.data |= ParseHex(start[4], &error) << 4;
426 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 } else if (len == 7) {
428 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
429 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 value.data |= ParseHex(start[1], &error) << 20;
431 value.data |= ParseHex(start[2], &error) << 16;
432 value.data |= ParseHex(start[3], &error) << 12;
433 value.data |= ParseHex(start[4], &error) << 8;
434 value.data |= ParseHex(start[5], &error) << 4;
435 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 } else if (len == 9) {
437 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 value.data |= ParseHex(start[1], &error) << 28;
439 value.data |= ParseHex(start[2], &error) << 24;
440 value.data |= ParseHex(start[3], &error) << 20;
441 value.data |= ParseHex(start[4], &error) << 16;
442 value.data |= ParseHex(start[5], &error) << 12;
443 value.data |= ParseHex(start[6], &error) << 8;
444 value.data |= ParseHex(start[7], &error) << 4;
445 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 } else {
447 return {};
448 }
449 return error ? std::unique_ptr<BinaryPrimitive>()
450 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700451}
452
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453Maybe<bool> ParseBool(const StringPiece& str) {
454 StringPiece trimmed_str(util::TrimWhitespace(str));
455 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
458 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 return Maybe<bool>(false);
460 }
461 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800462}
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464Maybe<uint32_t> ParseInt(const StringPiece& str) {
465 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 android::Res_value value;
467 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
468 return value.data;
469 }
470 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700471}
472
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
474 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700475
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 android::Res_value value;
478 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
479 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
480 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800481 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 return id;
483 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700484 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 }
486 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700487}
488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489Maybe<int> ParseSdkVersion(const StringPiece& str) {
490 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 android::Res_value value;
494 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
495 return static_cast<int>(value.data);
496 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700497
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
500 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 return entry.second;
502 }
503 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700504}
505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
507 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700508 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
509 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 }
511 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700512}
513
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700514std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
515 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
516 val ? 0xffffffffu : 0u);
517}
518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700520 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 android::Res_value value;
522 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
523 return {};
524 }
525 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526}
527
Shane Farmerd05b9132018-02-14 15:40:35 -0800528std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
529 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
530}
531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700533 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 android::Res_value value;
535 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
536 return {};
537 }
538 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700539}
540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700543 case android::Res_value::TYPE_NULL:
544 case android::Res_value::TYPE_REFERENCE:
545 case android::Res_value::TYPE_ATTRIBUTE:
546 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800547 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700549
550 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552
553 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700555
556 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700558
559 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700561
562 case android::Res_value::TYPE_INT_DEC:
563 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 return android::ResTable_map::TYPE_INTEGER |
565 android::ResTable_map::TYPE_ENUM |
566 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700567
568 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700570
571 case android::Res_value::TYPE_INT_COLOR_ARGB8:
572 case android::Res_value::TYPE_INT_COLOR_RGB8:
573 case android::Res_value::TYPE_INT_COLOR_ARGB4:
574 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700576
577 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 return 0;
579 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700580}
581
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582std::unique_ptr<Item> TryParseItemForAttribute(
583 const StringPiece& value, uint32_t type_mask,
584 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700585 using android::ResTable_map;
586
587 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700589 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700593 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 if (create && on_create_reference) {
596 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 return std::move(reference);
599 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700601 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700603 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 if (color) {
605 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700608
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700609 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700611 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 if (boolean) {
613 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700614 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700616
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700617 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700619 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 if (integer) {
621 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700622 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700625 const uint32_t float_mask =
626 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700629 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700631 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700634 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 }
636 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637}
638
639/**
640 * We successively try to parse the string as a resource type that the Attribute
641 * allows.
642 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700646 using android::ResTable_map;
647
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700649 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 if (value) {
651 return value;
652 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700653
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700654 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700656 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 if (enum_value) {
658 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700659 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700661
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700662 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700664 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 if (flag_value) {
666 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700667 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 }
669 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700670}
671
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700672std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 out << "res/" << res_file.name.type;
675 if (res_file.config != ConfigDescription{}) {
676 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 }
678 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800679
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
681 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800687}
688
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700689std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
690 const android::ResStringPool& src_pool,
691 const android::Res_value& res_value,
692 StringPool* dst_pool) {
693 if (type == ResourceType::kId) {
694 return util::make_unique<Id>();
695 }
696
697 const uint32_t data = util::DeviceToHost32(res_value.data);
698 switch (res_value.dataType) {
699 case android::Res_value::TYPE_STRING: {
700 const std::string str = util::GetString(src_pool, data);
701 const android::ResStringPool_span* spans = src_pool.styleAt(data);
702
703 // Check if the string has a valid style associated with it.
704 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
705 StyleString style_str = {str};
706 while (spans->name.index != android::ResStringPool_span::END) {
707 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
708 spans->firstChar, spans->lastChar});
709 spans++;
710 }
711 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700712 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700713 } else {
714 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
715 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700716 std::unique_ptr<FileReference> file_ref =
717 util::make_unique<FileReference>(dst_pool->MakeRef(
718 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000719 if (type == ResourceType::kRaw) {
720 file_ref->type = ResourceFile::Type::kUnknown;
721 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700722 file_ref->type = ResourceFile::Type::kBinaryXml;
723 } else if (util::EndsWith(*file_ref->path, ".png")) {
724 file_ref->type = ResourceFile::Type::kPng;
725 }
726 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700727 }
728
729 // There are no styles associated with this string, so treat it as a simple string.
730 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
731 }
732 } break;
733
734 case android::Res_value::TYPE_REFERENCE:
735 case android::Res_value::TYPE_ATTRIBUTE:
736 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
737 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
738 Reference::Type ref_type = Reference::Type::kResource;
739 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
740 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
741 ref_type = Reference::Type::kAttribute;
742 }
743
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700744 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700745 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700746 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700747 }
748
749 // This is a normal reference.
750 return util::make_unique<Reference>(data, ref_type);
751 } break;
752 }
753
754 // Treat this as a raw binary primitive.
755 return util::make_unique<BinaryPrimitive>(res_value);
756}
757
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800758// Converts the codepoint to UTF-8 and appends it to the string.
759static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
760 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
761 if (len < 0) {
762 return false;
763 }
764
765 const size_t start_append_pos = output->size();
766
767 // Make room for the next character.
768 output->resize(output->size() + len);
769
770 char* dst = &*(output->begin() + start_append_pos);
771 utf32_to_utf8(&codepoint, 1, dst, len + 1);
772 return true;
773}
774
775// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
776// Unicode codepoint represented by the escape sequence to the string.
777static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
778 char32_t code = 0;
779 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
780 char32_t codepoint = iter->Next();
781 char32_t a;
782 if (codepoint >= U'0' && codepoint <= U'9') {
783 a = codepoint - U'0';
784 } else if (codepoint >= U'a' && codepoint <= U'f') {
785 a = codepoint - U'a' + 10;
786 } else if (codepoint >= U'A' && codepoint <= U'F') {
787 a = codepoint - U'A' + 10;
788 } else {
789 return {};
790 }
791 code = (code << 4) | a;
792 }
793 return AppendCodepointToUtf8String(code, output);
794}
795
796StringBuilder::StringBuilder(bool preserve_spaces)
797 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
798}
799
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700800StringBuilder& StringBuilder::AppendText(const std::string& text, bool preserve_spaces) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800801 if (!error_.empty()) {
802 return *this;
803 }
804
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700805 // Enable preserving spaces if it is enabled for this append or the StringBuilder was constructed
806 // to preserve spaces
807 preserve_spaces = (preserve_spaces) ? preserve_spaces : preserve_spaces_;
808
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800809 const size_t previous_len = xml_string_.text.size();
810 Utf8Iterator iter(text);
811 while (iter.HasNext()) {
812 char32_t codepoint = iter.Next();
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700813 if (!preserve_spaces && !quote_ && iswspace(codepoint)) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800814 if (!last_codepoint_was_space_) {
815 // Emit a space if it's the first.
816 xml_string_.text += ' ';
817 last_codepoint_was_space_ = true;
818 }
819
820 // Keep eating spaces.
821 continue;
822 }
823
824 // This is not a space.
825 last_codepoint_was_space_ = false;
826
827 if (codepoint == U'\\') {
828 if (iter.HasNext()) {
829 codepoint = iter.Next();
830 switch (codepoint) {
831 case U't':
832 xml_string_.text += '\t';
833 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800834 case U'n':
835 xml_string_.text += '\n';
836 break;
837
838 case U'#':
839 case U'@':
840 case U'?':
841 case U'"':
842 case U'\'':
843 case U'\\':
844 xml_string_.text += static_cast<char>(codepoint);
845 break;
846
847 case U'u':
848 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
849 error_ =
850 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
851 return *this;
852 }
853 break;
854
855 default:
856 // Ignore the escape character and just include the codepoint.
857 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
858 break;
859 }
860 }
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700861 } else if (!preserve_spaces && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800862 // Only toggle the quote state when we are not preserving spaces.
863 quote_ = !quote_;
864
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700865 } else if (!preserve_spaces && !quote_ && codepoint == U'\'') {
866 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800867 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
868 return *this;
869
870 } else {
871 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
872 }
873 }
874
875 // Accumulate the added string's UTF-16 length.
876 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
877 const size_t utf8_length = xml_string_.text.size();
878 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
879 if (len < 0) {
880 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
881 return *this;
882 }
883
884 utf16_len_ += static_cast<uint32_t>(len);
885 return *this;
886}
887
888StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
889 if (!error_.empty()) {
890 return 0u;
891 }
892
893 // When we start a span, all state associated with whitespace truncation and quotation is ended.
894 ResetTextState();
895 Span span;
896 span.name = name;
897 span.first_char = span.last_char = utf16_len_;
898 xml_string_.spans.push_back(std::move(span));
899 return xml_string_.spans.size() - 1;
900}
901
902void StringBuilder::EndSpan(SpanHandle handle) {
903 if (!error_.empty()) {
904 return;
905 }
906
907 // When we end a span, all state associated with whitespace truncation and quotation is ended.
908 ResetTextState();
909 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
910}
911
912StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
913 if (!error_.empty()) {
914 return 0u;
915 }
916
917 UntranslatableSection section;
918 section.start = section.end = xml_string_.text.size();
919 xml_string_.untranslatable_sections.push_back(section);
920 return xml_string_.untranslatable_sections.size() - 1;
921}
922
923void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
924 if (!error_.empty()) {
925 return;
926 }
927 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
928}
929
930FlattenedXmlString StringBuilder::GetFlattenedString() const {
931 return xml_string_;
932}
933
934std::string StringBuilder::to_string() const {
935 return xml_string_.text;
936}
937
938StringBuilder::operator bool() const {
939 return error_.empty();
940}
941
942std::string StringBuilder::GetError() const {
943 return error_;
944}
945
946void StringBuilder::ResetTextState() {
947 quote_ = preserve_spaces_;
948 last_codepoint_was_space_ = false;
949}
950
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951} // namespace ResourceUtils
952} // namespace aapt