blob: 0dedc91d2d2e06bb874c7056057c27f847282b2e [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"
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070026#include "proto/ProtoDeserialize.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
Adam Lesinski4ffea042017-08-10 15:37:28 -070030using ::android::StringPiece;
Adam Lesinski59e04c62016-02-04 15:59:23 -080031
Adam Lesinski59e04c62016-02-04 15:59:23 -080032namespace aapt {
33
Adam Lesinski4ffea042017-08-10 15:37:28 -070034bool DumpCompiledFile(const pb::internal::CompiledFile& pb_file, const void* data, size_t len,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070035 const Source& source, IAaptContext* context) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070036 ResourceFile file;
37 std::string error;
38 if (!DeserializeCompiledFileFromPb(pb_file, &file, &error)) {
39 context->GetDiagnostics()->Warn(DiagMessage(source)
40 << "failed to read compiled file: " << error);
Pierre Lecesneaadf27e2017-05-05 14:58:21 +010041 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080043
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070044 std::cout << "Resource: " << file.name << "\n"
45 << "Config: " << file.config << "\n"
46 << "Source: " << file.source << "\n";
Pierre Lecesneaadf27e2017-05-05 14:58:21 +010047 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -080048}
49
Pierre Lecesneaadf27e2017-05-05 14:58:21 +010050bool TryDumpFile(IAaptContext* context, const std::string& file_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 std::string err;
Adam Lesinskid0f492d2017-04-03 18:12:45 -070052 std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(file_path, &err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 if (zip) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070054 ResourceTable table;
55 if (io::IFile* file = zip->FindFile("resources.arsc.flat")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070057 if (data == nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070058 context->GetDiagnostics()->Error(DiagMessage(file_path)
59 << "failed to open resources.arsc.flat");
Pierre Lecesneaadf27e2017-05-05 14:58:21 +010060 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
Adam Lesinski64587af2016-02-18 18:33:06 -080062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 pb::ResourceTable pb_table;
64 if (!pb_table.ParseFromArray(data->data(), data->size())) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070065 context->GetDiagnostics()->Error(DiagMessage(file_path) << "invalid resources.arsc.flat");
Pierre Lecesneaadf27e2017-05-05 14:58:21 +010066 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
Adam Lesinski64587af2016-02-18 18:33:06 -080068
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070069 ResourceTable table;
70 if (!DeserializeTableFromPb(pb_table, &table, &err)) {
71 context->GetDiagnostics()->Error(DiagMessage(file_path)
72 << "failed to parse table: " << err);
73 return false;
74 }
75 } else if (io::IFile* file = zip->FindFile("resources.arsc")) {
76 std::unique_ptr<io::IData> data = file->OpenAsData();
77 if (!data) {
78 context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.arsc");
79 return false;
80 }
81
82 BinaryResourceParser parser(context, &table, Source(file_path), data->data(), data->size());
83 if (!parser.Parse()) {
Pierre Lecesneaadf27e2017-05-05 14:58:21 +010084 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 }
Adam Lesinski64587af2016-02-18 18:33:06 -080086 }
87
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070088 DebugPrintTableOptions options;
89 options.show_sources = true;
90 Debug::PrintTable(&table, options);
91 return true;
92 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070093
Adam Lesinski8cdca1b2017-09-28 15:50:03 -070094 err.clear();
95
96 Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
97 if (!file) {
98 context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
99 return false;
100 }
101
102 android::FileMap* file_map = &file.value();
103
104 // Check to see if this is a loose ResourceTable.
105 pb::ResourceTable pb_table;
106 if (pb_table.ParseFromArray(file_map->getDataPtr(), file_map->getDataLength())) {
107 ResourceTable table;
108 if (DeserializeTableFromPb(pb_table, &table, &err)) {
109 DebugPrintTableOptions options;
110 options.show_sources = true;
111 Debug::PrintTable(&table, options);
112 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 }
114 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700115
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700116 // Try as a compiled file.
117 CompiledFileInputStream input(file_map->getDataPtr(), file_map->getDataLength());
118 uint32_t num_files = 0;
119 if (!input.ReadLittleEndian32(&num_files)) {
120 return false;
121 }
122
123 for (uint32_t i = 0; i < num_files; i++) {
124 pb::internal::CompiledFile compiled_file;
125 if (!input.ReadCompiledFile(&compiled_file)) {
126 context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
Pierre Lecesneaadf27e2017-05-05 14:58:21 +0100127 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800128 }
129
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700130 uint64_t offset, len;
131 if (!input.ReadDataMetaData(&offset, &len)) {
132 context->GetDiagnostics()->Warn(DiagMessage() << "failed to read meta data");
133 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800134 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700136 const void* data = static_cast<const uint8_t*>(file_map->getDataPtr()) + offset;
137 if (!DumpCompiledFile(compiled_file, data, len, Source(file_path), context)) {
138 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
140 }
Pierre Lecesneaadf27e2017-05-05 14:58:21 +0100141 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800142}
143
144class DumpContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 public:
Adam Lesinskib522f042017-04-21 16:57:59 -0700146 PackageType GetPackageType() override {
147 // Doesn't matter.
148 return PackageType::kApp;
149 }
150
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700151 IDiagnostics* GetDiagnostics() override {
152 return &diagnostics_;
153 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 abort();
157 return nullptr;
158 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 static std::string empty;
162 return empty;
163 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800164
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700165 uint8_t GetPackageId() override {
166 return 0;
167 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 abort();
171 return nullptr;
172 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800173
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700174 bool IsVerbose() override {
175 return verbose_;
176 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800177
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700178 void SetVerbose(bool val) {
179 verbose_ = val;
180 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800181
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700182 int GetMinSdkVersion() override {
183 return 0;
184 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 StdErrDiagnostics diagnostics_;
188 bool verbose_ = false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800189};
190
191/**
192 * Entry point for dump command.
193 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194int Dump(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 bool verbose = false;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700196 Flags flags = Flags().OptionalSwitch("-v", "increase verbosity of output", &verbose);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 if (!flags.Parse("aapt2 dump", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 return 1;
199 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800200
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 DumpContext context;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 context.SetVerbose(verbose);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 for (const std::string& arg : flags.GetArgs()) {
Pierre Lecesneaadf27e2017-05-05 14:58:21 +0100205 if (!TryDumpFile(&context, arg)) {
206 return 1;
207 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 }
Pierre Lecesneaadf27e2017-05-05 14:58:21 +0100209
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 return 0;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800211}
212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213} // namespace aapt