Remove idmap path 256 length limit

Overlay and target package paths can be longer than 256 characters.
Currently, the idmap will fail to be generated if either path
is longer than 256 characters.

This change removes the 256 character limit and makes parsing variable
length strings easier in libandroidfw.

Bug: 174676094
Test: idmap2_tests && libandroidfw_tests
Change-Id: Ic240cdb8700566b2ac2ade08da58bea852e4ae0c
diff --git a/cmds/idmap2/libidmap2/Idmap.cpp b/cmds/idmap2/libidmap2/Idmap.cpp
index 1129413..4745cc6 100644
--- a/cmds/idmap2/libidmap2/Idmap.cpp
+++ b/cmds/idmap2/libidmap2/Idmap.cpp
@@ -69,38 +69,26 @@
   return false;
 }
 
-// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
-bool WARN_UNUSED ReadString256(std::istream& stream, char out[kIdmapStringLength]) {
-  char buf[kIdmapStringLength];
-  memset(buf, 0, sizeof(buf));
-  if (!stream.read(buf, sizeof(buf))) {
-    return false;
-  }
-  if (buf[sizeof(buf) - 1] != '\0') {
-    return false;
-  }
-  memcpy(out, buf, sizeof(buf));
-  return true;
-}
-
-Result<std::string> ReadString(std::istream& stream) {
+bool WARN_UNUSED ReadString(std::istream& stream, std::string* out) {
   uint32_t size;
   if (!Read32(stream, &size)) {
-    return Error("failed to read string size");
+    return false;
   }
   if (size == 0) {
-    return std::string("");
+    *out = "";
+    return true;
   }
   std::string buf(size, '\0');
   if (!stream.read(buf.data(), size)) {
-    return Error("failed to read string of size %u", size);
+    return false;
   }
   uint32_t padding_size = CalculatePadding(size);
   std::string padding(padding_size, '\0');
   if (!stream.read(padding.data(), padding_size)) {
-    return Error("failed to read string padding of size %u", padding_size);
+    return false;
   }
-  return buf;
+  *out = buf;
+  return true;
 }
 
 }  // namespace
@@ -119,28 +107,23 @@
   if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
       !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
       !Read32(stream, &idmap_header->fulfilled_policies_) ||
-      !Read32(stream, &enforce_overlayable) || !ReadString256(stream, idmap_header->target_path_) ||
-      !ReadString256(stream, idmap_header->overlay_path_)) {
+      !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
+      !ReadString(stream, &idmap_header->overlay_path_) ||
+      !ReadString(stream, &idmap_header->debug_info_)) {
     return nullptr;
   }
 
   idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
-
-  auto debug_str = ReadString(stream);
-  if (!debug_str) {
-    return nullptr;
-  }
-  idmap_header->debug_info_ = std::move(*debug_str);
-
   return std::move(idmap_header);
 }
 
-Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overlay_path,
+Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
+                                     const std::string& overlay_path,
                                      PolicyBitmask fulfilled_policies,
                                      bool enforce_overlayable) const {
   const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path);
   if (!target_zip) {
-    return Error("failed to open target %s", target_path);
+    return Error("failed to open target %s", target_path.c_str());
   }
 
   const Result<uint32_t> target_crc = GetPackageCrc(*target_zip);
@@ -150,7 +133,7 @@
 
   const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path);
   if (!overlay_zip) {
-    return Error("failed to overlay target %s", overlay_path);
+    return Error("failed to overlay target %s", overlay_path.c_str());
   }
 
   const Result<uint32_t> overlay_crc = GetPackageCrc(*overlay_zip);
@@ -162,9 +145,9 @@
                     enforce_overlayable);
 }
 
-Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overlay_path,
-                                     uint32_t target_crc, uint32_t overlay_crc,
-                                     PolicyBitmask fulfilled_policies,
+Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
+                                     const std::string& overlay_path, uint32_t target_crc,
+                                     uint32_t overlay_crc, PolicyBitmask fulfilled_policies,
                                      bool enforce_overlayable) const {
   if (magic_ != kIdmapMagic) {
     return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
@@ -194,14 +177,14 @@
                  enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
   }
 
-  if (strcmp(target_path, target_path_) != 0) {
-    return Error("bad target path: idmap version %s, file system version %s", target_path,
-                 target_path_);
+  if (target_path != target_path_) {
+    return Error("bad target path: idmap version %s, file system version %s", target_path.c_str(),
+                 target_path_.c_str());
   }
 
-  if (strcmp(overlay_path, overlay_path_) != 0) {
-    return Error("bad overlay path: idmap version %s, file system version %s", overlay_path,
-                 overlay_path_);
+  if (overlay_path != overlay_path_) {
+    return Error("bad overlay path: idmap version %s, file system version %s", overlay_path.c_str(),
+                 overlay_path_.c_str());
   }
 
   return Unit{};
@@ -262,12 +245,9 @@
   }
 
   // Read raw string pool bytes.
-  auto string_pool_data = ReadString(stream);
-  if (!string_pool_data) {
+  if (!ReadString(stream, &data->string_pool_data_)) {
     return nullptr;
   }
-  data->string_pool_data_ = std::move(*string_pool_data);
-
   return std::move(data);
 }
 
@@ -368,23 +348,10 @@
     return Error(crc.GetError(), "failed to get zip CRC for overlay");
   }
   header->overlay_crc_ = *crc;
-
   header->fulfilled_policies_ = fulfilled_policies;
   header->enforce_overlayable_ = enforce_overlayable;
-
-  if (target_apk_path.size() > sizeof(header->target_path_)) {
-    return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
-                 sizeof(header->target_path_));
-  }
-  memset(header->target_path_, 0, sizeof(header->target_path_));
-  memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
-
-  if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
-    return Error("overlay apk path \"%s\" longer than maximum size %zu", overlay_apk_path.c_str(),
-                 sizeof(header->target_path_));
-  }
-  memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
-  memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
+  header->target_path_ = target_apk_path;
+  header->overlay_path_ = overlay_apk_path;
 
   auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
   if (!overlay_info) {