| 1 | /* |
| 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 | |
| 17 | #include "ResourceUtils.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <sstream> |
| 21 | |
| 22 | #include "android-base/stringprintf.h" |
| 23 | #include "androidfw/ResourceTypes.h" |
| 24 | #include "androidfw/ResourceUtils.h" |
| 25 | |
| 26 | #include "NameMangler.h" |
| 27 | #include "SdkConstants.h" |
| 28 | #include "format/binary/ResourceTypeExtensions.h" |
| 29 | #include "text/Unicode.h" |
| 30 | #include "text/Utf8Iterator.h" |
| 31 | #include "util/Files.h" |
| 32 | #include "util/Util.h" |
| 33 | |
| 34 | using ::aapt::text::Utf8Iterator; |
| 35 | using ::android::ConfigDescription; |
| 36 | using ::android::StringPiece; |
| 37 | using ::android::StringPiece16; |
| 38 | using ::android::base::StringPrintf; |
| 39 | |
| 40 | namespace aapt { |
| 41 | namespace ResourceUtils { |
| 42 | |
| 43 | std::optional<ResourceName> ToResourceName(const android::ResTable::resource_name& name_in) { |
| 44 | // TODO: Remove this when ResTable and AssetManager(1) are removed from AAPT2 |
| 45 | ResourceName name_out; |
| 46 | if (!name_in.package) { |
| 47 | return {}; |
| 48 | } |
| 49 | |
| 50 | name_out.package = |
| 51 | util::Utf16ToUtf8(StringPiece16(name_in.package, name_in.packageLen)); |
| 52 | |
| 53 | std::optional<ResourceNamedTypeRef> type; |
| 54 | std::string converted; |
| 55 | if (name_in.type) { |
| 56 | converted = util::Utf16ToUtf8(StringPiece16(name_in.type, name_in.typeLen)); |
| 57 | type = ParseResourceNamedType(converted); |
| 58 | } else if (name_in.type8) { |
| 59 | type = ParseResourceNamedType(StringPiece(name_in.type8, name_in.typeLen)); |
| 60 | } else { |
| 61 | return {}; |
| 62 | } |
| 63 | |
| 64 | if (!type) { |
| 65 | return {}; |
| 66 | } |
| 67 | |
| 68 | name_out.type = type->ToResourceNamedType(); |
| 69 | |
| 70 | if (name_in.name) { |
| 71 | name_out.entry = |
| 72 | util::Utf16ToUtf8(StringPiece16(name_in.name, name_in.nameLen)); |
| 73 | } else if (name_in.name8) { |
| 74 | name_out.entry.assign(name_in.name8, name_in.nameLen); |
| 75 | } else { |
| 76 | return {}; |
| 77 | } |
| 78 | return name_out; |
| 79 | } |
| 80 | |
| 81 | std::optional<ResourceName> ToResourceName(const android::AssetManager2::ResourceName& name_in) { |
| 82 | ResourceName name_out; |
| 83 | if (!name_in.package) { |
| 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | name_out.package = std::string(name_in.package, name_in.package_len); |
| 88 | |
| 89 | std::optional<ResourceNamedTypeRef> type; |
| 90 | std::string converted; |
| 91 | if (name_in.type16) { |
| 92 | converted = util::Utf16ToUtf8(StringPiece16(name_in.type16, name_in.type_len)); |
| 93 | type = ParseResourceNamedType(converted); |
| 94 | } else if (name_in.type) { |
| 95 | type = ParseResourceNamedType(StringPiece(name_in.type, name_in.type_len)); |
| 96 | } else { |
| 97 | return {}; |
| 98 | } |
| 99 | |
| 100 | if (!type) { |
| 101 | return {}; |
| 102 | } |
| 103 | |
| 104 | name_out.type = type->ToResourceNamedType(); |
| 105 | |
| 106 | if (name_in.entry16) { |
| 107 | name_out.entry = |
| 108 | util::Utf16ToUtf8(StringPiece16(name_in.entry16, name_in.entry_len)); |
| 109 | } else if (name_in.entry) { |
| 110 | name_out.entry = std::string(name_in.entry, name_in.entry_len); |
| 111 | } else { |
| 112 | return {}; |
| 113 | } |
| 114 | return name_out; |
| 115 | } |
| 116 | |
| 117 | bool ParseResourceName(const StringPiece& str, ResourceNameRef* out_ref, |
| 118 | bool* out_private) { |
| 119 | if (str.empty()) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | size_t offset = 0; |
| 124 | bool priv = false; |
| 125 | if (str.data()[0] == '*') { |
| 126 | priv = true; |
| 127 | offset = 1; |
| 128 | } |
| 129 | |
| 130 | StringPiece package; |
| 131 | StringPiece type; |
| 132 | StringPiece entry; |
| 133 | if (!android::ExtractResourceName(str.substr(offset, str.size() - offset), &package, &type, |
| 134 | &entry)) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type); |
| 139 | if (!parsed_type) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | if (entry.empty()) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | if (out_ref) { |
| 148 | out_ref->package = package; |
| 149 | out_ref->type = *parsed_type; |
| 150 | out_ref->entry = entry; |
| 151 | } |
| 152 | |
| 153 | if (out_private) { |
| 154 | *out_private = priv; |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool ParseReference(const StringPiece& str, ResourceNameRef* out_ref, |
| 160 | bool* out_create, bool* out_private) { |
| 161 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 162 | if (trimmed_str.empty()) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool create = false; |
| 167 | bool priv = false; |
| 168 | if (trimmed_str.data()[0] == '@') { |
| 169 | size_t offset = 1; |
| 170 | if (trimmed_str.data()[1] == '+') { |
| 171 | create = true; |
| 172 | offset += 1; |
| 173 | } |
| 174 | |
| 175 | ResourceNameRef name; |
| 176 | if (!ParseResourceName( |
| 177 | trimmed_str.substr(offset, trimmed_str.size() - offset), &name, |
| 178 | &priv)) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if (create && priv) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if (create && name.type.type != ResourceType::kId) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (out_ref) { |
| 191 | *out_ref = name; |
| 192 | } |
| 193 | |
| 194 | if (out_create) { |
| 195 | *out_create = create; |
| 196 | } |
| 197 | |
| 198 | if (out_private) { |
| 199 | *out_private = priv; |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | bool IsReference(const StringPiece& str) { |
| 207 | return ParseReference(str, nullptr, nullptr, nullptr); |
| 208 | } |
| 209 | |
| 210 | bool ParseAttributeReference(const StringPiece& str, ResourceNameRef* out_ref) { |
| 211 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 212 | if (trimmed_str.empty()) { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | if (*trimmed_str.data() == '?') { |
| 217 | StringPiece package; |
| 218 | StringPiece type; |
| 219 | StringPiece entry; |
| 220 | if (!android::ExtractResourceName(trimmed_str.substr(1, trimmed_str.size() - 1), &package, |
| 221 | &type, &entry)) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | if (!type.empty() && type != "attr") { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | if (entry.empty()) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | if (out_ref) { |
| 234 | out_ref->package = package; |
| 235 | out_ref->type = ResourceNamedTypeWithDefaultName(ResourceType::kAttr); |
| 236 | out_ref->entry = entry; |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | bool IsAttributeReference(const StringPiece& str) { |
| 244 | return ParseAttributeReference(str, nullptr); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Style parent's are a bit different. We accept the following formats: |
| 249 | * |
| 250 | * @[[*]package:][style/]<entry> |
| 251 | * ?[[*]package:]style/<entry> |
| 252 | * <[*]package>:[style/]<entry> |
| 253 | * [[*]package:style/]<entry> |
| 254 | */ |
| 255 | std::optional<Reference> ParseStyleParentReference(const StringPiece& str, std::string* out_error) { |
| 256 | if (str.empty()) { |
| 257 | return {}; |
| 258 | } |
| 259 | |
| 260 | StringPiece name = str; |
| 261 | |
| 262 | bool has_leading_identifiers = false; |
| 263 | bool private_ref = false; |
| 264 | |
| 265 | // Skip over these identifiers. A style's parent is a normal reference. |
| 266 | if (name.data()[0] == '@' || name.data()[0] == '?') { |
| 267 | has_leading_identifiers = true; |
| 268 | name = name.substr(1, name.size() - 1); |
| 269 | } |
| 270 | |
| 271 | if (name.data()[0] == '*') { |
| 272 | private_ref = true; |
| 273 | name = name.substr(1, name.size() - 1); |
| 274 | } |
| 275 | |
| 276 | ResourceNameRef ref; |
| 277 | ref.type = ResourceNamedTypeWithDefaultName(ResourceType::kStyle); |
| 278 | |
| 279 | StringPiece type_str; |
| 280 | android::ExtractResourceName(name, &ref.package, &type_str, &ref.entry); |
| 281 | if (!type_str.empty()) { |
| 282 | // If we have a type, make sure it is a Style. |
| 283 | const ResourceType* parsed_type = ParseResourceType(type_str); |
| 284 | if (!parsed_type || *parsed_type != ResourceType::kStyle) { |
| 285 | std::stringstream err; |
| 286 | err << "invalid resource type '" << type_str << "' for parent of style"; |
| 287 | *out_error = err.str(); |
| 288 | return {}; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (!has_leading_identifiers && ref.package.empty() && !type_str.empty()) { |
| 293 | std::stringstream err; |
| 294 | err << "invalid parent reference '" << str << "'"; |
| 295 | *out_error = err.str(); |
| 296 | return {}; |
| 297 | } |
| 298 | |
| 299 | Reference result(ref); |
| 300 | result.private_reference = private_ref; |
| 301 | return result; |
| 302 | } |
| 303 | |
| 304 | std::optional<Reference> ParseXmlAttributeName(const StringPiece& str) { |
| 305 | StringPiece trimmed_str = util::TrimWhitespace(str); |
| 306 | const char* start = trimmed_str.data(); |
| 307 | const char* const end = start + trimmed_str.size(); |
| 308 | const char* p = start; |
| 309 | |
| 310 | Reference ref; |
| 311 | if (p != end && *p == '*') { |
| 312 | ref.private_reference = true; |
| 313 | start++; |
| 314 | p++; |
| 315 | } |
| 316 | |
| 317 | StringPiece package; |
| 318 | StringPiece name; |
| 319 | while (p != end) { |
| 320 | if (*p == ':') { |
| 321 | package = StringPiece(start, p - start); |
| 322 | name = StringPiece(p + 1, end - (p + 1)); |
| 323 | break; |
| 324 | } |
| 325 | p++; |
| 326 | } |
| 327 | |
| 328 | ref.name = ResourceName(package, ResourceNamedTypeWithDefaultName(ResourceType::kAttr), |
| 329 | name.empty() ? trimmed_str : name); |
| 330 | return std::optional<Reference>(std::move(ref)); |
| 331 | } |
| 332 | |
| 333 | std::unique_ptr<Reference> TryParseReference(const StringPiece& str, |
| 334 | bool* out_create) { |
| 335 | ResourceNameRef ref; |
| 336 | bool private_ref = false; |
| 337 | if (ParseReference(str, &ref, out_create, &private_ref)) { |
| 338 | std::unique_ptr<Reference> value = util::make_unique<Reference>(ref); |
| 339 | value->private_reference = private_ref; |
| 340 | return value; |
| 341 | } |
| 342 | |
| 343 | if (ParseAttributeReference(str, &ref)) { |
| 344 | if (out_create) { |
| 345 | *out_create = false; |
| 346 | } |
| 347 | return util::make_unique<Reference>(ref, Reference::Type::kAttribute); |
| 348 | } |
| 349 | return {}; |
| 350 | } |
| 351 | |
| 352 | std::unique_ptr<Item> TryParseNullOrEmpty(const StringPiece& str) { |
| 353 | const StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 354 | if (trimmed_str == "@null") { |
| 355 | return MakeNull(); |
| 356 | } else if (trimmed_str == "@empty") { |
| 357 | return MakeEmpty(); |
| 358 | } |
| 359 | return {}; |
| 360 | } |
| 361 | |
| 362 | std::unique_ptr<Reference> MakeNull() { |
| 363 | // TYPE_NULL with data set to 0 is interpreted by the runtime as an error. |
| 364 | // Instead we set the data type to TYPE_REFERENCE with a value of 0. |
| 365 | return util::make_unique<Reference>(); |
| 366 | } |
| 367 | |
| 368 | std::unique_ptr<BinaryPrimitive> MakeEmpty() { |
| 369 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_NULL, |
| 370 | android::Res_value::DATA_NULL_EMPTY); |
| 371 | } |
| 372 | |
| 373 | std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr, |
| 374 | const StringPiece& str) { |
| 375 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 376 | for (const Attribute::Symbol& symbol : enum_attr->symbols) { |
| 377 | // Enum symbols are stored as @package:id/symbol resources, |
| 378 | // so we need to match against the 'entry' part of the identifier. |
| 379 | const ResourceName& enum_symbol_resource_name = symbol.symbol.name.value(); |
| 380 | if (trimmed_str == enum_symbol_resource_name.entry) { |
| 381 | android::Res_value value = {}; |
| 382 | value.dataType = symbol.type; |
| 383 | value.data = symbol.value; |
| 384 | return util::make_unique<BinaryPrimitive>(value); |
| 385 | } |
| 386 | } |
| 387 | return {}; |
| 388 | } |
| 389 | |
| 390 | std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* flag_attr, |
| 391 | const StringPiece& str) { |
| 392 | android::Res_value flags = {}; |
| 393 | flags.dataType = android::Res_value::TYPE_INT_HEX; |
| 394 | flags.data = 0u; |
| 395 | |
| 396 | if (util::TrimWhitespace(str).empty()) { |
| 397 | // Empty string is a valid flag (0). |
| 398 | return util::make_unique<BinaryPrimitive>(flags); |
| 399 | } |
| 400 | |
| 401 | for (const StringPiece& part : util::Tokenize(str, '|')) { |
| 402 | StringPiece trimmed_part = util::TrimWhitespace(part); |
| 403 | |
| 404 | bool flag_set = false; |
| 405 | for (const Attribute::Symbol& symbol : flag_attr->symbols) { |
| 406 | // Flag symbols are stored as @package:id/symbol resources, |
| 407 | // so we need to match against the 'entry' part of the identifier. |
| 408 | const ResourceName& flag_symbol_resource_name = |
| 409 | symbol.symbol.name.value(); |
| 410 | if (trimmed_part == flag_symbol_resource_name.entry) { |
| 411 | flags.data |= symbol.value; |
| 412 | flag_set = true; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (!flag_set) { |
| 418 | return {}; |
| 419 | } |
| 420 | } |
| 421 | return util::make_unique<BinaryPrimitive>(flags); |
| 422 | } |
| 423 | |
| 424 | static uint32_t ParseHex(char c, bool* out_error) { |
| 425 | if (c >= '0' && c <= '9') { |
| 426 | return c - '0'; |
| 427 | } else if (c >= 'a' && c <= 'f') { |
| 428 | return c - 'a' + 0xa; |
| 429 | } else if (c >= 'A' && c <= 'F') { |
| 430 | return c - 'A' + 0xa; |
| 431 | } else { |
| 432 | *out_error = true; |
| 433 | return 0xffffffffu; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | std::unique_ptr<BinaryPrimitive> TryParseColor(const StringPiece& str) { |
| 438 | StringPiece color_str(util::TrimWhitespace(str)); |
| 439 | const char* start = color_str.data(); |
| 440 | const size_t len = color_str.size(); |
| 441 | if (len == 0 || start[0] != '#') { |
| 442 | return {}; |
| 443 | } |
| 444 | |
| 445 | android::Res_value value = {}; |
| 446 | bool error = false; |
| 447 | if (len == 4) { |
| 448 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB4; |
| 449 | value.data = 0xff000000u; |
| 450 | value.data |= ParseHex(start[1], &error) << 20; |
| 451 | value.data |= ParseHex(start[1], &error) << 16; |
| 452 | value.data |= ParseHex(start[2], &error) << 12; |
| 453 | value.data |= ParseHex(start[2], &error) << 8; |
| 454 | value.data |= ParseHex(start[3], &error) << 4; |
| 455 | value.data |= ParseHex(start[3], &error); |
| 456 | } else if (len == 5) { |
| 457 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB4; |
| 458 | value.data |= ParseHex(start[1], &error) << 28; |
| 459 | value.data |= ParseHex(start[1], &error) << 24; |
| 460 | value.data |= ParseHex(start[2], &error) << 20; |
| 461 | value.data |= ParseHex(start[2], &error) << 16; |
| 462 | value.data |= ParseHex(start[3], &error) << 12; |
| 463 | value.data |= ParseHex(start[3], &error) << 8; |
| 464 | value.data |= ParseHex(start[4], &error) << 4; |
| 465 | value.data |= ParseHex(start[4], &error); |
| 466 | } else if (len == 7) { |
| 467 | value.dataType = android::Res_value::TYPE_INT_COLOR_RGB8; |
| 468 | value.data = 0xff000000u; |
| 469 | value.data |= ParseHex(start[1], &error) << 20; |
| 470 | value.data |= ParseHex(start[2], &error) << 16; |
| 471 | value.data |= ParseHex(start[3], &error) << 12; |
| 472 | value.data |= ParseHex(start[4], &error) << 8; |
| 473 | value.data |= ParseHex(start[5], &error) << 4; |
| 474 | value.data |= ParseHex(start[6], &error); |
| 475 | } else if (len == 9) { |
| 476 | value.dataType = android::Res_value::TYPE_INT_COLOR_ARGB8; |
| 477 | value.data |= ParseHex(start[1], &error) << 28; |
| 478 | value.data |= ParseHex(start[2], &error) << 24; |
| 479 | value.data |= ParseHex(start[3], &error) << 20; |
| 480 | value.data |= ParseHex(start[4], &error) << 16; |
| 481 | value.data |= ParseHex(start[5], &error) << 12; |
| 482 | value.data |= ParseHex(start[6], &error) << 8; |
| 483 | value.data |= ParseHex(start[7], &error) << 4; |
| 484 | value.data |= ParseHex(start[8], &error); |
| 485 | } else { |
| 486 | return {}; |
| 487 | } |
| 488 | return error ? std::unique_ptr<BinaryPrimitive>() |
| 489 | : util::make_unique<BinaryPrimitive>(value); |
| 490 | } |
| 491 | |
| 492 | std::optional<bool> ParseBool(const StringPiece& str) { |
| 493 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 494 | if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") { |
| 495 | return std::optional<bool>(true); |
| 496 | } else if (trimmed_str == "false" || trimmed_str == "FALSE" || |
| 497 | trimmed_str == "False") { |
| 498 | return std::optional<bool>(false); |
| 499 | } |
| 500 | return {}; |
| 501 | } |
| 502 | |
| 503 | std::optional<uint32_t> ParseInt(const StringPiece& str) { |
| 504 | std::u16string str16 = util::Utf8ToUtf16(str); |
| 505 | android::Res_value value; |
| 506 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 507 | return value.data; |
| 508 | } |
| 509 | return {}; |
| 510 | } |
| 511 | |
| 512 | std::optional<ResourceId> ParseResourceId(const StringPiece& str) { |
| 513 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 514 | |
| 515 | std::u16string str16 = util::Utf8ToUtf16(trimmed_str); |
| 516 | android::Res_value value; |
| 517 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 518 | if (value.dataType == android::Res_value::TYPE_INT_HEX) { |
| 519 | ResourceId id(value.data); |
| 520 | if (id.is_valid()) { |
| 521 | return id; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | return {}; |
| 526 | } |
| 527 | |
| 528 | std::optional<int> ParseSdkVersion(const StringPiece& str) { |
| 529 | StringPiece trimmed_str(util::TrimWhitespace(str)); |
| 530 | |
| 531 | std::u16string str16 = util::Utf8ToUtf16(trimmed_str); |
| 532 | android::Res_value value; |
| 533 | if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 534 | return static_cast<int>(value.data); |
| 535 | } |
| 536 | |
| 537 | // Try parsing the code name. |
| 538 | std::optional<int> entry = GetDevelopmentSdkCodeNameVersion(trimmed_str); |
| 539 | if (entry) { |
| 540 | return entry.value(); |
| 541 | } |
| 542 | |
| 543 | // Try parsing codename from "[codename].[preview_sdk_fingerprint]" value. |
| 544 | const StringPiece::const_iterator begin = std::begin(trimmed_str); |
| 545 | const StringPiece::const_iterator end = std::end(trimmed_str); |
| 546 | const StringPiece::const_iterator codename_end = std::find(begin, end, '.'); |
| 547 | entry = GetDevelopmentSdkCodeNameVersion(trimmed_str.substr(begin, codename_end)); |
| 548 | if (entry) { |
| 549 | return entry.value(); |
| 550 | } |
| 551 | return {}; |
| 552 | } |
| 553 | |
| 554 | std::unique_ptr<BinaryPrimitive> TryParseBool(const StringPiece& str) { |
| 555 | if (std::optional<bool> maybe_result = ParseBool(str)) { |
| 556 | const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u; |
| 557 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, data); |
| 558 | } |
| 559 | return {}; |
| 560 | } |
| 561 | |
| 562 | std::unique_ptr<BinaryPrimitive> MakeBool(bool val) { |
| 563 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_BOOLEAN, |
| 564 | val ? 0xffffffffu : 0u); |
| 565 | } |
| 566 | |
| 567 | std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) { |
| 568 | std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str)); |
| 569 | android::Res_value value; |
| 570 | if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { |
| 571 | return {}; |
| 572 | } |
| 573 | return util::make_unique<BinaryPrimitive>(value); |
| 574 | } |
| 575 | |
| 576 | std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) { |
| 577 | return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val); |
| 578 | } |
| 579 | |
| 580 | std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) { |
| 581 | std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str)); |
| 582 | android::Res_value value; |
| 583 | if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) { |
| 584 | return {}; |
| 585 | } |
| 586 | return util::make_unique<BinaryPrimitive>(value); |
| 587 | } |
| 588 | |
| 589 | uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) { |
| 590 | switch (type) { |
| 591 | case android::Res_value::TYPE_NULL: |
| 592 | case android::Res_value::TYPE_REFERENCE: |
| 593 | case android::Res_value::TYPE_ATTRIBUTE: |
| 594 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
| 595 | case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: |
| 596 | return android::ResTable_map::TYPE_REFERENCE; |
| 597 | |
| 598 | case android::Res_value::TYPE_STRING: |
| 599 | return android::ResTable_map::TYPE_STRING; |
| 600 | |
| 601 | case android::Res_value::TYPE_FLOAT: |
| 602 | return android::ResTable_map::TYPE_FLOAT; |
| 603 | |
| 604 | case android::Res_value::TYPE_DIMENSION: |
| 605 | return android::ResTable_map::TYPE_DIMENSION; |
| 606 | |
| 607 | case android::Res_value::TYPE_FRACTION: |
| 608 | return android::ResTable_map::TYPE_FRACTION; |
| 609 | |
| 610 | case android::Res_value::TYPE_INT_DEC: |
| 611 | case android::Res_value::TYPE_INT_HEX: |
| 612 | return android::ResTable_map::TYPE_INTEGER | |
| 613 | android::ResTable_map::TYPE_ENUM | |
| 614 | android::ResTable_map::TYPE_FLAGS; |
| 615 | |
| 616 | case android::Res_value::TYPE_INT_BOOLEAN: |
| 617 | return android::ResTable_map::TYPE_BOOLEAN; |
| 618 | |
| 619 | case android::Res_value::TYPE_INT_COLOR_ARGB8: |
| 620 | case android::Res_value::TYPE_INT_COLOR_RGB8: |
| 621 | case android::Res_value::TYPE_INT_COLOR_ARGB4: |
| 622 | case android::Res_value::TYPE_INT_COLOR_RGB4: |
| 623 | return android::ResTable_map::TYPE_COLOR; |
| 624 | |
| 625 | default: |
| 626 | return 0; |
| 627 | }; |
| 628 | } |
| 629 | |
| 630 | std::unique_ptr<Item> TryParseItemForAttribute( |
| 631 | const StringPiece& value, uint32_t type_mask, |
| 632 | const std::function<bool(const ResourceName&)>& on_create_reference) { |
| 633 | using android::ResTable_map; |
| 634 | |
| 635 | auto null_or_empty = TryParseNullOrEmpty(value); |
| 636 | if (null_or_empty) { |
| 637 | return null_or_empty; |
| 638 | } |
| 639 | |
| 640 | bool create = false; |
| 641 | auto reference = TryParseReference(value, &create); |
| 642 | if (reference) { |
| 643 | reference->type_flags = type_mask; |
| 644 | if (create && on_create_reference) { |
| 645 | if (!on_create_reference(reference->name.value())) { |
| 646 | return {}; |
| 647 | } |
| 648 | } |
| 649 | return std::move(reference); |
| 650 | } |
| 651 | |
| 652 | if (type_mask & ResTable_map::TYPE_COLOR) { |
| 653 | // Try parsing this as a color. |
| 654 | auto color = TryParseColor(value); |
| 655 | if (color) { |
| 656 | return std::move(color); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | if (type_mask & ResTable_map::TYPE_BOOLEAN) { |
| 661 | // Try parsing this as a boolean. |
| 662 | auto boolean = TryParseBool(value); |
| 663 | if (boolean) { |
| 664 | return std::move(boolean); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (type_mask & ResTable_map::TYPE_INTEGER) { |
| 669 | // Try parsing this as an integer. |
| 670 | auto integer = TryParseInt(value); |
| 671 | if (integer) { |
| 672 | return std::move(integer); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | const uint32_t float_mask = |
| 677 | ResTable_map::TYPE_FLOAT | ResTable_map::TYPE_DIMENSION | ResTable_map::TYPE_FRACTION; |
| 678 | if (type_mask & float_mask) { |
| 679 | // Try parsing this as a float. |
| 680 | auto floating_point = TryParseFloat(value); |
| 681 | if (floating_point) { |
| 682 | if (type_mask & AndroidTypeToAttributeTypeMask(floating_point->value.dataType)) { |
| 683 | return std::move(floating_point); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | return {}; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * We successively try to parse the string as a resource type that the Attribute |
| 692 | * allows. |
| 693 | */ |
| 694 | std::unique_ptr<Item> TryParseItemForAttribute( |
| 695 | const StringPiece& str, const Attribute* attr, |
| 696 | const std::function<bool(const ResourceName&)>& on_create_reference) { |
| 697 | using android::ResTable_map; |
| 698 | |
| 699 | const uint32_t type_mask = attr->type_mask; |
| 700 | auto value = TryParseItemForAttribute(str, type_mask, on_create_reference); |
| 701 | if (value) { |
| 702 | return value; |
| 703 | } |
| 704 | |
| 705 | if (type_mask & ResTable_map::TYPE_ENUM) { |
| 706 | // Try parsing this as an enum. |
| 707 | auto enum_value = TryParseEnumSymbol(attr, str); |
| 708 | if (enum_value) { |
| 709 | return std::move(enum_value); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (type_mask & ResTable_map::TYPE_FLAGS) { |
| 714 | // Try parsing this as a flag. |
| 715 | auto flag_value = TryParseFlagSymbol(attr, str); |
| 716 | if (flag_value) { |
| 717 | return std::move(flag_value); |
| 718 | } |
| 719 | } |
| 720 | return {}; |
| 721 | } |
| 722 | |
| 723 | std::string BuildResourceFileName(const ResourceFile& res_file, const NameMangler* mangler) { |
| 724 | std::stringstream out; |
| 725 | out << "res/" << res_file.name.type; |
| 726 | if (res_file.config != ConfigDescription{}) { |
| 727 | out << "-" << res_file.config; |
| 728 | } |
| 729 | out << "/"; |
| 730 | |
| 731 | if (mangler && mangler->ShouldMangle(res_file.name.package)) { |
| 732 | out << NameMangler::MangleEntry(res_file.name.package, res_file.name.entry); |
| 733 | } else { |
| 734 | out << res_file.name.entry; |
| 735 | } |
| 736 | out << file::GetExtension(res_file.source.path); |
| 737 | return out.str(); |
| 738 | } |
| 739 | |
| 740 | std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config, |
| 741 | const android::ResStringPool& src_pool, |
| 742 | const android::Res_value& res_value, |
| 743 | StringPool* dst_pool) { |
| 744 | if (type == ResourceType::kId) { |
| 745 | if (res_value.dataType != android::Res_value::TYPE_REFERENCE && |
| 746 | res_value.dataType != android::Res_value::TYPE_DYNAMIC_REFERENCE) { |
| 747 | // plain "id" resources are actually encoded as unused values (aapt1 uses an empty string, |
| 748 | // while aapt2 uses a false boolean). |
| 749 | return util::make_unique<Id>(); |
| 750 | } |
| 751 | // fall through to regular reference deserialization logic |
| 752 | } |
| 753 | |
| 754 | const uint32_t data = util::DeviceToHost32(res_value.data); |
| 755 | switch (res_value.dataType) { |
| 756 | case android::Res_value::TYPE_STRING: { |
| 757 | const std::string str = util::GetString(src_pool, data); |
| 758 | auto spans_result = src_pool.styleAt(data); |
| 759 | |
| 760 | // Check if the string has a valid style associated with it. |
| 761 | if (spans_result.has_value() && |
| 762 | (*spans_result)->name.index != android::ResStringPool_span::END) { |
| 763 | const android::ResStringPool_span* spans = spans_result->unsafe_ptr(); |
| 764 | StyleString style_str = {str}; |
| 765 | while (spans->name.index != android::ResStringPool_span::END) { |
| 766 | style_str.spans.push_back(Span{util::GetString(src_pool, spans->name.index), |
| 767 | spans->firstChar, spans->lastChar}); |
| 768 | spans++; |
| 769 | } |
| 770 | return util::make_unique<StyledString>(dst_pool->MakeRef( |
| 771 | style_str, StringPool::Context(StringPool::Context::kNormalPriority, config))); |
| 772 | } else { |
| 773 | if (type != ResourceType::kString && util::StartsWith(str, "res/")) { |
| 774 | // This must be a FileReference. |
| 775 | std::unique_ptr<FileReference> file_ref = |
| 776 | util::make_unique<FileReference>(dst_pool->MakeRef( |
| 777 | str, StringPool::Context(StringPool::Context::kHighPriority, config))); |
| 778 | if (type == ResourceType::kRaw) { |
| 779 | file_ref->type = ResourceFile::Type::kUnknown; |
| 780 | } else if (util::EndsWith(*file_ref->path, ".xml")) { |
| 781 | file_ref->type = ResourceFile::Type::kBinaryXml; |
| 782 | } else if (util::EndsWith(*file_ref->path, ".png")) { |
| 783 | file_ref->type = ResourceFile::Type::kPng; |
| 784 | } |
| 785 | return std::move(file_ref); |
| 786 | } |
| 787 | |
| 788 | // There are no styles associated with this string, so treat it as a simple string. |
| 789 | return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config))); |
| 790 | } |
| 791 | } break; |
| 792 | |
| 793 | case android::Res_value::TYPE_REFERENCE: |
| 794 | case android::Res_value::TYPE_ATTRIBUTE: |
| 795 | case android::Res_value::TYPE_DYNAMIC_REFERENCE: |
| 796 | case android::Res_value::TYPE_DYNAMIC_ATTRIBUTE: { |
| 797 | Reference::Type ref_type = Reference::Type::kResource; |
| 798 | if (res_value.dataType == android::Res_value::TYPE_ATTRIBUTE || |
| 799 | res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) { |
| 800 | ref_type = Reference::Type::kAttribute; |
| 801 | } |
| 802 | |
| 803 | if (data == 0u) { |
| 804 | // A reference of 0, must be the magic @null reference. |
| 805 | return util::make_unique<Reference>(); |
| 806 | } |
| 807 | |
| 808 | // This is a normal reference. |
| 809 | auto reference = util::make_unique<Reference>(data, ref_type); |
| 810 | if (res_value.dataType == android::Res_value::TYPE_DYNAMIC_REFERENCE || |
| 811 | res_value.dataType == android::Res_value::TYPE_DYNAMIC_ATTRIBUTE) { |
| 812 | reference->is_dynamic = true; |
| 813 | } |
| 814 | return reference; |
| 815 | } break; |
| 816 | } |
| 817 | |
| 818 | // Treat this as a raw binary primitive. |
| 819 | return util::make_unique<BinaryPrimitive>(res_value); |
| 820 | } |
| 821 | |
| 822 | // Converts the codepoint to UTF-8 and appends it to the string. |
| 823 | static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) { |
| 824 | ssize_t len = utf32_to_utf8_length(&codepoint, 1); |
| 825 | if (len < 0) { |
| 826 | return false; |
| 827 | } |
| 828 | |
| 829 | const size_t start_append_pos = output->size(); |
| 830 | |
| 831 | // Make room for the next character. |
| 832 | output->resize(output->size() + len); |
| 833 | |
| 834 | char* dst = &*(output->begin() + start_append_pos); |
| 835 | utf32_to_utf8(&codepoint, 1, dst, len + 1); |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | // Reads up to 4 UTF-8 characters that represent a Unicode escape sequence, and appends the |
| 840 | // Unicode codepoint represented by the escape sequence to the string. |
| 841 | static bool AppendUnicodeEscapeSequence(Utf8Iterator* iter, std::string* output) { |
| 842 | char32_t code = 0; |
| 843 | for (size_t i = 0; i < 4 && iter->HasNext(); i++) { |
| 844 | char32_t codepoint = iter->Next(); |
| 845 | char32_t a; |
| 846 | if (codepoint >= U'0' && codepoint <= U'9') { |
| 847 | a = codepoint - U'0'; |
| 848 | } else if (codepoint >= U'a' && codepoint <= U'f') { |
| 849 | a = codepoint - U'a' + 10; |
| 850 | } else if (codepoint >= U'A' && codepoint <= U'F') { |
| 851 | a = codepoint - U'A' + 10; |
| 852 | } else { |
| 853 | return {}; |
| 854 | } |
| 855 | code = (code << 4) | a; |
| 856 | } |
| 857 | return AppendCodepointToUtf8String(code, output); |
| 858 | } |
| 859 | |
| 860 | StringBuilder::StringBuilder(bool preserve_spaces) |
| 861 | : preserve_spaces_(preserve_spaces), quote_(preserve_spaces) { |
| 862 | } |
| 863 | |
| 864 | StringBuilder& StringBuilder::AppendText(const std::string& text) { |
| 865 | if (!error_.empty()) { |
| 866 | return *this; |
| 867 | } |
| 868 | |
| 869 | const size_t previous_len = xml_string_.text.size(); |
| 870 | Utf8Iterator iter(text); |
| 871 | while (iter.HasNext()) { |
| 872 | char32_t codepoint = iter.Next(); |
| 873 | if (!preserve_spaces_ && !quote_ && (codepoint <= std::numeric_limits<char>::max()) |
| 874 | && isspace(static_cast<char>(codepoint))) { |
| 875 | if (!last_codepoint_was_space_) { |
| 876 | // Emit a space if it's the first. |
| 877 | xml_string_.text += ' '; |
| 878 | last_codepoint_was_space_ = true; |
| 879 | } |
| 880 | |
| 881 | // Keep eating spaces. |
| 882 | continue; |
| 883 | } |
| 884 | |
| 885 | // This is not a space. |
| 886 | last_codepoint_was_space_ = false; |
| 887 | |
| 888 | if (codepoint == U'\\') { |
| 889 | if (iter.HasNext()) { |
| 890 | codepoint = iter.Next(); |
| 891 | switch (codepoint) { |
| 892 | case U't': |
| 893 | xml_string_.text += '\t'; |
| 894 | break; |
| 895 | case U'n': |
| 896 | xml_string_.text += '\n'; |
| 897 | break; |
| 898 | |
| 899 | case U'#': |
| 900 | case U'@': |
| 901 | case U'?': |
| 902 | case U'"': |
| 903 | case U'\'': |
| 904 | case U'\\': |
| 905 | xml_string_.text += static_cast<char>(codepoint); |
| 906 | break; |
| 907 | |
| 908 | case U'u': |
| 909 | if (!AppendUnicodeEscapeSequence(&iter, &xml_string_.text)) { |
| 910 | error_ = |
| 911 | StringPrintf("invalid unicode escape sequence in string\n\"%s\"", text.c_str()); |
| 912 | return *this; |
| 913 | } |
| 914 | break; |
| 915 | |
| 916 | default: |
| 917 | // Ignore the escape character and just include the codepoint. |
| 918 | AppendCodepointToUtf8String(codepoint, &xml_string_.text); |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | } else if (!preserve_spaces_ && codepoint == U'"') { |
| 923 | // Only toggle the quote state when we are not preserving spaces. |
| 924 | quote_ = !quote_; |
| 925 | |
| 926 | } else if (!preserve_spaces_ && !quote_ && codepoint == U'\'') { |
| 927 | // This should be escaped when we are not preserving spaces |
| 928 | error_ = StringPrintf("unescaped apostrophe in string\n\"%s\"", text.c_str()); |
| 929 | return *this; |
| 930 | |
| 931 | } else { |
| 932 | AppendCodepointToUtf8String(codepoint, &xml_string_.text); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | // Accumulate the added string's UTF-16 length. |
| 937 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(xml_string_.text.c_str()); |
| 938 | const size_t utf8_length = xml_string_.text.size(); |
| 939 | ssize_t len = utf8_to_utf16_length(utf8_data + previous_len, utf8_length - previous_len); |
| 940 | if (len < 0) { |
| 941 | error_ = StringPrintf("invalid unicode code point in string\n\"%s\"", utf8_data + previous_len); |
| 942 | return *this; |
| 943 | } |
| 944 | |
| 945 | utf16_len_ += static_cast<uint32_t>(len); |
| 946 | return *this; |
| 947 | } |
| 948 | |
| 949 | StringBuilder::SpanHandle StringBuilder::StartSpan(const std::string& name) { |
| 950 | if (!error_.empty()) { |
| 951 | return 0u; |
| 952 | } |
| 953 | |
| 954 | // When we start a span, all state associated with whitespace truncation and quotation is ended. |
| 955 | ResetTextState(); |
| 956 | Span span; |
| 957 | span.name = name; |
| 958 | span.first_char = span.last_char = utf16_len_; |
| 959 | xml_string_.spans.push_back(std::move(span)); |
| 960 | return xml_string_.spans.size() - 1; |
| 961 | } |
| 962 | |
| 963 | void StringBuilder::EndSpan(SpanHandle handle) { |
| 964 | if (!error_.empty()) { |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | // When we end a span, all state associated with whitespace truncation and quotation is ended. |
| 969 | ResetTextState(); |
| 970 | xml_string_.spans[handle].last_char = utf16_len_ - 1u; |
| 971 | } |
| 972 | |
| 973 | StringBuilder::UntranslatableHandle StringBuilder::StartUntranslatable() { |
| 974 | if (!error_.empty()) { |
| 975 | return 0u; |
| 976 | } |
| 977 | |
| 978 | UntranslatableSection section; |
| 979 | section.start = section.end = xml_string_.text.size(); |
| 980 | xml_string_.untranslatable_sections.push_back(section); |
| 981 | return xml_string_.untranslatable_sections.size() - 1; |
| 982 | } |
| 983 | |
| 984 | void StringBuilder::EndUntranslatable(UntranslatableHandle handle) { |
| 985 | if (!error_.empty()) { |
| 986 | return; |
| 987 | } |
| 988 | xml_string_.untranslatable_sections[handle].end = xml_string_.text.size(); |
| 989 | } |
| 990 | |
| 991 | FlattenedXmlString StringBuilder::GetFlattenedString() const { |
| 992 | return xml_string_; |
| 993 | } |
| 994 | |
| 995 | std::string StringBuilder::to_string() const { |
| 996 | return xml_string_.text; |
| 997 | } |
| 998 | |
| 999 | StringBuilder::operator bool() const { |
| 1000 | return error_.empty(); |
| 1001 | } |
| 1002 | |
| 1003 | std::string StringBuilder::GetError() const { |
| 1004 | return error_; |
| 1005 | } |
| 1006 | |
| 1007 | void StringBuilder::ResetTextState() { |
| 1008 | quote_ = preserve_spaces_; |
| 1009 | last_codepoint_was_space_ = false; |
| 1010 | } |
| 1011 | |
| 1012 | } // namespace ResourceUtils |
| 1013 | } // namespace aapt |