LoadedArsc.h revision 7ad1110ecd6a840fcd2895c62668828a1ca029c6
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/ResourceTypes.h"
26
27namespace android {
28
29class Chunk;
30class LoadedPackage;
31
32// Read-only view into a resource table. This class validates all data
33// when loading, including offsets and lengths.
34class LoadedArsc {
35 public:
36  // Load the resource table from memory. The data's lifetime must out-live the
37  // object returned from this method.
38  static std::unique_ptr<LoadedArsc> Load(const void* data, size_t len);
39
40  ~LoadedArsc();
41
42  // Returns the string pool where all string resource values
43  // (Res_value::dataType == Res_value::TYPE_STRING) are indexed.
44  inline const ResStringPool* GetStringPool() const { return &global_string_pool_; }
45
46  struct Entry {
47    // A pointer to the resource table entry for this resource.
48    // If the size of the entry is > sizeof(ResTable_entry), it can be cast to
49    // a ResTable_map_entry and processed as a bag/map.
50    const ResTable_entry* entry = nullptr;
51
52    // The string pool reference to the type's name. This uses a different string pool than
53    // the global string pool, but this is hidden from the caller.
54    StringPoolRef type_string_ref;
55
56    // The string pool reference to the entry's name. This uses a different string pool than
57    // the global string pool, but this is hidden from the caller.
58    StringPoolRef entry_string_ref;
59  };
60
61  // Finds the resource with ID `resid` with the best value for configuration `config`.
62  // The parameter `out_entry` will be filled with the resulting resource entry.
63  // The resource entry can be a simple entry (ResTable_entry) or a complex bag
64  // (ResTable_entry_map).
65  bool FindEntry(uint32_t resid, const ResTable_config& config, Entry* out_entry,
66                 ResTable_config* selected_config, uint32_t* out_flags) const;
67
68  // Gets a pointer to the name of the package in `resid`, or nullptr if the package doesn't exist.
69  const std::string* GetPackageNameForId(uint32_t resid) const;
70
71 private:
72  DISALLOW_COPY_AND_ASSIGN(LoadedArsc);
73
74  LoadedArsc() = default;
75  bool LoadTable(const Chunk& chunk);
76
77  ResStringPool global_string_pool_;
78  std::vector<std::unique_ptr<LoadedPackage>> packages_;
79};
80
81}  // namespace android
82
83#endif /* LOADEDARSC_H_ */
84