blob: 13973d64fe68c559ed87e8c8629321f4b11eabd6 [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 Kongstad02751232018-04-27 13:16:32 +020019#include <cstdarg>
20#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020021
22#include "android-base/macros.h"
23#include "android-base/stringprintf.h"
24#include "androidfw/ApkAssets.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
28using android::ApkAssets;
29
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
32// verbatim copy fomr PrettyPrintVisitor.cpp, move to common utils
33#define RESID(pkg, type, entry) (((pkg) << 24) | ((type) << 16) | (entry))
34
35void 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");
43 print(header.GetTargetPath().to_string(), "target path");
44 print(header.GetOverlayPath().to_string(), "overlay path");
45
46 target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string());
47 if (target_apk_) {
48 target_am_.SetApkAssets({target_apk_.get()});
49 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070050
51 overlay_apk_ = ApkAssets::Load(header.GetOverlayPath().to_string());
52 if (overlay_apk_) {
53 overlay_am_.SetApkAssets({overlay_apk_.get()});
54 }
Mårten Kongstad02751232018-04-27 13:16:32 +020055}
56
57void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070058 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
59 const bool overlay_package_loaded = !overlay_am_.GetApkAssets().empty();
60
61 for (auto& target_entry : data.GetTargetEntries()) {
62 Result<std::string> target_name(Error(""));
63 if (target_package_loaded) {
64 target_name = utils::ResToTypeEntryName(target_am_, target_entry.target_id);
65 }
66 if (target_name) {
67 print(target_entry.target_id, "target id: %s", target_name->c_str());
68 } else {
69 print(target_entry.target_id, "target id");
70 }
71
72 print(target_entry.data_type, "type: %s",
73 utils::DataTypeToString(target_entry.data_type).data());
74
75 Result<std::string> overlay_name(Error(""));
76 if (overlay_package_loaded && (target_entry.data_type == Res_value::TYPE_REFERENCE ||
77 target_entry.data_type == Res_value::TYPE_DYNAMIC_REFERENCE)) {
78 overlay_name = utils::ResToTypeEntryName(overlay_am_, target_entry.data_value);
79 }
80 if (overlay_name) {
81 print(target_entry.data_value, "value: %s", overlay_name->c_str());
82 } else {
83 print(target_entry.data_value, "value");
84 }
85 }
86
87 for (auto& overlay_entry : data.GetOverlayEntries()) {
88 Result<std::string> overlay_name(Error(""));
89 if (overlay_package_loaded) {
90 overlay_name = utils::ResToTypeEntryName(overlay_am_, overlay_entry.overlay_id);
91 }
92
93 if (overlay_name) {
94 print(overlay_entry.overlay_id, "overlay id: %s", overlay_name->c_str());
95 } else {
96 print(overlay_entry.overlay_id, "overlay id");
97 }
98
99 Result<std::string> target_name(Error(""));
100 if (target_package_loaded) {
101 target_name = utils::ResToTypeEntryName(target_am_, overlay_entry.target_id);
102 }
103
104 if (target_name) {
105 print(overlay_entry.target_id, "target id: %s", target_name->c_str());
106 } else {
107 print(overlay_entry.target_id, "target id");
108 }
109 }
110
111 const size_t string_pool_length = data.GetHeader()->GetStringPoolLength();
112 if (string_pool_length > 0) {
113 print_raw(string_pool_length, "%zu raw string pool bytes", string_pool_length);
114 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200115}
116
117void RawPrintVisitor::visit(const IdmapData::Header& header) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700118 print(header.GetTargetPackageId(), "target package id");
119 print(header.GetOverlayPackageId(), "overlay package id");
120 print(header.GetTargetEntryCount(), "target entry count");
121 print(header.GetOverlayEntryCount(), "overlay entry count");
122 print(header.GetStringPoolIndexOffset(), "string pool index offset");
123 print(header.GetStringPoolLength(), "string pool byte length");
Mårten Kongstad02751232018-04-27 13:16:32 +0200124}
125
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700126// NOLINTNEXTLINE(cert-dcl50-cpp)
127void RawPrintVisitor::print(uint8_t value, const char* fmt, ...) {
128 va_list ap;
129 va_start(ap, fmt);
130 std::string comment;
131 base::StringAppendV(&comment, fmt, ap);
132 va_end(ap);
Mårten Kongstad02751232018-04-27 13:16:32 +0200133
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700134 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
135 << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200136
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700137 offset_ += sizeof(uint8_t);
Mårten Kongstad02751232018-04-27 13:16:32 +0200138}
139
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800140// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200141void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) {
142 va_list ap;
143 va_start(ap, fmt);
144 std::string comment;
145 base::StringAppendV(&comment, fmt, ap);
146 va_end(ap);
147
148 stream_ << base::StringPrintf("%08zx: %04x", offset_, value) << " " << comment << std::endl;
149
150 offset_ += sizeof(uint16_t);
151}
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(uint32_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: %08x", offset_, value) << " " << comment << std::endl;
162
163 offset_ += sizeof(uint32_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(const std::string& 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
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700174 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
Mårten Kongstad02751232018-04-27 13:16:32 +0200175 << std::endl;
176
177 offset_ += kIdmapStringLength;
178}
179
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700180// NOLINTNEXTLINE(cert-dcl50-cpp)
181void RawPrintVisitor::print_raw(uint32_t length, 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: ", offset_) << "........ " << comment << std::endl;
189
190 offset_ += length;
191}
192
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100193} // namespace android::idmap2