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 | |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 45 | void BinaryStreamVisitor::WriteString256(const StringPiece& value) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 52 | void BinaryStreamVisitor::WriteString(const std::string& value) { |
| 53 | // pad with null to nearest word boundary; include at least one terminating null |
| 54 | size_t padding_size = 4 - (value.size() % 4); |
| 55 | Write32(value.size() + padding_size); |
| 56 | stream_.write(value.c_str(), value.size()); |
| 57 | stream_.write("\0\0\0\0", padding_size); |
| 58 | } |
| 59 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 60 | void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) { |
| 61 | // nothing to do |
| 62 | } |
| 63 | |
| 64 | void BinaryStreamVisitor::visit(const IdmapHeader& header) { |
| 65 | Write32(header.GetMagic()); |
| 66 | Write32(header.GetVersion()); |
| 67 | Write32(header.GetTargetCrc()); |
| 68 | Write32(header.GetOverlayCrc()); |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame^] | 69 | Write32(header.GetFulfilledPolicies()); |
| 70 | Write8(static_cast<uint8_t>(header.GetEnforceOverlayable())); |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 71 | WriteString256(header.GetTargetPath()); |
| 72 | WriteString256(header.GetOverlayPath()); |
| 73 | WriteString(header.GetDebugInfo()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 76 | void BinaryStreamVisitor::visit(const IdmapData& data) { |
| 77 | for (const auto& target_entry : data.GetTargetEntries()) { |
| 78 | Write32(target_entry.target_id); |
| 79 | Write8(target_entry.data_type); |
| 80 | Write32(target_entry.data_value); |
| 81 | } |
| 82 | |
| 83 | for (const auto& overlay_entry : data.GetOverlayEntries()) { |
| 84 | Write32(overlay_entry.overlay_id); |
| 85 | Write32(overlay_entry.target_id); |
| 86 | } |
| 87 | |
| 88 | Write(data.GetStringPoolData(), data.GetHeader()->GetStringPoolLength()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void BinaryStreamVisitor::visit(const IdmapData::Header& header) { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 92 | Write8(header.GetTargetPackageId()); |
| 93 | Write8(header.GetOverlayPackageId()); |
| 94 | Write32(header.GetTargetEntryCount()); |
| 95 | Write32(header.GetOverlayEntryCount()); |
| 96 | Write32(header.GetStringPoolIndexOffset()); |
| 97 | Write32(header.GetStringPoolLength()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 100 | } // namespace android::idmap2 |