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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/BinaryStreamVisitor.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <cstring> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/macros.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 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 27 | void BinaryStreamVisitor::Write(const void* value, size_t length) { |
| 28 | stream_.write(reinterpret_cast<const char*>(value), length); |
| 29 | } |
| 30 | |
| 31 | void BinaryStreamVisitor::Write8(uint8_t value) { |
| 32 | stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t)); |
| 33 | } |
| 34 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 35 | void BinaryStreamVisitor::Write16(uint16_t value) { |
| 36 | uint16_t x = htodl(value); |
| 37 | stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t)); |
| 38 | } |
| 39 | |
| 40 | void BinaryStreamVisitor::Write32(uint32_t value) { |
| 41 | uint32_t x = htodl(value); |
| 42 | stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t)); |
| 43 | } |
| 44 | |
| 45 | void 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 | |
| 52 | void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) { |
| 53 | // nothing to do |
| 54 | } |
| 55 | |
| 56 | void 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 Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 65 | void 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 Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void BinaryStreamVisitor::visit(const IdmapData::Header& header) { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 81 | 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 Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 89 | } // namespace android::idmap2 |