blob: 00ef0c7f8cf0a6a9a7561bfe34eedd65c655b525 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/BinaryStreamVisitor.h"
18
Mårten Kongstad02751232018-04-27 13:16:32 +020019#include <algorithm>
20#include <cstring>
21#include <string>
22
23#include "android-base/macros.h"
24
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010025namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020026
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070027void BinaryStreamVisitor::Write8(uint8_t value) {
28 stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t));
29}
30
Mårten Kongstad02751232018-04-27 13:16:32 +020031void BinaryStreamVisitor::Write16(uint16_t value) {
32 uint16_t x = htodl(value);
33 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t));
34}
35
36void BinaryStreamVisitor::Write32(uint32_t value) {
37 uint32_t x = htodl(value);
38 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t));
39}
40
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070041void BinaryStreamVisitor::WriteString(StringPiece value) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070042 // 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 Kongstadd7e8a532019-10-11 08:32:04 +020046 stream_.write("\0\0\0\0", padding_size);
47}
48
Mårten Kongstad02751232018-04-27 13:16:32 +020049void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
50 // nothing to do
51}
52
53void BinaryStreamVisitor::visit(const IdmapHeader& header) {
54 Write32(header.GetMagic());
55 Write32(header.GetVersion());
56 Write32(header.GetTargetCrc());
57 Write32(header.GetOverlayCrc());
Ryan Mitchella7070132020-05-13 14:17:52 -070058 Write32(header.GetFulfilledPolicies());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070059 Write32(static_cast<uint8_t>(header.GetEnforceOverlayable()));
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080060 WriteString(header.GetTargetPath());
61 WriteString(header.GetOverlayPath());
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080062 WriteString(header.GetOverlayName());
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020063 WriteString(header.GetDebugInfo());
Mårten Kongstad02751232018-04-27 13:16:32 +020064}
65
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070066void BinaryStreamVisitor::visit(const IdmapData& data) {
67 for (const auto& target_entry : data.GetTargetEntries()) {
68 Write32(target_entry.target_id);
Yurii Zubrytskyid67e90e2024-08-30 17:39:56 -070069 }
70 for (const auto& target_entry : data.GetTargetEntries()) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070071 Write32(target_entry.overlay_id);
72 }
73
Yurii Zubrytskyid67e90e2024-08-30 17:39:56 -070074 uint32_t current_inline_entry_values_count = 0;
75 for (const auto& target_inline_entry : data.GetTargetInlineEntries()) {
76 Write32(target_inline_entry.target_id);
77 }
78 for (const auto& target_inline_entry : data.GetTargetInlineEntries()) {
79 Write32(current_inline_entry_values_count);
80 Write32(target_inline_entry.values.size());
81 current_inline_entry_values_count += target_inline_entry.values.size();
Jeremy Meyerbe2b7792022-08-23 17:42:50 +000082 }
83
84 std::vector<ConfigDescription> configs;
85 configs.reserve(data.GetHeader()->GetConfigCount());
Yurii Zubrytskyid67e90e2024-08-30 17:39:56 -070086 for (const auto& target_entry : data.GetTargetInlineEntries()) {
87 for (const auto& target_entry_value : target_entry.values) {
88 auto config_it = std::find(configs.begin(), configs.end(), target_entry_value.first);
89 if (config_it != configs.end()) {
90 Write32(config_it - configs.begin());
91 } else {
92 Write32(configs.size());
93 configs.push_back(target_entry_value.first);
94 }
95 // We're writing a Res_value entry here, and the first 3 bytes of that are
96 // sizeof() + a padding 0 byte
97 static constexpr decltype(android::Res_value::size) kSize = sizeof(android::Res_value);
98 Write16(kSize);
99 Write8(0U);
100 Write8(target_entry_value.second.data_type);
101 Write32(target_entry_value.second.data_value);
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000102 }
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000103 }
104
Yurii Zubrytskyid67e90e2024-08-30 17:39:56 -0700105 if (!configs.empty()) {
106 stream_.write(reinterpret_cast<const char*>(&configs.front()),
107 sizeof(configs.front()) * configs.size());
108 if (configs.size() >= 100) {
109 // Let's write a message to future us so that they know when to replace the linear search
110 // in `configs` vector with something more efficient.
111 LOG(WARNING) << "Idmap got " << configs.size()
112 << " configurations, time to fix the bruteforce search";
113 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700114 }
115
116 for (const auto& overlay_entry : data.GetOverlayEntries()) {
117 Write32(overlay_entry.overlay_id);
Yurii Zubrytskyid67e90e2024-08-30 17:39:56 -0700118 }
119 for (const auto& overlay_entry : data.GetOverlayEntries()) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700120 Write32(overlay_entry.target_id);
121 }
122
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700123 WriteString(data.GetStringPoolData());
Mårten Kongstad02751232018-04-27 13:16:32 +0200124}
125
126void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700127 Write32(header.GetTargetEntryCount());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700128 Write32(header.GetTargetInlineEntryCount());
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000129 Write32(header.GetTargetInlineEntryValueCount());
130 Write32(header.GetConfigCount());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700131 Write32(header.GetOverlayEntryCount());
132 Write32(header.GetStringPoolIndexOffset());
Mårten Kongstad02751232018-04-27 13:16:32 +0200133}
134
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100135} // namespace android::idmap2