blob: b517aa3a0c01b31f5bed394918366366318c985a [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/RawPrintVisitor.h"
18
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020019#include <algorithm>
Mårten Kongstad02751232018-04-27 13:16:32 +020020#include <cstdarg>
21#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020022
23#include "android-base/macros.h"
24#include "android-base/stringprintf.h"
25#include "androidfw/ApkAssets.h"
Ryan Mitchella7070132020-05-13 14:17:52 -070026#include "idmap2/PolicyUtils.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010028#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020029
30using android::ApkAssets;
Ryan Mitchella7070132020-05-13 14:17:52 -070031using android::idmap2::policy::PoliciesToDebugString;
Mårten Kongstad02751232018-04-27 13:16:32 +020032
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010033namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020034
Mårten Kongstad02751232018-04-27 13:16:32 +020035void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
36}
37
38void 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 Mitchella7070132020-05-13 14:17:52 -070043 print(header.GetFulfilledPolicies(), "fulfilled policies: %s",
44 PoliciesToDebugString(header.GetFulfilledPolicies()).c_str());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070045 print(static_cast<uint32_t>(header.GetEnforceOverlayable()), "enforce overlayable");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080046 print(header.GetTargetPath(), true /* print_value */, "target path");
47 print(header.GetOverlayPath(), true /* print_value */, "overlay path");
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080048 print(header.GetOverlayName(), true /* print_value */, "overlay name");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080049 print(header.GetDebugInfo(), false /* print_value */, "debug info");
Mårten Kongstad02751232018-04-27 13:16:32 +020050
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080051 auto target_apk_ = ApkAssets::Load(header.GetTargetPath());
Mårten Kongstad02751232018-04-27 13:16:32 +020052 if (target_apk_) {
53 target_am_.SetApkAssets({target_apk_.get()});
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070054 apk_assets_.push_back(std::move(target_apk_));
Mårten Kongstad02751232018-04-27 13:16:32 +020055 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070056
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080057 auto overlay_apk_ = ApkAssets::Load(header.GetOverlayPath());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070058 if (overlay_apk_) {
59 overlay_am_.SetApkAssets({overlay_apk_.get()});
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070060 apk_assets_.push_back(std::move(overlay_apk_));
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070061 }
Mårten Kongstad02751232018-04-27 13:16:32 +020062}
63
64void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070065 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 Mitchelle753ffe2019-09-23 09:47:02 -070079 Result<std::string> overlay_name(Error(""));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070080 if (overlay_package_loaded) {
81 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070082 }
83 if (overlay_name) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070084 print(target_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070085 } else {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070086 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 Mitchell0699f1d2020-12-03 15:41:42 -0800101 pad(sizeof(Res_value::size) + sizeof(Res_value::res0));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700102
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 Mitchelle753ffe2019-09-23 09:47:02 -0700117 }
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 Mitchell0699f1d2020-12-03 15:41:42 -0800144 print(data.GetStringPoolData(), false /* print_value */, "string pool");
Mårten Kongstad02751232018-04-27 13:16:32 +0200145}
146
147void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700148 print(header.GetTargetPackageId(), "target package id");
149 print(header.GetOverlayPackageId(), "overlay package id");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800150 align();
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700151 print(header.GetTargetEntryCount(), "target entry count");
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700152 print(header.GetTargetInlineEntryCount(), "target inline entry count");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700153 print(header.GetOverlayEntryCount(), "overlay entry count");
154 print(header.GetStringPoolIndexOffset(), "string pool index offset");
Mårten Kongstad02751232018-04-27 13:16:32 +0200155}
156
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700157// NOLINTNEXTLINE(cert-dcl50-cpp)
158void 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 Kongstad02751232018-04-27 13:16:32 +0200164
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700165 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
166 << std::endl;
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700167 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200168}
169
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800170// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200171void 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 Kongstad02751232018-04-27 13:16:32 +0200179 offset_ += sizeof(uint16_t);
180}
181
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800182// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200183void 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 Kongstad02751232018-04-27 13:16:32 +0200191 offset_ += sizeof(uint32_t);
192}
193
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800194// NOLINTNEXTLINE(cert-dcl50-cpp)
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800195void RawPrintVisitor::print(const std::string& value, bool print_value, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200196 va_list ap;
197 va_start(ap, fmt);
198 std::string comment;
199 base::StringAppendV(&comment, fmt, ap);
200 va_end(ap);
201
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800202 stream_ << base::StringPrintf("%08zx: %08x", offset_, (uint32_t)value.size()) << " " << comment
203 << " size" << std::endl;
204 offset_ += sizeof(uint32_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200205
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800206 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 Kongstad02751232018-04-27 13:16:32 +0200213}
214
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800215void RawPrintVisitor::align() {
216 offset_ += CalculatePadding(offset_);
217}
218
219void RawPrintVisitor::pad(size_t padding) {
220 offset_ += padding;
221}
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100222} // namespace android::idmap2