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