blob: 1dc123e45949ea68c83192bbfa0c21974e0f91da [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 Lesinski467f1712015-11-16 17:35:44 -080019#include "flatten/ResourceTypeExtensions.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080020#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "util/Util.h"
22
23#include <androidfw/ResourceTypes.h>
24#include <sstream>
25
26namespace aapt {
27namespace ResourceUtils {
28
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080029bool extractResourceName(const StringPiece16& str, StringPiece16* outPackage,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030 StringPiece16* outType, StringPiece16* outEntry) {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080031 bool hasPackageSeparator = false;
32 bool hasTypeSeparator = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033 const char16_t* start = str.data();
34 const char16_t* end = start + str.size();
35 const char16_t* current = start;
36 while (current != end) {
37 if (outType->size() == 0 && *current == u'/') {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080038 hasTypeSeparator = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039 outType->assign(start, current - start);
40 start = current + 1;
41 } else if (outPackage->size() == 0 && *current == u':') {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080042 hasPackageSeparator = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043 outPackage->assign(start, current - start);
44 start = current + 1;
45 }
46 current++;
47 }
48 outEntry->assign(start, end - start);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080049
50 return !(hasPackageSeparator && outPackage->empty()) && !(hasTypeSeparator && outType->empty());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051}
52
Adam Lesinski467f1712015-11-16 17:35:44 -080053bool parseResourceName(const StringPiece16& str, ResourceNameRef* outRef, bool* outPrivate) {
54 size_t offset = 0;
55 bool priv = false;
56 if (str.data()[0] == u'*') {
57 priv = true;
58 offset = 1;
59 }
60
61 StringPiece16 package;
62 StringPiece16 type;
63 StringPiece16 entry;
64 if (!extractResourceName(str.substr(offset, str.size() - offset), &package, &type, &entry)) {
65 return false;
66 }
67
68 const ResourceType* parsedType = parseResourceType(type);
69 if (!parsedType) {
70 return false;
71 }
72
73 if (entry.empty()) {
74 return false;
75 }
76
77 if (outRef) {
78 outRef->package = package;
79 outRef->type = *parsedType;
80 outRef->entry = entry;
81 }
82
83 if (outPrivate) {
84 *outPrivate = priv;
85 }
86 return true;
87}
88
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089bool tryParseReference(const StringPiece16& str, ResourceNameRef* outRef, bool* outCreate,
90 bool* outPrivate) {
91 StringPiece16 trimmedStr(util::trimWhitespace(str));
92 if (trimmedStr.empty()) {
93 return false;
94 }
95
96 bool create = false;
97 bool priv = false;
98 if (trimmedStr.data()[0] == u'@') {
99 size_t offset = 1;
100 if (trimmedStr.data()[1] == u'+') {
101 create = true;
102 offset += 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 }
Adam Lesinski467f1712015-11-16 17:35:44 -0800104
105 ResourceNameRef name;
106 if (!parseResourceName(trimmedStr.substr(offset, trimmedStr.size() - offset),
107 &name, &priv)) {
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800108 return false;
109 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110
Adam Lesinski467f1712015-11-16 17:35:44 -0800111 if (create && priv) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 return false;
113 }
114
Adam Lesinski467f1712015-11-16 17:35:44 -0800115 if (create && name.type != ResourceType::kId) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 return false;
117 }
118
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800119 if (outRef) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800120 *outRef = name;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800121 }
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800122
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123 if (outCreate) {
124 *outCreate = create;
125 }
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800126
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 if (outPrivate) {
128 *outPrivate = priv;
129 }
130 return true;
131 }
132 return false;
133}
134
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800135bool isReference(const StringPiece16& str) {
136 return tryParseReference(str, nullptr, nullptr, nullptr);
137}
138
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139bool tryParseAttributeReference(const StringPiece16& str, ResourceNameRef* outRef) {
140 StringPiece16 trimmedStr(util::trimWhitespace(str));
141 if (trimmedStr.empty()) {
142 return false;
143 }
144
145 if (*trimmedStr.data() == u'?') {
146 StringPiece16 package;
147 StringPiece16 type;
148 StringPiece16 entry;
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800149 if (!extractResourceName(trimmedStr.substr(1, trimmedStr.size() - 1),
150 &package, &type, &entry)) {
151 return false;
152 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
154 if (!type.empty() && type != u"attr") {
155 return false;
156 }
157
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800158 if (entry.empty()) {
159 return false;
160 }
161
162 if (outRef) {
163 outRef->package = package;
164 outRef->type = ResourceType::kAttr;
165 outRef->entry = entry;
166 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167 return true;
168 }
169 return false;
170}
171
Adam Lesinski7298bc9c2015-11-16 12:31:52 -0800172bool isAttributeReference(const StringPiece16& str) {
173 return tryParseAttributeReference(str, nullptr);
174}
175
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176/*
177 * Style parent's are a bit different. We accept the following formats:
178 *
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800179 * @[[*]package:]style/<entry>
180 * ?[[*]package:]style/<entry>
181 * <[*]package>:[style/]<entry>
182 * [[*]package:style/]<entry>
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183 */
184Maybe<Reference> parseStyleParentReference(const StringPiece16& str, std::string* outError) {
185 if (str.empty()) {
186 return {};
187 }
188
189 StringPiece16 name = str;
190
191 bool hasLeadingIdentifiers = false;
192 bool privateRef = false;
193
194 // Skip over these identifiers. A style's parent is a normal reference.
195 if (name.data()[0] == u'@' || name.data()[0] == u'?') {
196 hasLeadingIdentifiers = true;
197 name = name.substr(1, name.size() - 1);
Adam Lesinski24b8ff02015-12-16 14:01:57 -0800198 }
199
200 if (name.data()[0] == u'*') {
201 privateRef = true;
202 name = name.substr(1, name.size() - 1);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203 }
204
205 ResourceNameRef ref;
206 ref.type = ResourceType::kStyle;
207
208 StringPiece16 typeStr;
209 extractResourceName(name, &ref.package, &typeStr, &ref.entry);
210 if (!typeStr.empty()) {
211 // If we have a type, make sure it is a Style.
212 const ResourceType* parsedType = parseResourceType(typeStr);
213 if (!parsedType || *parsedType != ResourceType::kStyle) {
214 std::stringstream err;
215 err << "invalid resource type '" << typeStr << "' for parent of style";
216 *outError = err.str();
217 return {};
218 }
219 } else {
220 // No type was defined, this should not have a leading identifier.
221 if (hasLeadingIdentifiers) {
222 std::stringstream err;
223 err << "invalid parent reference '" << str << "'";
224 *outError = err.str();
225 return {};
226 }
227 }
228
229 if (!hasLeadingIdentifiers && ref.package.empty() && !typeStr.empty()) {
230 std::stringstream err;
231 err << "invalid parent reference '" << str << "'";
232 *outError = err.str();
233 return {};
234 }
235
236 Reference result(ref);
237 result.privateReference = privateRef;
238 return result;
239}
240
241std::unique_ptr<Reference> tryParseReference(const StringPiece16& str, bool* outCreate) {
242 ResourceNameRef ref;
243 bool privateRef = false;
244 if (tryParseReference(str, &ref, outCreate, &privateRef)) {
245 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
246 value->privateReference = privateRef;
247 return value;
248 }
249
250 if (tryParseAttributeReference(str, &ref)) {
251 if (outCreate) {
252 *outCreate = false;
253 }
254 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
255 }
256 return {};
257}
258
259std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str) {
260 StringPiece16 trimmedStr(util::trimWhitespace(str));
261 android::Res_value value = { };
262 if (trimmedStr == u"@null") {
263 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
264 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
265 value.dataType = android::Res_value::TYPE_REFERENCE;
266 } else if (trimmedStr == u"@empty") {
267 // TYPE_NULL with value of DATA_NULL_EMPTY is handled fine by the runtime.
268 value.dataType = android::Res_value::TYPE_NULL;
269 value.data = android::Res_value::DATA_NULL_EMPTY;
270 } else {
271 return {};
272 }
273 return util::make_unique<BinaryPrimitive>(value);
274}
275
276std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
277 const StringPiece16& str) {
278 StringPiece16 trimmedStr(util::trimWhitespace(str));
279 for (const Attribute::Symbol& symbol : enumAttr->symbols) {
280 // Enum symbols are stored as @package:id/symbol resources,
281 // so we need to match against the 'entry' part of the identifier.
282 const ResourceName& enumSymbolResourceName = symbol.symbol.name.value();
283 if (trimmedStr == enumSymbolResourceName.entry) {
284 android::Res_value value = { };
285 value.dataType = android::Res_value::TYPE_INT_DEC;
286 value.data = symbol.value;
287 return util::make_unique<BinaryPrimitive>(value);
288 }
289 }
290 return {};
291}
292
293std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* flagAttr,
294 const StringPiece16& str) {
295 android::Res_value flags = { };
296 flags.dataType = android::Res_value::TYPE_INT_DEC;
297
298 for (StringPiece16 part : util::tokenize(str, u'|')) {
299 StringPiece16 trimmedPart = util::trimWhitespace(part);
300
301 bool flagSet = false;
302 for (const Attribute::Symbol& symbol : flagAttr->symbols) {
303 // Flag symbols are stored as @package:id/symbol resources,
304 // so we need to match against the 'entry' part of the identifier.
305 const ResourceName& flagSymbolResourceName = symbol.symbol.name.value();
306 if (trimmedPart == flagSymbolResourceName.entry) {
307 flags.data |= symbol.value;
308 flagSet = true;
309 break;
310 }
311 }
312
313 if (!flagSet) {
314 return {};
315 }
316 }
317 return util::make_unique<BinaryPrimitive>(flags);
318}
319
320static uint32_t parseHex(char16_t c, bool* outError) {
321 if (c >= u'0' && c <= u'9') {
322 return c - u'0';
323 } else if (c >= u'a' && c <= u'f') {
324 return c - u'a' + 0xa;
325 } else if (c >= u'A' && c <= u'F') {
326 return c - u'A' + 0xa;
327 } else {
328 *outError = true;
329 return 0xffffffffu;
330 }
331}
332
333std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str) {
334 StringPiece16 colorStr(util::trimWhitespace(str));
335 const char16_t* start = colorStr.data();
336 const size_t len = colorStr.size();
337 if (len == 0 || start[0] != u'#') {
338 return {};
339 }
340
341 android::Res_value value = { };
342 bool error = false;
343 if (len == 4) {
344 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
345 value.data = 0xff000000u;
346 value.data |= parseHex(start[1], &error) << 20;
347 value.data |= parseHex(start[1], &error) << 16;
348 value.data |= parseHex(start[2], &error) << 12;
349 value.data |= parseHex(start[2], &error) << 8;
350 value.data |= parseHex(start[3], &error) << 4;
351 value.data |= parseHex(start[3], &error);
352 } else if (len == 5) {
353 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
354 value.data |= parseHex(start[1], &error) << 28;
355 value.data |= parseHex(start[1], &error) << 24;
356 value.data |= parseHex(start[2], &error) << 20;
357 value.data |= parseHex(start[2], &error) << 16;
358 value.data |= parseHex(start[3], &error) << 12;
359 value.data |= parseHex(start[3], &error) << 8;
360 value.data |= parseHex(start[4], &error) << 4;
361 value.data |= parseHex(start[4], &error);
362 } else if (len == 7) {
363 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
364 value.data = 0xff000000u;
365 value.data |= parseHex(start[1], &error) << 20;
366 value.data |= parseHex(start[2], &error) << 16;
367 value.data |= parseHex(start[3], &error) << 12;
368 value.data |= parseHex(start[4], &error) << 8;
369 value.data |= parseHex(start[5], &error) << 4;
370 value.data |= parseHex(start[6], &error);
371 } else if (len == 9) {
372 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
373 value.data |= parseHex(start[1], &error) << 28;
374 value.data |= parseHex(start[2], &error) << 24;
375 value.data |= parseHex(start[3], &error) << 20;
376 value.data |= parseHex(start[4], &error) << 16;
377 value.data |= parseHex(start[5], &error) << 12;
378 value.data |= parseHex(start[6], &error) << 8;
379 value.data |= parseHex(start[7], &error) << 4;
380 value.data |= parseHex(start[8], &error);
381 } else {
382 return {};
383 }
384 return error ? std::unique_ptr<BinaryPrimitive>() : util::make_unique<BinaryPrimitive>(value);
385}
386
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800387bool tryParseBool(const StringPiece16& str, bool* outValue) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700388 StringPiece16 trimmedStr(util::trimWhitespace(str));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389 if (trimmedStr == u"true" || trimmedStr == u"TRUE") {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800390 if (outValue) {
391 *outValue = true;
392 }
393 return true;
394 } else if (trimmedStr == u"false" || trimmedStr == u"FALSE") {
395 if (outValue) {
396 *outValue = false;
397 }
398 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800400 return false;
401}
402
403std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str) {
404 bool result = false;
405 if (tryParseBool(str, &result)) {
406 android::Res_value value = {};
407 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
408
409 if (result) {
410 value.data = 0xffffffffu;
411 } else {
412 value.data = 0;
413 }
414 return util::make_unique<BinaryPrimitive>(value);
415 }
416 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417}
418
419std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str) {
420 android::Res_value value;
421 if (!android::ResTable::stringToInt(str.data(), str.size(), &value)) {
422 return {};
423 }
424 return util::make_unique<BinaryPrimitive>(value);
425}
426
427std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str) {
428 android::Res_value value;
429 if (!android::ResTable::stringToFloat(str.data(), str.size(), &value)) {
430 return {};
431 }
432 return util::make_unique<BinaryPrimitive>(value);
433}
434
435uint32_t androidTypeToAttributeTypeMask(uint16_t type) {
436 switch (type) {
437 case android::Res_value::TYPE_NULL:
438 case android::Res_value::TYPE_REFERENCE:
439 case android::Res_value::TYPE_ATTRIBUTE:
440 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
441 return android::ResTable_map::TYPE_REFERENCE;
442
443 case android::Res_value::TYPE_STRING:
444 return android::ResTable_map::TYPE_STRING;
445
446 case android::Res_value::TYPE_FLOAT:
447 return android::ResTable_map::TYPE_FLOAT;
448
449 case android::Res_value::TYPE_DIMENSION:
450 return android::ResTable_map::TYPE_DIMENSION;
451
452 case android::Res_value::TYPE_FRACTION:
453 return android::ResTable_map::TYPE_FRACTION;
454
455 case android::Res_value::TYPE_INT_DEC:
456 case android::Res_value::TYPE_INT_HEX:
457 return android::ResTable_map::TYPE_INTEGER | android::ResTable_map::TYPE_ENUM
458 | android::ResTable_map::TYPE_FLAGS;
459
460 case android::Res_value::TYPE_INT_BOOLEAN:
461 return android::ResTable_map::TYPE_BOOLEAN;
462
463 case android::Res_value::TYPE_INT_COLOR_ARGB8:
464 case android::Res_value::TYPE_INT_COLOR_RGB8:
465 case android::Res_value::TYPE_INT_COLOR_ARGB4:
466 case android::Res_value::TYPE_INT_COLOR_RGB4:
467 return android::ResTable_map::TYPE_COLOR;
468
469 default:
470 return 0;
471 };
472}
473
474std::unique_ptr<Item> parseItemForAttribute(
475 const StringPiece16& value, uint32_t typeMask,
476 std::function<void(const ResourceName&)> onCreateReference) {
477 std::unique_ptr<BinaryPrimitive> nullOrEmpty = tryParseNullOrEmpty(value);
478 if (nullOrEmpty) {
479 return std::move(nullOrEmpty);
480 }
481
482 bool create = false;
483 std::unique_ptr<Reference> reference = tryParseReference(value, &create);
484 if (reference) {
485 if (create && onCreateReference) {
486 onCreateReference(reference->name.value());
487 }
488 return std::move(reference);
489 }
490
491 if (typeMask & android::ResTable_map::TYPE_COLOR) {
492 // Try parsing this as a color.
493 std::unique_ptr<BinaryPrimitive> color = tryParseColor(value);
494 if (color) {
495 return std::move(color);
496 }
497 }
498
499 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
500 // Try parsing this as a boolean.
501 std::unique_ptr<BinaryPrimitive> boolean = tryParseBool(value);
502 if (boolean) {
503 return std::move(boolean);
504 }
505 }
506
507 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
508 // Try parsing this as an integer.
509 std::unique_ptr<BinaryPrimitive> integer = tryParseInt(value);
510 if (integer) {
511 return std::move(integer);
512 }
513 }
514
515 const uint32_t floatMask = android::ResTable_map::TYPE_FLOAT
516 | android::ResTable_map::TYPE_DIMENSION | android::ResTable_map::TYPE_FRACTION;
517 if (typeMask & floatMask) {
518 // Try parsing this as a float.
519 std::unique_ptr<BinaryPrimitive> floatingPoint = tryParseFloat(value);
520 if (floatingPoint) {
521 if (typeMask & androidTypeToAttributeTypeMask(floatingPoint->value.dataType)) {
522 return std::move(floatingPoint);
523 }
524 }
525 }
526 return {};
527}
528
529/**
530 * We successively try to parse the string as a resource type that the Attribute
531 * allows.
532 */
533std::unique_ptr<Item> parseItemForAttribute(
534 const StringPiece16& str, const Attribute* attr,
535 std::function<void(const ResourceName&)> onCreateReference) {
536 const uint32_t typeMask = attr->typeMask;
537 std::unique_ptr<Item> value = parseItemForAttribute(str, typeMask, onCreateReference);
538 if (value) {
539 return value;
540 }
541
542 if (typeMask & android::ResTable_map::TYPE_ENUM) {
543 // Try parsing this as an enum.
544 std::unique_ptr<BinaryPrimitive> enumValue = tryParseEnumSymbol(attr, str);
545 if (enumValue) {
546 return std::move(enumValue);
547 }
548 }
549
550 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
551 // Try parsing this as a flag.
552 std::unique_ptr<BinaryPrimitive> flagValue = tryParseFlagSymbol(attr, str);
553 if (flagValue) {
554 return std::move(flagValue);
555 }
556 }
557 return {};
558}
559
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800560std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler) {
561 std::stringstream out;
562 out << "res/" << resFile.name.type;
563 if (resFile.config != ConfigDescription{}) {
564 out << "-" << resFile.config;
565 }
566 out << "/";
567
568 if (mangler && mangler->shouldMangle(resFile.name.package)) {
569 out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry);
570 } else {
571 out << resFile.name.entry;
572 }
573 out << file::getExtension(resFile.source.path);
574 return out.str();
575}
576
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700577} // namespace ResourceUtils
578} // namespace aapt