blob: 2920c2abb57ab58d90a38aae64a8e81b851aa279 [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 Lesinski59e04c62016-02-04 15:59:23 -080019#include "Debug.h"
20#include "Diagnostics.h"
21#include "Flags.h"
Adam Lesinski64587af2016-02-18 18:33:06 -080022#include "io/ZipArchive.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080023#include "process/IResourceTableConsumer.h"
24#include "proto/ProtoSerialize.h"
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070025#include "unflatten/BinaryResourceParser.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080026#include "util/Files.h"
27#include "util/StringPiece.h"
28
Adam Lesinski59e04c62016-02-04 15:59:23 -080029namespace aapt {
30
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031void DumpCompiledFile(const pb::CompiledFile& pb_file, const void* data,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 size_t len, const Source& source, IAaptContext* context) {
33 std::unique_ptr<ResourceFile> file =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 DeserializeCompiledFileFromPb(pb_file, source, context->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 context->GetDiagnostics()->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 << "failed to read compiled file");
38 return;
39 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 std::cout << "Resource: " << file->name << "\n"
42 << "Config: " << file->config << "\n"
43 << "Source: " << file->source << "\n";
Adam Lesinski59e04c62016-02-04 15:59:23 -080044}
45
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046void TryDumpFile(IAaptContext* context, const std::string& file_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 std::unique_ptr<ResourceTable> table;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 std::string err;
50 std::unique_ptr<io::ZipFileCollection> zip =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 io::ZipFileCollection::Create(file_path, &err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 if (zip) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 io::IFile* file = zip->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 if (file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 context->GetDiagnostics()->Error(
58 DiagMessage(file_path) << "failed to open resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return;
60 }
Adam Lesinski64587af2016-02-18 18:33:06 -080061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 pb::ResourceTable pb_table;
63 if (!pb_table.ParseFromArray(data->data(), data->size())) {
64 context->GetDiagnostics()->Error(DiagMessage(file_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 << "invalid resources.arsc.flat");
66 return;
67 }
Adam Lesinski64587af2016-02-18 18:33:06 -080068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 table = DeserializeTableFromPb(pb_table, Source(file_path),
70 context->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 if (!table) {
72 return;
73 }
Adam Lesinski64587af2016-02-18 18:33:06 -080074 }
75
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070076 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 file = zip->FindFile("resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 if (file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 context->GetDiagnostics()->Error(DiagMessage(file_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 << "failed to open resources.arsc");
83 return;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070084 }
85
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 table = util::make_unique<ResourceTable>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 BinaryResourceParser parser(context, table.get(), Source(file_path),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 data->data(), data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 if (!parser.Parse()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070091 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 }
93 }
94 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 return;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800101 }
102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 android::FileMap* file_map = &file.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104
105 // Try as a compiled table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 pb::ResourceTable pb_table;
107 if (pb_table.ParseFromArray(file_map->getDataPtr(),
108 file_map->getDataLength())) {
109 table = DeserializeTableFromPb(pb_table, Source(file_path),
110 context->GetDiagnostics());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800111 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112
113 if (!table) {
114 // Try as a compiled file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 CompiledFileInputStream input(file_map->getDataPtr(),
116 file_map->getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 uint32_t num_files = 0;
119 if (!input.ReadLittleEndian32(&num_files)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 return;
121 }
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 for (uint32_t i = 0; i < num_files; i++) {
124 pb::CompiledFile compiled_file;
125 if (!input.ReadCompiledFile(&compiled_file)) {
126 context->GetDiagnostics()->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 << "failed to read compiled file");
128 return;
129 }
130
131 uint64_t offset, len;
132 if (!input.ReadDataMetaData(&offset, &len)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 context->GetDiagnostics()->Warn(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 << "failed to read meta data");
135 return;
136 }
137
138 const void* data =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 static_cast<const uint8_t*>(file_map->getDataPtr()) + offset;
140 DumpCompiledFile(compiled_file, data, len, Source(file_path), context);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
142 }
143 }
144
145 if (table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 DebugPrintTableOptions options;
147 options.show_sources = true;
148 Debug::PrintTable(table.get(), options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800150}
151
152class DumpContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 abort();
158 return nullptr;
159 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 static std::string empty;
163 return empty;
164 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 uint8_t GetPackageId() override { return 0; }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 abort();
170 return nullptr;
171 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 bool IsVerbose() override { return verbose_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 int GetMinSdkVersion() override { return 0; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 StdErrDiagnostics diagnostics_;
181 bool verbose_ = false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800182};
183
184/**
185 * Entry point for dump command.
186 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187int Dump(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 bool verbose = false;
189 Flags flags =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 Flags().OptionalSwitch("-v", "increase verbosity of output", &verbose);
191 if (!flags.Parse("aapt2 dump", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 return 1;
193 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 DumpContext context;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 context.SetVerbose(verbose);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 for (const std::string& arg : flags.GetArgs()) {
199 TryDumpFile(&context, arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 }
201 return 0;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800202}
203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204} // namespace aapt