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