Fail idmap parsing earlier if version differs

Variable length strings are now in the idmap format. Idmap attempts to
parse the old idmap files using the new format and can interpret old
data as the length of a variable length string.

Fail parsing idmap header early if the idmap magic or version differs
from what is expected.

Bug: 177295273
Test: boot, apply change, reboot
Change-Id: I2914f11547424457fdb78de0ad03976e1fc81084
diff --git a/cmds/idmap2/libidmap2/Idmap.cpp b/cmds/idmap2/libidmap2/Idmap.cpp
index 5af84b0..a0cc407 100644
--- a/cmds/idmap2/libidmap2/Idmap.cpp
+++ b/cmds/idmap2/libidmap2/Idmap.cpp
@@ -103,9 +103,18 @@
 
 std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
   std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
+  if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_)) {
+    return nullptr;
+  }
+
+  if (idmap_header->magic_ != kIdmapMagic ||
+      idmap_header->version_ != kIdmapCurrentVersion) {
+    // Do not continue parsing if the file is not a current version idmap.
+    return nullptr;
+  }
+
   uint32_t enforce_overlayable;
-  if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
-      !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
+  if (!Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
       !Read32(stream, &idmap_header->fulfilled_policies_) ||
       !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
       !ReadString(stream, &idmap_header->overlay_path_) ||