blob: 8fc3d6580165242b45dd3f0d96f912aef5164817 [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::IsWhitespace;
34using ::aapt::text::Utf8Iterator;
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042Maybe<ResourceName> ToResourceName(
43 const android::ResTable::resource_name& name_in) {
44 ResourceName name_out;
45 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return {};
47 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 name_out.package =
50 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 if (name_in.type) {
54 type = ParseResourceType(
55 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
56 } else if (name_in.type8) {
57 type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 } else {
59 return {};
60 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 if (!type) {
63 return {};
64 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 if (name_in.name) {
69 name_out.entry =
70 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
71 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080072 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 } else {
74 return {};
75 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070077}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
80 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 if (str.empty()) {
82 return false;
83 }
84
85 size_t offset = 0;
86 bool priv = false;
87 if (str.data()[0] == '*') {
88 priv = true;
89 offset = 1;
90 }
91
92 StringPiece package;
93 StringPiece type;
94 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -080095 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
96 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 return false;
98 }
99
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 const ResourceType* parsed_type = ParseResourceType(type);
101 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 return false;
103 }
104
105 if (entry.empty()) {
106 return false;
107 }
108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 if (out_ref) {
110 out_ref->package = package;
111 out_ref->type = *parsed_type;
112 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 }
114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 if (out_private) {
116 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
118 return true;
119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
122 bool* out_create, bool* out_private) {
123 StringPiece trimmed_str(util::TrimWhitespace(str));
124 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 return false;
126 }
127
128 bool create = false;
129 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 create = true;
134 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800135 }
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 if (!ParseResourceName(
139 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 &priv)) {
141 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800142 }
143
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 if (create && priv) {
145 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800146 }
147
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 if (create && name.type != ResourceType::kId) {
149 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800150 }
151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 if (out_ref) {
153 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 if (out_create) {
157 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800158 }
159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 if (out_private) {
161 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800162 }
163 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 }
165 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166}
167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168bool IsReference(const StringPiece& str) {
169 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800170}
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
173 StringPiece trimmed_str(util::TrimWhitespace(str));
174 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 }
177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 StringPiece package;
180 StringPiece type;
181 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800182 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
183 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 return false;
185 }
186
187 if (!type.empty() && type != "attr") {
188 return false;
189 }
190
191 if (entry.empty()) {
192 return false;
193 }
194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 if (out_ref) {
196 out_ref->package = package;
197 out_ref->type = ResourceType::kAttr;
198 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
200 return true;
201 }
202 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203}
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205bool IsAttributeReference(const StringPiece& str) {
206 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800207}
208
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209/*
210 * Style parent's are a bit different. We accept the following formats:
211 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800212 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800213 * ?[[*]package:]style/<entry>
214 * <[*]package>:[style/]<entry>
215 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
218 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 if (str.empty()) {
220 return {};
221 }
222
223 StringPiece name = str;
224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 bool has_leading_identifiers = false;
226 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227
228 // Skip over these identifiers. A style's parent is a normal reference.
229 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 name = name.substr(1, name.size() - 1);
232 }
233
234 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 name = name.substr(1, name.size() - 1);
237 }
238
239 ResourceNameRef ref;
240 ref.type = ResourceType::kStyle;
241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800243 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 const ResourceType* parsed_type = ParseResourceType(type_str);
247 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 err << "invalid resource type '" << type_str << "' for parent of style";
250 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 std::stringstream err;
257 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 return {};
260 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265}
266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
268 StringPiece trimmed_str = util::TrimWhitespace(str);
269 const char* start = trimmed_str.data();
270 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 Reference ref;
274 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 start++;
277 p++;
278 }
279
280 StringPiece package;
281 StringPiece name;
282 while (p != end) {
283 if (*p == ':') {
284 package = StringPiece(start, p - start);
285 name = StringPiece(p + 1, end - (p + 1));
286 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700287 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 p++;
289 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700290
Adam Lesinskid5083f62017-01-16 15:07:21 -0800291 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700293}
294
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
296 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 bool private_ref = false;
299 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 return value;
303 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 if (ParseAttributeReference(str, &ref)) {
306 if (out_create) {
307 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700308 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
310 }
311 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700312}
313
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700314std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
315 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700317 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700319 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700321 return {};
322}
323
324std::unique_ptr<Reference> MakeNull() {
325 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
326 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
327 return util::make_unique<Reference>();
328}
329
330std::unique_ptr<BinaryPrimitive> MakeEmpty() {
331 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
332 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700333}
334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700336 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 StringPiece trimmed_str(util::TrimWhitespace(str));
338 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 // Enum symbols are stored as @package:id/symbol resources,
340 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
342 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 android::Res_value value = {};
344 value.dataType = android::Res_value::TYPE_INT_DEC;
345 value.data = symbol.value;
346 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700347 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 }
349 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350}
351
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700353 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 android::Res_value flags = {};
355 flags.dataType = android::Res_value::TYPE_INT_HEX;
356 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700360 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 }
362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 for (StringPiece part : util::Tokenize(str, '|')) {
364 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 bool flag_set = false;
367 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368 // Flag symbols are stored as @package:id/symbol resources,
369 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 const ResourceName& flag_symbol_resource_name =
371 symbol.symbol.name.value();
372 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 break;
376 }
377 }
378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 return {};
381 }
382 }
383 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700384}
385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 if (c >= '0' && c <= '9') {
388 return c - '0';
389 } else if (c >= 'a' && c <= 'f') {
390 return c - 'a' + 0xa;
391 } else if (c >= 'A' && c <= 'F') {
392 return c - 'A' + 0xa;
393 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 return 0xffffffffu;
396 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700397}
398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
400 StringPiece color_str(util::TrimWhitespace(str));
401 const char* start = color_str.data();
402 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 if (len == 0 || start[0] != '#') {
404 return {};
405 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 android::Res_value value = {};
408 bool error = false;
409 if (len == 4) {
410 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
411 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 value.data |= ParseHex(start[1], &error) << 20;
413 value.data |= ParseHex(start[1], &error) << 16;
414 value.data |= ParseHex(start[2], &error) << 12;
415 value.data |= ParseHex(start[2], &error) << 8;
416 value.data |= ParseHex(start[3], &error) << 4;
417 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 } else if (len == 5) {
419 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 value.data |= ParseHex(start[1], &error) << 28;
421 value.data |= ParseHex(start[1], &error) << 24;
422 value.data |= ParseHex(start[2], &error) << 20;
423 value.data |= ParseHex(start[2], &error) << 16;
424 value.data |= ParseHex(start[3], &error) << 12;
425 value.data |= ParseHex(start[3], &error) << 8;
426 value.data |= ParseHex(start[4], &error) << 4;
427 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 } else if (len == 7) {
429 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
430 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 value.data |= ParseHex(start[1], &error) << 20;
432 value.data |= ParseHex(start[2], &error) << 16;
433 value.data |= ParseHex(start[3], &error) << 12;
434 value.data |= ParseHex(start[4], &error) << 8;
435 value.data |= ParseHex(start[5], &error) << 4;
436 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 } else if (len == 9) {
438 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 value.data |= ParseHex(start[1], &error) << 28;
440 value.data |= ParseHex(start[2], &error) << 24;
441 value.data |= ParseHex(start[3], &error) << 20;
442 value.data |= ParseHex(start[4], &error) << 16;
443 value.data |= ParseHex(start[5], &error) << 12;
444 value.data |= ParseHex(start[6], &error) << 8;
445 value.data |= ParseHex(start[7], &error) << 4;
446 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 } else {
448 return {};
449 }
450 return error ? std::unique_ptr<BinaryPrimitive>()
451 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700452}
453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454Maybe<bool> ParseBool(const StringPiece& str) {
455 StringPiece trimmed_str(util::TrimWhitespace(str));
456 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
459 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 return Maybe<bool>(false);
461 }
462 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800463}
464
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465Maybe<uint32_t> ParseInt(const StringPiece& str) {
466 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 android::Res_value value;
468 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
469 return value.data;
470 }
471 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700472}
473
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
475 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700476
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478 android::Res_value value;
479 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
480 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
481 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800482 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 return id;
484 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700485 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 }
487 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700488}
489
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490Maybe<int> ParseSdkVersion(const StringPiece& str) {
491 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 android::Res_value value;
495 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
496 return static_cast<int>(value.data);
497 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700498
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
501 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 return entry.second;
503 }
504 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700505}
506
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
508 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700509 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
510 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 }
512 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700513}
514
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700515std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
516 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
517 val ? 0xffffffffu : 0u);
518}
519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700521 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 android::Res_value value;
523 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
524 return {};
525 }
526 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700527}
528
Shane Farmerd05b9132018-02-14 15:40:35 -0800529std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
530 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
531}
532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700534 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 android::Res_value value;
536 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
537 return {};
538 }
539 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700540}
541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700544 case android::Res_value::TYPE_NULL:
545 case android::Res_value::TYPE_REFERENCE:
546 case android::Res_value::TYPE_ATTRIBUTE:
547 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800548 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550
551 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700553
554 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700556
557 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700559
560 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700562
563 case android::Res_value::TYPE_INT_DEC:
564 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 return android::ResTable_map::TYPE_INTEGER |
566 android::ResTable_map::TYPE_ENUM |
567 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700568
569 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700571
572 case android::Res_value::TYPE_INT_COLOR_ARGB8:
573 case android::Res_value::TYPE_INT_COLOR_RGB8:
574 case android::Res_value::TYPE_INT_COLOR_ARGB4:
575 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700577
578 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 return 0;
580 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700581}
582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583std::unique_ptr<Item> TryParseItemForAttribute(
584 const StringPiece& value, uint32_t type_mask,
585 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700586 using android::ResTable_map;
587
588 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700590 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700592
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700594 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 if (create && on_create_reference) {
597 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700598 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return std::move(reference);
600 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700602 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700604 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 if (color) {
606 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700607 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700610 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700612 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 if (boolean) {
614 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700615 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700618 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700620 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 if (integer) {
622 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700625
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700626 const uint32_t float_mask =
627 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700630 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700632 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 }
637 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638}
639
640/**
641 * We successively try to parse the string as a resource type that the Attribute
642 * allows.
643 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700647 using android::ResTable_map;
648
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700650 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 if (value) {
652 return value;
653 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700654
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700655 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700657 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 if (enum_value) {
659 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700660 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700662
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700663 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700665 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 if (flag_value) {
667 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700668 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
670 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700671}
672
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700673std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 out << "res/" << res_file.name.type;
676 if (res_file.config != ConfigDescription{}) {
677 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 }
679 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
682 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800688}
689
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700690std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
691 const android::ResStringPool& src_pool,
692 const android::Res_value& res_value,
693 StringPool* dst_pool) {
694 if (type == ResourceType::kId) {
695 return util::make_unique<Id>();
696 }
697
698 const uint32_t data = util::DeviceToHost32(res_value.data);
699 switch (res_value.dataType) {
700 case android::Res_value::TYPE_STRING: {
701 const std::string str = util::GetString(src_pool, data);
702 const android::ResStringPool_span* spans = src_pool.styleAt(data);
703
704 // Check if the string has a valid style associated with it.
705 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
706 StyleString style_str = {str};
707 while (spans->name.index != android::ResStringPool_span::END) {
708 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
709 spans->firstChar, spans->lastChar});
710 spans++;
711 }
712 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700713 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700714 } else {
715 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
716 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700717 std::unique_ptr<FileReference> file_ref =
718 util::make_unique<FileReference>(dst_pool->MakeRef(
719 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
Pierre Lecesne70fdf762017-11-27 19:29:42 +0000720 if (type == ResourceType::kRaw) {
721 file_ref->type = ResourceFile::Type::kUnknown;
722 } else if (util::EndsWith(*file_ref->path, ".xml")) {
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700723 file_ref->type = ResourceFile::Type::kBinaryXml;
724 } else if (util::EndsWith(*file_ref->path, ".png")) {
725 file_ref->type = ResourceFile::Type::kPng;
726 }
727 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700728 }
729
730 // There are no styles associated with this string, so treat it as a simple string.
731 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
732 }
733 } break;
734
735 case android::Res_value::TYPE_REFERENCE:
736 case android::Res_value::TYPE_ATTRIBUTE:
737 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
738 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
739 Reference::Type ref_type = Reference::Type::kResource;
740 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
741 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
742 ref_type = Reference::Type::kAttribute;
743 }
744
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700745 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700746 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700747 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700748 }
749
750 // This is a normal reference.
751 return util::make_unique<Reference>(data, ref_type);
752 } break;
753 }
754
755 // Treat this as a raw binary primitive.
756 return util::make_unique<BinaryPrimitive>(res_value);
757}
758
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800759// Converts the codepoint to UTF-8 and appends it to the string.
760static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
761 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
762 if (len < 0) {
763 return false;
764 }
765
766 const size_t start_append_pos = output->size();
767
768 // Make room for the next character.
769 output->resize(output->size() + len);
770
771 char* dst = &*(output->begin() + start_append_pos);
772 utf32_to_utf8(&codepoint, 1, dst, len + 1);
773 return true;
774}
775
776// Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the
777// Unicode codepoint represented by the escape sequence to the string.
778static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) {
779 char32_t code = 0;
780 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
781 char32_t codepoint = iter->Next();
782 char32_t a;
783 if (codepoint >= U'0' && codepoint <= U'9') {
784 a = codepoint - U'0';
785 } else if (codepoint >= U'a' && codepoint <= U'f') {
786 a = codepoint - U'a' + 10;
787 } else if (codepoint >= U'A' && codepoint <= U'F') {
788 a = codepoint - U'A' + 10;
789 } else {
790 return {};
791 }
792 code = (code << 4) | a;
793 }
794 return AppendCodepointToUtf8String(code, output);
795}
796
797StringBuilder::StringBuilder(bool preserve_spaces)
798 : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) {
799}
800
801StringBuilder& StringBuilder::AppendText(const std::string& text) {
802 if (!error_.empty()) {
803 return *this;
804 }
805
806 const size_t previous_len = xml_string_.text.size();
807 Utf8Iterator iter(text);
808 while (iter.HasNext()) {
809 char32_t codepoint = iter.Next();
810 if (!quote_ && text::IsWhitespace(codepoint)) {
811 if (!last_codepoint_was_space_) {
812 // Emit a space if it's the first.
813 xml_string_.text += ' ';
814 last_codepoint_was_space_ = true;
815 }
816
817 // Keep eating spaces.
818 continue;
819 }
820
821 // This is not a space.
822 last_codepoint_was_space_ = false;
823
824 if (codepoint == U'\\') {
825 if (iter.HasNext()) {
826 codepoint = iter.Next();
827 switch (codepoint) {
828 case U't':
829 xml_string_.text += '\t';
830 break;
831
832 case U'n':
833 xml_string_.text += '\n';
834 break;
835
836 case U'#':
837 case U'@':
838 case U'?':
839 case U'"':
840 case U'\'':
841 case U'\\':
842 xml_string_.text += static_cast<char>(codepoint);
843 break;
844
845 case U'u':
846 if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) {
847 error_ =
848 StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str());
849 return *this;
850 }
851 break;
852
853 default:
854 // Ignore the escape character and just include the codepoint.
855 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
856 break;
857 }
858 }
859 } else if (!preserve_spaces_ && codepoint == U'"') {
860 // Only toggle the quote state when we are not preserving spaces.
861 quote_ = !quote_;
862
863 } else if (!quote_ && codepoint == U'\'') {
864 // This should be escaped.
865 error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str());
866 return *this;
867
868 } else {
869 AppendCodepointToUtf8String(codepoint, &xml_string_.text);
870 }
871 }
872
873 // Accumulate the added string's UTF-16 length.
874 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str());
875 const size_t utf8_length = xml_string_.text.size();
876 ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len);
877 if (len < 0) {
878 error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len);
879 return *this;
880 }
881
882 utf16_len_ += static_cast<uint32_t>(len);
883 return *this;
884}
885
886StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) {
887 if (!error_.empty()) {
888 return 0u;
889 }
890
891 // When we start a span, all state associated with whitespace truncation and quotation is ended.
892 ResetTextState();
893 Span span;
894 span.name = name;
895 span.first_char = span.last_char = utf16_len_;
896 xml_string_.spans.push_back(std::move(span));
897 return xml_string_.spans.size() - 1;
898}
899
900void StringBuilder::EndSpan(SpanHandle handle) {
901 if (!error_.empty()) {
902 return;
903 }
904
905 // When we end a span, all state associated with whitespace truncation and quotation is ended.
906 ResetTextState();
907 xml_string_.spans[handle].last_char = utf16_len_ - 1u;
908}
909
910StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() {
911 if (!error_.empty()) {
912 return 0u;
913 }
914
915 UntranslatableSection section;
916 section.start = section.end = xml_string_.text.size();
917 xml_string_.untranslatable_sections.push_back(section);
918 return xml_string_.untranslatable_sections.size() - 1;
919}
920
921void StringBuilder::EndUntranslatable(UntranslatableHandle handle) {
922 if (!error_.empty()) {
923 return;
924 }
925 xml_string_.untranslatable_sections[handle].end = xml_string_.text.size();
926}
927
928FlattenedXmlString StringBuilder::GetFlattenedString() const {
929 return xml_string_;
930}
931
932std::string StringBuilder::to_string() const {
933 return xml_string_.text;
934}
935
936StringBuilder::operator bool() const {
937 return error_.empty();
938}
939
940std::string StringBuilder::GetError() const {
941 return error_;
942}
943
944void StringBuilder::ResetTextState() {
945 quote_ = preserve_spaces_;
946 last_codepoint_was_space_ = false;
947}
948
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949} // namespace ResourceUtils
950} // namespace aapt