blob: b78e942d4049df301a8d9de6ca15e21bb3c7185c [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 <cstdarg>
18#include <string>
Mårten Kongstad02751232018-04-27 13:16:32 +020019
20#include "android-base/macros.h"
21#include "android-base/stringprintf.h"
22#include "androidfw/ApkAssets.h"
23
24#include "idmap2/RawPrintVisitor.h"
25#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010026#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027
28using android::ApkAssets;
29
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
32// verbatim copy fomr PrettyPrintVisitor.cpp, move to common utils
33#define RESID(pkg, type, entry) (((pkg) << 24) | ((type) << 16) | (entry))
34
35void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
36}
37
38void 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
52void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
53}
54
55void 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
Mårten Kongstadb8779022018-11-29 09:53:17 +010061void RawPrintVisitor::visit(const IdmapData::TypeEntry& type_entry) {
Mårten Kongstad02751232018-04-27 13:16:32 +020062 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
63
Mårten Kongstadb8779022018-11-29 09:53:17 +010064 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");
Mårten Kongstad02751232018-04-27 13:16:32 +020068
Mårten Kongstadb8779022018-11-29 09:53:17 +010069 for (uint16_t i = 0; i < type_entry.GetEntryCount(); i++) {
70 const EntryId entry = type_entry.GetEntry(i);
Mårten Kongstad02751232018-04-27 13:16:32 +020071 if (entry == kNoEntry) {
72 print(kPadding, "no entry");
73 } else {
Mårten Kongstadb8779022018-11-29 09:53:17 +010074 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);
Mårten Kongstad0f763112018-11-19 14:14:37 +010078 Result<std::string> name;
Mårten Kongstad02751232018-04-27 13:16:32 +020079 if (target_package_loaded) {
Mårten Kongstad0f763112018-11-19 14:14:37 +010080 name = utils::ResToTypeEntryName(target_am_, target_resid);
Mårten Kongstad02751232018-04-27 13:16:32 +020081 }
Mårten Kongstad0f763112018-11-19 14:14:37 +010082 if (name) {
Mårten Kongstad02751232018-04-27 13:16:32 +020083 print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x %s", target_resid, overlay_resid,
Mårten Kongstad0f763112018-11-19 14:14:37 +010084 name->c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +020085 } else {
86 print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x", target_resid, overlay_resid);
87 }
88 }
89 }
90}
91
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -080092// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +020093void 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
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800105// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200106void 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
Chih-Hung Hsieh55773ba2019-01-14 11:09:03 -0800118// NOLINTNEXTLINE(cert-dcl50-cpp)
Mårten Kongstad02751232018-04-27 13:16:32 +0200119void 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
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100132} // namespace android::idmap2