blob: d7f073944542e81e76632ddc94399d09d6adcf6c [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");
48 print(header.GetDebugInfo(), false /* print_value */, "debug info");
Mårten Kongstad02751232018-04-27 13:16:32 +020049
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080050 auto target_apk_ = ApkAssets::Load(header.GetTargetPath());
Mårten Kongstad02751232018-04-27 13:16:32 +020051 if (target_apk_) {
52 target_am_.SetApkAssets({target_apk_.get()});
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070053 apk_assets_.push_back(std::move(target_apk_));
Mårten Kongstad02751232018-04-27 13:16:32 +020054 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070055
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080056 auto overlay_apk_ = ApkAssets::Load(header.GetOverlayPath());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070057 if (overlay_apk_) {
58 overlay_am_.SetApkAssets({overlay_apk_.get()});
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070059 apk_assets_.push_back(std::move(overlay_apk_));
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070060 }
Mårten Kongstad02751232018-04-27 13:16:32 +020061}
62
63void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070064 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 Mitchelle753ffe2019-09-23 09:47:02 -070078 Result<std::string> overlay_name(Error(""));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070079 if (overlay_package_loaded) {
80 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070081 }
82 if (overlay_name) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070083 print(target_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070084 } else {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070085 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 Mitchell0699f1d2020-12-03 15:41:42 -0800100 pad(sizeof(Res_value::size) + sizeof(Res_value::res0));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700101
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 Mitchelle753ffe2019-09-23 09:47:02 -0700116 }
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 Mitchell0699f1d2020-12-03 15:41:42 -0800143 print(data.GetStringPoolData(), false /* print_value */, "string pool");
Mårten Kongstad02751232018-04-27 13:16:32 +0200144}
145
146void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700147 print(header.GetTargetPackageId(), "target package id");
148 print(header.GetOverlayPackageId(), "overlay package id");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800149 align();
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700150 print(header.GetTargetEntryCount(), "target entry count");
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700151 print(header.GetTargetInlineEntryCount(), "target inline entry count");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700152 print(header.GetOverlayEntryCount(), "overlay entry count");
153 print(header.GetStringPoolIndexOffset(), "string pool index offset");
Mårten Kongstad02751232018-04-27 13:16:32 +0200154}
155
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700156// NOLINTNEXTLINE(cert-dcl50-cpp)
157void 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 Kongstad02751232018-04-27 13:16:32 +0200163
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700164 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
165 << std::endl;
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700166 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200167}
168
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800169// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200170void 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 Kongstad02751232018-04-27 13:16:32 +0200178 offset_ += sizeof(uint16_t);
179}
180
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800181// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200182void 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 Kongstad02751232018-04-27 13:16:32 +0200190 offset_ += sizeof(uint32_t);
191}
192
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800193// NOLINTNEXTLINE(cert-dcl50-cpp)
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800194void RawPrintVisitor::print(const std::string& value, bool print_value, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200195 va_list ap;
196 va_start(ap, fmt);
197 std::string comment;
198 base::StringAppendV(&comment, fmt, ap);
199 va_end(ap);
200
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800201 stream_ << base::StringPrintf("%08zx: %08x", offset_, (uint32_t)value.size()) << " " << comment
202 << " size" << std::endl;
203 offset_ += sizeof(uint32_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200204
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800205 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 Kongstad02751232018-04-27 13:16:32 +0200212}
213
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800214void RawPrintVisitor::align() {
215 offset_ += CalculatePadding(offset_);
216}
217
218void RawPrintVisitor::pad(size_t padding) {
219 offset_ += padding;
220}
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100221} // namespace android::idmap2