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/PrettyPrintVisitor.h"
18
19 #include <istream>
20 #include <string>
21 #include <utility>
22
23 #include "android-base/macros.h"
24 #include "android-base/stringprintf.h"
25 #include "androidfw/ApkAssets.h"
26 #include "idmap2/ResourceUtils.h"
27 #include "idmap2/Result.h"
28
29 namespace android::idmap2 {
30
31 #define TAB " "
32
visit(const Idmap & idmap ATTRIBUTE_UNUSED)33 void PrettyPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
34 }
35
visit(const IdmapHeader & header)36 void PrettyPrintVisitor::visit(const IdmapHeader& header) {
37 stream_ << "Paths:" << '\n'
38 << TAB "target path : " << header.GetTargetPath() << '\n'
39 << TAB "overlay path : " << header.GetOverlayPath() << '\n';
40
41 if (!header.GetOverlayName().empty()) {
42 stream_ << "Overlay name: " << header.GetOverlayName() << '\n';
43 }
44
45 const std::string& debug = header.GetDebugInfo();
46 if (!debug.empty()) {
47 std::istringstream debug_stream(debug);
48 std::string line;
49 stream_ << "Debug info:" << '\n';
50 while (std::getline(debug_stream, line)) {
51 stream_ << TAB << line << '\n';
52 }
53 }
54
55 if (auto target = TargetResourceContainer::FromPath(header.GetTargetPath())) {
56 target_ = std::move(*target);
57 }
58 if (auto overlay = OverlayResourceContainer::FromPath(header.GetOverlayPath())) {
59 overlay_ = std::move(*overlay);
60 }
61 }
62
visit(const IdmapConstraints & constraints)63 void PrettyPrintVisitor::visit(const IdmapConstraints& constraints) {
64 stream_ << "Constraints:" << '\n';
65 if (constraints.constraints.empty()) {
66 stream_ << TAB << "None\n";
67 } else {
68 for (const IdmapConstraint& constraint : constraints.constraints) {
69 stream_ << TAB
70 << base::StringPrintf("Type: %d, Value: %d\n", constraint.constraint_type,
71 constraint.constraint_value);
72 }
73 }
74
75 stream_ << "Mapping:" << '\n';
76 }
77
visit(const IdmapData::Header & header ATTRIBUTE_UNUSED)78 void PrettyPrintVisitor::visit(const IdmapData::Header& header ATTRIBUTE_UNUSED) {
79 }
80
visit(const IdmapData & data)81 void PrettyPrintVisitor::visit(const IdmapData& data) {
82 static constexpr const char* kUnknownResourceName = "???";
83
84 const ResStringPool string_pool(data.GetStringPoolData().data(), data.GetStringPoolData().size());
85 const size_t string_pool_offset = data.GetHeader()->GetStringPoolIndexOffset();
86
87 for (const auto& target_entry : data.GetTargetEntries()) {
88 std::string target_name = kUnknownResourceName;
89 if (target_ != nullptr) {
90 if (auto name = target_->GetResourceName(target_entry.target_id)) {
91 target_name = *name;
92 }
93 }
94
95 std::string overlay_name = kUnknownResourceName;
96 if (overlay_ != nullptr) {
97 if (auto name = overlay_->GetResourceName(target_entry.overlay_id)) {
98 overlay_name = *name;
99 }
100 }
101
102 stream_ << TAB
103 << base::StringPrintf("0x%08x -> 0x%08x (%s -> %s)", target_entry.target_id,
104 target_entry.overlay_id, target_name.c_str(),
105 overlay_name.c_str())
106 << '\n';
107 }
108
109 for (auto& target_entry : data.GetTargetInlineEntries()) {
110 for(auto iter = target_entry.values.begin(); iter != target_entry.values.end(); ++iter) {
111 auto value = iter->second;
112 stream_ << TAB << base::StringPrintf("0x%08x -> ", target_entry.target_id)
113 << utils::DataTypeToString(value.data_type);
114
115 if (value.data_type == Res_value::TYPE_STRING) {
116 auto str = string_pool.stringAt(value.data_value - string_pool_offset);
117 stream_ << " \"" << str.value_or(StringPiece16(u"")) << "\"";
118 } else {
119 stream_ << " " << base::StringPrintf("0x%08x", value.data_value);
120 }
121 }
122
123 std::string target_name = kUnknownResourceName;
124 if (target_ != nullptr) {
125 if (auto name = target_->GetResourceName(target_entry.target_id)) {
126 target_name = *name;
127 }
128 }
129
130 stream_ << " (" << target_name << ")" << '\n';
131 }
132 }
133
134 } // namespace android::idmap2
135