blob: 3b0940ae06ef5d60fc90823660ba9277db6caffa [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
45void BinaryStreamVisitor::WriteString(const StringPiece& value) {
46 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
52void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
53 // nothing to do
54}
55
56void BinaryStreamVisitor::visit(const IdmapHeader& header) {
57 Write32(header.GetMagic());
58 Write32(header.GetVersion());
59 Write32(header.GetTargetCrc());
60 Write32(header.GetOverlayCrc());
61 WriteString(header.GetTargetPath());
62 WriteString(header.GetOverlayPath());
63}
64
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070065void BinaryStreamVisitor::visit(const IdmapData& data) {
66 for (const auto& target_entry : data.GetTargetEntries()) {
67 Write32(target_entry.target_id);
68 Write8(target_entry.data_type);
69 Write32(target_entry.data_value);
70 }
71
72 for (const auto& overlay_entry : data.GetOverlayEntries()) {
73 Write32(overlay_entry.overlay_id);
74 Write32(overlay_entry.target_id);
75 }
76
77 Write(data.GetStringPoolData(), data.GetHeader()->GetStringPoolLength());
Mårten Kongstad02751232018-04-27 13:16:32 +020078}
79
80void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070081 Write8(header.GetTargetPackageId());
82 Write8(header.GetOverlayPackageId());
83 Write32(header.GetTargetEntryCount());
84 Write32(header.GetOverlayEntryCount());
85 Write32(header.GetStringPoolIndexOffset());
86 Write32(header.GetStringPoolLength());
Mårten Kongstad02751232018-04-27 13:16:32 +020087}
88
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010089} // namespace android::idmap2