1 package org.robolectric.res.android; 2 3 // transliterated from 4 // https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/libs/androidfw/Idmap.cpp and 5 // https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/libs/androidfw/include/androidfw/Idmap.h 6 7 import static org.robolectric.res.android.Util.ATRACE_CALL; 8 import static org.robolectric.res.android.Util.SIZEOF_CPTR; 9 import static org.robolectric.res.android.Util.SIZEOF_INT; 10 import static org.robolectric.res.android.Util.dtohl; 11 import static org.robolectric.res.android.Util.dtohs; 12 import static org.robolectric.res.android.Util.logError; 13 14 import java.util.HashMap; 15 import java.util.Map; 16 import org.robolectric.res.android.ResourceTypes.IdmapEntry_header; 17 import org.robolectric.res.android.ResourceTypes.Idmap_header; 18 19 // #define ATRACE_TAG ATRACE_TAG_RESOURCES 20 // 21 // #include "androidfw/Idmap.h" 22 // 23 // #include "android-base/logging.h" 24 // #include "android-base/stringprintf.h" 25 // #include "utils/ByteOrder.h" 26 // #include "utils/Trace.h" 27 // 28 // #ifdef _WIN32 29 // #ifdef ERROR 30 // #undef ERROR 31 // #endif 32 // #endif 33 // 34 // #include "androidfw/ResourceTypes.h" 35 // 36 // using ::android::base::StringPrintf; 37 // 38 // namespace android { 39 class Idmap { 40 is_valid_package_id(short id)41 static boolean is_valid_package_id(short id) { 42 return id != 0 && id <= 255; 43 } 44 is_valid_type_id(short id)45 static boolean is_valid_type_id(short id) { 46 // Type IDs and package IDs have the same constraints in the IDMAP. 47 return is_valid_package_id(id); 48 } 49 50 // Represents a loaded/parsed IDMAP for a Runtime Resource Overlay (RRO). 51 // An RRO and its target APK have different resource IDs assigned to their resources. Overlaying 52 // a resource is done by resource name. An IDMAP is a generated mapping between the resource IDs 53 // of the RRO and the target APK for each resource with the same name. 54 // A LoadedIdmap can be set alongside the overlay's LoadedArsc to allow the overlay ApkAssets to 55 // masquerade as the target ApkAssets resources. 56 static class LoadedIdmap { 57 Idmap_header header_ = null; 58 String overlay_apk_path_; 59 final Map<Byte, IdmapEntry_header> type_map_ = new HashMap<>(); 60 LoadedIdmap(Idmap_header header_)61 public LoadedIdmap(Idmap_header header_) { 62 this.header_ = header_; 63 } 64 65 // Performs a lookup of the expected entry ID for the given IDMAP entry header. 66 // Returns true if the mapping exists and fills `output_entry_id` with the result. Lookup(IdmapEntry_header header, int input_entry_id, final Ref<Integer> output_entry_id)67 static boolean Lookup(IdmapEntry_header header, int input_entry_id, 68 final Ref<Integer> output_entry_id) { 69 if (input_entry_id < dtohs(header.entry_id_offset)) { 70 // After applying the offset, the entry is not present. 71 return false; 72 } 73 74 input_entry_id -= dtohs(header.entry_id_offset); 75 if (input_entry_id >= dtohs(header.entry_count)) { 76 // The entry is not present. 77 return false; 78 } 79 80 int result = dtohl(header.entries[input_entry_id]); 81 if (result == 0xffffffff) { 82 return false; 83 } 84 output_entry_id.set(result); 85 return true; 86 } 87 is_word_aligned(int offset)88 static boolean is_word_aligned(int offset) { 89 return (offset & 0x03) == 0; 90 } 91 92 @SuppressWarnings("DoNotCallSuggester") IsValidIdmapHeader(StringPiece data)93 static boolean IsValidIdmapHeader(StringPiece data) { 94 throw new UnsupportedOperationException(); 95 // if (!is_word_aligned(data.data())) { 96 // LOG(ERROR) << "Idmap header is not word aligned."; 97 // return false; 98 // } 99 // 100 // if (data.size() < sizeof(Idmap_header)) { 101 // LOG(ERROR) << "Idmap header is too small."; 102 // return false; 103 // } 104 // 105 // const Idmap_header* header = reinterpret_cast<const Idmap_header*>(data.data()); 106 // if (dtohl(header->magic) != kIdmapMagic) { 107 // LOG(ERROR) << StringPrintf("Invalid Idmap file: bad magic value (was 0x%08x, expected 0x%08x)", 108 // dtohl(header->magic), kIdmapMagic); 109 // return false; 110 // } 111 // 112 // if (dtohl(header->version) != kIdmapCurrentVersion) { 113 // // We are strict about versions because files with this format are auto-generated and don't need 114 // // backwards compatibility. 115 // LOG(ERROR) << StringPrintf("Version mismatch in Idmap (was 0x%08x, expected 0x%08x)", 116 // dtohl(header->version), kIdmapCurrentVersion); 117 // return false; 118 // } 119 // 120 // if (!is_valid_package_id(dtohs(header->target_package_id))) { 121 // LOG(ERROR) << StringPrintf("Target package ID in Idmap is invalid: 0x%02x", 122 // dtohs(header->target_package_id)); 123 // return false; 124 // } 125 // 126 // if (dtohs(header->type_count) > 255) { 127 // LOG(ERROR) << StringPrintf("Idmap has too many type mappings (was %d, max 255)", 128 // (int)dtohs(header->type_count)); 129 // return false; 130 // } 131 // return true; 132 } 133 134 // LoadedIdmap::LoadedIdmap(const Idmap_header* header) : header_(header) { 135 // size_t length = strnlen(reinterpret_cast<const char*>(header_->overlay_path), 136 // arraysize(header_->overlay_path)); 137 // overlay_apk_path_.assign(reinterpret_cast<const char*>(header_->overlay_path), length); 138 // } 139 // Loads an IDMAP from a chunk of memory. Returns nullptr if the IDMAP data was malformed. Load(StringPiece idmap_data)140 LoadedIdmap Load(StringPiece idmap_data) { 141 ATRACE_CALL(); 142 if (!IsValidIdmapHeader(idmap_data)) { 143 return emptyBraces(); 144 } 145 146 // Idmap_header header = reinterpret_cast<const Idmap_header*>(idmap_data.data()); 147 Idmap_header header = idmap_data.asIdmap_header(); 148 149 // Can't use make_unique because LoadedImpl constructor is private. 150 LoadedIdmap loaded_idmap = new LoadedIdmap(header); 151 152 // const byte* data_ptr = reinterpret_cast<const byte*>(idmap_data.data()) + sizeof(*header); 153 StringPiece data_ptr = new StringPiece(idmap_data.myBuf(), 154 idmap_data.myOffset() + SIZEOF_CPTR); 155 // int data_size = idmap_data.size() - sizeof(*header); 156 int data_size = idmap_data.size() - SIZEOF_CPTR; 157 158 int type_maps_encountered = 0; 159 while (data_size >= IdmapEntry_header.SIZEOF) { 160 if (!is_word_aligned(data_ptr.myOffset())) { 161 logError("Type mapping in Idmap is not word aligned"); 162 return emptyBraces(); 163 } 164 165 // Validate the type IDs. 166 // IdmapEntry_header entry_header = reinterpret_cast<const IdmapEntry_header*>(data_ptr); 167 IdmapEntry_header entry_header = new IdmapEntry_header(data_ptr.myBuf(), data_ptr.myOffset()); 168 if (!is_valid_type_id(dtohs(entry_header.target_type_id)) || !is_valid_type_id(dtohs(entry_header.overlay_type_id))) { 169 logError(String.format("Invalid type map (0x%02x -> 0x%02x)", 170 dtohs(entry_header.target_type_id), 171 dtohs(entry_header.overlay_type_id))); 172 return emptyBraces(); 173 } 174 175 // Make sure there is enough space for the entries declared in the header. 176 if ((data_size - SIZEOF_CPTR) / SIZEOF_INT < dtohs(entry_header.entry_count)) { 177 logError(String.format("Idmap too small for the number of entries (%d)", 178 (int) dtohs(entry_header.entry_count))); 179 return emptyBraces(); 180 } 181 182 // Only add a non-empty overlay. 183 if (dtohs(entry_header.entry_count) != 0) { 184 // loaded_idmap.type_map_[static_cast<byte>(dtohs(entry_header.overlay_type_id))] = 185 // entry_header; 186 loaded_idmap.type_map_.put((byte) dtohs(entry_header.overlay_type_id), 187 entry_header); 188 } 189 190 // int entry_size_bytes = 191 // sizeof(*entry_header) + (dtohs(entry_header.entry_count) * SIZEOF_INT); 192 int entry_size_bytes = 193 SIZEOF_CPTR + (dtohs(entry_header.entry_count) * SIZEOF_INT); 194 data_ptr = new StringPiece(data_ptr.myBuf(), data_ptr.myOffset() + entry_size_bytes); 195 data_size -= entry_size_bytes; 196 type_maps_encountered++; 197 } 198 199 // Verify that we parsed all the type maps. 200 if (type_maps_encountered != dtohs(header.type_count)) { 201 logError("Parsed " + type_maps_encountered + " type maps but expected " 202 + (int) dtohs(header.type_count)); 203 return emptyBraces(); 204 } 205 // return std.move(loaded_idmap); 206 return loaded_idmap; 207 } 208 emptyBraces()209 private LoadedIdmap emptyBraces() { 210 return new LoadedIdmap(null); 211 } 212 213 // Returns the package ID for which this overlay should apply. TargetPackageId()214 int TargetPackageId() { 215 return dtohs(header_.target_package_id); 216 } 217 218 // Returns the path to the RRO (Runtime Resource Overlay) APK for which this IDMAP was generated. OverlayApkPath()219 String OverlayApkPath() { 220 return overlay_apk_path_; 221 } 222 223 // Returns the mapping of target entry ID to overlay entry ID for the given target type. GetEntryMapForType(byte type_id)224 IdmapEntry_header GetEntryMapForType(byte type_id) { 225 // auto iter = type_map_.find(type_id); 226 // if (iter != type_map_.end()) { 227 // return iter.second; 228 // } 229 // return null; 230 return type_map_.get(type_id); 231 } 232 // 233 // } // namespace android 234 } 235 }