diff options
| author | 2020-12-14 21:29:26 +0000 | |
|---|---|---|
| committer | 2020-12-14 21:29:26 +0000 | |
| commit | ab46005fe8962e305d5ac6dfe8af711f404a672a (patch) | |
| tree | bc17474d7e3fca9d4001ef2ee960d1ff0d1b23d6 /libs/androidfw/ResourceUtils.cpp | |
| parent | 54c5257fb1aa44dc95dc4ab448def1dcf15add89 (diff) | |
| parent | 80094e39f90801c44cd80ab0f98df505828ea1f3 (diff) | |
Revert^2 "libandroidfw hardening for IncFs" am: 80094e39f9
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1519909
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: I9a570e6b8ef32bebe5477b1d784b07857c165e06
Diffstat (limited to 'libs/androidfw/ResourceUtils.cpp')
| -rw-r--r-- | libs/androidfw/ResourceUtils.cpp | 77 |
1 files changed, 46 insertions, 31 deletions
diff --git a/libs/androidfw/ResourceUtils.cpp b/libs/androidfw/ResourceUtils.cpp index c63dff8f9104..a34aa7239250 100644 --- a/libs/androidfw/ResourceUtils.cpp +++ b/libs/androidfw/ResourceUtils.cpp @@ -48,61 +48,76 @@ bool ExtractResourceName(const StringPiece& str, StringPiece* out_package, Strin !(has_type_separator && out_type->empty()); } -bool ToResourceName(const StringPoolRef& type_string_ref, - const StringPoolRef& entry_string_ref, - const StringPiece& package_name, - AssetManager2::ResourceName* out_name) { - out_name->package = package_name.data(); - out_name->package_len = package_name.size(); - - out_name->type = type_string_ref.string8(&out_name->type_len); - out_name->type16 = nullptr; - if (out_name->type == nullptr) { - out_name->type16 = type_string_ref.string16(&out_name->type_len); - if (out_name->type16 == nullptr) { - return false; +base::expected<AssetManager2::ResourceName, NullOrIOError> ToResourceName( + const StringPoolRef& type_string_ref, const StringPoolRef& entry_string_ref, + const StringPiece& package_name) { + AssetManager2::ResourceName name{ + .package = package_name.data(), + .package_len = package_name.size(), + }; + + if (base::expected<StringPiece, NullOrIOError> type_str = type_string_ref.string8()) { + name.type = type_str->data(); + name.type_len = type_str->size(); + } else if (UNLIKELY(IsIOError(type_str))) { + return base::unexpected(type_str.error()); + } + + if (name.type == nullptr) { + if (base::expected<StringPiece16, NullOrIOError> type16_str = type_string_ref.string16()) { + name.type16 = type16_str->data(); + name.type_len = type16_str->size(); + } else if (!type16_str.has_value()) { + return base::unexpected(type16_str.error()); } } - out_name->entry = entry_string_ref.string8(&out_name->entry_len); - out_name->entry16 = nullptr; - if (out_name->entry == nullptr) { - out_name->entry16 = entry_string_ref.string16(&out_name->entry_len); - if (out_name->entry16 == nullptr) { - return false; + if (base::expected<StringPiece, NullOrIOError> entry_str = entry_string_ref.string8()) { + name.entry = entry_str->data(); + name.entry_len = entry_str->size(); + } else if (UNLIKELY(IsIOError(entry_str))) { + return base::unexpected(entry_str.error()); + } + + if (name.entry == nullptr) { + if (base::expected<StringPiece16, NullOrIOError> entry16_str = entry_string_ref.string16()) { + name.entry16 = entry16_str->data(); + name.entry_len = entry16_str->size(); + } else if (!entry16_str.has_value()) { + return base::unexpected(entry16_str.error()); } } - return true; + return name; } -std::string ToFormattedResourceString(AssetManager2::ResourceName* resource_name) { +std::string ToFormattedResourceString(const AssetManager2::ResourceName& resource_name) { std::string result; - if (resource_name->package != nullptr) { - result.append(resource_name->package, resource_name->package_len); + if (resource_name.package != nullptr) { + result.append(resource_name.package, resource_name.package_len); } - if (resource_name->type != nullptr || resource_name->type16 != nullptr) { + if (resource_name.type != nullptr || resource_name.type16 != nullptr) { if (!result.empty()) { result += ":"; } - if (resource_name->type != nullptr) { - result.append(resource_name->type, resource_name->type_len); + if (resource_name.type != nullptr) { + result.append(resource_name.type, resource_name.type_len); } else { - result += util::Utf16ToUtf8(StringPiece16(resource_name->type16, resource_name->type_len)); + result += util::Utf16ToUtf8(StringPiece16(resource_name.type16, resource_name.type_len)); } } - if (resource_name->entry != nullptr || resource_name->entry16 != nullptr) { + if (resource_name.entry != nullptr || resource_name.entry16 != nullptr) { if (!result.empty()) { result += "/"; } - if (resource_name->entry != nullptr) { - result.append(resource_name->entry, resource_name->entry_len); + if (resource_name.entry != nullptr) { + result.append(resource_name.entry, resource_name.entry_len); } else { - result += util::Utf16ToUtf8(StringPiece16(resource_name->entry16, resource_name->entry_len)); + result += util::Utf16ToUtf8(StringPiece16(resource_name.entry16, resource_name.entry_len)); } } |