blob: 7dc88dedc96e1590fc4bc01d0b5fdf4f0b2bb85f [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 Lesinskid0f116b2016-07-08 15:00:32 -0700127bool tryParseReference(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 Lesinski2ae4a872015-11-02 16:10:55 -0800174 return tryParseReference(str, nullptr, nullptr, nullptr);
175}
176
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700177bool tryParseAttributeReference(const StringPiece& str, ResourceNameRef* outRef) {
178 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 Lesinski7298bc9c2015-11-16 12:31:52 -0800211 return tryParseAttributeReference(str, nullptr);
212}
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;
274 if (tryParseReference(str, &ref, outCreate, &privateRef)) {
275 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
276 value->privateReference = privateRef;
277 return value;
278 }
279
280 if (tryParseAttributeReference(str, &ref)) {
281 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 Lesinskid0f116b2016-07-08 15:00:32 -0700423bool tryParseBool(const StringPiece& str, bool* outValue) {
424 StringPiece trimmedStr(util::trimWhitespace(str));
425 if (trimmedStr == "true" || trimmedStr == "TRUE" || trimmedStr == "True") {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800426 if (outValue) {
427 *outValue = true;
428 }
429 return true;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700430 } else if (trimmedStr == "false" || trimmedStr == "FALSE" || trimmedStr == "False") {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800431 if (outValue) {
432 *outValue = false;
433 }
434 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700435 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800436 return false;
437}
438
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700439Maybe<ResourceId> tryParseResourceId(const StringPiece& str) {
440 StringPiece trimmedStr(util::trimWhitespace(str));
441
442 std::u16string str16 = util::utf8ToUtf16(trimmedStr);
443 android::Res_value value;
444 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
445 if (value.dataType == android::Res_value::TYPE_INT_HEX) {
446 ResourceId id(value.data);
447 if (id.isValid()) {
448 return id;
449 }
450 }
451 }
452 return {};
453}
454
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700455Maybe<int> tryParseSdkVersion(const StringPiece& str) {
456 StringPiece trimmedStr(util::trimWhitespace(str));
457
458 std::u16string str16 = util::utf8ToUtf16(trimmedStr);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700459 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700460 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700461 return static_cast<int>(value.data);
462 }
463
464 // Try parsing the code name.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700465 std::pair<StringPiece, int> entry = getDevelopmentSdkCodeNameAndVersion();
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700466 if (entry.first == trimmedStr) {
467 return entry.second;
468 }
469 return {};
470}
471
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700472std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800473 bool result = false;
474 if (tryParseBool(str, &result)) {
475 android::Res_value value = {};
476 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
477
478 if (result) {
479 value.data = 0xffffffffu;
480 } else {
481 value.data = 0;
482 }
483 return util::make_unique<BinaryPrimitive>(value);
484 }
485 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486}
487
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700488std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str) {
489 std::u16string str16 = util::utf8ToUtf16(str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700490 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700491 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 return {};
493 }
494 return util::make_unique<BinaryPrimitive>(value);
495}
496
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700497std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str) {
498 std::u16string str16 = util::utf8ToUtf16(str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700499 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700500 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 return {};
502 }
503 return util::make_unique<BinaryPrimitive>(value);
504}
505
506uint32_t androidTypeToAttributeTypeMask(uint16_t type) {
507 switch (type) {
508 case android::Res_value::TYPE_NULL:
509 case android::Res_value::TYPE_REFERENCE:
510 case android::Res_value::TYPE_ATTRIBUTE:
511 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
512 return android::ResTable_map::TYPE_REFERENCE;
513
514 case android::Res_value::TYPE_STRING:
515 return android::ResTable_map::TYPE_STRING;
516
517 case android::Res_value::TYPE_FLOAT:
518 return android::ResTable_map::TYPE_FLOAT;
519
520 case android::Res_value::TYPE_DIMENSION:
521 return android::ResTable_map::TYPE_DIMENSION;
522
523 case android::Res_value::TYPE_FRACTION:
524 return android::ResTable_map::TYPE_FRACTION;
525
526 case android::Res_value::TYPE_INT_DEC:
527 case android::Res_value::TYPE_INT_HEX:
528 return android::ResTable_map::TYPE_INTEGER | android::ResTable_map::TYPE_ENUM
529 | android::ResTable_map::TYPE_FLAGS;
530
531 case android::Res_value::TYPE_INT_BOOLEAN:
532 return android::ResTable_map::TYPE_BOOLEAN;
533
534 case android::Res_value::TYPE_INT_COLOR_ARGB8:
535 case android::Res_value::TYPE_INT_COLOR_RGB8:
536 case android::Res_value::TYPE_INT_COLOR_ARGB4:
537 case android::Res_value::TYPE_INT_COLOR_RGB4:
538 return android::ResTable_map::TYPE_COLOR;
539
540 default:
541 return 0;
542 };
543}
544
545std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700546 const StringPiece& value,
547 uint32_t typeMask,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700548 std::function<void(const ResourceName&)> onCreateReference) {
549 std::unique_ptr<BinaryPrimitive> nullOrEmpty = tryParseNullOrEmpty(value);
550 if (nullOrEmpty) {
551 return std::move(nullOrEmpty);
552 }
553
554 bool create = false;
555 std::unique_ptr<Reference> reference = tryParseReference(value, &create);
556 if (reference) {
557 if (create && onCreateReference) {
558 onCreateReference(reference->name.value());
559 }
560 return std::move(reference);
561 }
562
563 if (typeMask & android::ResTable_map::TYPE_COLOR) {
564 // Try parsing this as a color.
565 std::unique_ptr<BinaryPrimitive> color = tryParseColor(value);
566 if (color) {
567 return std::move(color);
568 }
569 }
570
571 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
572 // Try parsing this as a boolean.
573 std::unique_ptr<BinaryPrimitive> boolean = tryParseBool(value);
574 if (boolean) {
575 return std::move(boolean);
576 }
577 }
578
579 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
580 // Try parsing this as an integer.
581 std::unique_ptr<BinaryPrimitive> integer = tryParseInt(value);
582 if (integer) {
583 return std::move(integer);
584 }
585 }
586
587 const uint32_t floatMask = android::ResTable_map::TYPE_FLOAT
588 | android::ResTable_map::TYPE_DIMENSION | android::ResTable_map::TYPE_FRACTION;
589 if (typeMask & floatMask) {
590 // Try parsing this as a float.
591 std::unique_ptr<BinaryPrimitive> floatingPoint = tryParseFloat(value);
592 if (floatingPoint) {
593 if (typeMask & androidTypeToAttributeTypeMask(floatingPoint->value.dataType)) {
594 return std::move(floatingPoint);
595 }
596 }
597 }
598 return {};
599}
600
601/**
602 * We successively try to parse the string as a resource type that the Attribute
603 * allows.
604 */
605std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700606 const StringPiece& str, const Attribute* attr,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700607 std::function<void(const ResourceName&)> onCreateReference) {
608 const uint32_t typeMask = attr->typeMask;
609 std::unique_ptr<Item> value = parseItemForAttribute(str, typeMask, onCreateReference);
610 if (value) {
611 return value;
612 }
613
614 if (typeMask & android::ResTable_map::TYPE_ENUM) {
615 // Try parsing this as an enum.
616 std::unique_ptr<BinaryPrimitive> enumValue = tryParseEnumSymbol(attr, str);
617 if (enumValue) {
618 return std::move(enumValue);
619 }
620 }
621
622 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
623 // Try parsing this as a flag.
624 std::unique_ptr<BinaryPrimitive> flagValue = tryParseFlagSymbol(attr, str);
625 if (flagValue) {
626 return std::move(flagValue);
627 }
628 }
629 return {};
630}
631
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800632std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler) {
633 std::stringstream out;
634 out << "res/" << resFile.name.type;
635 if (resFile.config != ConfigDescription{}) {
636 out << "-" << resFile.config;
637 }
638 out << "/";
639
640 if (mangler && mangler->shouldMangle(resFile.name.package)) {
641 out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry);
642 } else {
643 out << resFile.name.entry;
644 }
645 out << file::getExtension(resFile.source.path);
646 return out.str();
647}
648
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700649} // namespace ResourceUtils
650} // namespace aapt