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