blob: 1bb7d9beee45f792449aaa03c60790b0eb579ecb [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 Lesinski467f1712015-11-16 17:35:44 -080026#include "flatten/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 Lesinskid5083f62017-01-16 15:07:21 -080030using android::StringPiece;
31using android::StringPiece16;
32
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 Lesinskice5e56e2016-10-21 17:56:45 -0700308std::unique_ptr<BinaryPrimitive> TryParseNullOrEmpty(const StringPiece& str) {
309 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 android::Res_value value = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 if (trimmed_str == "@null") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
313 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
314 value.dataType = android::Res_value::TYPE_REFERENCE;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 } else if (trimmed_str == "@empty") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 // TYPE_NULL with value of DATA_NULL_EMPTY is handled fine by the runtime.
317 value.dataType = android::Res_value::TYPE_NULL;
318 value.data = android::Res_value::DATA_NULL_EMPTY;
319 } else {
320 return {};
321 }
322 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700323}
324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700326 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 StringPiece trimmed_str(util::TrimWhitespace(str));
328 for (const Attribute::Symbol& symbol : enum_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 // Enum symbols are stored as @package:id/symbol resources,
330 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value();
332 if (trimmed_str == enum_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 android::Res_value value = {};
334 value.dataType = android::Res_value::TYPE_INT_DEC;
335 value.data = symbol.value;
336 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700337 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 }
339 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700340}
341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700343 const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 android::Res_value flags = {};
345 flags.dataType = android::Res_value::TYPE_INT_HEX;
346 flags.data = 0u;
Adam Lesinski52364f72016-01-11 13:10:24 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 if (util::TrimWhitespace(str).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 // Empty string is a valid flag (0).
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 }
352
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 for (StringPiece part : util::Tokenize(str, '|')) {
354 StringPiece trimmed_part = util::TrimWhitespace(part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 bool flag_set = false;
357 for (const Attribute::Symbol& symbol : flag_attr->symbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 // Flag symbols are stored as @package:id/symbol resources,
359 // so we need to match against the 'entry' part of the identifier.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 const ResourceName& flag_symbol_resource_name =
361 symbol.symbol.name.value();
362 if (trimmed_part == flag_symbol_resource_name.entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 flags.data |= symbol.value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 flag_set = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 break;
366 }
367 }
368
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 if (!flag_set) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 return {};
371 }
372 }
373 return util::make_unique<BinaryPrimitive>(flags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700374}
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376static uint32_t ParseHex(char c, bool* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 if (c >= '0' && c <= '9') {
378 return c - '0';
379 } else if (c >= 'a' && c <= 'f') {
380 return c - 'a' + 0xa;
381 } else if (c >= 'A' && c <= 'F') {
382 return c - 'A' + 0xa;
383 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 *out_error = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 return 0xffffffffu;
386 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700387}
388
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) {
390 StringPiece color_str(util::TrimWhitespace(str));
391 const char* start = color_str.data();
392 const size_t len = color_str.size();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 if (len == 0 || start[0] != '#') {
394 return {};
395 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700396
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 android::Res_value value = {};
398 bool error = false;
399 if (len == 4) {
400 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
401 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 value.data |= ParseHex(start[1], &error) << 20;
403 value.data |= ParseHex(start[1], &error) << 16;
404 value.data |= ParseHex(start[2], &error) << 12;
405 value.data |= ParseHex(start[2], &error) << 8;
406 value.data |= ParseHex(start[3], &error) << 4;
407 value.data |= ParseHex(start[3], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 } else if (len == 5) {
409 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 value.data |= ParseHex(start[1], &error) << 28;
411 value.data |= ParseHex(start[1], &error) << 24;
412 value.data |= ParseHex(start[2], &error) << 20;
413 value.data |= ParseHex(start[2], &error) << 16;
414 value.data |= ParseHex(start[3], &error) << 12;
415 value.data |= ParseHex(start[3], &error) << 8;
416 value.data |= ParseHex(start[4], &error) << 4;
417 value.data |= ParseHex(start[4], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 } else if (len == 7) {
419 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
420 value.data = 0xff000000u;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 value.data |= ParseHex(start[1], &error) << 20;
422 value.data |= ParseHex(start[2], &error) << 16;
423 value.data |= ParseHex(start[3], &error) << 12;
424 value.data |= ParseHex(start[4], &error) << 8;
425 value.data |= ParseHex(start[5], &error) << 4;
426 value.data |= ParseHex(start[6], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 } else if (len == 9) {
428 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 value.data |= ParseHex(start[1], &error) << 28;
430 value.data |= ParseHex(start[2], &error) << 24;
431 value.data |= ParseHex(start[3], &error) << 20;
432 value.data |= ParseHex(start[4], &error) << 16;
433 value.data |= ParseHex(start[5], &error) << 12;
434 value.data |= ParseHex(start[6], &error) << 8;
435 value.data |= ParseHex(start[7], &error) << 4;
436 value.data |= ParseHex(start[8], &error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 } else {
438 return {};
439 }
440 return error ? std::unique_ptr<BinaryPrimitive>()
441 : util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700442}
443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444Maybe<bool> ParseBool(const StringPiece& str) {
445 StringPiece trimmed_str(util::TrimWhitespace(str));
446 if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return Maybe<bool>(true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 } else if (trimmed_str == "false" || trimmed_str == "FALSE" ||
449 trimmed_str == "False") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 return Maybe<bool>(false);
451 }
452 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800453}
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455Maybe<uint32_t> ParseInt(const StringPiece& str) {
456 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 android::Res_value value;
458 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
459 return value.data;
460 }
461 return {};
Adam Lesinski36c73a52016-08-11 13:39:24 -0700462}
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464Maybe<ResourceId> ParseResourceId(const StringPiece& str) {
465 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 android::Res_value value;
469 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
470 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
471 ResourceId id(value.data);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800472 if (id.is_valid_dynamic()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 return id;
474 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700475 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 }
477 return {};
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700478}
479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480Maybe<int> ParseSdkVersion(const StringPiece& str) {
481 StringPiece trimmed_str(util::TrimWhitespace(str));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700482
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 std::u16string str16 = util::Utf8ToUtf16(trimmed_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 android::Res_value value;
485 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
486 return static_cast<int>(value.data);
487 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700488
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 // Try parsing the code name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 std::pair<StringPiece, int> entry = GetDevelopmentSdkCodeNameAndVersion();
491 if (entry.first == trimmed_str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 return entry.second;
493 }
494 return {};
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700495}
496
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) {
498 if (Maybe<bool> maybe_result = ParseBool(str)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 android::Res_value value = {};
500 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 if (maybe_result.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 value.data = 0xffffffffu;
504 } else {
505 value.data = 0;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800506 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 return util::make_unique<BinaryPrimitive>(value);
508 }
509 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700510}
511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
513 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 android::Res_value value;
515 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
516 return {};
517 }
518 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519}
520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
522 std::u16string str16 = util::Utf8ToUtf16(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 android::Res_value value;
524 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
525 return {};
526 }
527 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700528}
529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 switch (type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700532 case android::Res_value::TYPE_NULL:
533 case android::Res_value::TYPE_REFERENCE:
534 case android::Res_value::TYPE_ATTRIBUTE:
535 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800536 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700538
539 case android::Res_value::TYPE_STRING:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 return android::ResTable_map::TYPE_STRING;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541
542 case android::Res_value::TYPE_FLOAT:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700544
545 case android::Res_value::TYPE_DIMENSION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700547
548 case android::Res_value::TYPE_FRACTION:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550
551 case android::Res_value::TYPE_INT_DEC:
552 case android::Res_value::TYPE_INT_HEX:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 return android::ResTable_map::TYPE_INTEGER |
554 android::ResTable_map::TYPE_ENUM |
555 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700556
557 case android::Res_value::TYPE_INT_BOOLEAN:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700559
560 case android::Res_value::TYPE_INT_COLOR_ARGB8:
561 case android::Res_value::TYPE_INT_COLOR_RGB8:
562 case android::Res_value::TYPE_INT_COLOR_ARGB4:
563 case android::Res_value::TYPE_INT_COLOR_RGB4:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 return android::ResTable_map::TYPE_COLOR;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700565
566 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 return 0;
568 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700569}
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571std::unique_ptr<Item> TryParseItemForAttribute(
572 const StringPiece& value, uint32_t type_mask,
573 const std::function<void(const ResourceName&)>& on_create_reference) {
574 std::unique_ptr<BinaryPrimitive> null_or_empty = TryParseNullOrEmpty(value);
575 if (null_or_empty) {
576 return std::move(null_or_empty);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 bool create = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 std::unique_ptr<Reference> reference = TryParseReference(value, &create);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 if (reference) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if (create && on_create_reference) {
583 on_create_reference(reference->name.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700584 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 return std::move(reference);
586 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 if (type_mask & android::ResTable_map::TYPE_COLOR) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 // Try parsing this as a color.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 std::unique_ptr<BinaryPrimitive> color = TryParseColor(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 if (color) {
592 return std::move(color);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700593 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700595
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 if (type_mask & android::ResTable_map::TYPE_BOOLEAN) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 // Try parsing this as a boolean.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 std::unique_ptr<BinaryPrimitive> boolean = TryParseBool(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 if (boolean) {
600 return std::move(boolean);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 if (type_mask & android::ResTable_map::TYPE_INTEGER) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 // Try parsing this as an integer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 std::unique_ptr<BinaryPrimitive> integer = TryParseInt(value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 if (integer) {
608 return std::move(integer);
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 Lesinskice5e56e2016-10-21 17:56:45 -0700612 const uint32_t float_mask = android::ResTable_map::TYPE_FLOAT |
613 android::ResTable_map::TYPE_DIMENSION |
614 android::ResTable_map::TYPE_FRACTION;
615 if (type_mask & float_mask) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 // Try parsing this as a float.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 std::unique_ptr<BinaryPrimitive> floating_point = TryParseFloat(value);
618 if (floating_point) {
619 if (type_mask &
620 AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) {
621 return std::move(floating_point);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 }
625 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700626}
627
628/**
629 * We successively try to parse the string as a resource type that the Attribute
630 * allows.
631 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 const StringPiece& str, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 const std::function<void(const ResourceName&)>& on_create_reference) {
635 const uint32_t type_mask = attr->type_mask;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 std::unique_ptr<Item> value =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 TryParseItemForAttribute(str, type_mask, on_create_reference);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 if (value) {
639 return value;
640 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 // Try parsing this as an enum.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 std::unique_ptr<BinaryPrimitive> enum_value = TryParseEnumSymbol(attr, str);
645 if (enum_value) {
646 return std::move(enum_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700647 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700649
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 // Try parsing this as a flag.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 std::unique_ptr<BinaryPrimitive> flag_value = TryParseFlagSymbol(attr, str);
653 if (flag_value) {
654 return std::move(flag_value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700655 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 }
657 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700658}
659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660std::string BuildResourceFileName(const ResourceFile& res_file,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 const NameMangler* mangler) {
662 std::stringstream out;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 out << "res/" << res_file.name.type;
664 if (res_file.config != ConfigDescription{}) {
665 out << "-" << res_file.config;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 }
667 out << "/";
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800668
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 if (mangler && mangler->ShouldMangle(res_file.name.package)) {
670 out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 out << res_file.name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 out << file::GetExtension(res_file.source.path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 return out.str();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800676}
677
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700678std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
679 const android::ResStringPool& src_pool,
680 const android::Res_value& res_value,
681 StringPool* dst_pool) {
682 if (type == ResourceType::kId) {
683 return util::make_unique<Id>();
684 }
685
686 const uint32_t data = util::DeviceToHost32(res_value.data);
687 switch (res_value.dataType) {
688 case android::Res_value::TYPE_STRING: {
689 const std::string str = util::GetString(src_pool, data);
690 const android::ResStringPool_span* spans = src_pool.styleAt(data);
691
692 // Check if the string has a valid style associated with it.
693 if (spans != nullptr && spans->name.index != android::ResStringPool_span::END) {
694 StyleString style_str = {str};
695 while (spans->name.index != android::ResStringPool_span::END) {
696 style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index),
697 spans->firstChar, spans->lastChar});
698 spans++;
699 }
700 return util::make_unique<StyledString>(dst_pool->MakeRef(
701 style_str, StringPool::Context(StringPool::Context::kStylePriority, config)));
702 } else {
703 if (type != ResourceType::kString && util::StartsWith(str, "res/")) {
704 // This must be a FileReference.
705 return util::make_unique<FileReference>(dst_pool->MakeRef(
706 str, StringPool::Context(StringPool::Context::kHighPriority, config)));
707 }
708
709 // There are no styles associated with this string, so treat it as a simple string.
710 return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
711 }
712 } break;
713
714 case android::Res_value::TYPE_REFERENCE:
715 case android::Res_value::TYPE_ATTRIBUTE:
716 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
717 case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: {
718 Reference::Type ref_type = Reference::Type::kResource;
719 if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE ||
720 res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) {
721 ref_type = Reference::Type::kAttribute;
722 }
723
724 if (data == 0) {
725 // A reference of 0, must be the magic @null reference.
726 return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_REFERENCE, 0u);
727 }
728
729 // This is a normal reference.
730 return util::make_unique<Reference>(data, ref_type);
731 } break;
732 }
733
734 // Treat this as a raw binary primitive.
735 return util::make_unique<BinaryPrimitive>(res_value);
736}
737
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738} // namespace ResourceUtils
739} // namespace aapt