blob: 24187d96fec537f3e1de5397ec9a6c55bb7cc8e1 [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
21#include "androidfw/ResourceTypes.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080022#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024#include "NameMangler.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070025#include "SdkConstants.h"
Adam Lesinski46708052017-09-29 14:49:15 -070026#include "format/binary/ResourceTypeExtensions.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080027#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "util/Util.h"
29
Adam Lesinski46708052017-09-29 14:49:15 -070030using ::android::StringPiece;
31using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace aapt {
34namespace ResourceUtils {
35
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036Maybe<ResourceName> ToResourceName(
37 const android::ResTable::resource_name& name_in) {
38 ResourceName name_out;
39 if (!name_in.package) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return {};
41 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 name_out.package =
44 util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen));
Adam Lesinskid0f116b2016-07-08 15:00:32 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 const ResourceType* type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 if (name_in.type) {
48 type = ParseResourceType(
49 util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)));
50 } else if (name_in.type8) {
51 type = ParseResourceType(StringPiece(name_in.type8, name_in.typeLen));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 } else {
53 return {};
54 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 if (!type) {
57 return {};
58 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070059
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 name_out.type = *type;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 if (name_in.name) {
63 name_out.entry =
64 util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen));
65 } else if (name_in.name8) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080066 name_out.entry.assign(name_in.name8, name_in.nameLen);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 } else {
68 return {};
69 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 return name_out;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref,
74 bool* out_private) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 if (str.empty()) {
76 return false;
77 }
78
79 size_t offset = 0;
80 bool priv = false;
81 if (str.data()[0] == '*') {
82 priv = true;
83 offset = 1;
84 }
85
86 StringPiece package;
87 StringPiece type;
88 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -080089 if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type,
90 &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return false;
92 }
93
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 const ResourceType* parsed_type = ParseResourceType(type);
95 if (!parsed_type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 return false;
97 }
98
99 if (entry.empty()) {
100 return false;
101 }
102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 if (out_ref) {
104 out_ref->package = package;
105 out_ref->type = *parsed_type;
106 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 if (out_private) {
110 *out_private = priv;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
112 return true;
113}
114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref,
116 bool* out_create, bool* out_private) {
117 StringPiece trimmed_str(util::TrimWhitespace(str));
118 if (trimmed_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 return false;
120 }
121
122 bool create = false;
123 bool priv = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 if (trimmed_str.data()[0] == '@') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 size_t offset = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 if (trimmed_str.data()[1] == '+') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 create = true;
128 offset += 1;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800129 }
130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 ResourceNameRef name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 if (!ParseResourceName(
133 trimmed_str.substr(offset, trimmed_str.size() - offset), &name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 &priv)) {
135 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800136 }
137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 if (create && priv) {
139 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800140 }
141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 if (create && name.type != ResourceType::kId) {
143 return false;
Adam Lesinski467f1712015-11-16 17:35:44 -0800144 }
145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 if (out_ref) {
147 *out_ref = name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 }
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 if (out_create) {
151 *out_create = create;
Adam Lesinski467f1712015-11-16 17:35:44 -0800152 }
153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (out_private) {
155 *out_private = priv;
Adam Lesinski467f1712015-11-16 17:35:44 -0800156 }
157 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
159 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160}
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162bool IsReference(const StringPiece& str) {
163 return ParseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800164}
165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) {
167 StringPiece trimmed_str(util::TrimWhitespace(str));
168 if (trimmed_str.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 }
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 if (*trimmed_str.data() == '?') {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 StringPiece package;
174 StringPiece type;
175 StringPiece entry;
Adam Lesinski929d6512017-01-16 19:11:19 -0800176 if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package,
177 &type, &entry)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 return false;
179 }
180
181 if (!type.empty() && type != "attr") {
182 return false;
183 }
184
185 if (entry.empty()) {
186 return false;
187 }
188
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 if (out_ref) {
190 out_ref->package = package;
191 out_ref->type = ResourceType::kAttr;
192 out_ref->entry = entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 }
194 return true;
195 }
196 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700197}
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199bool IsAttributeReference(const StringPiece& str) {
200 return ParseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800201}
202
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203/*
204 * Style parent's are a bit different. We accept the following formats:
205 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800206 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800207 * ?[[*]package:]style/<entry>
208 * <[*]package>:[style/]<entry>
209 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211Maybe<Reference> ParseStyleParentReference(const StringPiece& str,
212 std::string* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 if (str.empty()) {
214 return {};
215 }
216
217 StringPiece name = str;
218
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 bool has_leading_identifiers = false;
220 bool private_ref = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221
222 // Skip over these identifiers. A style's parent is a normal reference.
223 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 has_leading_identifiers = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 name = name.substr(1, name.size() - 1);
226 }
227
228 if (name.data()[0] == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 private_ref = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 name = name.substr(1, name.size() - 1);
231 }
232
233 ResourceNameRef ref;
234 ref.type = ResourceType::kStyle;
235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 StringPiece type_str;
Adam Lesinski929d6512017-01-16 19:11:19 -0800237 android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 if (!type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 // If we have a type, make sure it is a Style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 const ResourceType* parsed_type = ParseResourceType(type_str);
241 if (!parsed_type || *parsed_type != ResourceType::kStyle) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 std::stringstream err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 err << "invalid resource type '" << type_str << "' for parent of style";
244 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 std::stringstream err;
251 err << "invalid parent reference '" << str << "'";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 *out_error = err.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 return {};
254 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 Reference result(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 result.private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259}
260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261Maybe<Reference> ParseXmlAttributeName(const StringPiece& str) {
262 StringPiece trimmed_str = util::TrimWhitespace(str);
263 const char* start = trimmed_str.data();
264 const char* const end = start + trimmed_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 const char* p = start;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700266
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 Reference ref;
268 if (p != end && *p == '*') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 ref.private_reference = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 start++;
271 p++;
272 }
273
274 StringPiece package;
275 StringPiece name;
276 while (p != end) {
277 if (*p == ':') {
278 package = StringPiece(start, p - start);
279 name = StringPiece(p + 1, end - (p + 1));
280 break;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700281 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 p++;
283 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700284
Adam Lesinskid5083f62017-01-16 15:07:21 -0800285 ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 return Maybe<Reference>(std::move(ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700287}
288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289std::unique_ptr<Reference> TryParseReference(const StringPiece& str,
290 bool* out_create) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 bool private_ref = false;
293 if (ParseReference(str, &ref, out_create, &private_ref)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 value->private_reference = private_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return value;
297 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 if (ParseAttributeReference(str, &ref)) {
300 if (out_create) {
301 *out_create = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700302 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
304 }
305 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306}
307
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700308std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) {
309 const StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 if (trimmed_str == "@null") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700311 return MakeNull();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 } else if (trimmed_str == "@empty") {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700313 return MakeEmpty();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 }
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700315 return {};
316}
317
318std::unique_ptr<Reference> MakeNull() {
319 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
320 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
321 return util::make_unique<Reference>();
322}
323
324std::unique_ptr<BinaryPrimitive> MakeEmpty() {
325 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL,
326 android::Res_value::DATA_NULL_EMPTY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700327}
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700330 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 StringPiece trimmed_str(util::TrimWhitespace(str));
332 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 // Enum symbols are stored as @package:id/symbol resources,
334 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
336 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 android::Res_value value = {};
338 value.dataType = android::Res_value::TYPE_INT_DEC;
339 value.data = symbol.value;
340 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700341 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 }
343 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700344}
345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700347 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 android::Res_value flags = {};
349 flags.dataType = android::Res_value::TYPE_INT_HEX;
350 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800351
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700354 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 }
356
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 for (StringPiece part : util::Tokenize(str, '|')) {
358 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 bool flag_set = false;
361 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 // Flag symbols are stored as @package:id/symbol resources,
363 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 const ResourceName& flag_symbol_resource_name =
365 symbol.symbol.name.value();
366 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 break;
370 }
371 }
372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 return {};
375 }
376 }
377 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700378}
379
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 if (c >= '0' && c <= '9') {
382 return c - '0';
383 } else if (c >= 'a' && c <= 'f') {
384 return c - 'a' + 0xa;
385 } else if (c >= 'A' && c <= 'F') {
386 return c - 'A' + 0xa;
387 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 return 0xffffffffu;
390 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391}
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
394 StringPiece color_str(util::TrimWhitespace(str));
395 const char* start = color_str.data();
396 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 if (len == 0 || start[0] != '#') {
398 return {};
399 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700400
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 android::Res_value value = {};
402 bool error = false;
403 if (len == 4) {
404 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
405 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 value.data |= ParseHex(start[1], &error) << 20;
407 value.data |= ParseHex(start[1], &error) << 16;
408 value.data |= ParseHex(start[2], &error) << 12;
409 value.data |= ParseHex(start[2], &error) << 8;
410 value.data |= ParseHex(start[3], &error) << 4;
411 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 } else if (len == 5) {
413 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 value.data |= ParseHex(start[1], &error) << 28;
415 value.data |= ParseHex(start[1], &error) << 24;
416 value.data |= ParseHex(start[2], &error) << 20;
417 value.data |= ParseHex(start[2], &error) << 16;
418 value.data |= ParseHex(start[3], &error) << 12;
419 value.data |= ParseHex(start[3], &error) << 8;
420 value.data |= ParseHex(start[4], &error) << 4;
421 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 } else if (len == 7) {
423 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
424 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 value.data |= ParseHex(start[1], &error) << 20;
426 value.data |= ParseHex(start[2], &error) << 16;
427 value.data |= ParseHex(start[3], &error) << 12;
428 value.data |= ParseHex(start[4], &error) << 8;
429 value.data |= ParseHex(start[5], &error) << 4;
430 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 } else if (len == 9) {
432 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 value.data |= ParseHex(start[1], &error) << 28;
434 value.data |= ParseHex(start[2], &error) << 24;
435 value.data |= ParseHex(start[3], &error) << 20;
436 value.data |= ParseHex(start[4], &error) << 16;
437 value.data |= ParseHex(start[5], &error) << 12;
438 value.data |= ParseHex(start[6], &error) << 8;
439 value.data |= ParseHex(start[7], &error) << 4;
440 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 } else {
442 return {};
443 }
444 return error ? std::unique_ptr<BinaryPrimitive>()
445 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700446}
447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448Maybe<bool> ParseBool(const StringPiece& str) {
449 StringPiece trimmed_str(util::TrimWhitespace(str));
450 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
453 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 return Maybe<bool>(false);
455 }
456 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800457}
458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459Maybe<uint32_t> ParseInt(const StringPiece& str) {
460 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 android::Res_value value;
462 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
463 return value.data;
464 }
465 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700466}
467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
469 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700470
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 android::Res_value value;
473 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
474 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
475 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800476 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 return id;
478 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700479 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 }
481 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700482}
483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484Maybe<int> ParseSdkVersion(const StringPiece& str) {
485 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 android::Res_value value;
489 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
490 return static_cast<int>(value.data);
491 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700492
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
495 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 return entry.second;
497 }
498 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700499}
500
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
502 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700503 const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u;
504 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 }
506 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507}
508
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700509std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
510 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN,
511 val ? 0xffffffffu : 0u);
512}
513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700515 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 android::Res_value value;
517 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
518 return {};
519 }
520 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521}
522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
Adam Lesinski8a3bffe2017-06-27 12:27:43 -0700524 std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 android::Res_value value;
526 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
527 return {};
528 }
529 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700530}
531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700534 case android::Res_value::TYPE_NULL:
535 case android::Res_value::TYPE_REFERENCE:
536 case android::Res_value::TYPE_ATTRIBUTE:
537 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800538 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700540
541 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700543
544 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700546
547 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700549
550 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552
553 case android::Res_value::TYPE_INT_DEC:
554 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 return android::ResTable_map::TYPE_INTEGER |
556 android::ResTable_map::TYPE_ENUM |
557 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700558
559 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700561
562 case android::Res_value::TYPE_INT_COLOR_ARGB8:
563 case android::Res_value::TYPE_INT_COLOR_RGB8:
564 case android::Res_value::TYPE_INT_COLOR_ARGB4:
565 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700567
568 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 return 0;
570 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700571}
572
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573std::unique_ptr<Item> TryParseItemForAttribute(
574 const StringPiece& value, uint32_t type_mask,
575 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700576 using android::ResTable_map;
577
578 auto null_or_empty = TryParseNullOrEmpty(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 if (null_or_empty) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700580 return null_or_empty;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 bool create = false;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700584 auto reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 if (create && on_create_reference) {
587 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700588 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 return std::move(reference);
590 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700592 if (type_mask & ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 // Try parsing this as a color.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700594 auto color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 if (color) {
596 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700600 if (type_mask & ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 // Try parsing this as a boolean.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700602 auto boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 if (boolean) {
604 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700607
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700608 if (type_mask & ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 // Try parsing this as an integer.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700610 auto integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 if (integer) {
612 return std::move(integer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700615
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700616 const uint32_t float_mask =
617 ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 // Try parsing this as a float.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700620 auto floating_point = TryParseFloat(value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 if (floating_point) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700622 if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700625 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 }
627 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700628}
629
630/**
631 * We successively try to parse the string as a resource type that the Attribute
632 * allows.
633 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 const std::function<void(const ResourceName&)>& on_create_reference) {
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700637 using android::ResTable_map;
638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 const uint32_t type_mask = attr->type_mask;
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700640 auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 if (value) {
642 return value;
643 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700645 if (type_mask & ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 // Try parsing this as an enum.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700647 auto enum_value = TryParseEnumSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 if (enum_value) {
649 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700650 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700652
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700653 if (type_mask & ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 // Try parsing this as a flag.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700655 auto flag_value = TryParseFlagSymbol(attr, str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 if (flag_value) {
657 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700658 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 }
660 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700661}
662
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700663std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 out << "res/" << res_file.name.type;
666 if (res_file.config != ConfigDescription{}) {
667 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 }
669 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800670
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
672 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800678}
679
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700680std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
681 const android::ResStringPool& src_pool,
682 const android::Res_value& res_value,
683 StringPool* dst_pool) {
684 if (type == ResourceType::kId) {
685 return util::make_unique<Id>();
686 }
687
688 const uint32_t data = util::DeviceToHost32(res_value.data);
689 switch (res_value.dataType) {
690 case android::Res_value::TYPE_STRING: {
691 const std::string str = util::GetString(src_pool, data);
692 const android::ResStringPool_span* spans = src_pool.styleAt(data);
693
694 // Check if the string has a valid style associated with it.
695 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
696 StyleString style_str = {str};
697 while (spans->name.index != android::ResStringPool_span::END) {
698 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
699 spans->firstChar, spans->lastChar});
700 spans++;
701 }
702 return util::make_unique<StyledString>(dst_pool->MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700703 style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700704 } else {
705 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
706 // This must be a FileReference.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700707 std::unique_ptr<FileReference> file_ref =
708 util::make_unique<FileReference>(dst_pool->MakeRef(
709 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
710 if (util::EndsWith(*file_ref->path, ".xml")) {
711 file_ref->type = ResourceFile::Type::kBinaryXml;
712 } else if (util::EndsWith(*file_ref->path, ".png")) {
713 file_ref->type = ResourceFile::Type::kPng;
714 }
715 return std::move(file_ref);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700716 }
717
718 // There are no styles associated with this string, so treat it as a simple string.
719 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
720 }
721 } break;
722
723 case android::Res_value::TYPE_REFERENCE:
724 case android::Res_value::TYPE_ATTRIBUTE:
725 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
726 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
727 Reference::Type ref_type = Reference::Type::kResource;
728 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
729 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
730 ref_type = Reference::Type::kAttribute;
731 }
732
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700733 if (data == 0u) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700734 // A reference of 0, must be the magic @null reference.
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700735 return util::make_unique<Reference>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700736 }
737
738 // This is a normal reference.
739 return util::make_unique<Reference>(data, ref_type);
740 } break;
741 }
742
743 // Treat this as a raw binary primitive.
744 return util::make_unique<BinaryPrimitive>(res_value);
745}
746
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747} // namespace ResourceUtils
748} // namespace aapt