blob: 3531cd7c2f3610a9b2e7af842d8f42f1e07e2a72 [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 Kongstad1195a6b2021-05-11 12:57:01 +000021#include <string>
22#include <utility>
Mårten Kongstad02751232018-04-27 13:16:32 +020023
24#include "android-base/macros.h"
25#include "android-base/stringprintf.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
Ryan Mitchella7070132020-05-13 14:17:52 -070030using android::idmap2::policy::PoliciesToDebugString;
Mårten Kongstad02751232018-04-27 13:16:32 +020031
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010032namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020033
Mårten Kongstad02751232018-04-27 13:16:32 +020034void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
35}
36
37void RawPrintVisitor::visit(const IdmapHeader& header) {
38 print(header.GetMagic(), "magic");
39 print(header.GetVersion(), "version");
40 print(header.GetTargetCrc(), "target crc");
41 print(header.GetOverlayCrc(), "overlay crc");
Ryan Mitchella7070132020-05-13 14:17:52 -070042 print(header.GetFulfilledPolicies(), "fulfilled policies: %s",
43 PoliciesToDebugString(header.GetFulfilledPolicies()).c_str());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070044 print(static_cast<uint32_t>(header.GetEnforceOverlayable()), "enforce overlayable");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080045 print(header.GetTargetPath(), true /* print_value */, "target path");
46 print(header.GetOverlayPath(), true /* print_value */, "overlay path");
Ryan Mitchell30dc2e02020-12-02 11:43:18 -080047 print(header.GetOverlayName(), true /* print_value */, "overlay name");
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080048 print(header.GetDebugInfo(), false /* print_value */, "debug info");
Mårten Kongstad02751232018-04-27 13:16:32 +020049
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080050 if (auto target = TargetResourceContainer::FromPath(header.GetTargetPath())) {
51 target_ = std::move(*target);
Mårten Kongstad02751232018-04-27 13:16:32 +020052 }
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080053 if (auto overlay = OverlayResourceContainer::FromPath(header.GetOverlayPath())) {
54 overlay_ = std::move(*overlay);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070055 }
Mårten Kongstad02751232018-04-27 13:16:32 +020056}
57
58void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070059 for (auto& target_entry : data.GetTargetEntries()) {
60 Result<std::string> target_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080061 if (target_ != nullptr) {
62 target_name = target_->GetResourceName(target_entry.target_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070063 }
64 if (target_name) {
65 print(target_entry.target_id, "target id: %s", target_name->c_str());
66 } else {
67 print(target_entry.target_id, "target id");
68 }
69
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070070 Result<std::string> overlay_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080071 if (overlay_ != nullptr) {
72 overlay_name = overlay_->GetResourceName(target_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070073 }
74 if (overlay_name) {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070075 print(target_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070076 } else {
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070077 print(target_entry.overlay_id, "overlay id");
78 }
79 }
80
81 for (auto& target_entry : data.GetTargetInlineEntries()) {
82 Result<std::string> target_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080083 if (target_ != nullptr) {
84 target_name = target_->GetResourceName(target_entry.target_id);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070085 }
86 if (target_name) {
87 print(target_entry.target_id, "target id: %s", target_name->c_str());
88 } else {
89 print(target_entry.target_id, "target id");
90 }
91
Jeremy Meyerbe2b7792022-08-23 17:42:50 +000092
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080093 pad(sizeof(Res_value::size) + sizeof(Res_value::res0));
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070094
Jeremy Meyerbe2b7792022-08-23 17:42:50 +000095 for (auto& target_entry_value : target_entry.values) {
96 auto value = target_entry_value.second;
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070097
Jeremy Meyerbe2b7792022-08-23 17:42:50 +000098 print(target_entry_value.first.to_string(), false, "config: %s",
99 target_entry_value.first.toString().string());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700100
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000101 print(value.data_type, "type: %s",
102 utils::DataTypeToString(value.data_type).data());
103
104 Result<std::string> overlay_name(Error(""));
105 if (overlay_ != nullptr &&
106 (value.data_value == Res_value::TYPE_REFERENCE ||
107 value.data_value == Res_value::TYPE_DYNAMIC_REFERENCE)) {
108 overlay_name = overlay_->GetResourceName(value.data_value);
109 }
110
111 if (overlay_name) {
112 print(value.data_value, "data: %s", overlay_name->c_str());
113 } else {
114 print(value.data_value, "data");
115 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700116 }
117 }
118
119 for (auto& overlay_entry : data.GetOverlayEntries()) {
120 Result<std::string> overlay_name(Error(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800121 if (overlay_ != nullptr) {
122 overlay_name = overlay_->GetResourceName(overlay_entry.overlay_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700123 }
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(""));
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800132 if (target_ != nullptr) {
133 target_name = target_->GetResourceName(overlay_entry.target_id);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700134 }
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.GetTargetEntryCount(), "target entry count");
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700148 print(header.GetTargetInlineEntryCount(), "target inline entry count");
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000149 print(header.GetTargetInlineEntryValueCount(), "target inline entry value count");
150 print(header.GetConfigCount(), "config count");
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700151 print(header.GetOverlayEntryCount(), "overlay entry count");
152 print(header.GetStringPoolIndexOffset(), "string pool index offset");
Mårten Kongstad02751232018-04-27 13:16:32 +0200153}
154
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700155// NOLINTNEXTLINE(cert-dcl50-cpp)
156void RawPrintVisitor::print(uint8_t value, const char* fmt, ...) {
157 va_list ap;
158 va_start(ap, fmt);
159 std::string comment;
160 base::StringAppendV(&comment, fmt, ap);
161 va_end(ap);
Mårten Kongstad02751232018-04-27 13:16:32 +0200162
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700163 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
164 << std::endl;
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700165 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200166}
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(uint16_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: %04x", offset_, value) << " " << comment << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200177 offset_ += sizeof(uint16_t);
178}
179
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800180// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200181void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) {
182 va_list ap;
183 va_start(ap, fmt);
184 std::string comment;
185 base::StringAppendV(&comment, fmt, ap);
186 va_end(ap);
187
188 stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200189 offset_ += sizeof(uint32_t);
190}
191
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800192// NOLINTNEXTLINE(cert-dcl50-cpp)
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800193void RawPrintVisitor::print(const std::string& value, bool print_value, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200194 va_list ap;
195 va_start(ap, fmt);
196 std::string comment;
197 base::StringAppendV(&comment, fmt, ap);
198 va_end(ap);
199
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800200 stream_ << base::StringPrintf("%08zx: %08x", offset_, (uint32_t)value.size()) << " " << comment
201 << " size" << std::endl;
202 offset_ += sizeof(uint32_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200203
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800204 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment;
205 offset_ += value.size() + CalculatePadding(value.size());
206
207 if (print_value) {
208 stream_ << ": " << value;
209 }
210 stream_ << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200211}
212
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800213void RawPrintVisitor::align() {
214 offset_ += CalculatePadding(offset_);
215}
216
217void RawPrintVisitor::pad(size_t padding) {
218 offset_ += padding;
219}
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100220} // namespace android::idmap2