• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:" << std::endl
38           << TAB "target path  : " << header.GetTargetPath() << std::endl
39           << TAB "overlay path : " << header.GetOverlayPath() << std::endl;
40 
41   if (!header.GetOverlayName().empty()) {
42     stream_ << "Overlay name: " << header.GetOverlayName() << std::endl;
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:" << std::endl;
50     while (std::getline(debug_stream, line)) {
51       stream_ << TAB << line << std::endl;
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:" << std::endl;
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             << std::endl;
94   }
95 
96   for (auto& target_entry : data.GetTargetInlineEntries()) {
97     stream_ << TAB << base::StringPrintf("0x%08x -> ", target_entry.target_id)
98             << utils::DataTypeToString(target_entry.value.data_type);
99 
100     if (target_entry.value.data_type == Res_value::TYPE_STRING) {
101       auto str = string_pool.stringAt(target_entry.value.data_value - string_pool_offset);
102       stream_ << " \"" << str.value_or(StringPiece16(u"")) << "\"";
103     } else {
104       stream_ << " " << base::StringPrintf("0x%08x", target_entry.value.data_value);
105     }
106 
107     std::string target_name = kUnknownResourceName;
108     if (target_ != nullptr) {
109       if (auto name = target_->GetResourceName(target_entry.target_id)) {
110         target_name = *name;
111       }
112     }
113 
114     stream_ << " (" << target_name << ")" << std::endl;
115   }
116 }
117 
118 }  // namespace android::idmap2
119