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