blob: a016a36a2e3df5418324abb81051d71acd76e9d1 [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>
Mårten Kongstad02751232018-04-27 13:16:32 +020021
22#include "android-base/macros.h"
23#include "android-base/stringprintf.h"
Ryan Mitchella7070132020-05-13 14:17:52 -070024#include "idmap2/PolicyUtils.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020025#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010026#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027
Ryan Mitchella7070132020-05-13 14:17:52 -070028using android::idmap2::policy::PoliciesToDebugString;
Mårten Kongstad02751232018-04-27 13:16:32 +020029
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
Mårten Kongstad02751232018-04-27 13:16:32 +020032void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
33}
34
35void 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 Mitchella7070132020-05-13 14:17:52 -070040 print(header.GetFulfilledPolicies(), "fulfilled policies: %s",
41 PoliciesToDebugString(header.GetFulfilledPolicies()).c_str());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070042 print(static_cast<uint32_t>(header.GetEnforceOverlayable()), "enforce overlayable");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080043 print(header.GetTargetPath(), true /* print_value */, "target path");
44 print(header.GetOverlayPath(), true /* print_value */, "overlay path");
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080045 print(header.GetOverlayName(), true /* print_value */, "overlay name");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080046 print(header.GetDebugInfo(), false /* print_value */, "debug info");
Mårten Kongstad02751232018-04-27 13:16:32 +020047
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080048 if (auto target = TargetResourceContainer::FromPath(header.GetTargetPath())) {
49 target_ = std::move(*target);
Mårten Kongstad02751232018-04-27 13:16:32 +020050 }
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080051 if (auto overlay = OverlayResourceContainer::FromPath(header.GetOverlayPath())) {
52 overlay_ = std::move(*overlay);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070053 }
Mårten Kongstad02751232018-04-27 13:16:32 +020054}
55
56void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070057 for (auto& target_entry : data.GetTargetEntries()) {
58 Result<std::string> target_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080059 if (target_ != nullptr) {
60 target_name = target_->GetResourceName(target_entry.target_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070061 }
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 Mitchelle753ffe2019-09-23 09:47:02 -070068 Result<std::string> overlay_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080069 if (overlay_ != nullptr) {
70 overlay_name = overlay_->GetResourceName(target_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070071 }
72 if (overlay_name) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070073 print(target_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070074 } else {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070075 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 Mitchell2ed8bfa2021-01-08 13:34:28 -080081 if (target_ != nullptr) {
82 target_name = target_->GetResourceName(target_entry.target_id);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070083 }
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 Mitchell0699f1d2020-12-03 15:41:42 -080090 pad(sizeof(Res_value::size) + sizeof(Res_value::res0));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070091
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 Mitchell2ed8bfa2021-01-08 13:34:28 -080096 if (overlay_ != nullptr &&
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070097 (target_entry.value.data_value == Res_value::TYPE_REFERENCE ||
98 target_entry.value.data_value == Res_value::TYPE_DYNAMIC_REFERENCE)) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080099 overlay_name = overlay_->GetResourceName(target_entry.value.data_value);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700100 }
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 Mitchelle753ffe2019-09-23 09:47:02 -0700106 }
107 }
108
109 for (auto& overlay_entry : data.GetOverlayEntries()) {
110 Result<std::string> overlay_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800111 if (overlay_ != nullptr) {
112 overlay_name = overlay_->GetResourceName(overlay_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700113 }
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 Mitchell2ed8bfa2021-01-08 13:34:28 -0800122 if (target_ != nullptr) {
123 target_name = target_->GetResourceName(overlay_entry.target_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700124 }
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 Mitchell0699f1d2020-12-03 15:41:42 -0800133 print(data.GetStringPoolData(), false /* print_value */, "string pool");
Mårten Kongstad02751232018-04-27 13:16:32 +0200134}
135
136void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700137 print(header.GetTargetEntryCount(), "target entry count");
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700138 print(header.GetTargetInlineEntryCount(), "target inline entry count");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700139 print(header.GetOverlayEntryCount(), "overlay entry count");
140 print(header.GetStringPoolIndexOffset(), "string pool index offset");
Mårten Kongstad02751232018-04-27 13:16:32 +0200141}
142
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700143// NOLINTNEXTLINE(cert-dcl50-cpp)
144void 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 Kongstad02751232018-04-27 13:16:32 +0200150
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700151 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
152 << std::endl;
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700153 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200154}
155
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800156// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200157void 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 Kongstad02751232018-04-27 13:16:32 +0200165 offset_ += sizeof(uint16_t);
166}
167
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800168// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200169void 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 Kongstad02751232018-04-27 13:16:32 +0200177 offset_ += sizeof(uint32_t);
178}
179
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800180// NOLINTNEXTLINE(cert-dcl50-cpp)
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800181void RawPrintVisitor::print(const std::string& value, bool print_value, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200182 va_list ap;
183 va_start(ap, fmt);
184 std::string comment;
185 base::StringAppendV(&comment, fmt, ap);
186 va_end(ap);
187
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800188 stream_ << base::StringPrintf("%08zx: %08x", offset_, (uint32_t)value.size()) << " " << comment
189 << " size" << std::endl;
190 offset_ += sizeof(uint32_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200191
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800192 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 Kongstad02751232018-04-27 13:16:32 +0200199}
200
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800201void RawPrintVisitor::align() {
202 offset_ += CalculatePadding(offset_);
203}
204
205void RawPrintVisitor::pad(size_t padding) {
206 offset_ += padding;
207}
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100208} // namespace android::idmap2