blob: dbe5ac5adb983e0d2f72cc496621102f87f439ba [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;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020034using ::android::ConfigDescription;
Adam Lesinski46708052017-09-29 14:49:15 -070035using ::android::StringPiece;
36using ::android::StringPiece16;
Adam Lesinski2eed52e2018-02-21 15:55:58 -080037using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080038
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039namespace aapt {
40namespace ResourceUtils {
41
Ryan Mitchell0ce89732018-10-03 09:20:57 -070042constexpr int32_t kNonBreakingSpace = 0xa0;
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044Maybe<ResourceName> ToResourceName(
45 const android::ResTable::resource_name& name_in) {
46 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
82 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 if (str.empty()) {
84 return false;
85 }
86
87 size_t offset = 0;
88 bool priv = false;
89 if (str.data()[0] == '*') {
90 priv = true;
91 offset = 1;
92 }
93
94 StringPiece package;
95 StringPiece type;
96 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -080097 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
98 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 return false;
100 }
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 const ResourceType* parsed_type = ParseResourceType(type);
103 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 return false;
105 }
106
107 if (entry.empty()) {
108 return false;
109 }
110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 if (out_ref) {
112 out_ref->package = package;
113 out_ref->type = *parsed_type;
114 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 if (out_private) {
118 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
120 return true;
121}
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
124 bool* out_create, bool* out_private) {
125 StringPiece trimmed_str(util::TrimWhitespace(str));
126 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 return false;
128 }
129
130 bool create = false;
131 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 create = true;
136 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800137 }
138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 if (!ParseResourceName(
141 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 &priv)) {
143 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800144 }
145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 if (create && priv) {
147 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800148 }
149
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 if (create && name.type != ResourceType::kId) {
151 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800152 }
153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (out_ref) {
155 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 if (out_create) {
159 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800160 }
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 if (out_private) {
163 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800164 }
165 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 }
167 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168}
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170bool IsReference(const StringPiece& str) {
171 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800172}
173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
175 StringPiece trimmed_str(util::TrimWhitespace(str));
176 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 }
179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 StringPiece package;
182 StringPiece type;
183 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800184 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
185 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 return false;
187 }
188
189 if (!type.empty() && type != "attr") {
190 return false;
191 }
192
193 if (entry.empty()) {
194 return false;
195 }
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (out_ref) {
198 out_ref->package = package;
199 out_ref->type = ResourceType::kAttr;
200 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 }
202 return true;
203 }
204 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205}
206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207bool IsAttributeReference(const StringPiece& str) {
208 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800209}
210
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211/*
212 * Style parent's are a bit different. We accept the following formats:
213 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800214 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800215 * ?[[*]package:]style/<entry>
216 * <[*]package>:[style/]<entry>
217 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
220 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 if (str.empty()) {
222 return {};
223 }
224
225 StringPiece name = str;
226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 bool has_leading_identifiers = false;
228 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229
230 // Skip over these identifiers. A style's parent is a normal reference.
231 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 name = name.substr(1, name.size() - 1);
234 }
235
236 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 name = name.substr(1, name.size() - 1);
239 }
240
241 ResourceNameRef ref;
242 ref.type = ResourceType::kStyle;
243
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800245 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 const ResourceType* parsed_type = ParseResourceType(type_str);
249 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 err << "invalid resource type '" << type_str << "' for parent of style";
252 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 std::stringstream err;
259 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 return {};
262 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267}
268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
270 StringPiece trimmed_str = util::TrimWhitespace(str);
271 const char* start = trimmed_str.data();
272 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 Reference ref;
276 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 start++;
279 p++;
280 }
281
282 StringPiece package;
283 StringPiece name;
284 while (p != end) {
285 if (*p == ':') {
286 package = StringPiece(start, p - start);
287 name = StringPiece(p + 1, end - (p + 1));
288 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700289 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 p++;
291 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700292
Adam Lesinskid5083f62017-01-16 15:07:21 -0800293 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700295}
296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
298 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 bool private_ref = false;
301 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return value;
305 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 if (ParseAttributeReference(str, &ref)) {
308 if (out_create) {
309 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700310 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
312 }
313 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314}
315
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700316std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
317 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700319 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700321 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700323 return {};
324}
325
326std::unique_ptr<Reference> MakeNull() {
327 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
328 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
329 return util::make_unique<Reference>();
330}
331
332std::unique_ptr<BinaryPrimitive> MakeEmpty() {
333 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
334 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335}
336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700338 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 StringPiece trimmed_str(util::TrimWhitespace(str));
340 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 // Enum symbols are stored as @package:id/symbol resources,
342 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
344 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345 android::Res_value value = {};
346 value.dataType = android::Res_value::TYPE_INT_DEC;
347 value.data = symbol.value;
348 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700349 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 }
351 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700352}
353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700355 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 android::Res_value flags = {};
357 flags.dataType = android::Res_value::TYPE_INT_HEX;
358 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700362 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 }
364
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 for (StringPiece part : util::Tokenize(str, '|')) {
366 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 bool flag_set = false;
369 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 // Flag symbols are stored as @package:id/symbol resources,
371 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 const ResourceName& flag_symbol_resource_name =
373 symbol.symbol.name.value();
374 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 break;
378 }
379 }
380
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 return {};
383 }
384 }
385 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700386}
387
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 if (c >= '0' && c <= '9') {
390 return c - '0';
391 } else if (c >= 'a' && c <= 'f') {
392 return c - 'a' + 0xa;
393 } else if (c >= 'A' && c <= 'F') {
394 return c - 'A' + 0xa;
395 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 return 0xffffffffu;
398 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399}
400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
402 StringPiece color_str(util::TrimWhitespace(str));
403 const char* start = color_str.data();
404 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 if (len == 0 || start[0] != '#') {
406 return {};
407 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700408
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 android::Res_value value = {};
410 bool error = false;
411 if (len == 4) {
412 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
413 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 value.data |= ParseHex(start[1], &error) << 20;
415 value.data |= ParseHex(start[1], &error) << 16;
416 value.data |= ParseHex(start[2], &error) << 12;
417 value.data |= ParseHex(start[2], &error) << 8;
418 value.data |= ParseHex(start[3], &error) << 4;
419 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 } else if (len == 5) {
421 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 value.data |= ParseHex(start[1], &error) << 28;
423 value.data |= ParseHex(start[1], &error) << 24;
424 value.data |= ParseHex(start[2], &error) << 20;
425 value.data |= ParseHex(start[2], &error) << 16;
426 value.data |= ParseHex(start[3], &error) << 12;
427 value.data |= ParseHex(start[3], &error) << 8;
428 value.data |= ParseHex(start[4], &error) << 4;
429 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 } else if (len == 7) {
431 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
432 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 value.data |= ParseHex(start[1], &error) << 20;
434 value.data |= ParseHex(start[2], &error) << 16;
435 value.data |= ParseHex(start[3], &error) << 12;
436 value.data |= ParseHex(start[4], &error) << 8;
437 value.data |= ParseHex(start[5], &error) << 4;
438 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 } else if (len == 9) {
440 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 value.data |= ParseHex(start[1], &error) << 28;
442 value.data |= ParseHex(start[2], &error) << 24;
443 value.data |= ParseHex(start[3], &error) << 20;
444 value.data |= ParseHex(start[4], &error) << 16;
445 value.data |= ParseHex(start[5], &error) << 12;
446 value.data |= ParseHex(start[6], &error) << 8;
447 value.data |= ParseHex(start[7], &error) << 4;
448 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 } else {
450 return {};
451 }
452 return error ? std::unique_ptr<BinaryPrimitive>()
453 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454}
455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456Maybe<bool> ParseBool(const StringPiece& str) {
457 StringPiece trimmed_str(util::TrimWhitespace(str));
458 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
461 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 return Maybe<bool>(false);
463 }
464 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800465}
466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467Maybe<uint32_t> ParseInt(const StringPiece& str) {
468 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 android::Res_value value;
470 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
471 return value.data;
472 }
473 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700474}
475
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
477 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 android::Res_value value;
481 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
482 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
483 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800484 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 return id;
486 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700487 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 }
489 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700490}
491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492Maybe<int> ParseSdkVersion(const StringPiece& str) {
493 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 android::Res_value value;
497 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
498 return static_cast<int>(value.data);
499 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700500
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
503 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 return entry.second;
505 }
506 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700507}
508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
510 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700511 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
512 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 }
514 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700515}
516
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700517std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
518 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
519 val ? 0xffffffffu : 0u);
520}
521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700523 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 android::Res_value value;
525 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
526 return {};
527 }
528 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700529}
530
Shane Farmerd05b9132018-02-14 15:40:35 -0800531std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
532 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
533}
534
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700536 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 android::Res_value value;
538 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
539 return {};
540 }
541 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700542}
543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700546 case android::Res_value::TYPE_NULL:
547 case android::Res_value::TYPE_REFERENCE:
548 case android::Res_value::TYPE_ATTRIBUTE:
549 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800550 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552
553 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700555
556 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700558
559 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700561
562 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700564
565 case android::Res_value::TYPE_INT_DEC:
566 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 return android::ResTable_map::TYPE_INTEGER |
568 android::ResTable_map::TYPE_ENUM |
569 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700570
571 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700573
574 case android::Res_value::TYPE_INT_COLOR_ARGB8:
575 case android::Res_value::TYPE_INT_COLOR_RGB8:
576 case android::Res_value::TYPE_INT_COLOR_ARGB4:
577 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700579
580 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 return 0;
582 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700583}
584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585std::unique_ptr<Item> TryParseItemForAttribute(
586 const StringPiece& value, uint32_t type_mask,
587 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700588 using android::ResTable_map;
589
590 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700592 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700594
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700596 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 if (create && on_create_reference) {
599 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 return std::move(reference);
602 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700604 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700606 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 if (color) {
608 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700611
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700612 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700614 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 if (boolean) {
616 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700619
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700620 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700622 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 if (integer) {
624 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700625 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700627
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700628 const uint32_t float_mask =
629 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700632 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700634 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 }
639 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700640}
641
642/**
643 * We successively try to parse the string as a resource type that the Attribute
644 * allows.
645 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700649 using android::ResTable_map;
650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700652 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 if (value) {
654 return value;
655 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700656
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700657 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700659 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 if (enum_value) {
661 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700662 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700664
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700665 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700667 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 if (flag_value) {
669 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700670 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
672 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700673}
674
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700675std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 out << "res/" << res_file.name.type;
678 if (res_file.config != ConfigDescription{}) {
679 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 }
681 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
684 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800690}
691
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700692std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
693 const android::ResStringPool& src_pool,
694 const android::Res_value& res_value,
695 StringPool* dst_pool) {
696 if (type == ResourceType::kId) {
697 return util::make_unique<Id>();
698 }
699
700 const uint32_t data = util::DeviceToHost32(res_value.data);
701 switch (res_value.dataType) {
702 case android::Res_value::TYPE_STRING: {
703 const std::string str = util::GetString(src_pool, data);
704 const android::ResStringPool_span* spans = src_pool.styleAt(data);
705
706 // Check if the string has a valid style associated with it.
707 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
708 StyleString style_str = {str};
709 while (spans->name.index != android::ResStringPool_span::END) {
710 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
711 spans->firstChar, spans->lastChar});
712 spans++;
713 }
714 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700715 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700716 } else {
717 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
718 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700719 std::unique_ptr<FileReference> file_ref =
720 util::make_unique<FileReference>(dst_pool->MakeRef(
721 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000722 if (type == ResourceType::kRaw) {
723 file_ref->type = ResourceFile::Type::kUnknown;
724 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700725 file_ref->type = ResourceFile::Type::kBinaryXml;
726 } else if (util::EndsWith(*file_ref->path, ".png")) {
727 file_ref->type = ResourceFile::Type::kPng;
728 }
729 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700730 }
731
732 // There are no styles associated with this string, so treat it as a simple string.
733 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
734 }
735 } break;
736
737 case android::Res_value::TYPE_REFERENCE:
738 case android::Res_value::TYPE_ATTRIBUTE:
739 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
740 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
741 Reference::Type ref_type = Reference::Type::kResource;
742 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
743 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
744 ref_type = Reference::Type::kAttribute;
745 }
746
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700747 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700748 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700749 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700750 }
751
752 // This is a normal reference.
753 return util::make_unique<Reference>(data, ref_type);
754 } break;
755 }
756
757 // Treat this as a raw binary primitive.
758 return util::make_unique<BinaryPrimitive>(res_value);
759}
760
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800761// Converts the codepoint to UTF-8 and appends it to the string.
762static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
763 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
764 if (len < 0) {
765 return false;
766 }
767
768 const size_t start_append_pos = output->size();
769
770 // Make room for the next character.
771 output->resize(output->size() + len);
772
773 char* dst = &*(output->begin() + start_append_pos);
774 utf32_to_utf8(&codepoint, 1, dst, len + 1);
775 return true;
776}
777
778// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
779// Unicode codepoint represented by the escape sequence to the string.
780static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
781 char32_t code = 0;
782 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
783 char32_t codepoint = iter->Next();
784 char32_t a;
785 if (codepoint >= U'0' && codepoint <= U'9') {
786 a = codepoint - U'0';
787 } else if (codepoint >= U'a' && codepoint <= U'f') {
788 a = codepoint - U'a' + 10;
789 } else if (codepoint >= U'A' && codepoint <= U'F') {
790 a = codepoint - U'A' + 10;
791 } else {
792 return {};
793 }
794 code = (code << 4) | a;
795 }
796 return AppendCodepointToUtf8String(code, output);
797}
798
799StringBuilder::StringBuilder(bool preserve_spaces)
800 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
801}
802
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700803StringBuilder& StringBuilder::AppendText(const std::string& text, bool preserve_spaces) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800804 if (!error_.empty()) {
805 return *this;
806 }
807
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700808 // Enable preserving spaces if it is enabled for this append or the StringBuilder was constructed
809 // to preserve spaces
810 preserve_spaces = (preserve_spaces) ? preserve_spaces : preserve_spaces_;
811
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800812 const size_t previous_len = xml_string_.text.size();
813 Utf8Iterator iter(text);
814 while (iter.HasNext()) {
815 char32_t codepoint = iter.Next();
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700816 if (!preserve_spaces && !quote_ && codepoint != kNonBreakingSpace && iswspace(codepoint)) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800817 if (!last_codepoint_was_space_) {
818 // Emit a space if it's the first.
819 xml_string_.text += ' ';
820 last_codepoint_was_space_ = true;
821 }
822
823 // Keep eating spaces.
824 continue;
825 }
826
827 // This is not a space.
828 last_codepoint_was_space_ = false;
829
830 if (codepoint == U'\\') {
831 if (iter.HasNext()) {
832 codepoint = iter.Next();
833 switch (codepoint) {
834 case U't':
835 xml_string_.text += '\t';
836 break;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800837 case U'n':
838 xml_string_.text += '\n';
839 break;
840
841 case U'#':
842 case U'@':
843 case U'?':
844 case U'"':
845 case U'\'':
846 case U'\\':
847 xml_string_.text += static_cast<char>(codepoint);
848 break;
849
850 case U'u':
851 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
852 error_ =
853 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
854 return *this;
855 }
856 break;
857
858 default:
859 // Ignore the escape character and just include the codepoint.
860 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
861 break;
862 }
863 }
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700864 } else if (!preserve_spaces && codepoint == U'"') {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800865 // Only toggle the quote state when we are not preserving spaces.
866 quote_ = !quote_;
867
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700868 } else if (!preserve_spaces && !quote_ && codepoint == U'\'') {
869 // This should be escaped when we are not preserving spaces
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800870 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
871 return *this;
872
873 } else {
874 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
875 }
876 }
877
878 // Accumulate the added string's UTF-16 length.
879 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
880 const size_t utf8_length = xml_string_.text.size();
881 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
882 if (len < 0) {
883 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
884 return *this;
885 }
886
887 utf16_len_ += static_cast<uint32_t>(len);
888 return *this;
889}
890
891StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
892 if (!error_.empty()) {
893 return 0u;
894 }
895
896 // When we start a span, all state associated with whitespace truncation and quotation is ended.
897 ResetTextState();
898 Span span;
899 span.name = name;
900 span.first_char = span.last_char = utf16_len_;
901 xml_string_.spans.push_back(std::move(span));
902 return xml_string_.spans.size() - 1;
903}
904
905void StringBuilder::EndSpan(SpanHandle handle) {
906 if (!error_.empty()) {
907 return;
908 }
909
910 // When we end a span, all state associated with whitespace truncation and quotation is ended.
911 ResetTextState();
912 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
913}
914
915StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
916 if (!error_.empty()) {
917 return 0u;
918 }
919
920 UntranslatableSection section;
921 section.start = section.end = xml_string_.text.size();
922 xml_string_.untranslatable_sections.push_back(section);
923 return xml_string_.untranslatable_sections.size() - 1;
924}
925
926void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
927 if (!error_.empty()) {
928 return;
929 }
930 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
931}
932
933FlattenedXmlString StringBuilder::GetFlattenedString() const {
934 return xml_string_;
935}
936
937std::string StringBuilder::to_string() const {
938 return xml_string_.text;
939}
940
941StringBuilder::operator bool() const {
942 return error_.empty();
943}
944
945std::string StringBuilder::GetError() const {
946 return error_;
947}
948
949void StringBuilder::ResetTextState() {
950 quote_ = preserve_spaces_;
951 last_codepoint_was_space_ = false;
952}
953
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954} // namespace ResourceUtils
955} // namespace aapt