blob: 726f6c5c2c99a6056f382519fde025cfe195eb14 [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
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020041void BinaryStreamVisitor::WriteString256(const StringPiece& value) {
Mårten Kongstad02751232018-04-27 13:16:32 +020042 char buf[kIdmapStringLength];
43 memset(buf, 0, sizeof(buf));
44 memcpy(buf, value.data(), std::min(value.size(), sizeof(buf)));
45 stream_.write(buf, sizeof(buf));
46}
47
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070048void BinaryStreamVisitor::WriteString(const StringPiece& value) {
49 // pad with null to nearest word boundary;
50 size_t padding_size = CalculatePadding(value.size());
51 Write32(value.size());
52 stream_.write(value.data(), value.size());
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020053 stream_.write("\0\0\0\0", padding_size);
54}
55
Mårten Kongstad02751232018-04-27 13:16:32 +020056void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
57 // nothing to do
58}
59
60void BinaryStreamVisitor::visit(const IdmapHeader& header) {
61 Write32(header.GetMagic());
62 Write32(header.GetVersion());
63 Write32(header.GetTargetCrc());
64 Write32(header.GetOverlayCrc());
Ryan Mitchella7070132020-05-13 14:17:52 -070065 Write32(header.GetFulfilledPolicies());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070066 Write32(static_cast<uint8_t>(header.GetEnforceOverlayable()));
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020067 WriteString256(header.GetTargetPath());
68 WriteString256(header.GetOverlayPath());
69 WriteString(header.GetDebugInfo());
Mårten Kongstad02751232018-04-27 13:16:32 +020070}
71
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070072void BinaryStreamVisitor::visit(const IdmapData& data) {
73 for (const auto& target_entry : data.GetTargetEntries()) {
74 Write32(target_entry.target_id);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070075 Write32(target_entry.overlay_id);
76 }
77
78 static constexpr uint16_t kValueSize = 8U;
79 for (const auto& target_entry : data.GetTargetInlineEntries()) {
80 Write32(target_entry.target_id);
81 Write16(kValueSize);
82 Write8(0U); // padding
83 Write8(target_entry.value.data_type);
84 Write32(target_entry.value.data_value);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070085 }
86
87 for (const auto& overlay_entry : data.GetOverlayEntries()) {
88 Write32(overlay_entry.overlay_id);
89 Write32(overlay_entry.target_id);
90 }
91
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070092 WriteString(data.GetStringPoolData());
Mårten Kongstad02751232018-04-27 13:16:32 +020093}
94
95void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070096 Write8(header.GetTargetPackageId());
97 Write8(header.GetOverlayPackageId());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070098 Write8(0U); // padding
99 Write8(0U); // padding
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700100 Write32(header.GetTargetEntryCount());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700101 Write32(header.GetTargetInlineEntryCount());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700102 Write32(header.GetOverlayEntryCount());
103 Write32(header.GetStringPoolIndexOffset());
Mårten Kongstad02751232018-04-27 13:16:32 +0200104}
105
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100106} // namespace android::idmap2