Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 20 | #include "io/ZipArchive.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 21 | #include "process/IResourceTableConsumer.h" |
| 22 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 23 | #include "unflatten/BinaryResourceParser.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 24 | #include "util/Files.h" |
| 25 | #include "util/StringPiece.h" |
| 26 | |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 31 | // struct DumpOptions { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 32 | // |
| 33 | //}; |
| 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 35 | void dumpCompiledFile(const pb::CompiledFile& pbFile, const void* data, |
| 36 | size_t len, const Source& source, IAaptContext* context) { |
| 37 | std::unique_ptr<ResourceFile> file = |
| 38 | deserializeCompiledFileFromPb(pbFile, source, context->getDiagnostics()); |
| 39 | if (!file) { |
| 40 | context->getDiagnostics()->warn(DiagMessage() |
| 41 | << "failed to read compiled file"); |
| 42 | return; |
| 43 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 45 | std::cout << "Resource: " << file->name << "\n" |
| 46 | << "Config: " << file->config << "\n" |
| 47 | << "Source: " << file->source << "\n"; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 50 | void tryDumpFile(IAaptContext* context, const std::string& filePath) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 51 | std::unique_ptr<ResourceTable> table; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 53 | std::string err; |
| 54 | std::unique_ptr<io::ZipFileCollection> zip = |
| 55 | io::ZipFileCollection::create(filePath, &err); |
| 56 | if (zip) { |
| 57 | io::IFile* file = zip->findFile("resources.arsc.flat"); |
| 58 | if (file) { |
| 59 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 60 | if (!data) { |
| 61 | context->getDiagnostics()->error( |
| 62 | DiagMessage(filePath) << "failed to open resources.arsc.flat"); |
| 63 | return; |
| 64 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 66 | pb::ResourceTable pbTable; |
| 67 | if (!pbTable.ParseFromArray(data->data(), data->size())) { |
| 68 | context->getDiagnostics()->error(DiagMessage(filePath) |
| 69 | << "invalid resources.arsc.flat"); |
| 70 | return; |
| 71 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 73 | table = deserializeTableFromPb(pbTable, Source(filePath), |
| 74 | context->getDiagnostics()); |
| 75 | if (!table) { |
| 76 | return; |
| 77 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 80 | if (!table) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 81 | file = zip->findFile("resources.arsc"); |
| 82 | if (file) { |
| 83 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 84 | if (!data) { |
| 85 | context->getDiagnostics()->error(DiagMessage(filePath) |
| 86 | << "failed to open resources.arsc"); |
| 87 | return; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 90 | table = util::make_unique<ResourceTable>(); |
| 91 | BinaryResourceParser parser(context, table.get(), Source(filePath), |
| 92 | data->data(), data->size()); |
| 93 | if (!parser.parse()) { |
| 94 | return; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 95 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 96 | } |
| 97 | } |
| 98 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 100 | if (!table) { |
| 101 | Maybe<android::FileMap> file = file::mmapPath(filePath, &err); |
| 102 | if (!file) { |
| 103 | context->getDiagnostics()->error(DiagMessage(filePath) << err); |
| 104 | return; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 107 | android::FileMap* fileMap = &file.value(); |
| 108 | |
| 109 | // Try as a compiled table. |
| 110 | pb::ResourceTable pbTable; |
| 111 | if (pbTable.ParseFromArray(fileMap->getDataPtr(), |
| 112 | fileMap->getDataLength())) { |
| 113 | table = deserializeTableFromPb(pbTable, Source(filePath), |
| 114 | context->getDiagnostics()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 115 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 116 | |
| 117 | if (!table) { |
| 118 | // Try as a compiled file. |
| 119 | CompiledFileInputStream input(fileMap->getDataPtr(), |
| 120 | fileMap->getDataLength()); |
| 121 | |
| 122 | uint32_t numFiles = 0; |
| 123 | if (!input.ReadLittleEndian32(&numFiles)) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | for (uint32_t i = 0; i < numFiles; i++) { |
| 128 | pb::CompiledFile compiledFile; |
| 129 | if (!input.ReadCompiledFile(&compiledFile)) { |
| 130 | context->getDiagnostics()->warn(DiagMessage() |
| 131 | << "failed to read compiled file"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | uint64_t offset, len; |
| 136 | if (!input.ReadDataMetaData(&offset, &len)) { |
| 137 | context->getDiagnostics()->warn(DiagMessage() |
| 138 | << "failed to read meta data"); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | const void* data = |
| 143 | static_cast<const uint8_t*>(fileMap->getDataPtr()) + offset; |
| 144 | dumpCompiledFile(compiledFile, data, len, Source(filePath), context); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (table) { |
| 150 | DebugPrintTableOptions debugPrintTableOptions; |
| 151 | debugPrintTableOptions.showSources = true; |
| 152 | Debug::printTable(table.get(), debugPrintTableOptions); |
| 153 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | class DumpContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 157 | public: |
| 158 | IDiagnostics* getDiagnostics() override { return &mDiagnostics; } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 159 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 160 | NameMangler* getNameMangler() override { |
| 161 | abort(); |
| 162 | return nullptr; |
| 163 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 165 | const std::string& getCompilationPackage() override { |
| 166 | static std::string empty; |
| 167 | return empty; |
| 168 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 169 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 170 | uint8_t getPackageId() override { return 0; } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 171 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 172 | SymbolTable* getExternalSymbols() override { |
| 173 | abort(); |
| 174 | return nullptr; |
| 175 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 176 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 177 | bool verbose() override { return mVerbose; } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 178 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 179 | void setVerbose(bool val) { mVerbose = val; } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 180 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 181 | int getMinSdkVersion() override { return 0; } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 183 | private: |
| 184 | StdErrDiagnostics mDiagnostics; |
| 185 | bool mVerbose = false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /** |
| 189 | * Entry point for dump command. |
| 190 | */ |
| 191 | int dump(const std::vector<StringPiece>& args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 192 | bool verbose = false; |
| 193 | Flags flags = |
| 194 | Flags().optionalSwitch("-v", "increase verbosity of output", &verbose); |
| 195 | if (!flags.parse("aapt2 dump", args, &std::cerr)) { |
| 196 | return 1; |
| 197 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 198 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 199 | DumpContext context; |
| 200 | context.setVerbose(verbose); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 201 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 202 | for (const std::string& arg : flags.getArgs()) { |
| 203 | tryDumpFile(&context, arg); |
| 204 | } |
| 205 | return 0; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 208 | } // namespace aapt |