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