blob: 3f62a2ae20294bf10de2f30b0fbe584f0d6fe5e5 [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 Kongstadd7e8a532019-10-11 08:32:04 +020033namespace {
34
35size_t StringSizeWhenEncoded(const std::string& s) {
36 size_t null_bytes = 4 - (s.size() % 4);
37 return sizeof(uint32_t) + s.size() + null_bytes;
38}
39
40} // namespace
41
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010042namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020043
Mårten Kongstad02751232018-04-27 13:16:32 +020044void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
45}
46
47void RawPrintVisitor::visit(const IdmapHeader& header) {
48 print(header.GetMagic(), "magic");
49 print(header.GetVersion(), "version");
50 print(header.GetTargetCrc(), "target crc");
51 print(header.GetOverlayCrc(), "overlay crc");
Ryan Mitchella7070132020-05-13 14:17:52 -070052 print(header.GetFulfilledPolicies(), "fulfilled policies: %s",
53 PoliciesToDebugString(header.GetFulfilledPolicies()).c_str());
54 print(static_cast<uint8_t>(header.GetEnforceOverlayable()), "enforce overlayable");
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020055 print(header.GetTargetPath().to_string(), kIdmapStringLength, "target path");
56 print(header.GetOverlayPath().to_string(), kIdmapStringLength, "overlay path");
57 print("...", StringSizeWhenEncoded(header.GetDebugInfo()), "debug info");
Mårten Kongstad02751232018-04-27 13:16:32 +020058
59 target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string());
60 if (target_apk_) {
61 target_am_.SetApkAssets({target_apk_.get()});
62 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070063
64 overlay_apk_ = ApkAssets::Load(header.GetOverlayPath().to_string());
65 if (overlay_apk_) {
66 overlay_am_.SetApkAssets({overlay_apk_.get()});
67 }
Mårten Kongstad02751232018-04-27 13:16:32 +020068}
69
70void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070071 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
72 const bool overlay_package_loaded = !overlay_am_.GetApkAssets().empty();
73
74 for (auto& target_entry : data.GetTargetEntries()) {
75 Result<std::string> target_name(Error(""));
76 if (target_package_loaded) {
77 target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id);
78 }
79 if (target_name) {
80 print(target_entry.target_id, "target id: %s", target_name->c_str());
81 } else {
82 print(target_entry.target_id, "target id");
83 }
84
85 print(target_entry.data_type, "type: %s",
86 utils::DataTypeToString(target_entry.data_type).data());
87
88 Result<std::string> overlay_name(Error(""));
89 if (overlay_package_loaded && (target_entry.data_type == Res_value::TYPE_REFERENCE ||
90 target_entry.data_type == Res_value::TYPE_DYNAMIC_REFERENCE)) {
91 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.data_value);
92 }
93 if (overlay_name) {
94 print(target_entry.data_value, "value: %s", overlay_name->c_str());
95 } else {
96 print(target_entry.data_value, "value");
97 }
98 }
99
100 for (auto& overlay_entry : data.GetOverlayEntries()) {
101 Result<std::string> overlay_name(Error(""));
102 if (overlay_package_loaded) {
103 overlay_name = utils::ResToTypeEntryName(overlay_am_, overlay_entry.overlay_id);
104 }
105
106 if (overlay_name) {
107 print(overlay_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
108 } else {
109 print(overlay_entry.overlay_id, "overlay id");
110 }
111
112 Result<std::string> target_name(Error(""));
113 if (target_package_loaded) {
114 target_name = utils::ResToTypeEntryName(target_am_, overlay_entry.target_id);
115 }
116
117 if (target_name) {
118 print(overlay_entry.target_id, "target id: %s", target_name->c_str());
119 } else {
120 print(overlay_entry.target_id, "target id");
121 }
122 }
123
124 const size_t string_pool_length = data.GetHeader()->GetStringPoolLength();
125 if (string_pool_length > 0) {
126 print_raw(string_pool_length, "%zu raw string pool bytes", string_pool_length);
127 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200128}
129
130void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700131 print(header.GetTargetPackageId(), "target package id");
132 print(header.GetOverlayPackageId(), "overlay package id");
133 print(header.GetTargetEntryCount(), "target entry count");
134 print(header.GetOverlayEntryCount(), "overlay entry count");
135 print(header.GetStringPoolIndexOffset(), "string pool index offset");
136 print(header.GetStringPoolLength(), "string pool byte length");
Mårten Kongstad02751232018-04-27 13:16:32 +0200137}
138
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700139// NOLINTNEXTLINE(cert-dcl50-cpp)
140void RawPrintVisitor::print(uint8_t value, const char* fmt, ...) {
141 va_list ap;
142 va_start(ap, fmt);
143 std::string comment;
144 base::StringAppendV(&comment, fmt, ap);
145 va_end(ap);
Mårten Kongstad02751232018-04-27 13:16:32 +0200146
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700147 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
148 << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200149
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700150 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200151}
152
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800153// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200154void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) {
155 va_list ap;
156 va_start(ap, fmt);
157 std::string comment;
158 base::StringAppendV(&comment, fmt, ap);
159 va_end(ap);
160
161 stream_ << base::StringPrintf("%08zx: %04x", offset_, value) << " " << comment << std::endl;
162
163 offset_ += sizeof(uint16_t);
164}
165
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800166// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200167void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) {
168 va_list ap;
169 va_start(ap, fmt);
170 std::string comment;
171 base::StringAppendV(&comment, fmt, ap);
172 va_end(ap);
173
174 stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl;
175
176 offset_ += sizeof(uint32_t);
177}
178
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800179// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200180void RawPrintVisitor::print(const std::string& value, size_t encoded_size, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200181 va_list ap;
182 va_start(ap, fmt);
183 std::string comment;
184 base::StringAppendV(&comment, fmt, ap);
185 va_end(ap);
186
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700187 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 << std::endl;
189
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200190 offset_ += encoded_size;
Mårten Kongstad02751232018-04-27 13:16:32 +0200191}
192
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700193// NOLINTNEXTLINE(cert-dcl50-cpp)
194void RawPrintVisitor::print_raw(uint32_t length, const char* fmt, ...) {
195 va_list ap;
196 va_start(ap, fmt);
197 std::string comment;
198 base::StringAppendV(&comment, fmt, ap);
199 va_end(ap);
200
201 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << std::endl;
202
203 offset_ += length;
204}
205
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100206} // namespace android::idmap2