Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 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 | #include "idmap2/FabricatedOverlay.h" |
| 18 | |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 19 | #include <sys/stat.h> // umask |
| 20 | #include <sys/types.h> // umask |
| 21 | |
| 22 | #include <android-base/file.h> |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 23 | #include <androidfw/ResourceUtils.h> |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 24 | #include <androidfw/StringPool.h> |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 25 | #include <google/protobuf/io/coded_stream.h> |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 26 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 27 | #include <utils/ByteOrder.h> |
| 28 | #include <zlib.h> |
| 29 | |
| 30 | #include <fstream> |
Mårten Kongstad | 1195a6b | 2021-05-11 12:57:01 +0000 | [diff] [blame] | 31 | #include <map> |
| 32 | #include <memory> |
| 33 | #include <string> |
| 34 | #include <utility> |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android::idmap2 { |
| 37 | |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 38 | constexpr auto kBufferSize = 1024; |
| 39 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 40 | namespace { |
| 41 | bool Read32(std::istream& stream, uint32_t* out) { |
| 42 | uint32_t value; |
| 43 | if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) { |
| 44 | *out = dtohl(value); |
| 45 | return true; |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | void Write32(std::ostream& stream, uint32_t value) { |
| 51 | uint32_t x = htodl(value); |
| 52 | stream.write(reinterpret_cast<char*>(&x), sizeof(uint32_t)); |
| 53 | } |
| 54 | } // namespace |
| 55 | |
| 56 | FabricatedOverlay::FabricatedOverlay(pb::FabricatedOverlay&& overlay, |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 57 | std::string&& string_pool_data, |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 58 | std::vector<android::base::borrowed_fd> binary_files, |
| 59 | off_t total_binary_bytes, |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 60 | std::optional<uint32_t> crc_from_disk) |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 61 | : overlay_pb_(std::forward<pb::FabricatedOverlay>(overlay)), |
| 62 | string_pool_data_(std::move(string_pool_data)), |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 63 | binary_files_(std::move(binary_files)), |
| 64 | total_binary_bytes_(total_binary_bytes), |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 65 | crc_from_disk_(crc_from_disk) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | FabricatedOverlay::Builder::Builder(const std::string& package_name, const std::string& name, |
| 69 | const std::string& target_package_name) { |
| 70 | package_name_ = package_name; |
| 71 | name_ = name; |
| 72 | target_package_name_ = target_package_name; |
| 73 | } |
| 74 | |
| 75 | FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetOverlayable(const std::string& name) { |
| 76 | target_overlayable_ = name; |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue( |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 81 | const std::string& resource_name, uint8_t data_type, uint32_t data_value, |
| 82 | const std::string& configuration) { |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 83 | entries_.emplace_back( |
| 84 | Entry{resource_name, data_type, data_value, "", std::nullopt, configuration}); |
Jeremy Meyer | 0546659 | 2022-06-02 21:31:15 +0000 | [diff] [blame] | 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue( |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 89 | const std::string& resource_name, uint8_t data_type, const std::string& data_string_value, |
| 90 | const std::string& configuration) { |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 91 | entries_.emplace_back( |
| 92 | Entry{resource_name, data_type, 0, data_string_value, std::nullopt, configuration}); |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | FabricatedOverlay::Builder& FabricatedOverlay::Builder::SetResourceValue( |
| 97 | const std::string& resource_name, std::optional<android::base::borrowed_fd>&& binary_value, |
| 98 | const std::string& configuration) { |
| 99 | entries_.emplace_back(Entry{resource_name, 0, 0, "", binary_value, configuration}); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | Result<FabricatedOverlay> FabricatedOverlay::Builder::Build() { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 104 | using ConfigMap = std::map<std::string, TargetValue, std::less<>>; |
| 105 | using EntryMap = std::map<std::string, ConfigMap, std::less<>>; |
| 106 | using TypeMap = std::map<std::string, EntryMap, std::less<>>; |
| 107 | using PackageMap = std::map<std::string, TypeMap, std::less<>>; |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 108 | PackageMap package_map; |
| 109 | android::StringPool string_pool; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 110 | for (const auto& res_entry : entries_) { |
| 111 | StringPiece package_substr; |
| 112 | StringPiece type_name; |
| 113 | StringPiece entry_name; |
| 114 | if (!android::ExtractResourceName(StringPiece(res_entry.resource_name), &package_substr, |
| 115 | &type_name, &entry_name)) { |
| 116 | return Error("failed to parse resource name '%s'", res_entry.resource_name.c_str()); |
| 117 | } |
| 118 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 119 | std::string_view package_name = package_substr.empty() ? target_package_name_ : package_substr; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 120 | if (type_name.empty()) { |
| 121 | return Error("resource name '%s' missing type name", res_entry.resource_name.c_str()); |
| 122 | } |
| 123 | |
| 124 | if (entry_name.empty()) { |
| 125 | return Error("resource name '%s' missing entry name", res_entry.resource_name.c_str()); |
| 126 | } |
| 127 | |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 128 | auto package = package_map.find(package_name); |
| 129 | if (package == package_map.end()) { |
| 130 | package = package_map |
| 131 | .insert(std::make_pair(package_name, TypeMap())) |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 132 | .first; |
| 133 | } |
| 134 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 135 | auto type = package->second.find(type_name); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 136 | if (type == package->second.end()) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 137 | type = package->second.insert(std::make_pair(type_name, EntryMap())).first; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 140 | auto entry = type->second.find(entry_name); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 141 | if (entry == type->second.end()) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 142 | entry = type->second.insert(std::make_pair(entry_name, ConfigMap())).first; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 145 | auto value = entry->second.find(res_entry.configuration); |
| 146 | if (value == entry->second.end()) { |
| 147 | value = entry->second.insert(std::make_pair(res_entry.configuration, TargetValue())).first; |
| 148 | } |
| 149 | |
| 150 | value->second = TargetValue{res_entry.data_type, res_entry.data_value, |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 151 | res_entry.data_string_value, res_entry.data_binary_value}; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | pb::FabricatedOverlay overlay_pb; |
| 155 | overlay_pb.set_package_name(package_name_); |
| 156 | overlay_pb.set_name(name_); |
| 157 | overlay_pb.set_target_package_name(target_package_name_); |
| 158 | overlay_pb.set_target_overlayable(target_overlayable_); |
| 159 | |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 160 | std::vector<android::base::borrowed_fd> binary_files; |
| 161 | size_t total_binary_bytes = 0; |
| 162 | // 16 for the number of bytes in the frro file before the binary data |
| 163 | const size_t FRRO_HEADER_SIZE = 16; |
| 164 | |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 165 | for (auto& package : package_map) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 166 | auto package_pb = overlay_pb.add_packages(); |
| 167 | package_pb->set_name(package.first); |
| 168 | |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 169 | for (auto& type : package.second) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 170 | auto type_pb = package_pb->add_types(); |
| 171 | type_pb->set_name(type.first); |
| 172 | |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 173 | for (auto& entry : type.second) { |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 174 | for (const auto& value: entry.second) { |
| 175 | auto entry_pb = type_pb->add_entries(); |
| 176 | entry_pb->set_name(entry.first); |
| 177 | entry_pb->set_configuration(value.first); |
| 178 | pb::ResourceValue* pb_value = entry_pb->mutable_res_value(); |
| 179 | pb_value->set_data_type(value.second.data_type); |
| 180 | if (value.second.data_type == Res_value::TYPE_STRING) { |
| 181 | auto ref = string_pool.MakeRef(value.second.data_string_value); |
| 182 | pb_value->set_data_value(ref.index()); |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 183 | } else if (value.second.data_binary_value.has_value()) { |
| 184 | pb_value->set_data_type(Res_value::TYPE_STRING); |
| 185 | struct stat s; |
| 186 | if (fstat(value.second.data_binary_value->get(), &s) == -1) { |
| 187 | return Error("unable to get size of binary file: %d", errno); |
| 188 | } |
| 189 | std::string uri |
| 190 | = StringPrintf("frro:/%s?offset=%d&size=%d", frro_path_.c_str(), |
| 191 | static_cast<int> (FRRO_HEADER_SIZE + total_binary_bytes), |
| 192 | static_cast<int> (s.st_size)); |
| 193 | total_binary_bytes += s.st_size; |
| 194 | binary_files.emplace_back(value.second.data_binary_value->get()); |
| 195 | auto ref = string_pool.MakeRef(std::move(uri)); |
| 196 | pb_value->set_data_value(ref.index()); |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 197 | } else { |
| 198 | pb_value->set_data_value(value.second.data_value); |
| 199 | } |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 200 | } |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 204 | android::BigBuffer string_buffer(kBufferSize); |
| 205 | android::StringPool::FlattenUtf8(&string_buffer, string_pool, nullptr); |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 206 | return FabricatedOverlay(std::move(overlay_pb), string_buffer.to_string(), |
| 207 | std::move(binary_files), total_binary_bytes); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | Result<FabricatedOverlay> FabricatedOverlay::FromBinaryStream(std::istream& stream) { |
| 211 | uint32_t magic; |
| 212 | if (!Read32(stream, &magic)) { |
| 213 | return Error("Failed to read fabricated overlay magic."); |
| 214 | } |
| 215 | |
| 216 | if (magic != kFabricatedOverlayMagic) { |
| 217 | return Error("Not a fabricated overlay file."); |
| 218 | } |
| 219 | |
| 220 | uint32_t version; |
| 221 | if (!Read32(stream, &version)) { |
| 222 | return Error("Failed to read fabricated overlay version."); |
| 223 | } |
| 224 | |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 225 | if (version < 1 || version > 3) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 226 | return Error("Invalid fabricated overlay version '%u'.", version); |
| 227 | } |
| 228 | |
| 229 | uint32_t crc; |
| 230 | if (!Read32(stream, &crc)) { |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 231 | return Error("Failed to read fabricated overlay crc."); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | pb::FabricatedOverlay overlay{}; |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 235 | std::string sp_data; |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 236 | uint32_t total_binary_bytes; |
| 237 | if (version == 3) { |
| 238 | if (!Read32(stream, &total_binary_bytes)) { |
| 239 | return Error("Failed read total binary bytes."); |
| 240 | } |
| 241 | stream.seekg(total_binary_bytes, std::istream::cur); |
| 242 | } |
| 243 | if (version >= 2) { |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 244 | uint32_t sp_size; |
| 245 | if (!Read32(stream, &sp_size)) { |
| 246 | return Error("Failed read string pool size."); |
| 247 | } |
| 248 | std::string buf(sp_size, '\0'); |
| 249 | if (!stream.read(buf.data(), sp_size)) { |
| 250 | return Error("Failed to read string pool."); |
| 251 | } |
| 252 | sp_data = buf; |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 253 | } |
| 254 | if (!overlay.ParseFromIstream(&stream)) { |
| 255 | return Error("Failed read fabricated overlay proto."); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // If the proto version is the latest version, then the contents of the proto must be the same |
| 259 | // when the proto is re-serialized; otherwise, the crc must be calculated because migrating the |
| 260 | // proto to the latest version will likely change the contents of the fabricated overlay. |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 261 | return FabricatedOverlay(std::move(overlay), std::move(sp_data), {}, total_binary_bytes, |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 262 | version == kFabricatedOverlayCurrentVersion |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 263 | ? std::optional<uint32_t>(crc) |
| 264 | : std::nullopt); |
| 265 | } |
| 266 | |
| 267 | Result<FabricatedOverlay::SerializedData*> FabricatedOverlay::InitializeData() const { |
| 268 | if (!data_.has_value()) { |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 269 | auto pb_size = overlay_pb_.ByteSizeLong(); |
| 270 | auto pb_data = std::unique_ptr<uint8_t[]>(new uint8_t[pb_size]); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 271 | |
| 272 | // Ensure serialization is deterministic |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 273 | google::protobuf::io::ArrayOutputStream array_stream(pb_data.get(), pb_size); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 274 | google::protobuf::io::CodedOutputStream output_stream(&array_stream); |
| 275 | output_stream.SetSerializationDeterministic(true); |
| 276 | overlay_pb_.SerializeWithCachedSizes(&output_stream); |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 277 | if (output_stream.HadError() || pb_size != output_stream.ByteCount()) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 278 | return Error("Failed to serialize fabricated overlay."); |
| 279 | } |
| 280 | |
| 281 | // Calculate the crc using the proto data and the version. |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 282 | uint32_t pb_crc = crc32(0L, Z_NULL, 0); |
| 283 | pb_crc = crc32(pb_crc, reinterpret_cast<const uint8_t*>(&kFabricatedOverlayCurrentVersion), |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 284 | sizeof(uint32_t)); |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 285 | pb_crc = crc32(pb_crc, pb_data.get(), pb_size); |
| 286 | |
| 287 | data_ = SerializedData{std::move(pb_data), pb_size, pb_crc, string_pool_data_}; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 288 | } |
| 289 | return &(*data_); |
| 290 | } |
| 291 | Result<uint32_t> FabricatedOverlay::GetCrc() const { |
| 292 | if (crc_from_disk_.has_value()) { |
| 293 | return *crc_from_disk_; |
| 294 | } |
| 295 | auto data = InitializeData(); |
| 296 | if (!data) { |
| 297 | return data.GetError(); |
| 298 | } |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 299 | return (*data)->pb_crc; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 302 | Result<Unit> FabricatedOverlay::ToBinaryStream(std::ostream& stream) const { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 303 | auto data = InitializeData(); |
| 304 | if (!data) { |
| 305 | return data.GetError(); |
| 306 | } |
| 307 | |
| 308 | Write32(stream, kFabricatedOverlayMagic); |
| 309 | Write32(stream, kFabricatedOverlayCurrentVersion); |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 310 | Write32(stream, (*data)->pb_crc); |
Jeremy Meyer | a761f33 | 2022-10-21 17:42:14 +0000 | [diff] [blame] | 311 | Write32(stream, total_binary_bytes_); |
| 312 | std::string file_contents; |
| 313 | for (const android::base::borrowed_fd fd : binary_files_) { |
| 314 | if (!ReadFdToString(fd, &file_contents)) { |
| 315 | return Error("Failed to read binary file data."); |
| 316 | } |
| 317 | stream.write(file_contents.data(), file_contents.length()); |
| 318 | } |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 319 | Write32(stream, (*data)->sp_data.length()); |
| 320 | stream.write((*data)->sp_data.data(), (*data)->sp_data.length()); |
| 321 | if (stream.bad()) { |
| 322 | return Error("Failed to write string pool data."); |
| 323 | } |
| 324 | stream.write(reinterpret_cast<const char*>((*data)->pb_data.get()), (*data)->pb_data_size); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 325 | if (stream.bad()) { |
| 326 | return Error("Failed to write serialized fabricated overlay."); |
| 327 | } |
| 328 | |
| 329 | return Unit{}; |
| 330 | } |
| 331 | |
| 332 | using FabContainer = FabricatedOverlayContainer; |
| 333 | FabContainer::FabricatedOverlayContainer(FabricatedOverlay&& overlay, std::string&& path) |
| 334 | : overlay_(std::forward<FabricatedOverlay>(overlay)), path_(std::forward<std::string>(path)) { |
| 335 | } |
| 336 | |
| 337 | FabContainer::~FabricatedOverlayContainer() = default; |
| 338 | |
| 339 | Result<std::unique_ptr<FabContainer>> FabContainer::FromPath(std::string path) { |
| 340 | std::fstream fin(path); |
| 341 | auto overlay = FabricatedOverlay::FromBinaryStream(fin); |
| 342 | if (!overlay) { |
| 343 | return overlay.GetError(); |
| 344 | } |
| 345 | return std::unique_ptr<FabContainer>( |
| 346 | new FabricatedOverlayContainer(std::move(*overlay), std::move(path))); |
| 347 | } |
| 348 | |
| 349 | std::unique_ptr<FabricatedOverlayContainer> FabContainer::FromOverlay(FabricatedOverlay&& overlay) { |
| 350 | return std::unique_ptr<FabContainer>( |
| 351 | new FabricatedOverlayContainer(std::move(overlay), {} /* path */)); |
| 352 | } |
| 353 | |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 354 | OverlayManifestInfo FabContainer::GetManifestInfo() const { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 355 | const pb::FabricatedOverlay& overlay_pb = overlay_.overlay_pb_; |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 356 | return OverlayManifestInfo{ |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 357 | .package_name = overlay_pb.package_name(), |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 358 | .name = overlay_pb.name(), |
| 359 | .target_package = overlay_pb.target_package_name(), |
| 360 | .target_name = overlay_pb.target_overlayable(), |
| 361 | }; |
| 362 | } |
| 363 | |
Ryan Mitchell | 6a2ca78 | 2021-01-19 13:51:15 -0800 | [diff] [blame] | 364 | Result<OverlayManifestInfo> FabContainer::FindOverlayInfo(const std::string& name) const { |
| 365 | const OverlayManifestInfo info = GetManifestInfo(); |
| 366 | if (name != info.name) { |
| 367 | return Error("Failed to find name '%s' in fabricated overlay", name.c_str()); |
| 368 | } |
| 369 | return info; |
| 370 | } |
| 371 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 372 | Result<OverlayData> FabContainer::GetOverlayData(const OverlayManifestInfo& info) const { |
| 373 | const pb::FabricatedOverlay& overlay_pb = overlay_.overlay_pb_; |
| 374 | if (info.name != overlay_pb.name()) { |
| 375 | return Error("Failed to find name '%s' in fabricated overlay", info.name.c_str()); |
| 376 | } |
| 377 | |
| 378 | OverlayData result{}; |
| 379 | for (const auto& package : overlay_pb.packages()) { |
| 380 | for (const auto& type : package.types()) { |
| 381 | for (const auto& entry : type.entries()) { |
| 382 | auto name = base::StringPrintf("%s:%s/%s", package.name().c_str(), type.name().c_str(), |
| 383 | entry.name().c_str()); |
| 384 | const auto& res_value = entry.res_value(); |
| 385 | result.pairs.emplace_back(OverlayData::Value{ |
Jeremy Meyer | 77c8a93 | 2022-08-18 22:06:55 +0000 | [diff] [blame] | 386 | name, TargetValueWithConfig{.config = entry.configuration(), .value = TargetValue{ |
| 387 | .data_type = static_cast<uint8_t>(res_value.data_type()), |
| 388 | .data_value = res_value.data_value()}}}); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |
Jeremy Meyer | 6587102 | 2022-07-12 22:45:08 +0000 | [diff] [blame] | 392 | const uint32_t string_pool_data_length = overlay_.string_pool_data_.length(); |
| 393 | result.string_pool_data = OverlayData::InlineStringPoolData{ |
| 394 | .data = std::unique_ptr<uint8_t[]>(new uint8_t[string_pool_data_length]), |
| 395 | .data_length = string_pool_data_length, |
| 396 | .string_pool_offset = 0, |
| 397 | }; |
| 398 | memcpy(result.string_pool_data->data.get(), overlay_.string_pool_data_.data(), |
| 399 | string_pool_data_length); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 400 | return result; |
| 401 | } |
| 402 | |
| 403 | Result<uint32_t> FabContainer::GetCrc() const { |
| 404 | return overlay_.GetCrc(); |
| 405 | } |
| 406 | |
| 407 | const std::string& FabContainer::GetPath() const { |
| 408 | return path_; |
| 409 | } |
| 410 | |
| 411 | Result<std::string> FabContainer::GetResourceName(ResourceId /* id */) const { |
| 412 | return Error("Fabricated overlay does not contain resources."); |
| 413 | } |
| 414 | |
Mårten Kongstad | 1195a6b | 2021-05-11 12:57:01 +0000 | [diff] [blame] | 415 | } // namespace android::idmap2 |