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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 19 | #include "androidfw/StringPiece.h" |
| 20 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 21 | #include "Debug.h" |
| 22 | #include "Diagnostics.h" |
| 23 | #include "Flags.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 24 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 25 | #include "format/binary/BinaryResourceParser.h" |
| 26 | #include "format/proto/ProtoDeserialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 27 | #include "io/FileStream.h" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 28 | #include "io/ZipArchive.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 29 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 30 | #include "util/Files.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 32 | using ::android::StringPiece; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 34 | namespace aapt { |
| 35 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 36 | static const char* ResourceFileTypeToString(const ResourceFile::Type& type) { |
| 37 | switch (type) { |
| 38 | case ResourceFile::Type::kPng: |
| 39 | return "PNG"; |
| 40 | case ResourceFile::Type::kBinaryXml: |
| 41 | return "BINARY_XML"; |
| 42 | case ResourceFile::Type::kProtoXml: |
| 43 | return "PROTO_XML"; |
| 44 | default: |
| 45 | break; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | } |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 47 | return "UNKNOWN"; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 50 | static void DumpCompiledFile(const ResourceFile& file, const Source& source, off64_t offset, |
| 51 | size_t len) { |
| 52 | std::cout << "Resource: " << file.name << "\n" |
| 53 | << "Config: " << file.config << "\n" |
| 54 | << "Source: " << file.source << "\n" |
| 55 | << "Type: " << ResourceFileTypeToString(file.type) << "\n" |
| 56 | << "DataOff: " << offset << "\n" |
| 57 | << "DataLen: " << len << "\n"; |
| 58 | } |
| 59 | |
| 60 | static bool TryDumpFile(IAaptContext* context, const std::string& file_path) { |
| 61 | DebugPrintTableOptions print_options; |
| 62 | print_options.show_sources = true; |
| 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | std::string err; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 65 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(file_path, &err); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | if (zip) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 67 | ResourceTable table; |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame^] | 68 | if (io::IFile* file = zip->FindFile("resources.pb")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 70 | if (data == nullptr) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame^] | 71 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.pb"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 72 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | pb::ResourceTable pb_table; |
| 76 | if (!pb_table.ParseFromArray(data->data(), data->size())) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame^] | 77 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "invalid resources.pb"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 78 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 81 | if (!DeserializeTableFromPb(pb_table, &table, &err)) { |
| 82 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 83 | << "failed to parse table: " << err); |
| 84 | return false; |
| 85 | } |
| 86 | } else if (io::IFile* file = zip->FindFile("resources.arsc")) { |
| 87 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 88 | if (!data) { |
| 89 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.arsc"); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | BinaryResourceParser parser(context, &table, Source(file_path), data->data(), data->size()); |
| 94 | if (!parser.Parse()) { |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 95 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame^] | 99 | Debug::PrintTable(table, print_options); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 100 | return true; |
| 101 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 103 | err.clear(); |
| 104 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 105 | io::FileInputStream input(file_path); |
| 106 | if (input.HadError()) { |
| 107 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 108 | << "failed to open file: " << input.GetError()); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 112 | // Try as a compiled file. |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 113 | ContainerReader reader(&input); |
| 114 | if (reader.HadError()) { |
| 115 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 116 | << "failed to read container: " << reader.GetError()); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 120 | ContainerReaderEntry* entry; |
| 121 | while ((entry = reader.Next()) != nullptr) { |
| 122 | if (entry->Type() == ContainerEntryType::kResTable) { |
| 123 | pb::ResourceTable pb_table; |
| 124 | if (!entry->GetResTable(&pb_table)) { |
| 125 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 126 | << "failed to parse proto table: " << entry->GetError()); |
| 127 | continue; |
| 128 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 129 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 130 | ResourceTable table; |
| 131 | err.clear(); |
| 132 | if (!DeserializeTableFromPb(pb_table, &table, &err)) { |
| 133 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 134 | << "failed to parse table: " << err); |
| 135 | continue; |
| 136 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame^] | 138 | Debug::PrintTable(table, print_options); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 139 | } else if (entry->Type() == ContainerEntryType::kResFile) { |
| 140 | pb::internal::CompiledFile pb_compiled_file; |
| 141 | off64_t offset; |
| 142 | size_t length; |
| 143 | if (!entry->GetResFileOffsets(&pb_compiled_file, &offset, &length)) { |
| 144 | context->GetDiagnostics()->Error( |
| 145 | DiagMessage(file_path) << "failed to parse compiled proto file: " << entry->GetError()); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | ResourceFile file; |
| 150 | std::string error; |
| 151 | if (!DeserializeCompiledFileFromPb(pb_compiled_file, &file, &error)) { |
| 152 | context->GetDiagnostics()->Warn(DiagMessage(file_path) |
| 153 | << "failed to parse compiled file: " << error); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | DumpCompiledFile(file, Source(file_path), offset, length); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 160 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 163 | namespace { |
| 164 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 165 | class DumpContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 167 | PackageType GetPackageType() override { |
| 168 | // Doesn't matter. |
| 169 | return PackageType::kApp; |
| 170 | } |
| 171 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 172 | IDiagnostics* GetDiagnostics() override { |
| 173 | return &diagnostics_; |
| 174 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 175 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | NameMangler* GetNameMangler() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 177 | UNIMPLEMENTED(FATAL); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | return nullptr; |
| 179 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 180 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | static std::string empty; |
| 183 | return empty; |
| 184 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 185 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 186 | uint8_t GetPackageId() override { |
| 187 | return 0; |
| 188 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 189 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 191 | UNIMPLEMENTED(FATAL); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | return nullptr; |
| 193 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 194 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 195 | bool IsVerbose() override { |
| 196 | return verbose_; |
| 197 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 198 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 199 | void SetVerbose(bool val) { |
| 200 | verbose_ = val; |
| 201 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 203 | int GetMinSdkVersion() override { |
| 204 | return 0; |
| 205 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 | StdErrDiagnostics diagnostics_; |
| 209 | bool verbose_ = false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 210 | }; |
| 211 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 212 | } // namespace |
| 213 | |
| 214 | // Entry point for dump command. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | int Dump(const std::vector<StringPiece>& args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | bool verbose = false; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 217 | Flags flags = Flags().OptionalSwitch("-v", "increase verbosity of output", &verbose); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | if (!flags.Parse("aapt2 dump", args, &std::cerr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | return 1; |
| 220 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 221 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | DumpContext context; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | context.SetVerbose(verbose); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 224 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | for (const std::string& arg : flags.GetArgs()) { |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 226 | if (!TryDumpFile(&context, arg)) { |
| 227 | return 1; |
| 228 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | } |
| 230 | return 0; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | } // namespace aapt |