diff options
author | Ryan Mitchell <rtmitchell@google.com> | 2020-11-16 23:08:18 +0000 |
---|---|---|
committer | Ryan Mitchell <rtmitchell@google.com> | 2020-11-17 23:01:35 +0000 |
commit | db21f09a8e02bcfd3fefea68084667688268f1fa (patch) | |
tree | c9b22a2676936eaf56ee1160c9349632ccd7ba44 /libs/androidfw/ResourceUtils.cpp | |
parent | 687f5e163f24ff31f822f986fd7a99b8832b6286 (diff) |
Revert^2 "libandroidfw hardening for IncFs"
55ef6167a2c235bd88c7216238b2001b46795b79
Change-Id: I02d4890d181655dfd0a14c188468db512559d27b
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)); } } |