1 /*
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
17 #include "idmap2/RawPrintVisitor.h"
18
19 #include <algorithm>
20 #include <cstdarg>
21 #include <string>
22
23 #include "android-base/macros.h"
24 #include "android-base/stringprintf.h"
25 #include "androidfw/ApkAssets.h"
26 #include "idmap2/PolicyUtils.h"
27 #include "idmap2/ResourceUtils.h"
28 #include "idmap2/Result.h"
29
30 using android::ApkAssets;
31 using android::idmap2::policy::PoliciesToDebugString;
32
33 namespace {
34
StringSizeWhenEncoded(const std::string & s)35 size_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
42 namespace android::idmap2 {
43
visit(const Idmap & idmap ATTRIBUTE_UNUSED)44 void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
45 }
46
visit(const IdmapHeader & header)47 void 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");
52 print(header.GetFulfilledPolicies(), "fulfilled policies: %s",
53 PoliciesToDebugString(header.GetFulfilledPolicies()).c_str());
54 print(static_cast<uint8_t>(header.GetEnforceOverlayable()), "enforce overlayable");
55 print(header.GetTargetPath().to_string(), kIdmapStringLength, "target path");
56 print(header.GetOverlayPath().to_string(), kIdmapStringLength, "overlay path");
57 print("...", StringSizeWhenEncoded(header.GetDebugInfo()), "debug info");
58
59 target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string());
60 if (target_apk_) {
61 target_am_.SetApkAssets({target_apk_.get()});
62 }
63
64 overlay_apk_ = ApkAssets::Load(header.GetOverlayPath().to_string());
65 if (overlay_apk_) {
66 overlay_am_.SetApkAssets({overlay_apk_.get()});
67 }
68 }
69
visit(const IdmapData & data ATTRIBUTE_UNUSED)70 void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
71 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 }
128 }
129
visit(const IdmapData::Header & header)130 void RawPrintVisitor::visit(const IdmapData::Header& header) {
131 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");
137 }
138
139 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(uint8_t value,const char * fmt,...)140 void 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);
146
147 stream_ << base::StringPrintf("%08zx: %02x", offset_, value) << " " << comment
148 << std::endl;
149
150 offset_ += sizeof(uint8_t);
151 }
152
153 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(uint16_t value,const char * fmt,...)154 void 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
166 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(uint32_t value,const char * fmt,...)167 void 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
179 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(const std::string & value,size_t encoded_size,const char * fmt,...)180 void RawPrintVisitor::print(const std::string& value, size_t encoded_size, const char* fmt, ...) {
181 va_list ap;
182 va_start(ap, fmt);
183 std::string comment;
184 base::StringAppendV(&comment, fmt, ap);
185 va_end(ap);
186
187 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
188 << std::endl;
189
190 offset_ += encoded_size;
191 }
192
193 // NOLINTNEXTLINE(cert-dcl50-cpp)
print_raw(uint32_t length,const char * fmt,...)194 void 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
206 } // namespace android::idmap2
207