blob: 8f0dd3af07882440ac7bd47dcdf9606831e221aa [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
17#include "Debug.h"
18#include "Diagnostics.h"
19#include "Flags.h"
Adam Lesinski64587af2016-02-18 18:33:06 -080020#include "io/ZipArchive.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080021#include "process/IResourceTableConsumer.h"
22#include "proto/ProtoSerialize.h"
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070023#include "unflatten/BinaryResourceParser.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080024#include "util/Files.h"
25#include "util/StringPiece.h"
26
27#include <vector>
28
29namespace aapt {
30
31//struct DumpOptions {
32//
33//};
34
35void dumpCompiledFile(const pb::CompiledFile& pbFile, const void* data, size_t len,
36 const Source& source, IAaptContext* context) {
37 std::unique_ptr<ResourceFile> file = deserializeCompiledFileFromPb(pbFile, source,
38 context->getDiagnostics());
39 if (!file) {
40 return;
41 }
42
43 std::cout << "Resource: " << file->name << "\n"
44 << "Config: " << file->config << "\n"
45 << "Source: " << file->source << "\n";
46}
47
Adam Lesinski59e04c62016-02-04 15:59:23 -080048void tryDumpFile(IAaptContext* context, const std::string& filePath) {
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070049 std::unique_ptr<ResourceTable> table;
50
Adam Lesinski59e04c62016-02-04 15:59:23 -080051 std::string err;
Adam Lesinski64587af2016-02-18 18:33:06 -080052 std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::create(filePath, &err);
53 if (zip) {
54 io::IFile* file = zip->findFile("resources.arsc.flat");
55 if (file) {
56 std::unique_ptr<io::IData> data = file->openAsData();
57 if (!data) {
58 context->getDiagnostics()->error(DiagMessage(filePath)
59 << "failed to open resources.arsc.flat");
60 return;
61 }
62
63 pb::ResourceTable pbTable;
64 if (!pbTable.ParseFromArray(data->data(), data->size())) {
65 context->getDiagnostics()->error(DiagMessage(filePath)
66 << "invalid resources.arsc.flat");
67 return;
68 }
69
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070070 table = deserializeTableFromPb(
Adam Lesinski64587af2016-02-18 18:33:06 -080071 pbTable, Source(filePath), context->getDiagnostics());
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070072 if (!table) {
73 return;
Adam Lesinski64587af2016-02-18 18:33:06 -080074 }
75 }
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070076
77 if (!table) {
78 file = zip->findFile("resources.arsc");
79 if (file) {
80 std::unique_ptr<io::IData> data = file->openAsData();
81 if (!data) {
82 context->getDiagnostics()->error(DiagMessage(filePath)
83 << "failed to open resources.arsc");
84 return;
85 }
86
87 table = util::make_unique<ResourceTable>();
88 BinaryResourceParser parser(context, table.get(), Source(filePath),
89 data->data(), data->size());
90 if (!parser.parse()) {
91 return;
92 }
93 }
94 }
Adam Lesinski64587af2016-02-18 18:33:06 -080095 }
96
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -070097 if (!table) {
98 Maybe<android::FileMap> file = file::mmapPath(filePath, &err);
99 if (!file) {
100 context->getDiagnostics()->error(DiagMessage(filePath) << err);
101 return;
102 }
103
104 android::FileMap* fileMap = &file.value();
105
106 // Try as a compiled table.
107 pb::ResourceTable pbTable;
108 if (pbTable.ParseFromArray(fileMap->getDataPtr(), fileMap->getDataLength())) {
109 table = deserializeTableFromPb(pbTable, Source(filePath), context->getDiagnostics());
110 }
111
112 if (!table) {
113 // Try as a compiled file.
114 CompiledFileInputStream input(fileMap->getDataPtr(), fileMap->getDataLength());
115 if (const pb::CompiledFile* pbFile = input.CompiledFile()) {
116 dumpCompiledFile(*pbFile, input.data(), input.size(), Source(filePath), context);
117 return;
118 }
119 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800120 }
121
Adam Lesinski5e8fa3a2016-06-27 16:21:42 -0700122 if (table) {
123 DebugPrintTableOptions debugPrintTableOptions;
124 debugPrintTableOptions.showSources = true;
125 Debug::printTable(table.get(), debugPrintTableOptions);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800126 }
127}
128
129class DumpContext : public IAaptContext {
130public:
131 IDiagnostics* getDiagnostics() override {
132 return &mDiagnostics;
133 }
134
135 NameMangler* getNameMangler() override {
136 abort();
137 return nullptr;
138 }
139
Adam Lesinski64587af2016-02-18 18:33:06 -0800140 const std::u16string& getCompilationPackage() override {
141 static std::u16string empty;
142 return empty;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800143 }
144
145 uint8_t getPackageId() override {
146 return 0;
147 }
148
Adam Lesinski64587af2016-02-18 18:33:06 -0800149 SymbolTable* getExternalSymbols() override {
Adam Lesinski59e04c62016-02-04 15:59:23 -0800150 abort();
151 return nullptr;
152 }
153
Adam Lesinski355f2852016-02-13 20:26:45 -0800154 bool verbose() override {
155 return mVerbose;
156 }
157
158 void setVerbose(bool val) {
159 mVerbose = val;
160 }
161
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700162 int getMinSdkVersion() override {
163 return 0;
164 }
165
Adam Lesinski59e04c62016-02-04 15:59:23 -0800166private:
167 StdErrDiagnostics mDiagnostics;
Adam Lesinski355f2852016-02-13 20:26:45 -0800168 bool mVerbose = false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800169};
170
171/**
172 * Entry point for dump command.
173 */
174int dump(const std::vector<StringPiece>& args) {
Adam Lesinski355f2852016-02-13 20:26:45 -0800175 bool verbose = false;
176 Flags flags = Flags()
177 .optionalSwitch("-v", "increase verbosity of output", &verbose);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800178 if (!flags.parse("aapt2 dump", args, &std::cerr)) {
179 return 1;
180 }
181
182 DumpContext context;
Adam Lesinski355f2852016-02-13 20:26:45 -0800183 context.setVerbose(verbose);
Adam Lesinski59e04c62016-02-04 15:59:23 -0800184
185 for (const std::string& arg : flags.getArgs()) {
186 tryDumpFile(&context, arg);
187 }
188 return 0;
189}
190
191} // namespace aapt