LoadedArsc.h revision c6aada9c5789b2777b19c522d3cd5052bbe784a4
1/* 2 * Copyright (C) 2016 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#ifndef LOADEDARSC_H_ 18#define LOADEDARSC_H_ 19 20#include <memory> 21#include <vector> 22 23#include "android-base/macros.h" 24 25#include "androidfw/ByteBucketArray.h" 26#include "androidfw/Chunk.h" 27#include "androidfw/ResourceTypes.h" 28#include "androidfw/Util.h" 29 30namespace android { 31 32class DynamicPackageEntry { 33 public: 34 DynamicPackageEntry() = default; 35 DynamicPackageEntry(std::string&& package_name, int package_id) 36 : package_name(std::move(package_name)), package_id(package_id) {} 37 38 std::string package_name; 39 int package_id = 0; 40}; 41 42struct LoadedArscEntry { 43 // A pointer to the resource table entry for this resource. 44 // If the size of the entry is > sizeof(ResTable_entry), it can be cast to 45 // a ResTable_map_entry and processed as a bag/map. 46 const ResTable_entry* entry = nullptr; 47 48 // The dynamic package ID map for the package from which this resource came from. 49 const DynamicRefTable* dynamic_ref_table = nullptr; 50 51 // The string pool reference to the type's name. This uses a different string pool than 52 // the global string pool, but this is hidden from the caller. 53 StringPoolRef type_string_ref; 54 55 // The string pool reference to the entry's name. This uses a different string pool than 56 // the global string pool, but this is hidden from the caller. 57 StringPoolRef entry_string_ref; 58}; 59 60struct TypeSpec; 61class LoadedArsc; 62 63class LoadedPackage { 64 friend class LoadedArsc; 65 66 public: 67 bool FindEntry(uint8_t type_idx, uint16_t entry_idx, const ResTable_config& config, 68 LoadedArscEntry* out_entry, ResTable_config* out_selected_config, 69 uint32_t* out_flags) const; 70 71 inline const ResStringPool* GetTypeStringPool() const { return &type_string_pool_; } 72 73 inline const ResStringPool* GetKeyStringPool() const { return &key_string_pool_; } 74 75 inline const std::string& GetPackageName() const { return package_name_; } 76 77 inline int GetPackageId() const { return package_id_; } 78 79 inline bool IsDynamic() const { return dynamic_; } 80 81 inline const std::vector<DynamicPackageEntry>& GetDynamicPackageMap() const { 82 return dynamic_package_map_; 83 } 84 85 private: 86 DISALLOW_COPY_AND_ASSIGN(LoadedPackage); 87 88 static std::unique_ptr<LoadedPackage> Load(const Chunk& chunk); 89 90 LoadedPackage() = default; 91 92 ResStringPool type_string_pool_; 93 ResStringPool key_string_pool_; 94 std::string package_name_; 95 int package_id_ = -1; 96 bool dynamic_ = false; 97 int type_id_offset_ = 0; 98 99 ByteBucketArray<util::unique_cptr<TypeSpec>> type_specs_; 100 std::vector<DynamicPackageEntry> dynamic_package_map_; 101}; 102 103// Read-only view into a resource table. This class validates all data 104// when loading, including offsets and lengths. 105class LoadedArsc { 106 public: 107 // Load the resource table from memory. The data's lifetime must out-live the 108 // object returned from this method. 109 static std::unique_ptr<LoadedArsc> Load(const void* data, size_t len, 110 bool load_as_shared_library = false); 111 112 ~LoadedArsc(); 113 114 // Returns the string pool where all string resource values 115 // (Res_value::dataType == Res_value::TYPE_STRING) are indexed. 116 inline const ResStringPool* GetStringPool() const { return &global_string_pool_; } 117 118 // Finds the resource with ID `resid` with the best value for configuration `config`. 119 // The parameter `out_entry` will be filled with the resulting resource entry. 120 // The resource entry can be a simple entry (ResTable_entry) or a complex bag 121 // (ResTable_entry_map). 122 bool FindEntry(uint32_t resid, const ResTable_config& config, LoadedArscEntry* out_entry, 123 ResTable_config* selected_config, uint32_t* out_flags) const; 124 125 // Gets a pointer to the name of the package in `resid`, or nullptr if the package doesn't exist. 126 const LoadedPackage* GetPackageForId(uint32_t resid) const; 127 128 inline const std::vector<std::unique_ptr<const LoadedPackage>>& GetPackages() const { 129 return packages_; 130 } 131 132 private: 133 DISALLOW_COPY_AND_ASSIGN(LoadedArsc); 134 135 LoadedArsc() = default; 136 bool LoadTable(const Chunk& chunk, bool load_as_shared_library); 137 138 ResStringPool global_string_pool_; 139 std::vector<std::unique_ptr<const LoadedPackage>> packages_; 140}; 141 142} // namespace android 143 144#endif /* LOADEDARSC_H_ */ 145