blob: 1bbfb28a78704bc318792f39c4f30ecbb48f23ce [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 Lesinskid0f492d2017-04-03 18:12:45 -070034void DumpCompiledFile(const pb::CompiledFile& pb_file, const void* data, size_t len,
35 const Source& source, IAaptContext* context) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 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 Lesinskid0f492d2017-04-03 18:12:45 -070039 context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return;
41 }
Adam Lesinski59e04c62016-02-04 15:59:23 -080042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 std::cout << "Resource: " << file->name << "\n"
44 << "Config: " << file->config << "\n"
45 << "Source: " << file->source << "\n";
Adam Lesinski59e04c62016-02-04 15:59:23 -080046}
47
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048void TryDumpFile(IAaptContext* context, const std::string& file_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 std::unique_ptr<ResourceTable> table;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070050
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 Lesinskice5e56e2016-10-21 17:56:45 -070054 io::IFile* file = zip->FindFile("resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 if (file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 if (!data) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070058 context->GetDiagnostics()->Error(DiagMessage(file_path)
59 << "failed to open resources.arsc.flat");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return;
61 }
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");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return;
67 }
Adam Lesinski64587af2016-02-18 18:33:06 -080068
Adam Lesinskid0f492d2017-04-03 18:12:45 -070069 table = DeserializeTableFromPb(pb_table, Source(file_path), context->GetDiagnostics());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 if (!table) {
71 return;
72 }
Adam Lesinski64587af2016-02-18 18:33:06 -080073 }
74
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070075 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 file = zip->FindFile("resources.arsc");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 if (file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::unique_ptr<io::IData> data = file->OpenAsData();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 if (!data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 context->GetDiagnostics()->Error(DiagMessage(file_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 << "failed to open resources.arsc");
82 return;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070083 }
84
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 table = util::make_unique<ResourceTable>();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070086 BinaryResourceParser parser(context, table.get(), Source(file_path), data->data(),
87 data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 if (!parser.Parse()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 return;
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070090 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 }
92 }
93 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 if (!table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 if (!file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 return;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800100 }
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 android::FileMap* file_map = &file.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103
104 // Try as a compiled table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 pb::ResourceTable pb_table;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700106 if (pb_table.ParseFromArray(file_map->getDataPtr(), file_map->getDataLength())) {
107 table = DeserializeTableFromPb(pb_table, Source(file_path), context->GetDiagnostics());
Adam Lesinski59e04c62016-02-04 15:59:23 -0800108 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109
110 if (!table) {
111 // Try as a compiled file.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700112 CompiledFileInputStream input(file_map->getDataPtr(), file_map->getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 uint32_t num_files = 0;
115 if (!input.ReadLittleEndian32(&num_files)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 return;
117 }
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 for (uint32_t i = 0; i < num_files; i++) {
120 pb::CompiledFile compiled_file;
121 if (!input.ReadCompiledFile(&compiled_file)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700122 context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return;
124 }
125
126 uint64_t offset, len;
127 if (!input.ReadDataMetaData(&offset, &len)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700128 context->GetDiagnostics()->Warn(DiagMessage() << "failed to read meta data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return;
130 }
131
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700132 const void* data = static_cast<const uint8_t*>(file_map->getDataPtr()) + offset;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 DumpCompiledFile(compiled_file, data, len, Source(file_path), context);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
135 }
136 }
137
138 if (table) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 DebugPrintTableOptions options;
140 options.show_sources = true;
141 Debug::PrintTable(table.get(), options);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800143}
144
145class DumpContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700147 IDiagnostics* GetDiagnostics() override {
148 return &diagnostics_;
149 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 abort();
153 return nullptr;
154 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 static std::string empty;
158 return empty;
159 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800160
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700161 uint8_t GetPackageId() override {
162 return 0;
163 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 abort();
167 return nullptr;
168 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800169
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700170 bool IsVerbose() override {
171 return verbose_;
172 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800173
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700174 void SetVerbose(bool val) {
175 verbose_ = val;
176 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800177
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700178 int GetMinSdkVersion() override {
179 return 0;
180 }
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;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700192 Flags flags = Flags().OptionalSwitch("-v", "increase verbosity of output", &verbose);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 if (!flags.Parse("aapt2 dump", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 return 1;
195 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800196
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 DumpContext context;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 context.SetVerbose(verbose);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 for (const std::string& arg : flags.GetArgs()) {
201 TryDumpFile(&context, arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 }
203 return 0;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800204}
205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206} // namespace aapt