Idmap format changes for bidirectional lookup
This change modifies the idmap file format to allow for target resources
to map to arbitrary type/value combinations and to allow overlay
resources to be mapped back to target resource ids so references to
overlay resources can appear as references to target resources at
runtime.
The mappings of target resources to overlay resources and vice-versa are
both encoded as sparse arrays. Instead of looking up a resource by
indexing into an array that maps to the overlay resource id, the runtime
will binary search over the sparse array to find the type and value that
overlays the target resource.
Bug: 135943783
Test: idmap2_tests
Change-Id: I5d5344cdb7fe35f4f2e8d6781016299dea5d1e20
diff --git a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp
index dee2d21..3b0940a 100644
--- a/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp
+++ b/cmds/idmap2/libidmap2/BinaryStreamVisitor.cpp
@@ -24,6 +24,14 @@
namespace android::idmap2 {
+void BinaryStreamVisitor::Write(const void* value, size_t length) {
+ stream_.write(reinterpret_cast<const char*>(value), length);
+}
+
+void BinaryStreamVisitor::Write8(uint8_t value) {
+ stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t));
+}
+
void BinaryStreamVisitor::Write16(uint16_t value) {
uint16_t x = htodl(value);
stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t));
@@ -54,26 +62,28 @@
WriteString(header.GetOverlayPath());
}
-void BinaryStreamVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
- // nothing to do
+void BinaryStreamVisitor::visit(const IdmapData& data) {
+ for (const auto& target_entry : data.GetTargetEntries()) {
+ Write32(target_entry.target_id);
+ Write8(target_entry.data_type);
+ Write32(target_entry.data_value);
+ }
+
+ for (const auto& overlay_entry : data.GetOverlayEntries()) {
+ Write32(overlay_entry.overlay_id);
+ Write32(overlay_entry.target_id);
+ }
+
+ Write(data.GetStringPoolData(), data.GetHeader()->GetStringPoolLength());
}
void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
- Write16(header.GetTargetPackageId());
- Write16(header.GetTypeCount());
-}
-
-void BinaryStreamVisitor::visit(const IdmapData::TypeEntry& type_entry) {
- const uint16_t entryCount = type_entry.GetEntryCount();
-
- Write16(type_entry.GetTargetTypeId());
- Write16(type_entry.GetOverlayTypeId());
- Write16(entryCount);
- Write16(type_entry.GetEntryOffset());
- for (uint16_t i = 0; i < entryCount; i++) {
- EntryId entry_id = type_entry.GetEntry(i);
- Write32(entry_id != kNoEntry ? static_cast<uint32_t>(entry_id) : kPadding);
- }
+ Write8(header.GetTargetPackageId());
+ Write8(header.GetOverlayPackageId());
+ Write32(header.GetTargetEntryCount());
+ Write32(header.GetOverlayEntryCount());
+ Write32(header.GetStringPoolIndexOffset());
+ Write32(header.GetStringPoolLength());
}
} // namespace android::idmap2