diff options
Diffstat (limited to 'tools/aapt2/dump/Dump.cpp')
-rw-r--r-- | tools/aapt2/dump/Dump.cpp | 110 |
1 files changed, 75 insertions, 35 deletions
diff --git a/tools/aapt2/dump/Dump.cpp b/tools/aapt2/dump/Dump.cpp index 56b9f9a3e081..f61ec945a1c1 100644 --- a/tools/aapt2/dump/Dump.cpp +++ b/tools/aapt2/dump/Dump.cpp @@ -20,6 +20,7 @@ #include "io/ZipArchive.h" #include "process/IResourceTableConsumer.h" #include "proto/ProtoSerialize.h" +#include "unflatten/BinaryResourceParser.h" #include "util/Files.h" #include "util/StringPiece.h" @@ -36,6 +37,7 @@ void dumpCompiledFile(const pb::CompiledFile& pbFile, const void* data, size_t l std::unique_ptr<ResourceFile> file = deserializeCompiledFileFromPb(pbFile, source, context->getDiagnostics()); if (!file) { + context->getDiagnostics()->warn(DiagMessage() << "failed to read compiled file"); return; } @@ -44,18 +46,9 @@ void dumpCompiledFile(const pb::CompiledFile& pbFile, const void* data, size_t l << "Source: " << file->source << "\n"; } -void dumpCompiledTable(const pb::ResourceTable& pbTable, const Source& source, - IAaptContext* context) { - std::unique_ptr<ResourceTable> table = deserializeTableFromPb(pbTable, source, - context->getDiagnostics()); - if (!table) { - return; - } - - Debug::printTable(table.get()); -} - void tryDumpFile(IAaptContext* context, const std::string& filePath) { + std::unique_ptr<ResourceTable> table; + std::string err; std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::create(filePath, &err); if (zip) { @@ -75,37 +68,80 @@ void tryDumpFile(IAaptContext* context, const std::string& filePath) { return; } - std::unique_ptr<ResourceTable> table = deserializeTableFromPb( + table = deserializeTableFromPb( pbTable, Source(filePath), context->getDiagnostics()); - if (table) { - DebugPrintTableOptions debugPrintTableOptions; - debugPrintTableOptions.showSources = true; - Debug::printTable(table.get(), debugPrintTableOptions); + if (!table) { + return; } } - return; - } - Maybe<android::FileMap> file = file::mmapPath(filePath, &err); - if (!file) { - context->getDiagnostics()->error(DiagMessage(filePath) << err); - return; + if (!table) { + file = zip->findFile("resources.arsc"); + if (file) { + std::unique_ptr<io::IData> data = file->openAsData(); + if (!data) { + context->getDiagnostics()->error(DiagMessage(filePath) + << "failed to open resources.arsc"); + return; + } + + table = util::make_unique<ResourceTable>(); + BinaryResourceParser parser(context, table.get(), Source(filePath), + data->data(), data->size()); + if (!parser.parse()) { + return; + } + } + } } - android::FileMap* fileMap = &file.value(); + if (!table) { + Maybe<android::FileMap> file = file::mmapPath(filePath, &err); + if (!file) { + context->getDiagnostics()->error(DiagMessage(filePath) << err); + return; + } - // Try as a compiled table. - pb::ResourceTable pbTable; - if (pbTable.ParseFromArray(fileMap->getDataPtr(), fileMap->getDataLength())) { - dumpCompiledTable(pbTable, Source(filePath), context); - return; + android::FileMap* fileMap = &file.value(); + + // Try as a compiled table. + pb::ResourceTable pbTable; + if (pbTable.ParseFromArray(fileMap->getDataPtr(), fileMap->getDataLength())) { + table = deserializeTableFromPb(pbTable, Source(filePath), context->getDiagnostics()); + } + + if (!table) { + // Try as a compiled file. + CompiledFileInputStream input(fileMap->getDataPtr(), fileMap->getDataLength()); + + uint32_t numFiles = 0; + if (!input.ReadLittleEndian32(&numFiles)) { + return; + } + + for (uint32_t i = 0; i < numFiles; i++) { + pb::CompiledFile compiledFile; + if (!input.ReadCompiledFile(&compiledFile)) { + context->getDiagnostics()->warn(DiagMessage() << "failed to read compiled file"); + return; + } + + uint64_t offset, len; + if (!input.ReadDataMetaData(&offset, &len)) { + context->getDiagnostics()->warn(DiagMessage() << "failed to read meta data"); + return; + } + + const void* data = static_cast<const uint8_t*>(fileMap->getDataPtr()) + offset; + dumpCompiledFile(compiledFile, data, len, Source(filePath), context); + } + } } - // Try as a compiled file. - CompiledFileInputStream input(fileMap->getDataPtr(), fileMap->getDataLength()); - if (const pb::CompiledFile* pbFile = input.CompiledFile()) { - dumpCompiledFile(*pbFile, input.data(), input.size(), Source(filePath), context); - return; + if (table) { + DebugPrintTableOptions debugPrintTableOptions; + debugPrintTableOptions.showSources = true; + Debug::printTable(table.get(), debugPrintTableOptions); } } @@ -120,8 +156,8 @@ public: return nullptr; } - const std::u16string& getCompilationPackage() override { - static std::u16string empty; + const std::string& getCompilationPackage() override { + static std::string empty; return empty; } @@ -142,6 +178,10 @@ public: mVerbose = val; } + int getMinSdkVersion() override { + return 0; + } + private: StdErrDiagnostics mDiagnostics; bool mVerbose = false; |