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