Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
| 19 | #include "androidfw/Idmap.h" |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | #include "android-base/stringprintf.h" |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 23 | #include "androidfw/misc.h" |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 24 | #include "androidfw/ResourceTypes.h" |
| 25 | #include "androidfw/Util.h" |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 26 | #include "utils/ByteOrder.h" |
| 27 | #include "utils/Trace.h" |
| 28 | |
| 29 | #ifdef _WIN32 |
| 30 | #ifdef ERROR |
| 31 | #undef ERROR |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 35 | using ::android::base::StringPrintf; |
| 36 | |
| 37 | namespace android { |
| 38 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 39 | // See frameworks/base/cmds/idmap2/include/idmap2/Idmap.h for full idmap file format specification. |
| 40 | struct Idmap_header { |
| 41 | // Always 0x504D4449 ('IDMP') |
| 42 | uint32_t magic; |
| 43 | uint32_t version; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 44 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 45 | uint32_t target_crc32; |
| 46 | uint32_t overlay_crc32; |
| 47 | |
| 48 | uint32_t fulfilled_policies; |
| 49 | uint32_t enforce_overlayable; |
| 50 | |
| 51 | // overlay_path, target_path, and other string values encoded in the idmap header and read and |
| 52 | // stored in separate structures. This allows the idmap header data to be casted to this struct |
| 53 | // without having to read/store each header entry separately. |
| 54 | }; |
| 55 | |
| 56 | struct Idmap_data_header { |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 57 | uint32_t target_entry_count; |
| 58 | uint32_t target_inline_entry_count; |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 59 | uint32_t target_inline_entry_value_count; |
| 60 | uint32_t configuration_count; |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 61 | uint32_t overlay_entry_count; |
| 62 | |
| 63 | uint32_t string_pool_index_offset; |
| 64 | }; |
| 65 | |
| 66 | struct Idmap_target_entry { |
| 67 | uint32_t target_id; |
| 68 | uint32_t overlay_id; |
| 69 | }; |
| 70 | |
| 71 | struct Idmap_target_entry_inline { |
| 72 | uint32_t target_id; |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 73 | uint32_t start_value_index; |
| 74 | uint32_t value_count; |
| 75 | }; |
| 76 | |
| 77 | struct Idmap_target_entry_inline_value { |
| 78 | uint32_t config_index; |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 79 | Res_value value; |
| 80 | }; |
| 81 | |
| 82 | struct Idmap_overlay_entry { |
| 83 | uint32_t overlay_id; |
| 84 | uint32_t target_id; |
| 85 | }; |
MÃ¥rten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 86 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 87 | OverlayStringPool::OverlayStringPool(const LoadedIdmap* loaded_idmap) |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 88 | : data_header_(loaded_idmap->data_header_), |
| 89 | idmap_string_pool_(loaded_idmap->string_pool_.get()) { }; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 90 | |
| 91 | OverlayStringPool::~OverlayStringPool() { |
| 92 | uninit(); |
| 93 | } |
| 94 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 95 | base::expected<StringPiece16, NullOrIOError> OverlayStringPool::stringAt(size_t idx) const { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 96 | const size_t offset = dtohl(data_header_->string_pool_index_offset); |
Ryan Mitchell | df9e732 | 2019-12-12 10:23:54 -0800 | [diff] [blame] | 97 | if (idmap_string_pool_ != nullptr && idx >= ResStringPool::size() && idx >= offset) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 98 | return idmap_string_pool_->stringAt(idx - offset); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 101 | return ResStringPool::stringAt(idx); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 104 | base::expected<StringPiece, NullOrIOError> OverlayStringPool::string8At(size_t idx) const { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 105 | const size_t offset = dtohl(data_header_->string_pool_index_offset); |
Ryan Mitchell | df9e732 | 2019-12-12 10:23:54 -0800 | [diff] [blame] | 106 | if (idmap_string_pool_ != nullptr && idx >= ResStringPool::size() && idx >= offset) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 107 | return idmap_string_pool_->string8At(idx - offset); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 110 | return ResStringPool::string8At(idx); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ryan Mitchell | df9e732 | 2019-12-12 10:23:54 -0800 | [diff] [blame] | 113 | size_t OverlayStringPool::size() const { |
| 114 | return ResStringPool::size() + (idmap_string_pool_ != nullptr ? idmap_string_pool_->size() : 0U); |
| 115 | } |
| 116 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 117 | OverlayDynamicRefTable::OverlayDynamicRefTable(const Idmap_data_header* data_header, |
| 118 | const Idmap_overlay_entry* entries, |
| 119 | uint8_t target_assigned_package_id) |
| 120 | : data_header_(data_header), |
| 121 | entries_(entries), |
| 122 | target_assigned_package_id_(target_assigned_package_id) { }; |
| 123 | |
| 124 | status_t OverlayDynamicRefTable::lookupResourceId(uint32_t* resId) const { |
| 125 | const Idmap_overlay_entry* first_entry = entries_; |
| 126 | const Idmap_overlay_entry* end_entry = entries_ + dtohl(data_header_->overlay_entry_count); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 127 | auto entry = std::lower_bound(first_entry, end_entry, *resId, |
| 128 | [](const Idmap_overlay_entry& e1, const uint32_t overlay_id) { |
| 129 | return dtohl(e1.overlay_id) < overlay_id; |
| 130 | }); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 131 | |
| 132 | if (entry == end_entry || dtohl(entry->overlay_id) != *resId) { |
| 133 | // A mapping for the target resource id could not be found. |
| 134 | return DynamicRefTable::lookupResourceId(resId); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 135 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 136 | |
| 137 | *resId = (0x00FFFFFFU & dtohl(entry->target_id)) |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 138 | | (((uint32_t) target_assigned_package_id_) << 24U); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 139 | return NO_ERROR; |
| 140 | } |
| 141 | |
| 142 | status_t OverlayDynamicRefTable::lookupResourceIdNoRewrite(uint32_t* resId) const { |
| 143 | return DynamicRefTable::lookupResourceId(resId); |
| 144 | } |
| 145 | |
| 146 | IdmapResMap::IdmapResMap(const Idmap_data_header* data_header, |
| 147 | const Idmap_target_entry* entries, |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 148 | const Idmap_target_entry_inline* inline_entries, |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 149 | const Idmap_target_entry_inline_value* inline_entry_values, |
| 150 | const ConfigDescription* configs, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 151 | uint8_t target_assigned_package_id, |
| 152 | const OverlayDynamicRefTable* overlay_ref_table) |
| 153 | : data_header_(data_header), |
| 154 | entries_(entries), |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 155 | inline_entries_(inline_entries), |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 156 | inline_entry_values_(inline_entry_values), |
| 157 | configurations_(configs), |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 158 | target_assigned_package_id_(target_assigned_package_id), |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 159 | overlay_ref_table_(overlay_ref_table) { } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 160 | |
| 161 | IdmapResMap::Result IdmapResMap::Lookup(uint32_t target_res_id) const { |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 162 | if ((target_res_id >> 24U) != target_assigned_package_id_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 163 | // The resource id must have the same package id as the target package. |
| 164 | return {}; |
| 165 | } |
| 166 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 167 | // The resource ids encoded within the idmap are build-time resource ids so do not consider the |
| 168 | // package id when determining if the resource in the target package is overlaid. |
| 169 | target_res_id &= 0x00FFFFFFU; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 170 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 171 | // Check if the target resource is mapped to an overlay resource. |
| 172 | auto first_entry = entries_; |
| 173 | auto end_entry = entries_ + dtohl(data_header_->target_entry_count); |
| 174 | auto entry = std::lower_bound(first_entry, end_entry, target_res_id, |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 175 | [](const Idmap_target_entry& e, const uint32_t target_id) { |
| 176 | return (0x00FFFFFFU & dtohl(e.target_id)) < target_id; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 177 | }); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 178 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 179 | if (entry != end_entry && (0x00FFFFFFU & dtohl(entry->target_id)) == target_res_id) { |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 180 | uint32_t overlay_resource_id = dtohl(entry->overlay_id); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 181 | // Lookup the resource without rewriting the overlay resource id back to the target resource id |
| 182 | // being looked up. |
| 183 | overlay_ref_table_->lookupResourceIdNoRewrite(&overlay_resource_id); |
| 184 | return Result(overlay_resource_id); |
| 185 | } |
| 186 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 187 | // Check if the target resources is mapped to an inline table entry. |
| 188 | auto first_inline_entry = inline_entries_; |
| 189 | auto end_inline_entry = inline_entries_ + dtohl(data_header_->target_inline_entry_count); |
| 190 | auto inline_entry = std::lower_bound(first_inline_entry, end_inline_entry, target_res_id, |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 191 | [](const Idmap_target_entry_inline& e, |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 192 | const uint32_t target_id) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 193 | return (0x00FFFFFFU & dtohl(e.target_id)) < target_id; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 194 | }); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 195 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 196 | if (inline_entry != end_inline_entry && |
| 197 | (0x00FFFFFFU & dtohl(inline_entry->target_id)) == target_res_id) { |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 198 | std::map<ConfigDescription, Res_value> values_map; |
| 199 | for (int i = 0; i < inline_entry->value_count; i++) { |
| 200 | const auto& value = inline_entry_values_[inline_entry->start_value_index + i]; |
| 201 | const auto& config = configurations_[value.config_index]; |
| 202 | values_map[config] = value.value; |
| 203 | } |
| 204 | return Result(values_map); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 205 | } |
| 206 | return {}; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 209 | namespace { |
| 210 | template <typename T> |
| 211 | const T* ReadType(const uint8_t** in_out_data_ptr, size_t* in_out_size, const std::string& label, |
| 212 | size_t count = 1) { |
| 213 | if (!util::IsFourByteAligned(*in_out_data_ptr)) { |
| 214 | LOG(ERROR) << "Idmap " << label << " is not word aligned."; |
| 215 | return {}; |
| 216 | } |
| 217 | if ((*in_out_size / sizeof(T)) < count) { |
| 218 | LOG(ERROR) << "Idmap too small for the number of " << label << " entries (" |
| 219 | << count << ")."; |
| 220 | return nullptr; |
| 221 | } |
| 222 | auto data_ptr = *in_out_data_ptr; |
| 223 | const size_t read_size = sizeof(T) * count; |
| 224 | *in_out_data_ptr += read_size; |
| 225 | *in_out_size -= read_size; |
| 226 | return reinterpret_cast<const T*>(data_ptr); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 229 | std::optional<std::string_view> ReadString(const uint8_t** in_out_data_ptr, size_t* in_out_size, |
| 230 | const std::string& label) { |
| 231 | const auto* len = ReadType<uint32_t>(in_out_data_ptr, in_out_size, label + " length"); |
| 232 | if (len == nullptr) { |
| 233 | return {}; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 234 | } |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 235 | const auto* data = ReadType<char>(in_out_data_ptr, in_out_size, label, *len); |
| 236 | if (data == nullptr) { |
| 237 | return {}; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 238 | } |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 239 | // Strings are padded to the next 4 byte boundary. |
| 240 | const uint32_t padding_size = (4U - ((size_t)*in_out_data_ptr & 0x3U)) % 4U; |
| 241 | for (uint32_t i = 0; i < padding_size; i++) { |
| 242 | if (**in_out_data_ptr != 0) { |
| 243 | LOG(ERROR) << " Idmap padding of " << label << " is non-zero."; |
| 244 | return {}; |
| 245 | } |
| 246 | *in_out_data_ptr += sizeof(uint8_t); |
| 247 | *in_out_size -= sizeof(uint8_t); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 248 | } |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 249 | return std::string_view(data, *len); |
| 250 | } |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 251 | } // namespace |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 252 | |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 253 | LoadedIdmap::LoadedIdmap(std::string&& idmap_path, |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 254 | const Idmap_header* header, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 255 | const Idmap_data_header* data_header, |
| 256 | const Idmap_target_entry* target_entries, |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 257 | const Idmap_target_entry_inline* target_inline_entries, |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 258 | const Idmap_target_entry_inline_value* inline_entry_values, |
| 259 | const ConfigDescription* configs, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 260 | const Idmap_overlay_entry* overlay_entries, |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 261 | std::unique_ptr<ResStringPool>&& string_pool, |
| 262 | std::string_view overlay_apk_path, |
| 263 | std::string_view target_apk_path) |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 264 | : header_(header), |
| 265 | data_header_(data_header), |
| 266 | target_entries_(target_entries), |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 267 | target_inline_entries_(target_inline_entries), |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 268 | inline_entry_values_(inline_entry_values), |
| 269 | configurations_(configs), |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 270 | overlay_entries_(overlay_entries), |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 271 | string_pool_(std::move(string_pool)), |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 272 | idmap_path_(std::move(idmap_path)), |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 273 | overlay_apk_path_(overlay_apk_path), |
| 274 | target_apk_path_(target_apk_path), |
| 275 | idmap_last_mod_time_(getFileModDate(idmap_path_.data())) {} |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 276 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 277 | std::unique_ptr<LoadedIdmap> LoadedIdmap::Load(StringPiece idmap_path, StringPiece idmap_data) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 278 | ATRACE_CALL(); |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 279 | size_t data_size = idmap_data.size(); |
| 280 | auto data_ptr = reinterpret_cast<const uint8_t*>(idmap_data.data()); |
| 281 | |
| 282 | // Parse the idmap header |
| 283 | auto header = ReadType<Idmap_header>(&data_ptr, &data_size, "header"); |
| 284 | if (header == nullptr) { |
| 285 | return {}; |
| 286 | } |
| 287 | if (dtohl(header->magic) != kIdmapMagic) { |
| 288 | LOG(ERROR) << StringPrintf("Invalid Idmap file: bad magic value (was 0x%08x, expected 0x%08x)", |
| 289 | dtohl(header->magic), kIdmapMagic); |
| 290 | return {}; |
| 291 | } |
| 292 | if (dtohl(header->version) != kIdmapCurrentVersion) { |
| 293 | // We are strict about versions because files with this format are generated at runtime and |
| 294 | // don't need backwards compatibility. |
| 295 | LOG(ERROR) << StringPrintf("Version mismatch in Idmap (was 0x%08x, expected 0x%08x)", |
| 296 | dtohl(header->version), kIdmapCurrentVersion); |
| 297 | return {}; |
| 298 | } |
| 299 | std::optional<std::string_view> overlay_path = ReadString(&data_ptr, &data_size, "overlay path"); |
| 300 | if (!overlay_path) { |
| 301 | return {}; |
| 302 | } |
| 303 | std::optional<std::string_view> target_path = ReadString(&data_ptr, &data_size, "target path"); |
| 304 | if (!target_path) { |
| 305 | return {}; |
| 306 | } |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 307 | if (!ReadString(&data_ptr, &data_size, "target name") || |
| 308 | !ReadString(&data_ptr, &data_size, "debug info")) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 309 | return {}; |
| 310 | } |
| 311 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 312 | // Parse the idmap data blocks. Currently idmap2 can only generate one data block. |
| 313 | auto data_header = ReadType<Idmap_data_header>(&data_ptr, &data_size, "data header"); |
| 314 | if (data_header == nullptr) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 315 | return {}; |
| 316 | } |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 317 | auto target_entries = ReadType<Idmap_target_entry>(&data_ptr, &data_size, "target", |
| 318 | dtohl(data_header->target_entry_count)); |
| 319 | if (target_entries == nullptr) { |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 320 | return {}; |
| 321 | } |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 322 | auto target_inline_entries = ReadType<Idmap_target_entry_inline>( |
| 323 | &data_ptr, &data_size, "target inline", dtohl(data_header->target_inline_entry_count)); |
| 324 | if (target_inline_entries == nullptr) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 325 | return {}; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 326 | } |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 327 | |
| 328 | auto target_inline_entry_values = ReadType<Idmap_target_entry_inline_value>( |
| 329 | &data_ptr, &data_size, "target inline values", |
| 330 | dtohl(data_header->target_inline_entry_value_count)); |
| 331 | if (target_inline_entry_values == nullptr) { |
| 332 | return {}; |
| 333 | } |
| 334 | |
| 335 | auto configurations = ReadType<ConfigDescription>( |
| 336 | &data_ptr, &data_size, "configurations", |
| 337 | dtohl(data_header->configuration_count)); |
| 338 | if (configurations == nullptr) { |
| 339 | return {}; |
| 340 | } |
| 341 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 342 | auto overlay_entries = ReadType<Idmap_overlay_entry>(&data_ptr, &data_size, "target inline", |
| 343 | dtohl(data_header->overlay_entry_count)); |
| 344 | if (overlay_entries == nullptr) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 345 | return {}; |
| 346 | } |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 347 | std::optional<std::string_view> string_pool = ReadString(&data_ptr, &data_size, "string pool"); |
| 348 | if (!string_pool) { |
| 349 | return {}; |
| 350 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 351 | auto idmap_string_pool = util::make_unique<ResStringPool>(); |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 352 | if (!string_pool->empty()) { |
| 353 | const status_t err = idmap_string_pool->setTo(string_pool->data(), string_pool->size()); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 354 | if (err != NO_ERROR) { |
| 355 | LOG(ERROR) << "idmap string pool corrupt."; |
| 356 | return {}; |
| 357 | } |
| 358 | } |
| 359 | |
Ryan Mitchell | 831b072 | 2021-01-13 10:11:18 -0800 | [diff] [blame] | 360 | if (data_size != 0) { |
| 361 | LOG(ERROR) << "idmap parsed with " << data_size << "bytes remaining"; |
| 362 | return {}; |
| 363 | } |
| 364 | |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 365 | // Can't use make_unique because LoadedIdmap constructor is private. |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 366 | return std::unique_ptr<LoadedIdmap>( |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 367 | new LoadedIdmap(std::string(idmap_path), header, data_header, target_entries, |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame] | 368 | target_inline_entries, target_inline_entry_values, configurations, |
| 369 | overlay_entries, std::move(idmap_string_pool), *target_path, *overlay_path)); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Ryan Mitchell | a909305 | 2020-03-26 17:15:01 -0700 | [diff] [blame] | 372 | bool LoadedIdmap::IsUpToDate() const { |
| 373 | return idmap_last_mod_time_ == getFileModDate(idmap_path_.c_str()); |
| 374 | } |
| 375 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 376 | } // namespace android |