• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 IDMAP2_INCLUDE_IDMAP2_FABRICATEDOVERLAY_H_
18 #define IDMAP2_INCLUDE_IDMAP2_FABRICATEDOVERLAY_H_
19 
20 #include <libidmap2/proto/fabricated_v1.pb.h>
21 
22 #include "androidfw/Streams.h"
23 
24 #include <istream>
25 #include <map>
26 #include <memory>
27 #include <ostream>
28 #include <string>
29 #include <unordered_map>
30 #include <vector>
31 
32 #include "idmap2/ResourceContainer.h"
33 #include "idmap2/Result.h"
34 #include <binder/ParcelFileDescriptor.h>
35 
36 namespace android::idmap2 {
37 
38 struct FabricatedOverlay {
39   struct Builder {
40     Builder(const std::string& package_name, const std::string& name,
41             const std::string& target_package_name);
42 
43     Builder& SetOverlayable(const std::string& name);
44 
45     Builder& SetResourceValue(const std::string& resource_name, uint8_t data_type,
46                               uint32_t data_value, const std::string& configuration);
47 
48     Builder& SetResourceValue(const std::string& resource_name, uint8_t data_type,
49                               const std::string& data_string_value,
50                               const std::string& configuration);
51 
52     Builder& SetResourceValue(const std::string& resource_name,
53                               std::optional<android::base::borrowed_fd>&& binary_value,
54                               off64_t data_binary_offset,
55                               size_t data_binary_size,
56                               const std::string& configuration,
57                               bool nine_patch);
58 
setFrroPathFabricatedOverlay::Builder59     inline Builder& setFrroPath(std::string frro_path) {
60       frro_path_ = std::move(frro_path);
61       return *this;
62     }
63 
64     WARN_UNUSED Result<FabricatedOverlay> Build();
65 
66    private:
67     struct Entry {
68       std::string resource_name;
69       DataType data_type;
70       DataValue data_value;
71       std::string data_string_value;
72       std::optional<android::base::borrowed_fd> data_binary_value;
73       off64_t data_binary_offset;
74       size_t data_binary_size;
75       std::string configuration;
76       bool nine_patch;
77     };
78 
79     std::string package_name_;
80     std::string name_;
81     std::string target_package_name_;
82     std::string target_overlayable_;
83     std::string frro_path_;
84     std::vector<Entry> entries_;
85   };
86 
87   struct BinaryData {
88     std::unique_ptr<android::InputStream> input_stream;
89     off64_t offset;
90     size_t size;
91   };
92 
93   Result<Unit> ToBinaryStream(std::ostream& stream) const;
94   static Result<FabricatedOverlay> FromBinaryStream(std::istream& stream);
95 
96  private:
97   struct SerializedData {
98     std::unique_ptr<uint8_t[]> pb_data;
99     size_t pb_data_size;
100     uint32_t pb_crc;
101     std::string sp_data;
102    };
103 
104   Result<SerializedData*> InitializeData() const;
105   Result<uint32_t> GetCrc() const;
106 
107   explicit FabricatedOverlay(pb::FabricatedOverlay&& overlay,
108                              std::string&& string_pool_data_,
109                              std::vector<FabricatedOverlay::BinaryData> binary_files_,
110                              off_t total_binary_bytes_,
111                              std::optional<uint32_t> crc_from_disk = {});
112 
113   pb::FabricatedOverlay overlay_pb_;
114   std::string string_pool_data_;
115   std::vector<FabricatedOverlay::BinaryData> binary_files_;
116   uint32_t total_binary_bytes_;
117   std::optional<uint32_t> crc_from_disk_;
118   mutable std::optional<SerializedData> data_;
119 
120   friend struct FabricatedOverlayContainer;
121 };
122 
123 struct FabricatedOverlayContainer : public OverlayResourceContainer {
124   static Result<std::unique_ptr<FabricatedOverlayContainer>> FromPath(std::string path);
125   static std::unique_ptr<FabricatedOverlayContainer> FromOverlay(FabricatedOverlay&& overlay);
126 
127   WARN_UNUSED OverlayManifestInfo GetManifestInfo() const;
128 
129   // inherited from OverlayResourceContainer
130   WARN_UNUSED Result<OverlayManifestInfo> FindOverlayInfo(const std::string& name) const override;
131   WARN_UNUSED Result<OverlayData> GetOverlayData(const OverlayManifestInfo& info) const override;
132 
133   // inherited from ResourceContainer
134   WARN_UNUSED Result<uint32_t> GetCrc() const override;
135   WARN_UNUSED const std::string& GetPath() const override;
136   WARN_UNUSED Result<std::string> GetResourceName(ResourceId id) const override;
137 
138   ~FabricatedOverlayContainer() override;
139 
140  private:
141   FabricatedOverlayContainer(FabricatedOverlay&& overlay, std::string&& path);
142   FabricatedOverlay overlay_;
143   std::string path_;
144 };
145 
146 }  // namespace android::idmap2
147 
148 #endif  // IDMAP2_INCLUDE_IDMAP2_FABRICATEDOVERLAY_H_
149