blob: 29969a23250b66b1a07a5b7853a5b4cc78100c97 [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
17#include <algorithm>
18#include <cstring>
19#include <string>
20
21#include "android-base/macros.h"
22
23#include "idmap2/BinaryStreamVisitor.h"
24
25namespace android {
26namespace idmap2 {
27
28void BinaryStreamVisitor::Write16(uint16_t value) {
29 uint16_t x = htodl(value);
30 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t));
31}
32
33void BinaryStreamVisitor::Write32(uint32_t value) {
34 uint32_t x = htodl(value);
35 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t));
36}
37
38void BinaryStreamVisitor::WriteString(const StringPiece& value) {
39 char buf[kIdmapStringLength];
40 memset(buf, 0, sizeof(buf));
41 memcpy(buf, value.data(), std::min(value.size(), sizeof(buf)));
42 stream_.write(buf, sizeof(buf));
43}
44
45void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
46 // nothing to do
47}
48
49void BinaryStreamVisitor::visit(const IdmapHeader& header) {
50 Write32(header.GetMagic());
51 Write32(header.GetVersion());
52 Write32(header.GetTargetCrc());
53 Write32(header.GetOverlayCrc());
54 WriteString(header.GetTargetPath());
55 WriteString(header.GetOverlayPath());
56}
57
58void BinaryStreamVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
59 // nothing to do
60}
61
62void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
63 Write16(header.GetTargetPackageId());
64 Write16(header.GetTypeCount());
65}
66
67void BinaryStreamVisitor::visit(const IdmapData::TypeEntry& te) {
68 const uint16_t entryCount = te.GetEntryCount();
69
70 Write16(te.GetTargetTypeId());
71 Write16(te.GetOverlayTypeId());
72 Write16(entryCount);
73 Write16(te.GetEntryOffset());
74 for (uint16_t i = 0; i < entryCount; i++) {
75 EntryId entry_id = te.GetEntry(i);
76 Write32(entry_id != kNoEntry ? static_cast<uint32_t>(entry_id) : kPadding);
77 }
78}
79
80} // namespace idmap2
81} // namespace android