blob: a6bf5fb6e6879ea3f2465d787abd4eeb97132573 [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
30namespace android {
31namespace idmap2 {
32
33// verbatim copy fomr PrettyPrintVisitor.cpp, move to common utils
34#define RESID(pkg, type, entry) (((pkg) << 24) | ((type) << 16) | (entry))
35
36void RawPrintVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
37}
38
39void RawPrintVisitor::visit(const IdmapHeader& header) {
40 print(header.GetMagic(), "magic");
41 print(header.GetVersion(), "version");
42 print(header.GetTargetCrc(), "target crc");
43 print(header.GetOverlayCrc(), "overlay crc");
44 print(header.GetTargetPath().to_string(), "target path");
45 print(header.GetOverlayPath().to_string(), "overlay path");
46
47 target_apk_ = ApkAssets::Load(header.GetTargetPath().to_string());
48 if (target_apk_) {
49 target_am_.SetApkAssets({target_apk_.get()});
50 }
51}
52
53void RawPrintVisitor::visit(const IdmapData& data ATTRIBUTE_UNUSED) {
54}
55
56void RawPrintVisitor::visit(const IdmapData::Header& header) {
57 print(static_cast<uint16_t>(header.GetTargetPackageId()), "target package id");
58 print(header.GetTypeCount(), "type count");
59 last_seen_package_id_ = header.GetTargetPackageId();
60}
61
Mårten Kongstadb8779022018-11-29 09:53:17 +010062void RawPrintVisitor::visit(const IdmapData::TypeEntry& type_entry) {
Mårten Kongstad02751232018-04-27 13:16:32 +020063 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
64
Mårten Kongstadb8779022018-11-29 09:53:17 +010065 print(static_cast<uint16_t>(type_entry.GetTargetTypeId()), "target type");
66 print(static_cast<uint16_t>(type_entry.GetOverlayTypeId()), "overlay type");
67 print(static_cast<uint16_t>(type_entry.GetEntryCount()), "entry count");
68 print(static_cast<uint16_t>(type_entry.GetEntryOffset()), "entry offset");
Mårten Kongstad02751232018-04-27 13:16:32 +020069
Mårten Kongstadb8779022018-11-29 09:53:17 +010070 for (uint16_t i = 0; i < type_entry.GetEntryCount(); i++) {
71 const EntryId entry = type_entry.GetEntry(i);
Mårten Kongstad02751232018-04-27 13:16:32 +020072 if (entry == kNoEntry) {
73 print(kPadding, "no entry");
74 } else {
Mårten Kongstadb8779022018-11-29 09:53:17 +010075 const ResourceId target_resid = RESID(last_seen_package_id_, type_entry.GetTargetTypeId(),
76 type_entry.GetEntryOffset() + i);
77 const ResourceId overlay_resid =
78 RESID(last_seen_package_id_, type_entry.GetOverlayTypeId(), entry);
Mårten Kongstad0f763112018-11-19 14:14:37 +010079 Result<std::string> name;
Mårten Kongstad02751232018-04-27 13:16:32 +020080 if (target_package_loaded) {
Mårten Kongstad0f763112018-11-19 14:14:37 +010081 name = utils::ResToTypeEntryName(target_am_, target_resid);
Mårten Kongstad02751232018-04-27 13:16:32 +020082 }
Mårten Kongstad0f763112018-11-19 14:14:37 +010083 if (name) {
Mårten Kongstad02751232018-04-27 13:16:32 +020084 print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x %s", target_resid, overlay_resid,
Mårten Kongstad0f763112018-11-19 14:14:37 +010085 name->c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +020086 } else {
87 print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x", target_resid, overlay_resid);
88 }
89 }
90 }
91}
92
93void 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
105void RawPrintVisitor::print(uint32_t value, const char* fmt, ...) {
106 va_list ap;
107 va_start(ap, fmt);
108 std::string comment;
109 base::StringAppendV(&comment, fmt, ap);
110 va_end(ap);
111
112 stream_ << base::StringPrintf("%08zx: %08x", offset_, value) << " " << comment << std::endl;
113
114 offset_ += sizeof(uint32_t);
115}
116
117void RawPrintVisitor::print(const std::string& value, const char* fmt, ...) {
118 va_list ap;
119 va_start(ap, fmt);
120 std::string comment;
121 base::StringAppendV(&comment, fmt, ap);
122 va_end(ap);
123
124 stream_ << base::StringPrintf("%08zx: ", offset_) << "........ " << comment << ": " << value
125 << std::endl;
126
127 offset_ += kIdmapStringLength;
128}
129
130} // namespace idmap2
131} // namespace android