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/RawPrintVisitor.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <cstdarg> |
| 20 | #include <string> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 21 | |
| 22 | #include "android-base/macros.h" |
| 23 | #include "android-base/stringprintf.h" |
| 24 | #include "androidfw/ApkAssets.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 25 | #include "idmap2/ResourceUtils.h" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 26 | #include "idmap2/Result.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 27 | |
| 28 | using android::ApkAssets; |
| 29 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 30 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 31 | |
| 32 | // verbatim copy fomr PrettyPrintVisitor.cpp, move to common utils |
| 33 | #define RESID(pkg, type, entry) (((pkg) << 24) | ((type) << 16) | (entry)) |
| 34 | |
| 35 | void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) { |
| 36 | } |
| 37 | |
| 38 | void RawPrintVisitor::visit(const IdmapHeader& header) { |
| 39 | print(header.GetMagic(), "magic"); |
| 40 | print(header.GetVersion(), "version"); |
| 41 | print(header.GetTargetCrc(), "target crc"); |
| 42 | print(header.GetOverlayCrc(), "overlay crc"); |
| 43 | print(header.GetTargetPath().to_string(), "target path"); |
| 44 | print(header.GetOverlayPath().to_string(), "overlay path"); |
| 45 | |
| 46 | target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string()); |
| 47 | if (target_apk_) { |
| 48 | target_am_.SetApkAssets({target_apk_.get()}); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) { |
| 53 | } |
| 54 | |
| 55 | void RawPrintVisitor::visit(const IdmapData::Header& header) { |
| 56 | print(static_cast<uint16_t>(header.GetTargetPackageId()), "target package id"); |
| 57 | print(header.GetTypeCount(), "type count"); |
| 58 | last_seen_package_id_ = header.GetTargetPackageId(); |
| 59 | } |
| 60 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 61 | void RawPrintVisitor::visit(const IdmapData::TypeEntry& type_entry) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 62 | const bool target_package_loaded = !target_am_.GetApkAssets().empty(); |
| 63 | |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 64 | print(static_cast<uint16_t>(type_entry.GetTargetTypeId()), "target type"); |
| 65 | print(static_cast<uint16_t>(type_entry.GetOverlayTypeId()), "overlay type"); |
| 66 | print(static_cast<uint16_t>(type_entry.GetEntryCount()), "entry count"); |
| 67 | print(static_cast<uint16_t>(type_entry.GetEntryOffset()), "entry offset"); |
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 | for (uint16_t i = 0; i < type_entry.GetEntryCount(); i++) { |
| 70 | const EntryId entry = type_entry.GetEntry(i); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 71 | if (entry == kNoEntry) { |
| 72 | print(kPadding, "no entry"); |
| 73 | } else { |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 74 | const ResourceId target_resid = RESID(last_seen_package_id_, type_entry.GetTargetTypeId(), |
| 75 | type_entry.GetEntryOffset() + i); |
| 76 | const ResourceId overlay_resid = |
| 77 | RESID(last_seen_package_id_, type_entry.GetOverlayTypeId(), entry); |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 78 | Result<std::string> name(Error("")); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 79 | if (target_package_loaded) { |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 80 | name = utils::ResToTypeEntryName(target_am_, target_resid); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 81 | } |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 82 | if (name) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 83 | print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x %s", target_resid, overlay_resid, |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 84 | name->c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 85 | } else { |
| 86 | print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x", target_resid, overlay_resid); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Chih-Hung Hsieh | 55773ba | 2019-01-14 11:09:03 -0800 | [diff] [blame] | 92 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 93 | void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) { |
| 94 | va_list ap; |
| 95 | va_start(ap, fmt); |
| 96 | std::string comment; |
| 97 | base::StringAppendV(&comment, fmt, ap); |
| 98 | va_end(ap); |
| 99 | |
| 100 | stream_ << base::StringPrintf("%08zx: %04x", offset_, value) << " " << comment << std::endl; |
| 101 | |
| 102 | offset_ += sizeof(uint16_t); |
| 103 | } |
| 104 | |
Chih-Hung Hsieh | 55773ba | 2019-01-14 11:09:03 -0800 | [diff] [blame] | 105 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 106 | void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) { |
| 107 | va_list ap; |
| 108 | va_start(ap, fmt); |
| 109 | std::string comment; |
| 110 | base::StringAppendV(&comment, fmt, ap); |
| 111 | va_end(ap); |
| 112 | |
| 113 | stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl; |
| 114 | |
| 115 | offset_ += sizeof(uint32_t); |
| 116 | } |
| 117 | |
Chih-Hung Hsieh | 55773ba | 2019-01-14 11:09:03 -0800 | [diff] [blame] | 118 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 119 | void RawPrintVisitor::print(const std::string& value, const char* fmt, ...) { |
| 120 | va_list ap; |
| 121 | va_start(ap, fmt); |
| 122 | std::string comment; |
| 123 | base::StringAppendV(&comment, fmt, ap); |
| 124 | va_end(ap); |
| 125 | |
| 126 | stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value |
| 127 | << std::endl; |
| 128 | |
| 129 | offset_ += kIdmapStringLength; |
| 130 | } |
| 131 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 132 | } // namespace android::idmap2 |