blob: 31d6435a6184bc645d0b018dbf5ae0773e738203 [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 Lesinskid0f116b2016-07-08 15:00:32 -0700439Maybe<int> tryParseSdkVersion(const StringPiece& str) {
440 StringPiece trimmedStr(util::trimWhitespace(str));
441
442 std::u16string str16 = util::utf8ToUtf16(trimmedStr);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700443 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700444 if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700445 return static_cast<int>(value.data);
446 }
447
448 // Try parsing the code name.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700449 std::pair<StringPiece, int> entry = getDevelopmentSdkCodeNameAndVersion();
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700450 if (entry.first == trimmedStr) {
451 return entry.second;
452 }
453 return {};
454}
455
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700456std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800457 bool result = false;
458 if (tryParseBool(str, &result)) {
459 android::Res_value value = {};
460 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
461
462 if (result) {
463 value.data = 0xffffffffu;
464 } else {
465 value.data = 0;
466 }
467 return util::make_unique<BinaryPrimitive>(value);
468 }
469 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700470}
471
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700472std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str) {
473 std::u16string str16 = util::utf8ToUtf16(str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700475 if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700476 return {};
477 }
478 return util::make_unique<BinaryPrimitive>(value);
479}
480
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700481std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str) {
482 std::u16string str16 = util::utf8ToUtf16(str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700483 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700484 if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700485 return {};
486 }
487 return util::make_unique<BinaryPrimitive>(value);
488}
489
490uint32_t androidTypeToAttributeTypeMask(uint16_t type) {
491 switch (type) {
492 case android::Res_value::TYPE_NULL:
493 case android::Res_value::TYPE_REFERENCE:
494 case android::Res_value::TYPE_ATTRIBUTE:
495 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
496 return android::ResTable_map::TYPE_REFERENCE;
497
498 case android::Res_value::TYPE_STRING:
499 return android::ResTable_map::TYPE_STRING;
500
501 case android::Res_value::TYPE_FLOAT:
502 return android::ResTable_map::TYPE_FLOAT;
503
504 case android::Res_value::TYPE_DIMENSION:
505 return android::ResTable_map::TYPE_DIMENSION;
506
507 case android::Res_value::TYPE_FRACTION:
508 return android::ResTable_map::TYPE_FRACTION;
509
510 case android::Res_value::TYPE_INT_DEC:
511 case android::Res_value::TYPE_INT_HEX:
512 return android::ResTable_map::TYPE_INTEGER | android::ResTable_map::TYPE_ENUM
513 | android::ResTable_map::TYPE_FLAGS;
514
515 case android::Res_value::TYPE_INT_BOOLEAN:
516 return android::ResTable_map::TYPE_BOOLEAN;
517
518 case android::Res_value::TYPE_INT_COLOR_ARGB8:
519 case android::Res_value::TYPE_INT_COLOR_RGB8:
520 case android::Res_value::TYPE_INT_COLOR_ARGB4:
521 case android::Res_value::TYPE_INT_COLOR_RGB4:
522 return android::ResTable_map::TYPE_COLOR;
523
524 default:
525 return 0;
526 };
527}
528
529std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700530 const StringPiece& value,
531 uint32_t typeMask,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700532 std::function<void(const ResourceName&)> onCreateReference) {
533 std::unique_ptr<BinaryPrimitive> nullOrEmpty = tryParseNullOrEmpty(value);
534 if (nullOrEmpty) {
535 return std::move(nullOrEmpty);
536 }
537
538 bool create = false;
539 std::unique_ptr<Reference> reference = tryParseReference(value, &create);
540 if (reference) {
541 if (create && onCreateReference) {
542 onCreateReference(reference->name.value());
543 }
544 return std::move(reference);
545 }
546
547 if (typeMask & android::ResTable_map::TYPE_COLOR) {
548 // Try parsing this as a color.
549 std::unique_ptr<BinaryPrimitive> color = tryParseColor(value);
550 if (color) {
551 return std::move(color);
552 }
553 }
554
555 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
556 // Try parsing this as a boolean.
557 std::unique_ptr<BinaryPrimitive> boolean = tryParseBool(value);
558 if (boolean) {
559 return std::move(boolean);
560 }
561 }
562
563 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
564 // Try parsing this as an integer.
565 std::unique_ptr<BinaryPrimitive> integer = tryParseInt(value);
566 if (integer) {
567 return std::move(integer);
568 }
569 }
570
571 const uint32_t floatMask = android::ResTable_map::TYPE_FLOAT
572 | android::ResTable_map::TYPE_DIMENSION | android::ResTable_map::TYPE_FRACTION;
573 if (typeMask & floatMask) {
574 // Try parsing this as a float.
575 std::unique_ptr<BinaryPrimitive> floatingPoint = tryParseFloat(value);
576 if (floatingPoint) {
577 if (typeMask & androidTypeToAttributeTypeMask(floatingPoint->value.dataType)) {
578 return std::move(floatingPoint);
579 }
580 }
581 }
582 return {};
583}
584
585/**
586 * We successively try to parse the string as a resource type that the Attribute
587 * allows.
588 */
589std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700590 const StringPiece& str, const Attribute* attr,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591 std::function<void(const ResourceName&)> onCreateReference) {
592 const uint32_t typeMask = attr->typeMask;
593 std::unique_ptr<Item> value = parseItemForAttribute(str, typeMask, onCreateReference);
594 if (value) {
595 return value;
596 }
597
598 if (typeMask & android::ResTable_map::TYPE_ENUM) {
599 // Try parsing this as an enum.
600 std::unique_ptr<BinaryPrimitive> enumValue = tryParseEnumSymbol(attr, str);
601 if (enumValue) {
602 return std::move(enumValue);
603 }
604 }
605
606 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
607 // Try parsing this as a flag.
608 std::unique_ptr<BinaryPrimitive> flagValue = tryParseFlagSymbol(attr, str);
609 if (flagValue) {
610 return std::move(flagValue);
611 }
612 }
613 return {};
614}
615
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800616std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler) {
617 std::stringstream out;
618 out << "res/" << resFile.name.type;
619 if (resFile.config != ConfigDescription{}) {
620 out << "-" << resFile.config;
621 }
622 out << "/";
623
624 if (mangler && mangler->shouldMangle(resFile.name.package)) {
625 out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry);
626 } else {
627 out << resFile.name.entry;
628 }
629 out << file::getExtension(resFile.source.path);
630 return out.str();
631}
632
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700633} // namespace ResourceUtils
634} // namespace aapt