blob: 57cfc8ef85b49389e2d4211b5ec4ed736aa480e3 [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>
19#include <utility>
20
21#include "android-base/macros.h"
22#include "android-base/stringprintf.h"
23#include "androidfw/ApkAssets.h"
24
25#include "idmap2/RawPrintVisitor.h"
26#include "idmap2/ResourceUtils.h"
27
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
62void RawPrintVisitor::visit(const IdmapData::TypeEntry& te) {
63 const bool target_package_loaded = !target_am_.GetApkAssets().empty();
64
65 print(static_cast<uint16_t>(te.GetTargetTypeId()), "target type");
66 print(static_cast<uint16_t>(te.GetOverlayTypeId()), "overlay type");
67 print(static_cast<uint16_t>(te.GetEntryCount()), "entry count");
68 print(static_cast<uint16_t>(te.GetEntryOffset()), "entry offset");
69
70 for (uint16_t i = 0; i < te.GetEntryCount(); i++) {
71 const EntryId entry = te.GetEntry(i);
72 if (entry == kNoEntry) {
73 print(kPadding, "no entry");
74 } else {
75 const ResourceId target_resid =
76 RESID(last_seen_package_id_, te.GetTargetTypeId(), te.GetEntryOffset() + i);
77 const ResourceId overlay_resid = RESID(last_seen_package_id_, te.GetOverlayTypeId(), entry);
78 bool lookup_ok = false;
79 std::string name;
80 if (target_package_loaded) {
81 std::tie(lookup_ok, name) = utils::ResToTypeEntryName(target_am_, target_resid);
82 }
83 if (lookup_ok) {
84 print(static_cast<uint32_t>(entry), "0x%08x -> 0x%08x %s", target_resid, overlay_resid,
85 name.c_str());
86 } 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