blob: 751c60c4add4cc84edf2254659b5301b47c62b96 [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"
Mårten Kongstad02751232018-04-27 13:16:32 +020026#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010027#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020028
29using android::ApkAssets;
30
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020031namespace {
32
33size_t StringSizeWhenEncoded(const std::string& s) {
34 size_t null_bytes = 4 - (s.size() % 4);
35 return sizeof(uint32_t) + s.size() + null_bytes;
36}
37
38} // namespace
39
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010040namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020041
42// verbatim copy fomr PrettyPrintVisitor.cpp, move to common utils
43#define RESID(pkg, type, entry) (((pkg) << 24) | ((type) << 16) | (entry))
44
45void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
46}
47
48void RawPrintVisitor::visit(const IdmapHeader& header) {
49 print(header.GetMagic(), "magic");
50 print(header.GetVersion(), "version");
51 print(header.GetTargetCrc(), "target crc");
52 print(header.GetOverlayCrc(), "overlay crc");
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020053 print(header.GetTargetPath().to_string(), kIdmapStringLength, "target path");
54 print(header.GetOverlayPath().to_string(), kIdmapStringLength, "overlay path");
55 print("...", StringSizeWhenEncoded(header.GetDebugInfo()), "debug info");
Mårten Kongstad02751232018-04-27 13:16:32 +020056
57 target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string());
58 if (target_apk_) {
59 target_am_.SetApkAssets({target_apk_.get()});
60 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070061
62 overlay_apk_ = ApkAssets::Load(header.GetOverlayPath().to_string());
63 if (overlay_apk_) {
64 overlay_am_.SetApkAssets({overlay_apk_.get()});
65 }
Mårten Kongstad02751232018-04-27 13:16:32 +020066}
67
68void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070069 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
70 const bool overlay_package_loaded = !overlay_am_.GetApkAssets().empty();
71
72 for (auto& target_entry : data.GetTargetEntries()) {
73 Result<std::string> target_name(Error(""));
74 if (target_package_loaded) {
75 target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id);
76 }
77 if (target_name) {
78 print(target_entry.target_id, "target id: %s", target_name->c_str());
79 } else {
80 print(target_entry.target_id, "target id");
81 }
82
83 print(target_entry.data_type, "type: %s",
84 utils::DataTypeToString(target_entry.data_type).data());
85
86 Result<std::string> overlay_name(Error(""));
87 if (overlay_package_loaded && (target_entry.data_type == Res_value::TYPE_REFERENCE ||
88 target_entry.data_type == Res_value::TYPE_DYNAMIC_REFERENCE)) {
89 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.data_value);
90 }
91 if (overlay_name) {
92 print(target_entry.data_value, "value: %s", overlay_name->c_str());
93 } else {
94 print(target_entry.data_value, "value");
95 }
96 }
97
98 for (auto& overlay_entry : data.GetOverlayEntries()) {
99 Result<std::string> overlay_name(Error(""));
100 if (overlay_package_loaded) {
101 overlay_name = utils::ResToTypeEntryName(overlay_am_, overlay_entry.overlay_id);
102 }
103
104 if (overlay_name) {
105 print(overlay_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
106 } else {
107 print(overlay_entry.overlay_id, "overlay id");
108 }
109
110 Result<std::string> target_name(Error(""));
111 if (target_package_loaded) {
112 target_name = utils::ResToTypeEntryName(target_am_, overlay_entry.target_id);
113 }
114
115 if (target_name) {
116 print(overlay_entry.target_id, "target id: %s", target_name->c_str());
117 } else {
118 print(overlay_entry.target_id, "target id");
119 }
120 }
121
122 const size_t string_pool_length = data.GetHeader()->GetStringPoolLength();
123 if (string_pool_length > 0) {
124 print_raw(string_pool_length, "%zu raw string pool bytes", string_pool_length);
125 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200126}
127
128void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700129 print(header.GetTargetPackageId(), "target package id");
130 print(header.GetOverlayPackageId(), "overlay package id");
131 print(header.GetTargetEntryCount(), "target entry count");
132 print(header.GetOverlayEntryCount(), "overlay entry count");
133 print(header.GetStringPoolIndexOffset(), "string pool index offset");
134 print(header.GetStringPoolLength(), "string pool byte length");
Mårten Kongstad02751232018-04-27 13:16:32 +0200135}
136
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700137// NOLINTNEXTLINE(cert-dcl50-cpp)
138void RawPrintVisitor::print(uint8_t value, const char* fmt, ...) {
139 va_list ap;
140 va_start(ap, fmt);
141 std::string comment;
142 base::StringAppendV(&comment, fmt, ap);
143 va_end(ap);
Mårten Kongstad02751232018-04-27 13:16:32 +0200144
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700145 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
146 << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200147
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700148 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200149}
150
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800151// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200152void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) {
153 va_list ap;
154 va_start(ap, fmt);
155 std::string comment;
156 base::StringAppendV(&comment, fmt, ap);
157 va_end(ap);
158
159 stream_ << base::StringPrintf("%08zx: %04x", offset_, value) << " " << comment << std::endl;
160
161 offset_ += sizeof(uint16_t);
162}
163
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800164// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200165void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) {
166 va_list ap;
167 va_start(ap, fmt);
168 std::string comment;
169 base::StringAppendV(&comment, fmt, ap);
170 va_end(ap);
171
172 stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl;
173
174 offset_ += sizeof(uint32_t);
175}
176
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800177// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200178void RawPrintVisitor::print(const std::string& value, size_t encoded_size, const char* fmt, ...) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200179 va_list ap;
180 va_start(ap, fmt);
181 std::string comment;
182 base::StringAppendV(&comment, fmt, ap);
183 va_end(ap);
184
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700185 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
Mårten Kongstad02751232018-04-27 13:16:32 +0200186 << std::endl;
187
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200188 offset_ += encoded_size;
Mårten Kongstad02751232018-04-27 13:16:32 +0200189}
190
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700191// NOLINTNEXTLINE(cert-dcl50-cpp)
192void RawPrintVisitor::print_raw(uint32_t length, const char* fmt, ...) {
193 va_list ap;
194 va_start(ap, fmt);
195 std::string comment;
196 base::StringAppendV(&comment, fmt, ap);
197 va_end(ap);
198
199 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << std::endl;
200
201 offset_ += length;
202}
203
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100204} // namespace android::idmap2