1 /* 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 "idmap2/BinaryStreamVisitor.h" 18 19 #include <algorithm> 20 #include <cstring> 21 #include <string> 22 23 #include "android-base/macros.h" 24 25 namespace android::idmap2 { 26 Write8(uint8_t value)27void BinaryStreamVisitor::Write8(uint8_t value) { 28 stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t)); 29 } 30 Write16(uint16_t value)31void BinaryStreamVisitor::Write16(uint16_t value) { 32 uint16_t x = htodl(value); 33 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t)); 34 } 35 Write32(uint32_t value)36void BinaryStreamVisitor::Write32(uint32_t value) { 37 uint32_t x = htodl(value); 38 stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t)); 39 } 40 WriteString(const StringPiece & value)41void BinaryStreamVisitor::WriteString(const StringPiece& value) { 42 // 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()); 46 stream_.write("\0\0\0\0", padding_size); 47 } 48 visit(const Idmap & idmap ATTRIBUTE_UNUSED)49void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) { 50 // nothing to do 51 } 52 visit(const IdmapHeader & header)53void BinaryStreamVisitor::visit(const IdmapHeader& header) { 54 Write32(header.GetMagic()); 55 Write32(header.GetVersion()); 56 Write32(header.GetTargetCrc()); 57 Write32(header.GetOverlayCrc()); 58 Write32(header.GetFulfilledPolicies()); 59 Write32(static_cast<uint8_t>(header.GetEnforceOverlayable())); 60 WriteString(header.GetTargetPath()); 61 WriteString(header.GetOverlayPath()); 62 WriteString(header.GetOverlayName()); 63 WriteString(header.GetDebugInfo()); 64 } 65 visit(const IdmapData & data)66void BinaryStreamVisitor::visit(const IdmapData& data) { 67 for (const auto& target_entry : data.GetTargetEntries()) { 68 Write32(target_entry.target_id); 69 Write32(target_entry.overlay_id); 70 } 71 72 static constexpr uint16_t kValueSize = 8U; 73 for (const auto& target_entry : data.GetTargetInlineEntries()) { 74 Write32(target_entry.target_id); 75 Write16(kValueSize); 76 Write8(0U); // padding 77 Write8(target_entry.value.data_type); 78 Write32(target_entry.value.data_value); 79 } 80 81 for (const auto& overlay_entry : data.GetOverlayEntries()) { 82 Write32(overlay_entry.overlay_id); 83 Write32(overlay_entry.target_id); 84 } 85 86 WriteString(data.GetStringPoolData()); 87 } 88 visit(const IdmapData::Header & header)89void BinaryStreamVisitor::visit(const IdmapData::Header& header) { 90 Write32(header.GetTargetEntryCount()); 91 Write32(header.GetTargetInlineEntryCount()); 92 Write32(header.GetOverlayEntryCount()); 93 Write32(header.GetStringPoolIndexOffset()); 94 } 95 96 } // namespace android::idmap2 97