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 | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 19 | #include <algorithm> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 20 | #include <cstdarg> |
| 21 | #include <string> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 22 | |
| 23 | #include "android-base/macros.h" |
| 24 | #include "android-base/stringprintf.h" |
| 25 | #include "androidfw/ApkAssets.h" |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 26 | #include "idmap2/PolicyUtils.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 27 | #include "idmap2/ResourceUtils.h" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 28 | #include "idmap2/Result.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 29 | |
| 30 | using android::ApkAssets; |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 31 | using android::idmap2::policy::PoliciesToDebugString; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 32 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 33 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 34 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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"); |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 43 | print(header.GetFulfilledPolicies(), "fulfilled policies: %s", |
| 44 | PoliciesToDebugString(header.GetFulfilledPolicies()).c_str()); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 45 | print(static_cast<uint32_t>(header.GetEnforceOverlayable()), "enforce overlayable"); |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 46 | print(header.GetTargetPath(), true /* print_value */, "target path"); |
| 47 | print(header.GetOverlayPath(), true /* print_value */, "overlay path"); |
| 48 | print(header.GetDebugInfo(), false /* print_value */, "debug info"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 49 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 50 | auto target_apk_ = ApkAssets::Load(header.GetTargetPath()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 51 | if (target_apk_) { |
| 52 | target_am_.SetApkAssets({target_apk_.get()}); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 53 | apk_assets_.push_back(std::move(target_apk_)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 54 | } |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 55 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 56 | auto overlay_apk_ = ApkAssets::Load(header.GetOverlayPath()); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 57 | if (overlay_apk_) { |
| 58 | overlay_am_.SetApkAssets({overlay_apk_.get()}); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 59 | apk_assets_.push_back(std::move(overlay_apk_)); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 60 | } |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 64 | const bool target_package_loaded = !target_am_.GetApkAssets().empty(); |
| 65 | const bool overlay_package_loaded = !overlay_am_.GetApkAssets().empty(); |
| 66 | |
| 67 | for (auto& target_entry : data.GetTargetEntries()) { |
| 68 | Result<std::string> target_name(Error("")); |
| 69 | if (target_package_loaded) { |
| 70 | target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id); |
| 71 | } |
| 72 | if (target_name) { |
| 73 | print(target_entry.target_id, "target id: %s", target_name->c_str()); |
| 74 | } else { |
| 75 | print(target_entry.target_id, "target id"); |
| 76 | } |
| 77 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 78 | Result<std::string> overlay_name(Error("")); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 79 | if (overlay_package_loaded) { |
| 80 | overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.overlay_id); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 81 | } |
| 82 | if (overlay_name) { |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 83 | print(target_entry.overlay_id, "overlay id: %s", overlay_name->c_str()); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 84 | } else { |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 85 | print(target_entry.overlay_id, "overlay id"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | for (auto& target_entry : data.GetTargetInlineEntries()) { |
| 90 | Result<std::string> target_name(Error("")); |
| 91 | if (target_package_loaded) { |
| 92 | target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id); |
| 93 | } |
| 94 | if (target_name) { |
| 95 | print(target_entry.target_id, "target id: %s", target_name->c_str()); |
| 96 | } else { |
| 97 | print(target_entry.target_id, "target id"); |
| 98 | } |
| 99 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 100 | pad(sizeof(Res_value::size) + sizeof(Res_value::res0)); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 101 | |
| 102 | print(target_entry.value.data_type, "type: %s", |
| 103 | utils::DataTypeToString(target_entry.value.data_type).data()); |
| 104 | |
| 105 | Result<std::string> overlay_name(Error("")); |
| 106 | if (overlay_package_loaded && |
| 107 | (target_entry.value.data_value == Res_value::TYPE_REFERENCE || |
| 108 | target_entry.value.data_value == Res_value::TYPE_DYNAMIC_REFERENCE)) { |
| 109 | overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.value.data_value); |
| 110 | } |
| 111 | |
| 112 | if (overlay_name) { |
| 113 | print(target_entry.value.data_value, "data: %s", overlay_name->c_str()); |
| 114 | } else { |
| 115 | print(target_entry.value.data_value, "data"); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | for (auto& overlay_entry : data.GetOverlayEntries()) { |
| 120 | Result<std::string> overlay_name(Error("")); |
| 121 | if (overlay_package_loaded) { |
| 122 | overlay_name = utils::ResToTypeEntryName(overlay_am_, overlay_entry.overlay_id); |
| 123 | } |
| 124 | |
| 125 | if (overlay_name) { |
| 126 | print(overlay_entry.overlay_id, "overlay id: %s", overlay_name->c_str()); |
| 127 | } else { |
| 128 | print(overlay_entry.overlay_id, "overlay id"); |
| 129 | } |
| 130 | |
| 131 | Result<std::string> target_name(Error("")); |
| 132 | if (target_package_loaded) { |
| 133 | target_name = utils::ResToTypeEntryName(target_am_, overlay_entry.target_id); |
| 134 | } |
| 135 | |
| 136 | if (target_name) { |
| 137 | print(overlay_entry.target_id, "target id: %s", target_name->c_str()); |
| 138 | } else { |
| 139 | print(overlay_entry.target_id, "target id"); |
| 140 | } |
| 141 | } |
| 142 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 143 | print(data.GetStringPoolData(), false /* print_value */, "string pool"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void RawPrintVisitor::visit(const IdmapData::Header& header) { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 147 | print(header.GetTargetPackageId(), "target package id"); |
| 148 | print(header.GetOverlayPackageId(), "overlay package id"); |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 149 | align(); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 150 | print(header.GetTargetEntryCount(), "target entry count"); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 151 | print(header.GetTargetInlineEntryCount(), "target inline entry count"); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 152 | print(header.GetOverlayEntryCount(), "overlay entry count"); |
| 153 | print(header.GetStringPoolIndexOffset(), "string pool index offset"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 156 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
| 157 | void RawPrintVisitor::print(uint8_t value, const char* fmt, ...) { |
| 158 | va_list ap; |
| 159 | va_start(ap, fmt); |
| 160 | std::string comment; |
| 161 | base::StringAppendV(&comment, fmt, ap); |
| 162 | va_end(ap); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 163 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 164 | stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment |
| 165 | << std::endl; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 166 | offset_ += sizeof(uint8_t); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Chih-Hung Hsieh | 55773ba | 2019-01-14 11:09:03 -0800 | [diff] [blame] | 169 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 170 | void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) { |
| 171 | va_list ap; |
| 172 | va_start(ap, fmt); |
| 173 | std::string comment; |
| 174 | base::StringAppendV(&comment, fmt, ap); |
| 175 | va_end(ap); |
| 176 | |
| 177 | stream_ << base::StringPrintf("%08zx: %04x", offset_, value) << " " << comment << std::endl; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 178 | offset_ += sizeof(uint16_t); |
| 179 | } |
| 180 | |
Chih-Hung Hsieh | 55773ba | 2019-01-14 11:09:03 -0800 | [diff] [blame] | 181 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 182 | void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) { |
| 183 | va_list ap; |
| 184 | va_start(ap, fmt); |
| 185 | std::string comment; |
| 186 | base::StringAppendV(&comment, fmt, ap); |
| 187 | va_end(ap); |
| 188 | |
| 189 | stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 190 | offset_ += sizeof(uint32_t); |
| 191 | } |
| 192 | |
Chih-Hung Hsieh | 55773ba | 2019-01-14 11:09:03 -0800 | [diff] [blame] | 193 | // NOLINTNEXTLINE(cert-dcl50-cpp) |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 194 | void RawPrintVisitor::print(const std::string& value, bool print_value, const char* fmt, ...) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 195 | va_list ap; |
| 196 | va_start(ap, fmt); |
| 197 | std::string comment; |
| 198 | base::StringAppendV(&comment, fmt, ap); |
| 199 | va_end(ap); |
| 200 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 201 | stream_ << base::StringPrintf("%08zx: %08x", offset_, (uint32_t)value.size()) << " " << comment |
| 202 | << " size" << std::endl; |
| 203 | offset_ += sizeof(uint32_t); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 204 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 205 | stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment; |
| 206 | offset_ += value.size() + CalculatePadding(value.size()); |
| 207 | |
| 208 | if (print_value) { |
| 209 | stream_ << ": " << value; |
| 210 | } |
| 211 | stream_ << std::endl; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame^] | 214 | void RawPrintVisitor::align() { |
| 215 | offset_ += CalculatePadding(offset_); |
| 216 | } |
| 217 | |
| 218 | void RawPrintVisitor::pad(size_t padding) { |
| 219 | offset_ += padding; |
| 220 | } |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 221 | } // namespace android::idmap2 |