blob: 82f5d26cbbb31f98e2129a23dda7b73fb7f1f6eb [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");
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020046 print(header.GetTargetPath().to_string(), kIdmapStringLength, "target path");
47 print(header.GetOverlayPath().to_string(), kIdmapStringLength, "overlay path");
Mårten Kongstad02751232018-04-27 13:16:32 +020048
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070049 uint32_t debug_info_size = header.GetDebugInfo().size();
50 print(debug_info_size, "debug info size");
51 print("...", debug_info_size + CalculatePadding(debug_info_size), "debug info");
52
53 auto target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string());
Mårten Kongstad02751232018-04-27 13:16:32 +020054 if (target_apk_) {
55 target_am_.SetApkAssets({target_apk_.get()});
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070056 apk_assets_.push_back(std::move(target_apk_));
Mårten Kongstad02751232018-04-27 13:16:32 +020057 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070058
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070059 auto overlay_apk_ = ApkAssets::Load(header.GetOverlayPath().to_string());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070060 if (overlay_apk_) {
61 overlay_am_.SetApkAssets({overlay_apk_.get()});
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070062 apk_assets_.push_back(std::move(overlay_apk_));
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070063 }
Mårten Kongstad02751232018-04-27 13:16:32 +020064}
65
66void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070067 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
68 const bool overlay_package_loaded = !overlay_am_.GetApkAssets().empty();
69
70 for (auto& target_entry : data.GetTargetEntries()) {
71 Result<std::string> target_name(Error(""));
72 if (target_package_loaded) {
73 target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id);
74 }
75 if (target_name) {
76 print(target_entry.target_id, "target id: %s", target_name->c_str());
77 } else {
78 print(target_entry.target_id, "target id");
79 }
80
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070081 Result<std::string> overlay_name(Error(""));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070082 if (overlay_package_loaded) {
83 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070084 }
85 if (overlay_name) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070086 print(target_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070087 } else {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070088 print(target_entry.overlay_id, "overlay id");
89 }
90 }
91
92 for (auto& target_entry : data.GetTargetInlineEntries()) {
93 Result<std::string> target_name(Error(""));
94 if (target_package_loaded) {
95 target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id);
96 }
97 if (target_name) {
98 print(target_entry.target_id, "target id: %s", target_name->c_str());
99 } else {
100 print(target_entry.target_id, "target id");
101 }
102
103 print("...", sizeof(Res_value::size) + sizeof(Res_value::res0), "padding");
104
105 print(target_entry.value.data_type, "type: %s",
106 utils::DataTypeToString(target_entry.value.data_type).data());
107
108 Result<std::string> overlay_name(Error(""));
109 if (overlay_package_loaded &&
110 (target_entry.value.data_value == Res_value::TYPE_REFERENCE ||
111 target_entry.value.data_value == Res_value::TYPE_DYNAMIC_REFERENCE)) {
112 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.value.data_value);
113 }
114
115 if (overlay_name) {
116 print(target_entry.value.data_value, "data: %s", overlay_name->c_str());
117 } else {
118 print(target_entry.value.data_value, "data");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700119 }
120 }
121
122 for (auto& overlay_entry : data.GetOverlayEntries()) {
123 Result<std::string> overlay_name(Error(""));
124 if (overlay_package_loaded) {
125 overlay_name = utils::ResToTypeEntryName(overlay_am_, overlay_entry.overlay_id);
126 }
127
128 if (overlay_name) {
129 print(overlay_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
130 } else {
131 print(overlay_entry.overlay_id, "overlay id");
132 }
133
134 Result<std::string> target_name(Error(""));
135 if (target_package_loaded) {
136 target_name = utils::ResToTypeEntryName(target_am_, overlay_entry.target_id);
137 }
138
139 if (target_name) {
140 print(overlay_entry.target_id, "target id: %s", target_name->c_str());
141 } else {
142 print(overlay_entry.target_id, "target id");
143 }
144 }
145
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700146 uint32_t string_pool_size = data.GetStringPoolData().size();
147 print(string_pool_size, "string pool size");
148 print("...", string_pool_size + CalculatePadding(string_pool_size), "string pool");
Mårten Kongstad02751232018-04-27 13:16:32 +0200149}
150
151void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700152 print(header.GetTargetPackageId(), "target package id");
153 print(header.GetOverlayPackageId(), "overlay package id");
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700154 print("...", sizeof(Idmap_data_header::p0), "padding");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700155 print(header.GetTargetEntryCount(), "target entry count");
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700156 print(header.GetTargetInlineEntryCount(), "target inline entry count");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700157 print(header.GetOverlayEntryCount(), "overlay entry count");
158 print(header.GetStringPoolIndexOffset(), "string pool index offset");
Mårten Kongstad02751232018-04-27 13:16:32 +0200159}
160
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700161// NOLINTNEXTLINE(cert-dcl50-cpp)
162void RawPrintVisitor::print(uint8_t value, const char* fmt, ...) {
163 va_list ap;
164 va_start(ap, fmt);
165 std::string comment;
166 base::StringAppendV(&comment, fmt, ap);
167 va_end(ap);
Mårten Kongstad02751232018-04-27 13:16:32 +0200168
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700169 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
170 << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200171
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700172 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200173}
174
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800175// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200176void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) {
177 va_list ap;
178 va_start(ap, fmt);
179 std::string comment;
180 base::StringAppendV(&comment, fmt, ap);
181 va_end(ap);
182
183 stream_ << base::StringPrintf("%08zx: %04x", offset_, value) << " " << comment << std::endl;
184
185 offset_ += sizeof(uint16_t);
186}
187
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800188// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200189void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) {
190 va_list ap;
191 va_start(ap, fmt);
192 std::string comment;
193 base::StringAppendV(&comment, fmt, ap);
194 va_end(ap);
195
196 stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl;
197
198 offset_ += sizeof(uint32_t);
199}
200
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800201// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200202void RawPrintVisitor::print(const std::string& value, size_t encoded_size, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200203 va_list ap;
204 va_start(ap, fmt);
205 std::string comment;
206 base::StringAppendV(&comment, fmt, ap);
207 va_end(ap);
208
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700209 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
Mårten Kongstad02751232018-04-27 13:16:32 +0200210 << std::endl;
211
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200212 offset_ += encoded_size;
Mårten Kongstad02751232018-04-27 13:16:32 +0200213}
214
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100215} // namespace android::idmap2