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   stream_ << "Mapping:" << '\n';
63 }
64 
visit(const IdmapData::Header & header ATTRIBUTE_UNUSED)65 void PrettyPrintVisitor::visit(const IdmapData::Header& header ATTRIBUTE_UNUSED) {
66 }
67 
visit(const IdmapData & data)68 void PrettyPrintVisitor::visit(const IdmapData& data) {
69   static constexpr const char* kUnknownResourceName = "???";
70 
71   const ResStringPool string_pool(data.GetStringPoolData().data(), data.GetStringPoolData().size());
72   const size_t string_pool_offset = data.GetHeader()->GetStringPoolIndexOffset();
73 
74   for (const auto& target_entry : data.GetTargetEntries()) {
75     std::string target_name = kUnknownResourceName;
76     if (target_ != nullptr) {
77       if (auto name = target_->GetResourceName(target_entry.target_id)) {
78         target_name = *name;
79       }
80     }
81 
82     std::string overlay_name = kUnknownResourceName;
83     if (overlay_ != nullptr) {
84       if (auto name = overlay_->GetResourceName(target_entry.overlay_id)) {
85         overlay_name = *name;
86       }
87     }
88 
89     stream_ << TAB
90             << base::StringPrintf("0x%08x -> 0x%08x (%s -> %s)", target_entry.target_id,
91                                   target_entry.overlay_id, target_name.c_str(),
92                                   overlay_name.c_str())
93             << '\n';
94   }
95 
96   for (auto& target_entry : data.GetTargetInlineEntries()) {
97     for(auto iter = target_entry.values.begin(); iter != target_entry.values.end(); ++iter) {
98       auto value = iter->second;
99       stream_ << TAB << base::StringPrintf("0x%08x -> ", target_entry.target_id)
100               << utils::DataTypeToString(value.data_type);
101 
102       if (value.data_type == Res_value::TYPE_STRING) {
103         auto str = string_pool.stringAt(value.data_value - string_pool_offset);
104         stream_ << " \"" << str.value_or(StringPiece16(u"")) << "\"";
105       } else {
106         stream_ << " " << base::StringPrintf("0x%08x", value.data_value);
107       }
108     }
109 
110     std::string target_name = kUnknownResourceName;
111     if (target_ != nullptr) {
112       if (auto name = target_->GetResourceName(target_entry.target_id)) {
113         target_name = *name;
114       }
115     }
116 
117     stream_ << " (" << target_name << ")" << '\n';
118   }
119 }
120 
121 }  // namespace android::idmap2
122