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 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 17 | #include "Dump.h" |
| 18 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 19 | #include <cinttypes> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 23 | #include "androidfw/StringPiece.h" |
| 24 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 25 | #include "Debug.h" |
| 26 | #include "Diagnostics.h" |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame^] | 27 | #include "LoadedApk.h" |
| 28 | #include "Util.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 29 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 30 | #include "format/binary/BinaryResourceParser.h" |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame^] | 31 | #include "format/binary/XmlFlattener.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 32 | #include "format/proto/ProtoDeserialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 33 | #include "io/FileStream.h" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 34 | #include "io/ZipArchive.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 35 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 36 | #include "text/Printer.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 37 | #include "util/Files.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 39 | using ::aapt::text::Printer; |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 40 | using ::android::StringPiece; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 41 | using ::android::base::StringPrintf; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 42 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 43 | namespace aapt { |
| 44 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 45 | static const char* ResourceFileTypeToString(const ResourceFile::Type& type) { |
| 46 | switch (type) { |
| 47 | case ResourceFile::Type::kPng: |
| 48 | return "PNG"; |
| 49 | case ResourceFile::Type::kBinaryXml: |
| 50 | return "BINARY_XML"; |
| 51 | case ResourceFile::Type::kProtoXml: |
| 52 | return "PROTO_XML"; |
| 53 | default: |
| 54 | break; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | } |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 56 | return "UNKNOWN"; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 59 | static void DumpCompiledFile(const ResourceFile& file, const Source& source, off64_t offset, |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 60 | size_t len, Printer* printer) { |
| 61 | printer->Print("Resource: "); |
| 62 | printer->Println(file.name.to_string()); |
| 63 | |
| 64 | printer->Print("Config: "); |
| 65 | printer->Println(file.config.to_string()); |
| 66 | |
| 67 | printer->Print("Source: "); |
| 68 | printer->Println(file.source.to_string()); |
| 69 | |
| 70 | printer->Print("Type: "); |
| 71 | printer->Println(ResourceFileTypeToString(file.type)); |
| 72 | |
| 73 | printer->Println(StringPrintf("Data: offset=%" PRIi64 " length=%zd", offset, len)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 76 | namespace { |
| 77 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 78 | class DumpContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 80 | PackageType GetPackageType() override { |
| 81 | // Doesn't matter. |
| 82 | return PackageType::kApp; |
| 83 | } |
| 84 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 85 | IDiagnostics* GetDiagnostics() override { |
| 86 | return &diagnostics_; |
| 87 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | NameMangler* GetNameMangler() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 90 | UNIMPLEMENTED(FATAL); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | return nullptr; |
| 92 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 93 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | static std::string empty; |
| 96 | return empty; |
| 97 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 98 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 99 | uint8_t GetPackageId() override { |
| 100 | return 0; |
| 101 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 104 | UNIMPLEMENTED(FATAL); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | return nullptr; |
| 106 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 107 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 108 | bool IsVerbose() override { |
| 109 | return verbose_; |
| 110 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 112 | void SetVerbose(bool val) { |
| 113 | verbose_ = val; |
| 114 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 115 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 116 | int GetMinSdkVersion() override { |
| 117 | return 0; |
| 118 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | StdErrDiagnostics diagnostics_; |
| 122 | bool verbose_ = false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 125 | } // namespace |
| 126 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame^] | 127 | // Use a smaller buffer so that there is less latency for dumping to stdout. |
| 128 | constexpr size_t kStdOutBufferSize = 1024u; |
| 129 | |
| 130 | int DumpAPCCommand::Action(const std::vector<std::string>& args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | DumpContext context; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame^] | 132 | DebugPrintTableOptions print_options; |
| 133 | print_options.show_sources = true; |
| 134 | print_options.show_values = !no_values_; |
| 135 | |
| 136 | if (args.size() < 1) { |
| 137 | diag_->Error(DiagMessage() << "No dump container specified."); |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 142 | Printer printer(&fout); |
| 143 | |
| 144 | for (auto container : args) { |
| 145 | io::FileInputStream input(container); |
| 146 | if (input.HadError()) { |
| 147 | context.GetDiagnostics()->Error(DiagMessage(container) |
| 148 | << "failed to open file: " << input.GetError()); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | // Try as a compiled file. |
| 153 | ContainerReader reader(&input); |
| 154 | if (reader.HadError()) { |
| 155 | context.GetDiagnostics()->Error(DiagMessage(container) |
| 156 | << "failed to read container: " << reader.GetError()); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | printer.Println("AAPT2 Container (APC)"); |
| 161 | ContainerReaderEntry* entry; |
| 162 | std::string error; |
| 163 | while ((entry = reader.Next()) != nullptr) { |
| 164 | if (entry->Type() == ContainerEntryType::kResTable) { |
| 165 | printer.Println("kResTable"); |
| 166 | |
| 167 | pb::ResourceTable pb_table; |
| 168 | if (!entry->GetResTable(&pb_table)) { |
| 169 | context.GetDiagnostics()->Error(DiagMessage(container) |
| 170 | << "failed to parse proto table: " |
| 171 | << entry->GetError()); |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | ResourceTable table; |
| 176 | error.clear(); |
| 177 | if (!DeserializeTableFromPb(pb_table, nullptr /*files*/, &table, &error)) { |
| 178 | context.GetDiagnostics()->Error(DiagMessage(container) |
| 179 | << "failed to parse table: " << error); |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | printer.Indent(); |
| 184 | Debug::PrintTable(table, print_options, &printer); |
| 185 | printer.Undent(); |
| 186 | } else if (entry->Type() == ContainerEntryType::kResFile) { |
| 187 | printer.Println("kResFile"); |
| 188 | pb::internal::CompiledFile pb_compiled_file; |
| 189 | off64_t offset; |
| 190 | size_t length; |
| 191 | if (!entry->GetResFileOffsets(&pb_compiled_file, &offset, &length)) { |
| 192 | context.GetDiagnostics()->Error( |
| 193 | DiagMessage(container) << "failed to parse compiled proto file: " |
| 194 | << entry->GetError()); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | ResourceFile file; |
| 199 | if (!DeserializeCompiledFileFromPb(pb_compiled_file, &file, &error)) { |
| 200 | context.GetDiagnostics()->Warn(DiagMessage(container) |
| 201 | << "failed to parse compiled file: " << error); |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | printer.Indent(); |
| 206 | DumpCompiledFile(file, Source(container), offset, length, &printer); |
| 207 | printer.Undent(); |
| 208 | } |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 209 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | } |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame^] | 211 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | return 0; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame^] | 215 | int DumpConfigsCommand::Action(const std::vector<std::string>& args) { |
| 216 | if (args.size() < 1) { |
| 217 | diag_->Error(DiagMessage() << "No dump apk specified."); |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); |
| 222 | if (!loaded_apk) { |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 227 | Printer printer(&fout); |
| 228 | |
| 229 | // Comparison function used to order configurations |
| 230 | auto compare = [](ConfigDescription c1, ConfigDescription c2) -> bool { |
| 231 | return c1.compare(c2) < 0; |
| 232 | }; |
| 233 | |
| 234 | // Insert the configurations into a set in order to keep every configuarion seen |
| 235 | std::set<ConfigDescription, decltype(compare)> configs(compare); |
| 236 | for (auto& package : loaded_apk->GetResourceTable()->packages) { |
| 237 | for (auto& type : package->types) { |
| 238 | for (auto& entry : type->entries) { |
| 239 | for (auto& value : entry->values) { |
| 240 | configs.insert(value->config); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // Print the configurations in order |
| 247 | for (auto& config : configs) { |
| 248 | printer.Print(StringPrintf("%s\n", config.to_string().data())); |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | int DumpStringsCommand::Action(const std::vector<std::string>& args) { |
| 255 | DumpContext context; |
| 256 | if (args.size() < 1) { |
| 257 | diag_->Error(DiagMessage() << "No dump apk specified."); |
| 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 262 | Printer printer(&fout); |
| 263 | |
| 264 | for (auto apk : args) { |
| 265 | auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); |
| 266 | if (!loaded_apk) { |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | // Load the run-time xml string pool using the flattened data |
| 271 | BigBuffer buffer(4096); |
| 272 | StringPool::FlattenUtf8(&buffer, loaded_apk->GetResourceTable()->string_pool, |
| 273 | context.GetDiagnostics()); |
| 274 | auto data = buffer.to_string(); |
| 275 | android::ResStringPool pool(data.data(), data.size(), false); |
| 276 | Debug::DumpResStringPool(&pool, &printer); |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | int DumpTableCommand::Action(const std::vector<std::string>& args) { |
| 283 | if (args.size() < 1) { |
| 284 | diag_->Error(DiagMessage() << "No dump apk specified."); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 289 | Printer printer(&fout); |
| 290 | |
| 291 | DebugPrintTableOptions print_options; |
| 292 | print_options.show_sources = true; |
| 293 | print_options.show_values = !no_values_; |
| 294 | |
| 295 | for (auto apk : args) { |
| 296 | auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); |
| 297 | if (!loaded_apk) { |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | if (loaded_apk->GetApkFormat()) { |
| 302 | printer.Println("Proto APK"); |
| 303 | } else { |
| 304 | printer.Println("Binary APK"); |
| 305 | } |
| 306 | |
| 307 | Debug::PrintTable(*loaded_apk->GetResourceTable(), print_options, &printer); |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int DumpXmlTreeCommand::Action(const std::vector<std::string>& args) { |
| 314 | if (args.size() < 1) { |
| 315 | diag_->Error(DiagMessage() << "No dump apk specified"); |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); |
| 320 | if (!loaded_apk) { |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 325 | Printer printer(&fout); |
| 326 | |
| 327 | // Dump the xml tree of every passed in file |
| 328 | for (auto file : files_) { |
| 329 | auto xml = loaded_apk->LoadXml(file, diag_); |
| 330 | if (!xml) { |
| 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | Debug::DumpXml(*xml, &printer); |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int DumpXmlStringsCommand::Action(const std::vector<std::string>& args) { |
| 341 | DumpContext context; |
| 342 | if (args.size() < 1) { |
| 343 | diag_->Error(DiagMessage() << "No dump apk specified."); |
| 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); |
| 348 | if (!loaded_apk) { |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 353 | Printer printer(&fout); |
| 354 | |
| 355 | // Dump the xml strings of every passed in file |
| 356 | for (auto xml_file : files_) { |
| 357 | android::ResXMLTree tree; |
| 358 | |
| 359 | if (loaded_apk->GetApkFormat() == kProto) { |
| 360 | auto xml = loaded_apk->LoadXml(xml_file, diag_); |
| 361 | if (!xml) { |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | // Flatten the xml document to get a binary representation of the proto xml file |
| 366 | BigBuffer buffer(4096); |
| 367 | XmlFlattenerOptions options = {}; |
| 368 | options.keep_raw_values = true; |
| 369 | XmlFlattener flattener(&buffer, options); |
| 370 | if (!flattener.Consume(&context, xml.get())) { |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | // Load the run-time xml tree using the flattened data |
| 375 | std::string data = buffer.to_string(); |
| 376 | tree.setTo(data.data(), data.size(), /** copyData */ true); |
| 377 | |
| 378 | } else if (loaded_apk->GetApkFormat() == kBinary) { |
| 379 | io::IFile* file = loaded_apk->GetFileCollection()->FindFile(xml_file); |
| 380 | if (!file) { |
| 381 | diag_->Error(DiagMessage(xml_file) << "file '" << xml_file << "' not found in APK"); |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 386 | if (!data) { |
| 387 | diag_->Error(DiagMessage() << "failed to open file"); |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | // Load the run-time xml tree from the file data |
| 392 | tree.setTo(data->data(), data->size(), /** copyData */ true); |
| 393 | } |
| 394 | |
| 395 | Debug::DumpResStringPool(&tree.getStrings(), &printer); |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /** Preform no action because a subcommand is required. */ |
| 402 | int DumpCommand::Action(const std::vector<std::string>& args) { |
| 403 | if (args.size() == 0) { |
| 404 | diag_->Error(DiagMessage() << "no subcommand specified"); |
| 405 | } else { |
| 406 | diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'"); |
| 407 | } |
| 408 | |
| 409 | Usage(&std::cerr); |
| 410 | return 1; |
| 411 | } |
| 412 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 413 | } // namespace aapt |