Revert^2 "libandroidfw hardening for IncFs"

55ef6167a2c235bd88c7216238b2001b46795b79

Change-Id: I02d4890d181655dfd0a14c188468db512559d27b
Merged-In: I02d4890d181655dfd0a14c188468db512559d27b
diff --git a/libs/androidfw/ResourceUtils.cpp b/libs/androidfw/ResourceUtils.cpp
index c63dff8..a34aa72 100644
--- a/libs/androidfw/ResourceUtils.cpp
+++ b/libs/androidfw/ResourceUtils.cpp
@@ -48,61 +48,76 @@
          !(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();
+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(),
+  };
 
-  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;
+  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));
     }
   }