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