blob: 255212ad4c66b5e32f3b4e7be71c77476d52f822 [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::Write(const void* value, size_t length) {
28 stream_.write(reinterpret_cast<const char*>(value), length);
29}
30
31void BinaryStreamVisitor::Write8(uint8_t value) {
32 stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t));
33}
34
Mårten Kongstad02751232018-04-27 13:16:32 +020035void BinaryStreamVisitor::Write16(uint16_t value) {
36 uint16_t x = htodl(value);
37 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t));
38}
39
40void BinaryStreamVisitor::Write32(uint32_t value) {
41 uint32_t x = htodl(value);
42 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t));
43}
44
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020045void BinaryStreamVisitor::WriteString256(const StringPiece& value) {
Mårten Kongstad02751232018-04-27 13:16:32 +020046 char buf[kIdmapStringLength];
47 memset(buf, 0, sizeof(buf));
48 memcpy(buf, value.data(), std::min(value.size(), sizeof(buf)));
49 stream_.write(buf, sizeof(buf));
50}
51
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020052void BinaryStreamVisitor::WriteString(const std::string& value) {
53 // pad with null to nearest word boundary; include at least one terminating null
54 size_t padding_size = 4 - (value.size() % 4);
55 Write32(value.size() + padding_size);
56 stream_.write(value.c_str(), value.size());
57 stream_.write("\0\0\0\0", padding_size);
58}
59
Mårten Kongstad02751232018-04-27 13:16:32 +020060void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
61 // nothing to do
62}
63
64void BinaryStreamVisitor::visit(const IdmapHeader& header) {
65 Write32(header.GetMagic());
66 Write32(header.GetVersion());
67 Write32(header.GetTargetCrc());
68 Write32(header.GetOverlayCrc());
Ryan Mitchella7070132020-05-13 14:17:52 -070069 Write32(header.GetFulfilledPolicies());
70 Write8(static_cast<uint8_t>(header.GetEnforceOverlayable()));
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020071 WriteString256(header.GetTargetPath());
72 WriteString256(header.GetOverlayPath());
73 WriteString(header.GetDebugInfo());
Mårten Kongstad02751232018-04-27 13:16:32 +020074}
75
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070076void BinaryStreamVisitor::visit(const IdmapData& data) {
77 for (const auto& target_entry : data.GetTargetEntries()) {
78 Write32(target_entry.target_id);
79 Write8(target_entry.data_type);
80 Write32(target_entry.data_value);
81 }
82
83 for (const auto& overlay_entry : data.GetOverlayEntries()) {
84 Write32(overlay_entry.overlay_id);
85 Write32(overlay_entry.target_id);
86 }
87
88 Write(data.GetStringPoolData(), data.GetHeader()->GetStringPoolLength());
Mårten Kongstad02751232018-04-27 13:16:32 +020089}
90
91void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070092 Write8(header.GetTargetPackageId());
93 Write8(header.GetOverlayPackageId());
94 Write32(header.GetTargetEntryCount());
95 Write32(header.GetOverlayEntryCount());
96 Write32(header.GetStringPoolIndexOffset());
97 Write32(header.GetStringPoolLength());
Mårten Kongstad02751232018-04-27 13:16:32 +020098}
99
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100100} // namespace android::idmap2