blob: 36c3e702574ed9ccc99c85cfe56715297c606f14 [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 *
179 * @[package:]style/<entry>
180 * ?[package:]style/<entry>
181 * <package>:[style/]<entry>
182 * [package:style/]<entry>
183 */
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);
198 if (name.data()[0] == u'*') {
199 privateRef = true;
200 name = name.substr(1, name.size() - 1);
201 }
202 }
203
204 ResourceNameRef ref;
205 ref.type = ResourceType::kStyle;
206
207 StringPiece16 typeStr;
208 extractResourceName(name, &ref.package, &typeStr, &ref.entry);
209 if (!typeStr.empty()) {
210 // If we have a type, make sure it is a Style.
211 const ResourceType* parsedType = parseResourceType(typeStr);
212 if (!parsedType || *parsedType != ResourceType::kStyle) {
213 std::stringstream err;
214 err << "invalid resource type '" << typeStr << "' for parent of style";
215 *outError = err.str();
216 return {};
217 }
218 } else {
219 // No type was defined, this should not have a leading identifier.
220 if (hasLeadingIdentifiers) {
221 std::stringstream err;
222 err << "invalid parent reference '" << str << "'";
223 *outError = err.str();
224 return {};
225 }
226 }
227
228 if (!hasLeadingIdentifiers && ref.package.empty() && !typeStr.empty()) {
229 std::stringstream err;
230 err << "invalid parent reference '" << str << "'";
231 *outError = err.str();
232 return {};
233 }
234
235 Reference result(ref);
236 result.privateReference = privateRef;
237 return result;
238}
239
240std::unique_ptr<Reference> tryParseReference(const StringPiece16& str, bool* outCreate) {
241 ResourceNameRef ref;
242 bool privateRef = false;
243 if (tryParseReference(str, &ref, outCreate, &privateRef)) {
244 std::unique_ptr<Reference> value = util::make_unique<Reference>(ref);
245 value->privateReference = privateRef;
246 return value;
247 }
248
249 if (tryParseAttributeReference(str, &ref)) {
250 if (outCreate) {
251 *outCreate = false;
252 }
253 return util::make_unique<Reference>(ref, Reference::Type::kAttribute);
254 }
255 return {};
256}
257
258std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str) {
259 StringPiece16 trimmedStr(util::trimWhitespace(str));
260 android::Res_value value = { };
261 if (trimmedStr == u"@null") {
262 // TYPE_NULL with data set to 0 is interpreted by the runtime as an error.
263 // Instead we set the data type to TYPE_REFERENCE with a value of 0.
264 value.dataType = android::Res_value::TYPE_REFERENCE;
265 } else if (trimmedStr == u"@empty") {
266 // TYPE_NULL with value of DATA_NULL_EMPTY is handled fine by the runtime.
267 value.dataType = android::Res_value::TYPE_NULL;
268 value.data = android::Res_value::DATA_NULL_EMPTY;
269 } else {
270 return {};
271 }
272 return util::make_unique<BinaryPrimitive>(value);
273}
274
275std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
276 const StringPiece16& str) {
277 StringPiece16 trimmedStr(util::trimWhitespace(str));
278 for (const Attribute::Symbol& symbol : enumAttr->symbols) {
279 // Enum symbols are stored as @package:id/symbol resources,
280 // so we need to match against the 'entry' part of the identifier.
281 const ResourceName& enumSymbolResourceName = symbol.symbol.name.value();
282 if (trimmedStr == enumSymbolResourceName.entry) {
283 android::Res_value value = { };
284 value.dataType = android::Res_value::TYPE_INT_DEC;
285 value.data = symbol.value;
286 return util::make_unique<BinaryPrimitive>(value);
287 }
288 }
289 return {};
290}
291
292std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* flagAttr,
293 const StringPiece16& str) {
294 android::Res_value flags = { };
295 flags.dataType = android::Res_value::TYPE_INT_DEC;
296
297 for (StringPiece16 part : util::tokenize(str, u'|')) {
298 StringPiece16 trimmedPart = util::trimWhitespace(part);
299
300 bool flagSet = false;
301 for (const Attribute::Symbol& symbol : flagAttr->symbols) {
302 // Flag symbols are stored as @package:id/symbol resources,
303 // so we need to match against the 'entry' part of the identifier.
304 const ResourceName& flagSymbolResourceName = symbol.symbol.name.value();
305 if (trimmedPart == flagSymbolResourceName.entry) {
306 flags.data |= symbol.value;
307 flagSet = true;
308 break;
309 }
310 }
311
312 if (!flagSet) {
313 return {};
314 }
315 }
316 return util::make_unique<BinaryPrimitive>(flags);
317}
318
319static uint32_t parseHex(char16_t c, bool* outError) {
320 if (c >= u'0' && c <= u'9') {
321 return c - u'0';
322 } else if (c >= u'a' && c <= u'f') {
323 return c - u'a' + 0xa;
324 } else if (c >= u'A' && c <= u'F') {
325 return c - u'A' + 0xa;
326 } else {
327 *outError = true;
328 return 0xffffffffu;
329 }
330}
331
332std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str) {
333 StringPiece16 colorStr(util::trimWhitespace(str));
334 const char16_t* start = colorStr.data();
335 const size_t len = colorStr.size();
336 if (len == 0 || start[0] != u'#') {
337 return {};
338 }
339
340 android::Res_value value = { };
341 bool error = false;
342 if (len == 4) {
343 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4;
344 value.data = 0xff000000u;
345 value.data |= parseHex(start[1], &error) << 20;
346 value.data |= parseHex(start[1], &error) << 16;
347 value.data |= parseHex(start[2], &error) << 12;
348 value.data |= parseHex(start[2], &error) << 8;
349 value.data |= parseHex(start[3], &error) << 4;
350 value.data |= parseHex(start[3], &error);
351 } else if (len == 5) {
352 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4;
353 value.data |= parseHex(start[1], &error) << 28;
354 value.data |= parseHex(start[1], &error) << 24;
355 value.data |= parseHex(start[2], &error) << 20;
356 value.data |= parseHex(start[2], &error) << 16;
357 value.data |= parseHex(start[3], &error) << 12;
358 value.data |= parseHex(start[3], &error) << 8;
359 value.data |= parseHex(start[4], &error) << 4;
360 value.data |= parseHex(start[4], &error);
361 } else if (len == 7) {
362 value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8;
363 value.data = 0xff000000u;
364 value.data |= parseHex(start[1], &error) << 20;
365 value.data |= parseHex(start[2], &error) << 16;
366 value.data |= parseHex(start[3], &error) << 12;
367 value.data |= parseHex(start[4], &error) << 8;
368 value.data |= parseHex(start[5], &error) << 4;
369 value.data |= parseHex(start[6], &error);
370 } else if (len == 9) {
371 value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8;
372 value.data |= parseHex(start[1], &error) << 28;
373 value.data |= parseHex(start[2], &error) << 24;
374 value.data |= parseHex(start[3], &error) << 20;
375 value.data |= parseHex(start[4], &error) << 16;
376 value.data |= parseHex(start[5], &error) << 12;
377 value.data |= parseHex(start[6], &error) << 8;
378 value.data |= parseHex(start[7], &error) << 4;
379 value.data |= parseHex(start[8], &error);
380 } else {
381 return {};
382 }
383 return error ? std::unique_ptr<BinaryPrimitive>() : util::make_unique<BinaryPrimitive>(value);
384}
385
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800386bool tryParseBool(const StringPiece16& str, bool* outValue) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700387 StringPiece16 trimmedStr(util::trimWhitespace(str));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700388 if (trimmedStr == u"true" || trimmedStr == u"TRUE") {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800389 if (outValue) {
390 *outValue = true;
391 }
392 return true;
393 } else if (trimmedStr == u"false" || trimmedStr == u"FALSE") {
394 if (outValue) {
395 *outValue = false;
396 }
397 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800399 return false;
400}
401
402std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str) {
403 bool result = false;
404 if (tryParseBool(str, &result)) {
405 android::Res_value value = {};
406 value.dataType = android::Res_value::TYPE_INT_BOOLEAN;
407
408 if (result) {
409 value.data = 0xffffffffu;
410 } else {
411 value.data = 0;
412 }
413 return util::make_unique<BinaryPrimitive>(value);
414 }
415 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416}
417
418std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str) {
419 android::Res_value value;
420 if (!android::ResTable::stringToInt(str.data(), str.size(), &value)) {
421 return {};
422 }
423 return util::make_unique<BinaryPrimitive>(value);
424}
425
426std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str) {
427 android::Res_value value;
428 if (!android::ResTable::stringToFloat(str.data(), str.size(), &value)) {
429 return {};
430 }
431 return util::make_unique<BinaryPrimitive>(value);
432}
433
434uint32_t androidTypeToAttributeTypeMask(uint16_t type) {
435 switch (type) {
436 case android::Res_value::TYPE_NULL:
437 case android::Res_value::TYPE_REFERENCE:
438 case android::Res_value::TYPE_ATTRIBUTE:
439 case android::Res_value::TYPE_DYNAMIC_REFERENCE:
440 return android::ResTable_map::TYPE_REFERENCE;
441
442 case android::Res_value::TYPE_STRING:
443 return android::ResTable_map::TYPE_STRING;
444
445 case android::Res_value::TYPE_FLOAT:
446 return android::ResTable_map::TYPE_FLOAT;
447
448 case android::Res_value::TYPE_DIMENSION:
449 return android::ResTable_map::TYPE_DIMENSION;
450
451 case android::Res_value::TYPE_FRACTION:
452 return android::ResTable_map::TYPE_FRACTION;
453
454 case android::Res_value::TYPE_INT_DEC:
455 case android::Res_value::TYPE_INT_HEX:
456 return android::ResTable_map::TYPE_INTEGER | android::ResTable_map::TYPE_ENUM
457 | android::ResTable_map::TYPE_FLAGS;
458
459 case android::Res_value::TYPE_INT_BOOLEAN:
460 return android::ResTable_map::TYPE_BOOLEAN;
461
462 case android::Res_value::TYPE_INT_COLOR_ARGB8:
463 case android::Res_value::TYPE_INT_COLOR_RGB8:
464 case android::Res_value::TYPE_INT_COLOR_ARGB4:
465 case android::Res_value::TYPE_INT_COLOR_RGB4:
466 return android::ResTable_map::TYPE_COLOR;
467
468 default:
469 return 0;
470 };
471}
472
473std::unique_ptr<Item> parseItemForAttribute(
474 const StringPiece16& value, uint32_t typeMask,
475 std::function<void(const ResourceName&)> onCreateReference) {
476 std::unique_ptr<BinaryPrimitive> nullOrEmpty = tryParseNullOrEmpty(value);
477 if (nullOrEmpty) {
478 return std::move(nullOrEmpty);
479 }
480
481 bool create = false;
482 std::unique_ptr<Reference> reference = tryParseReference(value, &create);
483 if (reference) {
484 if (create && onCreateReference) {
485 onCreateReference(reference->name.value());
486 }
487 return std::move(reference);
488 }
489
490 if (typeMask & android::ResTable_map::TYPE_COLOR) {
491 // Try parsing this as a color.
492 std::unique_ptr<BinaryPrimitive> color = tryParseColor(value);
493 if (color) {
494 return std::move(color);
495 }
496 }
497
498 if (typeMask & android::ResTable_map::TYPE_BOOLEAN) {
499 // Try parsing this as a boolean.
500 std::unique_ptr<BinaryPrimitive> boolean = tryParseBool(value);
501 if (boolean) {
502 return std::move(boolean);
503 }
504 }
505
506 if (typeMask & android::ResTable_map::TYPE_INTEGER) {
507 // Try parsing this as an integer.
508 std::unique_ptr<BinaryPrimitive> integer = tryParseInt(value);
509 if (integer) {
510 return std::move(integer);
511 }
512 }
513
514 const uint32_t floatMask = android::ResTable_map::TYPE_FLOAT
515 | android::ResTable_map::TYPE_DIMENSION | android::ResTable_map::TYPE_FRACTION;
516 if (typeMask & floatMask) {
517 // Try parsing this as a float.
518 std::unique_ptr<BinaryPrimitive> floatingPoint = tryParseFloat(value);
519 if (floatingPoint) {
520 if (typeMask & androidTypeToAttributeTypeMask(floatingPoint->value.dataType)) {
521 return std::move(floatingPoint);
522 }
523 }
524 }
525 return {};
526}
527
528/**
529 * We successively try to parse the string as a resource type that the Attribute
530 * allows.
531 */
532std::unique_ptr<Item> parseItemForAttribute(
533 const StringPiece16& str, const Attribute* attr,
534 std::function<void(const ResourceName&)> onCreateReference) {
535 const uint32_t typeMask = attr->typeMask;
536 std::unique_ptr<Item> value = parseItemForAttribute(str, typeMask, onCreateReference);
537 if (value) {
538 return value;
539 }
540
541 if (typeMask & android::ResTable_map::TYPE_ENUM) {
542 // Try parsing this as an enum.
543 std::unique_ptr<BinaryPrimitive> enumValue = tryParseEnumSymbol(attr, str);
544 if (enumValue) {
545 return std::move(enumValue);
546 }
547 }
548
549 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
550 // Try parsing this as a flag.
551 std::unique_ptr<BinaryPrimitive> flagValue = tryParseFlagSymbol(attr, str);
552 if (flagValue) {
553 return std::move(flagValue);
554 }
555 }
556 return {};
557}
558
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800559std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler) {
560 std::stringstream out;
561 out << "res/" << resFile.name.type;
562 if (resFile.config != ConfigDescription{}) {
563 out << "-" << resFile.config;
564 }
565 out << "/";
566
567 if (mangler && mangler->shouldMangle(resFile.name.package)) {
568 out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry);
569 } else {
570 out << resFile.name.entry;
571 }
572 out << file::getExtension(resFile.source.path);
573 return out.str();
574}
575
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700576} // namespace ResourceUtils
577} // namespace aapt