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 32 namespace android::idmap2 { 33 34 struct FabricatedOverlay { 35 struct Builder { 36 Builder(const std::string& package_name, const std::string& name, 37 const std::string& target_package_name); 38 39 Builder& SetOverlayable(const std::string& name); 40 41 Builder& SetResourceValue(const std::string& resource_name, uint8_t data_type, 42 uint32_t data_value); 43 44 WARN_UNUSED Result<FabricatedOverlay> Build(); 45 46 private: 47 struct Entry { 48 std::string resource_name; 49 DataType data_type; 50 DataValue data_value; 51 }; 52 53 std::string package_name_; 54 std::string name_; 55 std::string target_package_name_; 56 std::string target_overlayable_; 57 std::vector<Entry> entries_; 58 }; 59 60 Result<Unit> ToBinaryStream(std::ostream& stream) const; 61 static Result<FabricatedOverlay> FromBinaryStream(std::istream& stream); 62 63 private: 64 struct SerializedData { 65 std::unique_ptr<uint8_t[]> data; 66 size_t data_size; 67 uint32_t crc; 68 }; 69 70 Result<SerializedData*> InitializeData() const; 71 Result<uint32_t> GetCrc() const; 72 73 explicit FabricatedOverlay(pb::FabricatedOverlay&& overlay, 74 std::optional<uint32_t> crc_from_disk = {}); 75 76 pb::FabricatedOverlay overlay_pb_; 77 std::optional<uint32_t> crc_from_disk_; 78 mutable std::optional<SerializedData> data_; 79 80 friend struct FabricatedOverlayContainer; 81 }; 82 83 struct FabricatedOverlayContainer : public OverlayResourceContainer { 84 static Result<std::unique_ptr<FabricatedOverlayContainer>> FromPath(std::string path); 85 static std::unique_ptr<FabricatedOverlayContainer> FromOverlay(FabricatedOverlay&& overlay); 86 87 WARN_UNUSED OverlayManifestInfo GetManifestInfo() const; 88 89 // inherited from OverlayResourceContainer 90 WARN_UNUSED Result<OverlayManifestInfo> FindOverlayInfo(const std::string& name) const override; 91 WARN_UNUSED Result<OverlayData> GetOverlayData(const OverlayManifestInfo& info) const override; 92 93 // inherited from ResourceContainer 94 WARN_UNUSED Result<uint32_t> GetCrc() const override; 95 WARN_UNUSED const std::string& GetPath() const override; 96 WARN_UNUSED Result<std::string> GetResourceName(ResourceId id) const override; 97 98 ~FabricatedOverlayContainer() override; 99 100 private: 101 FabricatedOverlayContainer(FabricatedOverlay&& overlay, std::string&& path); 102 FabricatedOverlay overlay_; 103 std::string path_; 104 }; 105 106 } // namespace android::idmap2 107 108 #endif // IDMAP2_INCLUDE_IDMAP2_FABRICATEDOVERLAY_H_ 109