blob: f806d8072aaedc0be4eeb770a4c9caa6517e2927 [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
Adam Lesinskia6fe3452015-12-09 15:20:52 -080017#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "ResourceUtils.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070019#include "SdkConstants.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080020#include "flatten/ResourceTypeExtensions.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080021#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "util/Util.h"
23
24#include <androidfw/ResourceTypes.h>
25#include <sstream>
26
27namespace aapt {
28namespace ResourceUtils {
29
Adam Lesinskid0f116b2016-07-08 15:00:32 -070030Maybe<ResourceName> toResourceName(const android::ResTable::resource_name& nameIn) {
31 ResourceName nameOut;
32 if (!nameIn.package) {
33 return {};
34 }
35
36 nameOut.package = util::utf16ToUtf8(StringPiece16(nameIn.package, nameIn.packageLen));
37
38 const ResourceType* type;
39 if (nameIn.type) {
40 type = parseResourceType(util::utf16ToUtf8(StringPiece16(nameIn.type, nameIn.typeLen)));
41 } else if (nameIn.type8) {
42 type = parseResourceType(StringPiece(nameIn.type8, nameIn.typeLen));
43 } else {
44 return {};
45 }
46
47 if (!type) {
48 return {};
49 }
50
51 nameOut.type = *type;
52
53 if (nameIn.name) {
54 nameOut.entry = util::utf16ToUtf8(StringPiece16(nameIn.name, nameIn.nameLen));
55 } else if (nameIn.name8) {
56 nameOut.entry = StringPiece(nameIn.name8, nameIn.nameLen).toString();
57 } else {
58 return {};
59 }
60 return nameOut;
61}
62
63bool extractResourceName(const StringPiece& str, StringPiece* outPackage,
64 StringPiece* outType, StringPiece* outEntry) {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080065 bool hasPackageSeparator = false;
66 bool hasTypeSeparator = false;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070067 const char* start = str.data();
68 const char* end = start + str.size();
69 const char* current = start;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070 while (current != end) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070071 if (outType->size() == 0 && *current == '/') {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080072 hasTypeSeparator = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 outType->assign(start, current - start);
74 start = current + 1;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070075 } else if (outPackage->size() == 0 && *current == ':') {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080076 hasPackageSeparator = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077 outPackage->assign(start, current - start);
78 start = current + 1;
79 }
80 current++;
81 }
82 outEntry->assign(start, end - start);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080083
84 return !(hasPackageSeparator && outPackage->empty()) && !(hasTypeSeparator && outType->empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085}
86
Adam Lesinskid0f116b2016-07-08 15:00:32 -070087bool parseResourceName(const StringPiece& str, ResourceNameRef* outRef, bool* outPrivate) {
Adam Lesinski59e04c62016-02-04 15:59:23 -080088 if (str.empty()) {
89 return false;
90 }
91
Adam Lesinski467f1712015-11-16 17:35:44 -080092 size_t offset = 0;
93 bool priv = false;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070094 if (str.data()[0] == '*') {
Adam Lesinski467f1712015-11-16 17:35:44 -080095 priv = true;
96 offset = 1;
97 }
98
Adam Lesinskid0f116b2016-07-08 15:00:32 -070099 StringPiece package;
100 StringPiece type;
101 StringPiece entry;
Adam Lesinski467f1712015-11-16 17:35:44 -0800102 if (!extractResourceName(str.substr(offset, str.size() - offset), &package, &type, &entry)) {
103 return false;
104 }
105
106 const ResourceType* parsedType = parseResourceType(type);
107 if (!parsedType) {
108 return false;
109 }
110
111 if (entry.empty()) {
112 return false;
113 }
114
115 if (outRef) {
116 outRef->package = package;
117 outRef->type = *parsedType;
118 outRef->entry = entry;
119 }
120
121 if (outPrivate) {
122 *outPrivate = priv;
123 }
124 return true;
125}
126
Adam Lesinski36c73a52016-08-11 13:39:24 -0700127bool parseReference(const StringPiece& str, ResourceNameRef* outRef, bool* outCreate,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 bool* outPrivate) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700129 StringPiece trimmedStr(util::trimWhitespace(str));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130 if (trimmedStr.empty()) {
131 return false;
132 }
133
134 bool create = false;
135 bool priv = false;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700136 if (trimmedStr.data()[0] == '@') {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137 size_t offset = 1;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700138 if (trimmedStr.data()[1] == '+') {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 create = true;
140 offset += 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800142
143 ResourceNameRef name;
144 if (!parseResourceName(trimmedStr.substr(offset, trimmedStr.size() - offset),
145 &name, &priv)) {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800146 return false;
147 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Adam Lesinski467f1712015-11-16 17:35:44 -0800149 if (create && priv) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150 return false;
151 }
152
Adam Lesinski467f1712015-11-16 17:35:44 -0800153 if (create && name.type != ResourceType::kId) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154 return false;
155 }
156
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800157 if (outRef) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800158 *outRef = name;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800159 }
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800160
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161 if (outCreate) {
162 *outCreate = create;
163 }
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800164
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165 if (outPrivate) {
166 *outPrivate = priv;
167 }
168 return true;
169 }
170 return false;
171}
172
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700173bool isReference(const StringPiece& str) {
Adam Lesinski36c73a52016-08-11 13:39:24 -0700174 return parseReference(str, nullptr, nullptr, nullptr);
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800175}
176
Adam Lesinski36c73a52016-08-11 13:39:24 -0700177bool parseAttributeReference(const StringPiece& str, ResourceNameRef* outRef) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700178 StringPiece trimmedStr(util::trimWhitespace(str));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179 if (trimmedStr.empty()) {
180 return false;
181 }
182
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700183 if (*trimmedStr.data() == '?') {
184 StringPiece package;
185 StringPiece type;
186 StringPiece entry;
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800187 if (!extractResourceName(trimmedStr.substr(1, trimmedStr.size() - 1),
188 &package, &type, &entry)) {
189 return false;
190 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700192 if (!type.empty() && type != "attr") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700193 return false;
194 }
195
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800196 if (entry.empty()) {
197 return false;
198 }
199
200 if (outRef) {
201 outRef->package = package;
202 outRef->type = ResourceType::kAttr;
203 outRef->entry = entry;
204 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205 return true;
206 }
207 return false;
208}
209
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700210bool isAttributeReference(const StringPiece& str) {
Adam Lesinski36c73a52016-08-11 13:39:24 -0700211 return parseAttributeReference(str, nullptr);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800212}
213
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214/*
215 * Style parent's are a bit different. We accept the following formats:
216 *
Adam Lesinski52364f72016-01-11 13:10:24 -0800217 * @[[*]package:][style/]<entry>
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800218 * ?[[*]package:]style/<entry>
219 * <[*]package>:[style/]<entry>
220 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700222Maybe<Reference> parseStyleParentReference(const StringPiece& str, std::string* outError) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223 if (str.empty()) {
224 return {};
225 }
226
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700227 StringPiece name = str;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700228
229 bool hasLeadingIdentifiers = false;
230 bool privateRef = false;
231
232 // Skip over these identifiers. A style's parent is a normal reference.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700233 if (name.data()[0] == '@' || name.data()[0] == '?') {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234 hasLeadingIdentifiers = true;
235 name = name.substr(1, name.size() - 1);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800236 }
237
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700238 if (name.data()[0] == '*') {
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800239 privateRef = true;
240 name = name.substr(1, name.size() - 1);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241 }
242
243 ResourceNameRef ref;
244 ref.type = ResourceType::kStyle;
245
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700246 StringPiece typeStr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247 extractResourceName(name, &ref.package, &typeStr, &ref.entry);
248 if (!typeStr.empty()) {
249 // If we have a type, make sure it is a Style.
250 const ResourceType* parsedType = parseResourceType(typeStr);
251 if (!parsedType || *parsedType != ResourceType::kStyle) {
252 std::stringstream err;
253 err << "invalid resource type '" << typeStr << "' for parent of style";
254 *outError = err.str();
255 return {};
256 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257 }
258
259 if (!hasLeadingIdentifiers && ref.package.empty() && !typeStr.empty()) {
260 std::stringstream err;
261 err << "invalid parent reference '" << str << "'";
262 *outError = err.str();
263 return {};
264 }
265
266 Reference result(ref);
267 result.privateReference = privateRef;
268 return result;
269}
270
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700271std::unique_ptr<Reference> tryParseReference(const StringPiece& str, bool* outCreate) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272 ResourceNameRef ref;
273 bool privateRef = false;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700274 if (parseReference(str, &ref, outCreate, &privateRef)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700275 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
276 value->privateReference = privateRef;
277 return value;
278 }
279
Adam Lesinski36c73a52016-08-11 13:39:24 -0700280 if (parseAttributeReference(str, &ref)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 if (outCreate) {
282 *outCreate = false;
283 }
284 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
285 }
286 return {};
287}
288
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700289std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece& str) {
290 StringPiece trimmedStr(util::trimWhitespace(str));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700291 android::Res_value value = { };
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700292 if (trimmedStr == "@null") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
294 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
295 value.dataType = android::Res_value::TYPE_REFERENCE;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700296 } else if (trimmedStr == "@empty") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297 // TYPE_NULL with value of DATA_NULL_EMPTY is handled fine by the runtime.
298 value.dataType = android::Res_value::TYPE_NULL;
299 value.data = android::Res_value::DATA_NULL_EMPTY;
300 } else {
301 return {};
302 }
303 return util::make_unique<BinaryPrimitive>(value);
304}
305
306std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700307 const StringPiece& str) {
308 StringPiece trimmedStr(util::trimWhitespace(str));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700309 for (const Attribute::Symbol& symbol : enumAttr->symbols) {
310 // Enum symbols are stored as @package:id/symbol resources,
311 // so we need to match against the 'entry' part of the identifier.
312 const ResourceName& enumSymbolResourceName = symbol.symbol.name.value();
313 if (trimmedStr == enumSymbolResourceName.entry) {
314 android::Res_value value = { };
315 value.dataType = android::Res_value::TYPE_INT_DEC;
316 value.data = symbol.value;
317 return util::make_unique<BinaryPrimitive>(value);
318 }
319 }
320 return {};
321}
322
323std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* flagAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700324 const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700325 android::Res_value flags = { };
Adam Lesinski458b8772016-04-25 14:20:21 -0700326 flags.dataType = android::Res_value::TYPE_INT_HEX;
Adam Lesinski52364f72016-01-11 13:10:24 -0800327 flags.data = 0u;
328
329 if (util::trimWhitespace(str).empty()) {
330 // Empty string is a valid flag (0).
331 return util::make_unique<BinaryPrimitive>(flags);
332 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700333
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700334 for (StringPiece part : util::tokenize(str, '|')) {
335 StringPiece trimmedPart = util::trimWhitespace(part);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336
337 bool flagSet = false;
338 for (const Attribute::Symbol& symbol : flagAttr->symbols) {
339 // Flag symbols are stored as @package:id/symbol resources,
340 // so we need to match against the 'entry' part of the identifier.
341 const ResourceName& flagSymbolResourceName = symbol.symbol.name.value();
342 if (trimmedPart == flagSymbolResourceName.entry) {
343 flags.data |= symbol.value;
344 flagSet = true;
345 break;
346 }
347 }
348
349 if (!flagSet) {
350 return {};
351 }
352 }
353 return util::make_unique<BinaryPrimitive>(flags);
354}
355
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700356static uint32_t parseHex(char c, bool* outError) {
357 if (c >= '0' && c <= '9') {
358 return c - '0';
359 } else if (c >= 'a' && c <= 'f') {
360 return c - 'a' + 0xa;
361 } else if (c >= 'A' && c <= 'F') {
362 return c - 'A' + 0xa;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700363 } else {
364 *outError = true;
365 return 0xffffffffu;
366 }
367}
368
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700369std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece& str) {
370 StringPiece colorStr(util::trimWhitespace(str));
371 const char* start = colorStr.data();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700372 const size_t len = colorStr.size();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700373 if (len == 0 || start[0] != '#') {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700374 return {};
375 }
376
377 android::Res_value value = { };
378 bool error = false;
379 if (len == 4) {
380 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
381 value.data = 0xff000000u;
382 value.data |= parseHex(start[1], &error) << 20;
383 value.data |= parseHex(start[1], &error) << 16;
384 value.data |= parseHex(start[2], &error) << 12;
385 value.data |= parseHex(start[2], &error) << 8;
386 value.data |= parseHex(start[3], &error) << 4;
387 value.data |= parseHex(start[3], &error);
388 } else if (len == 5) {
389 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
390 value.data |= parseHex(start[1], &error) << 28;
391 value.data |= parseHex(start[1], &error) << 24;
392 value.data |= parseHex(start[2], &error) << 20;
393 value.data |= parseHex(start[2], &error) << 16;
394 value.data |= parseHex(start[3], &error) << 12;
395 value.data |= parseHex(start[3], &error) << 8;
396 value.data |= parseHex(start[4], &error) << 4;
397 value.data |= parseHex(start[4], &error);
398 } else if (len == 7) {
399 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
400 value.data = 0xff000000u;
401 value.data |= parseHex(start[1], &error) << 20;
402 value.data |= parseHex(start[2], &error) << 16;
403 value.data |= parseHex(start[3], &error) << 12;
404 value.data |= parseHex(start[4], &error) << 8;
405 value.data |= parseHex(start[5], &error) << 4;
406 value.data |= parseHex(start[6], &error);
407 } else if (len == 9) {
408 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
409 value.data |= parseHex(start[1], &error) << 28;
410 value.data |= parseHex(start[2], &error) << 24;
411 value.data |= parseHex(start[3], &error) << 20;
412 value.data |= parseHex(start[4], &error) << 16;
413 value.data |= parseHex(start[5], &error) << 12;
414 value.data |= parseHex(start[6], &error) << 8;
415 value.data |= parseHex(start[7], &error) << 4;
416 value.data |= parseHex(start[8], &error);
417 } else {
418 return {};
419 }
420 return error ? std::unique_ptr<BinaryPrimitive>() : util::make_unique<BinaryPrimitive>(value);
421}
422
Adam Lesinski36c73a52016-08-11 13:39:24 -0700423Maybe<bool> parseBool(const StringPiece& str) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700424 StringPiece trimmedStr(util::trimWhitespace(str));
425 if (trimmedStr == "true" || trimmedStr == "TRUE" || trimmedStr == "True") {
Adam Lesinski36c73a52016-08-11 13:39:24 -0700426 return Maybe<bool>(true);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700427 } else if (trimmedStr == "false" || trimmedStr == "FALSE" || trimmedStr == "False") {
Adam Lesinski36c73a52016-08-11 13:39:24 -0700428 return Maybe<bool>(false);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700429 }
Adam Lesinski36c73a52016-08-11 13:39:24 -0700430 return {};
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800431}
432
Adam Lesinski36c73a52016-08-11 13:39:24 -0700433Maybe<uint32_t> parseInt(const StringPiece& str) {
434 std::u16string str16 = util::utf8ToUtf16(str);
435 android::Res_value value;
436 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
437 return value.data;
438 }
439 return {};
440}
441
442Maybe<ResourceId> parseResourceId(const StringPiece& str) {
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700443 StringPiece trimmedStr(util::trimWhitespace(str));
444
445 std::u16string str16 = util::utf8ToUtf16(trimmedStr);
446 android::Res_value value;
447 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
448 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
449 ResourceId id(value.data);
450 if (id.isValid()) {
451 return id;
452 }
453 }
454 }
455 return {};
456}
457
Adam Lesinski36c73a52016-08-11 13:39:24 -0700458Maybe<int> parseSdkVersion(const StringPiece& str) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700459 StringPiece trimmedStr(util::trimWhitespace(str));
460
461 std::u16string str16 = util::utf8ToUtf16(trimmedStr);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700462 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700463 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700464 return static_cast<int>(value.data);
465 }
466
467 // Try parsing the code name.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700468 std::pair<StringPiece, int> entry = getDevelopmentSdkCodeNameAndVersion();
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700469 if (entry.first == trimmedStr) {
470 return entry.second;
471 }
472 return {};
473}
474
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700475std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str) {
Adam Lesinski36c73a52016-08-11 13:39:24 -0700476 if (Maybe<bool> maybeResult = parseBool(str)) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800477 android::Res_value value = {};
478 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
479
Adam Lesinski36c73a52016-08-11 13:39:24 -0700480 if (maybeResult.value()) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800481 value.data = 0xffffffffu;
482 } else {
483 value.data = 0;
484 }
485 return util::make_unique<BinaryPrimitive>(value);
486 }
487 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700488}
489
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700490std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str) {
491 std::u16string str16 = util::utf8ToUtf16(str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700493 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700494 return {};
495 }
496 return util::make_unique<BinaryPrimitive>(value);
497}
498
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700499std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str) {
500 std::u16string str16 = util::utf8ToUtf16(str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700502 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 return {};
504 }
505 return util::make_unique<BinaryPrimitive>(value);
506}
507
508uint32_t androidTypeToAttributeTypeMask(uint16_t type) {
509 switch (type) {
510 case android::Res_value::TYPE_NULL:
511 case android::Res_value::TYPE_REFERENCE:
512 case android::Res_value::TYPE_ATTRIBUTE:
513 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
514 return android::ResTable_map::TYPE_REFERENCE;
515
516 case android::Res_value::TYPE_STRING:
517 return android::ResTable_map::TYPE_STRING;
518
519 case android::Res_value::TYPE_FLOAT:
520 return android::ResTable_map::TYPE_FLOAT;
521
522 case android::Res_value::TYPE_DIMENSION:
523 return android::ResTable_map::TYPE_DIMENSION;
524
525 case android::Res_value::TYPE_FRACTION:
526 return android::ResTable_map::TYPE_FRACTION;
527
528 case android::Res_value::TYPE_INT_DEC:
529 case android::Res_value::TYPE_INT_HEX:
530 return android::ResTable_map::TYPE_INTEGER | android::ResTable_map::TYPE_ENUM
531 | android::ResTable_map::TYPE_FLAGS;
532
533 case android::Res_value::TYPE_INT_BOOLEAN:
534 return android::ResTable_map::TYPE_BOOLEAN;
535
536 case android::Res_value::TYPE_INT_COLOR_ARGB8:
537 case android::Res_value::TYPE_INT_COLOR_RGB8:
538 case android::Res_value::TYPE_INT_COLOR_ARGB4:
539 case android::Res_value::TYPE_INT_COLOR_RGB4:
540 return android::ResTable_map::TYPE_COLOR;
541
542 default:
543 return 0;
544 };
545}
546
Adam Lesinski36c73a52016-08-11 13:39:24 -0700547std::unique_ptr<Item> tryParseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700548 const StringPiece& value,
549 uint32_t typeMask,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550 std::function<void(const ResourceName&)> onCreateReference) {
551 std::unique_ptr<BinaryPrimitive> nullOrEmpty = tryParseNullOrEmpty(value);
552 if (nullOrEmpty) {
553 return std::move(nullOrEmpty);
554 }
555
556 bool create = false;
557 std::unique_ptr<Reference> reference = tryParseReference(value, &create);
558 if (reference) {
559 if (create && onCreateReference) {
560 onCreateReference(reference->name.value());
561 }
562 return std::move(reference);
563 }
564
565 if (typeMask & android::ResTable_map::TYPE_COLOR) {
566 // Try parsing this as a color.
567 std::unique_ptr<BinaryPrimitive> color = tryParseColor(value);
568 if (color) {
569 return std::move(color);
570 }
571 }
572
573 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
574 // Try parsing this as a boolean.
575 std::unique_ptr<BinaryPrimitive> boolean = tryParseBool(value);
576 if (boolean) {
577 return std::move(boolean);
578 }
579 }
580
581 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
582 // Try parsing this as an integer.
583 std::unique_ptr<BinaryPrimitive> integer = tryParseInt(value);
584 if (integer) {
585 return std::move(integer);
586 }
587 }
588
589 const uint32_t floatMask = android::ResTable_map::TYPE_FLOAT
590 | android::ResTable_map::TYPE_DIMENSION | android::ResTable_map::TYPE_FRACTION;
591 if (typeMask & floatMask) {
592 // Try parsing this as a float.
593 std::unique_ptr<BinaryPrimitive> floatingPoint = tryParseFloat(value);
594 if (floatingPoint) {
595 if (typeMask & androidTypeToAttributeTypeMask(floatingPoint->value.dataType)) {
596 return std::move(floatingPoint);
597 }
598 }
599 }
600 return {};
601}
602
603/**
604 * We successively try to parse the string as a resource type that the Attribute
605 * allows.
606 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700607std::unique_ptr<Item> tryParseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700608 const StringPiece& str, const Attribute* attr,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609 std::function<void(const ResourceName&)> onCreateReference) {
610 const uint32_t typeMask = attr->typeMask;
Adam Lesinski36c73a52016-08-11 13:39:24 -0700611 std::unique_ptr<Item> value = tryParseItemForAttribute(str, typeMask, onCreateReference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700612 if (value) {
613 return value;
614 }
615
616 if (typeMask & android::ResTable_map::TYPE_ENUM) {
617 // Try parsing this as an enum.
618 std::unique_ptr<BinaryPrimitive> enumValue = tryParseEnumSymbol(attr, str);
619 if (enumValue) {
620 return std::move(enumValue);
621 }
622 }
623
624 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
625 // Try parsing this as a flag.
626 std::unique_ptr<BinaryPrimitive> flagValue = tryParseFlagSymbol(attr, str);
627 if (flagValue) {
628 return std::move(flagValue);
629 }
630 }
631 return {};
632}
633
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800634std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler) {
635 std::stringstream out;
636 out << "res/" << resFile.name.type;
637 if (resFile.config != ConfigDescription{}) {
638 out << "-" << resFile.config;
639 }
640 out << "/";
641
642 if (mangler && mangler->shouldMangle(resFile.name.package)) {
643 out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry);
644 } else {
645 out << resFile.name.entry;
646 }
647 out << file::getExtension(resFile.source.path);
648 return out.str();
649}
650
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651} // namespace ResourceUtils
652} // namespace aapt