blob: bcede72037b2b6805b88a8438b77ffc2dd668152 [file] [log] [blame]
Adam Lesinski59e04c62016-02-04 15:59:23 -08001/*
2 * Copyright (C) 2016 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <vector>
18
Adam Lesinskid5083f62017-01-16 15:07:21 -080019#include "androidfw/StringPiece.h"
20
Adam Lesinski59e04c62016-02-04 15:59:23 -080021#include "Debug.h"
22#include "Diagnostics.h"
23#include "Flags.h"
Adam Lesinski64587af2016-02-18 18:33:06 -080024#include "io/ZipArchive.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080025#include "process/IResourceTableConsumer.h"
26#include "proto/ProtoSerialize.h"
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070027#include "unflatten/BinaryResourceParser.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080028#include "util/Files.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029
30using android::StringPiece;
Adam Lesinski59e04c62016-02-04 15:59:23 -080031
Adam Lesinski59e04c62016-02-04 15:59:23 -080032namespace aapt {
33
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034void DumpCompiledFile(const pb::CompiledFile& pb_file, const void* data,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 size_t len, const Source& source, IAaptContext* context) {
36 std::unique_ptr<ResourceFile> file =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 DeserializeCompiledFileFromPb(pb_file, source, context->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 context->GetDiagnostics()->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 << "failed to read compiled file");
41 return;
42 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080043
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 std::cout << "Resource: " << file->name << "\n"
45 << "Config: " << file->config << "\n"
46 << "Source: " << file->source << "\n";
Adam Lesinski59e04c62016-02-04 15:59:23 -080047}
48
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049void TryDumpFile(IAaptContext* context, const std::string& file_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 std::unique_ptr<ResourceTable> table;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 std::string err;
53 std::unique_ptr<io::ZipFileCollection> zip =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 io::ZipFileCollection::Create(file_path, &err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 if (zip) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 io::IFile* file = zip->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 if (file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 context->GetDiagnostics()->Error(
61 DiagMessage(file_path) << "failed to open resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return;
63 }
Adam Lesinski64587af2016-02-18 18:33:06 -080064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 pb::ResourceTable pb_table;
66 if (!pb_table.ParseFromArray(data->data(), data->size())) {
67 context->GetDiagnostics()->Error(DiagMessage(file_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 << "invalid resources.arsc.flat");
69 return;
70 }
Adam Lesinski64587af2016-02-18 18:33:06 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 table = DeserializeTableFromPb(pb_table, Source(file_path),
73 context->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 if (!table) {
75 return;
76 }
Adam Lesinski64587af2016-02-18 18:33:06 -080077 }
78
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070079 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 file = zip->FindFile("resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 if (file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 context->GetDiagnostics()->Error(DiagMessage(file_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 << "failed to open resources.arsc");
86 return;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070087 }
88
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 table = util::make_unique<ResourceTable>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 BinaryResourceParser parser(context, table.get(), Source(file_path),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 data->data(), data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 if (!parser.Parse()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 return;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070094 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
96 }
97 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 return;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800104 }
105
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 android::FileMap* file_map = &file.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107
108 // Try as a compiled table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 pb::ResourceTable pb_table;
110 if (pb_table.ParseFromArray(file_map->getDataPtr(),
111 file_map->getDataLength())) {
112 table = DeserializeTableFromPb(pb_table, Source(file_path),
113 context->GetDiagnostics());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800114 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115
116 if (!table) {
117 // Try as a compiled file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 CompiledFileInputStream input(file_map->getDataPtr(),
119 file_map->getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 uint32_t num_files = 0;
122 if (!input.ReadLittleEndian32(&num_files)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return;
124 }
125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 for (uint32_t i = 0; i < num_files; i++) {
127 pb::CompiledFile compiled_file;
128 if (!input.ReadCompiledFile(&compiled_file)) {
129 context->GetDiagnostics()->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 << "failed to read compiled file");
131 return;
132 }
133
134 uint64_t offset, len;
135 if (!input.ReadDataMetaData(&offset, &len)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 context->GetDiagnostics()->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 << "failed to read meta data");
138 return;
139 }
140
141 const void* data =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 static_cast<const uint8_t*>(file_map->getDataPtr()) + offset;
143 DumpCompiledFile(compiled_file, data, len, Source(file_path), context);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 }
145 }
146 }
147
148 if (table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 DebugPrintTableOptions options;
150 options.show_sources = true;
151 Debug::PrintTable(table.get(), options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800153}
154
155class DumpContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 abort();
161 return nullptr;
162 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 static std::string empty;
166 return empty;
167 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 uint8_t GetPackageId() override { return 0; }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 abort();
173 return nullptr;
174 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 bool IsVerbose() override { return verbose_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 int GetMinSdkVersion() override { return 0; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700181
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 StdErrDiagnostics diagnostics_;
184 bool verbose_ = false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800185};
186
187/**
188 * Entry point for dump command.
189 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190int Dump(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 bool verbose = false;
192 Flags flags =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 Flags().OptionalSwitch("-v", "increase verbosity of output", &verbose);
194 if (!flags.Parse("aapt2 dump", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 return 1;
196 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 DumpContext context;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 context.SetVerbose(verbose);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 for (const std::string& arg : flags.GetArgs()) {
202 TryDumpFile(&context, arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204 return 0;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800205}
206
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207} // namespace aapt