Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/BinaryStreamVisitor.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <cstring> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/macros.h" |
| 24 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 25 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 26 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 27 | void BinaryStreamVisitor::Write8(uint8_t value) { |
| 28 | stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t)); |
| 29 | } |
| 30 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 31 | void BinaryStreamVisitor::Write16(uint16_t value) { |
| 32 | uint16_t x = htodl(value); |
| 33 | stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t)); |
| 34 | } |
| 35 | |
| 36 | void BinaryStreamVisitor::Write32(uint32_t value) { |
| 37 | uint32_t x = htodl(value); |
| 38 | stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t)); |
| 39 | } |
| 40 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 41 | void BinaryStreamVisitor::WriteString(const StringPiece& value) { |
| 42 | // pad with null to nearest word boundary; |
| 43 | size_t padding_size = CalculatePadding(value.size()); |
| 44 | Write32(value.size()); |
| 45 | stream_.write(value.data(), value.size()); |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 46 | stream_.write("\0\0\0\0", padding_size); |
| 47 | } |
| 48 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 49 | void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) { |
| 50 | // nothing to do |
| 51 | } |
| 52 | |
| 53 | void BinaryStreamVisitor::visit(const IdmapHeader& header) { |
| 54 | Write32(header.GetMagic()); |
| 55 | Write32(header.GetVersion()); |
| 56 | Write32(header.GetTargetCrc()); |
| 57 | Write32(header.GetOverlayCrc()); |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 58 | Write32(header.GetFulfilledPolicies()); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 59 | Write32(static_cast<uint8_t>(header.GetEnforceOverlayable())); |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 60 | WriteString(header.GetTargetPath()); |
| 61 | WriteString(header.GetOverlayPath()); |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame] | 62 | WriteString(header.GetOverlayName()); |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 63 | WriteString(header.GetDebugInfo()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 66 | void BinaryStreamVisitor::visit(const IdmapData& data) { |
| 67 | for (const auto& target_entry : data.GetTargetEntries()) { |
| 68 | Write32(target_entry.target_id); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 69 | Write32(target_entry.overlay_id); |
| 70 | } |
| 71 | |
| 72 | static constexpr uint16_t kValueSize = 8U; |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame^] | 73 | std::vector<std::pair<ConfigDescription, TargetValue>> target_values; |
| 74 | target_values.reserve(data.GetHeader()->GetTargetInlineEntryValueCount()); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 75 | for (const auto& target_entry : data.GetTargetInlineEntries()) { |
| 76 | Write32(target_entry.target_id); |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame^] | 77 | Write32(target_values.size()); |
| 78 | Write32(target_entry.values.size()); |
| 79 | target_values.insert( |
| 80 | target_values.end(), target_entry.values.begin(), target_entry.values.end()); |
| 81 | } |
| 82 | |
| 83 | std::vector<ConfigDescription> configs; |
| 84 | configs.reserve(data.GetHeader()->GetConfigCount()); |
| 85 | for (const auto& target_entry_value : target_values) { |
| 86 | auto config_it = find(configs.begin(), configs.end(), target_entry_value.first); |
| 87 | if (config_it != configs.end()) { |
| 88 | Write32(config_it - configs.begin()); |
| 89 | } else { |
| 90 | Write32(configs.size()); |
| 91 | configs.push_back(target_entry_value.first); |
| 92 | } |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 93 | Write16(kValueSize); |
| 94 | Write8(0U); // padding |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame^] | 95 | Write8(target_entry_value.second.data_type); |
| 96 | Write32(target_entry_value.second.data_value); |
| 97 | } |
| 98 | |
| 99 | for( auto& cd : configs) { |
| 100 | cd.swapHtoD(); |
| 101 | stream_.write(reinterpret_cast<char*>(&cd), sizeof(cd)); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | for (const auto& overlay_entry : data.GetOverlayEntries()) { |
| 105 | Write32(overlay_entry.overlay_id); |
| 106 | Write32(overlay_entry.target_id); |
| 107 | } |
| 108 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 109 | WriteString(data.GetStringPoolData()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void BinaryStreamVisitor::visit(const IdmapData::Header& header) { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 113 | Write32(header.GetTargetEntryCount()); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 114 | Write32(header.GetTargetInlineEntryCount()); |
Jeremy Meyer | be2b779 | 2022-08-23 17:42:50 +0000 | [diff] [blame^] | 115 | Write32(header.GetTargetInlineEntryValueCount()); |
| 116 | Write32(header.GetConfigCount()); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 117 | Write32(header.GetOverlayEntryCount()); |
| 118 | Write32(header.GetStringPoolIndexOffset()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 121 | } // namespace android::idmap2 |