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 <cstdarg>
20 #include <string>
21 
22 #include "android-base/macros.h"
23 #include "android-base/stringprintf.h"
24 #include "androidfw/ApkAssets.h"
25 #include "idmap2/ResourceUtils.h"
26 #include "idmap2/Result.h"
27 
28 using android::ApkAssets;
29 
30 namespace android::idmap2 {
31 
32 // verbatim copy fomr PrettyPrintVisitor.cpp, move to common utils
33 #define RESID(pkg, type, entry) (((pkg) << 24) | ((type) << 16) | (entry))
34 
visit(const Idmap & idmap ATTRIBUTE_UNUSED)35 void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
36 }
37 
visit(const IdmapHeader & header)38 void 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   }
50 }
51 
visit(const IdmapData & data ATTRIBUTE_UNUSED)52 void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
53 }
54 
visit(const IdmapData::Header & header)55 void RawPrintVisitor::visit(const IdmapData::Header& header) {
56   print(static_cast<uint16_t>(header.GetTargetPackageId()), "target package id");
57   print(header.GetTypeCount(), "type count");
58   last_seen_package_id_ = header.GetTargetPackageId();
59 }
60 
visit(const IdmapData::TypeEntry & type_entry)61 void RawPrintVisitor::visit(const IdmapData::TypeEntry& type_entry) {
62   const bool target_package_loaded = !target_am_.GetApkAssets().empty();
63 
64   print(static_cast<uint16_t>(type_entry.GetTargetTypeId()), "target type");
65   print(static_cast<uint16_t>(type_entry.GetOverlayTypeId()), "overlay type");
66   print(static_cast<uint16_t>(type_entry.GetEntryCount()), "entry count");
67   print(static_cast<uint16_t>(type_entry.GetEntryOffset()), "entry offset");
68 
69   for (uint16_t i = 0; i < type_entry.GetEntryCount(); i++) {
70     const EntryId entry = type_entry.GetEntry(i);
71     if (entry == kNoEntry) {
72       print(kPadding, "no entry");
73     } else {
74       const ResourceId target_resid = RESID(last_seen_package_id_, type_entry.GetTargetTypeId(),
75                                             type_entry.GetEntryOffset() + i);
76       const ResourceId overlay_resid =
77           RESID(last_seen_package_id_, type_entry.GetOverlayTypeId(), entry);
78       Result<std::string> name(Error(""));
79       if (target_package_loaded) {
80         name = utils::ResToTypeEntryName(target_am_, target_resid);
81       }
82       if (name) {
83         print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x %s", target_resid, overlay_resid,
84               name->c_str());
85       } else {
86         print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x", target_resid, overlay_resid);
87       }
88     }
89   }
90 }
91 
92 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(uint16_t value,const char * fmt,...)93 void RawPrintVisitor::print(uint16_t value, const char* fmt, ...) {
94   va_list ap;
95   va_start(ap, fmt);
96   std::string comment;
97   base::StringAppendV(&comment, fmt, ap);
98   va_end(ap);
99 
100   stream_ << base::StringPrintf("%08zx:     %04x", offset_, value) << "  " << comment << std::endl;
101 
102   offset_ += sizeof(uint16_t);
103 }
104 
105 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(uint32_t value,const char * fmt,...)106 void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) {
107   va_list ap;
108   va_start(ap, fmt);
109   std::string comment;
110   base::StringAppendV(&comment, fmt, ap);
111   va_end(ap);
112 
113   stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << "  " << comment << std::endl;
114 
115   offset_ += sizeof(uint32_t);
116 }
117 
118 // NOLINTNEXTLINE(cert-dcl50-cpp)
print(const std::string & value,const char * fmt,...)119 void RawPrintVisitor::print(const std::string& value, const char* fmt, ...) {
120   va_list ap;
121   va_start(ap, fmt);
122   std::string comment;
123   base::StringAppendV(&comment, fmt, ap);
124   va_end(ap);
125 
126   stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
127           << std::endl;
128 
129   offset_ += kIdmapStringLength;
130 }
131 
132 }  // namespace android::idmap2
133