Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef AAPT2_DUMP_H |
| 18 | #define AAPT2_DUMP_H |
| 19 | |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame^] | 20 | #include <io/FileStream.h> |
| 21 | #include <io/ZipArchive.h> |
| 22 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 23 | #include "Command.h" |
| 24 | #include "Debug.h" |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 25 | #include "LoadedApk.h" |
Ryan Mitchell | fc225b2 | 2018-08-21 14:52:51 -0700 | [diff] [blame] | 26 | #include "dump/DumpManifest.h" |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 27 | |
| 28 | namespace aapt { |
| 29 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 30 | /** |
| 31 | * The base command for dumping information about apks. When the command is executed, the command |
| 32 | * performs the DumpApkCommand::Dump() operation on each apk provided as a file argument. |
| 33 | **/ |
| 34 | class DumpApkCommand : public Command { |
| 35 | public: |
| 36 | explicit DumpApkCommand(const std::string&& name, text::Printer* printer, IDiagnostics* diag) |
| 37 | : Command(name), printer_(printer), diag_(diag) { |
Fabien Sanglard | 362da82 | 2019-03-14 11:39:39 -0700 | [diff] [blame] | 38 | SetDescription("Dump information about an APK or APC."); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | text::Printer* GetPrinter() { |
| 42 | return printer_; |
| 43 | } |
| 44 | |
| 45 | IDiagnostics* GetDiagnostics() { |
| 46 | return diag_; |
| 47 | } |
| 48 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 49 | std::optional<std::string> GetPackageName(LoadedApk* apk) { |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 50 | xml::Element* manifest_el = apk->GetManifest()->root.get(); |
| 51 | if (!manifest_el) { |
| 52 | GetDiagnostics()->Error(DiagMessage() << "No AndroidManifest."); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 53 | return {}; |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
| 57 | if (!attr) { |
| 58 | GetDiagnostics()->Error(DiagMessage() << "No package name."); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 59 | return {}; |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 60 | } |
| 61 | return attr->value; |
| 62 | } |
| 63 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 64 | /** Perform the dump operation on the apk. */ |
| 65 | virtual int Dump(LoadedApk* apk) = 0; |
| 66 | |
| 67 | int Action(const std::vector<std::string>& args) final { |
| 68 | if (args.size() < 1) { |
| 69 | diag_->Error(DiagMessage() << "No dump apk specified."); |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | bool error = false; |
| 74 | for (auto apk : args) { |
| 75 | auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); |
| 76 | if (!loaded_apk) { |
| 77 | error = true; |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | error |= Dump(loaded_apk.get()); |
| 82 | } |
| 83 | |
| 84 | return error; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | text::Printer* printer_; |
| 89 | IDiagnostics* diag_; |
| 90 | }; |
| 91 | |
| 92 | /** Command that prints contents of files generated from the compilation stage. */ |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 93 | class DumpAPCCommand : public Command { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 94 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 95 | explicit DumpAPCCommand(text::Printer* printer, IDiagnostics* diag) |
| 96 | : Command("apc"), printer_(printer), diag_(diag) { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 97 | SetDescription("Print the contents of the AAPT2 Container (APC) generated fom compilation."); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 98 | AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.", |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 99 | &no_values_); |
| 100 | AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int Action(const std::vector<std::string>& args) override; |
| 104 | |
| 105 | private: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 106 | text::Printer* printer_; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 107 | IDiagnostics* diag_; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 108 | bool no_values_ = false; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 109 | bool verbose_ = false; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 112 | /** Easter egg command shown when users enter "badger" instead of "badging". */ |
| 113 | class DumpBadgerCommand : public Command { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 114 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 115 | explicit DumpBadgerCommand(text::Printer* printer) : Command("badger"), printer_(printer) { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int Action(const std::vector<std::string>& args) override; |
| 119 | |
| 120 | private: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 121 | text::Printer* printer_; |
| 122 | const static char kBadgerData[2925]; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 125 | class DumpBadgingCommand : public DumpApkCommand { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 126 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 127 | explicit DumpBadgingCommand(text::Printer* printer, IDiagnostics* diag) |
| 128 | : DumpApkCommand("badging", printer, diag) { |
| 129 | SetDescription("Print information extracted from the manifest of the APK."); |
| 130 | AddOptionalSwitch("--include-meta-data", "Include meta-data information.", |
| 131 | &options_.include_meta_data); |
| 132 | } |
| 133 | |
| 134 | int Dump(LoadedApk* apk) override { |
| 135 | return DumpManifest(apk, options_, GetPrinter(), GetDiagnostics()); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | DumpManifestOptions options_; |
| 140 | }; |
| 141 | |
| 142 | class DumpConfigsCommand : public DumpApkCommand { |
| 143 | public: |
| 144 | explicit DumpConfigsCommand(text::Printer* printer, IDiagnostics* diag) |
| 145 | : DumpApkCommand("configurations", printer, diag) { |
| 146 | SetDescription("Print every configuration used by a resource in the APK."); |
| 147 | } |
| 148 | |
| 149 | int Dump(LoadedApk* apk) override; |
| 150 | }; |
| 151 | |
| 152 | class DumpPackageNameCommand : public DumpApkCommand { |
| 153 | public: |
| 154 | explicit DumpPackageNameCommand(text::Printer* printer, IDiagnostics* diag) |
| 155 | : DumpApkCommand("packagename", printer, diag) { |
| 156 | SetDescription("Print the package name of the APK."); |
| 157 | } |
| 158 | |
| 159 | int Dump(LoadedApk* apk) override; |
| 160 | }; |
| 161 | |
| 162 | class DumpPermissionsCommand : public DumpApkCommand { |
| 163 | public: |
| 164 | explicit DumpPermissionsCommand(text::Printer* printer, IDiagnostics* diag) |
| 165 | : DumpApkCommand("permissions", printer, diag) { |
| 166 | SetDescription("Print the permissions extracted from the manifest of the APK."); |
| 167 | } |
| 168 | |
| 169 | int Dump(LoadedApk* apk) override { |
| 170 | DumpManifestOptions options; |
| 171 | options.only_permissions = true; |
| 172 | return DumpManifest(apk, options, GetPrinter(), GetDiagnostics()); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | class DumpStringsCommand : public DumpApkCommand { |
| 177 | public: |
| 178 | explicit DumpStringsCommand(text::Printer* printer, IDiagnostics* diag) |
| 179 | : DumpApkCommand("strings", printer, diag) { |
| 180 | SetDescription("Print the contents of the resource table string pool in the APK."); |
| 181 | } |
| 182 | |
| 183 | int Dump(LoadedApk* apk) override; |
| 184 | }; |
| 185 | |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 186 | /** Prints the graph of parents of a style in an APK. */ |
| 187 | class DumpStyleParentCommand : public DumpApkCommand { |
| 188 | public: |
| 189 | explicit DumpStyleParentCommand(text::Printer* printer, IDiagnostics* diag) |
| 190 | : DumpApkCommand("styleparents", printer, diag) { |
| 191 | SetDescription("Print the parents of a style in an APK."); |
| 192 | AddRequiredFlag("--style", "The name of the style to print", &style_); |
| 193 | } |
| 194 | |
| 195 | int Dump(LoadedApk* apk) override; |
| 196 | |
| 197 | private: |
| 198 | std::string style_; |
| 199 | }; |
| 200 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 201 | class DumpTableCommand : public DumpApkCommand { |
| 202 | public: |
| 203 | explicit DumpTableCommand(text::Printer* printer, IDiagnostics* diag) |
| 204 | : DumpApkCommand("resources", printer, diag) { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 205 | SetDescription("Print the contents of the resource table from the APK."); |
| 206 | AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.", |
| 207 | &no_values_); |
| 208 | AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_); |
| 209 | } |
| 210 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 211 | int Dump(LoadedApk* apk) override; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 212 | |
| 213 | private: |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 214 | bool no_values_ = false; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 215 | bool verbose_ = false; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 218 | class DumpXmlStringsCommand : public DumpApkCommand { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 219 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 220 | explicit DumpXmlStringsCommand(text::Printer* printer, IDiagnostics* diag) |
| 221 | : DumpApkCommand("xmlstrings", printer, diag) { |
| 222 | SetDescription("Print the string pool of a compiled xml in an APK."); |
| 223 | AddRequiredFlagList("--file", "A compiled xml file to print", &files_); |
| 224 | } |
| 225 | |
| 226 | int Dump(LoadedApk* apk) override; |
| 227 | |
| 228 | private: |
| 229 | std::vector<std::string> files_; |
| 230 | }; |
| 231 | |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame^] | 232 | class DumpChunks : public DumpApkCommand { |
| 233 | public: |
| 234 | DumpChunks(text::Printer* printer, IDiagnostics* diag) : DumpApkCommand("chunks", printer, diag) { |
| 235 | SetDescription("Print the chunk information of the compiled resources.arsc in the APK."); |
| 236 | } |
| 237 | |
| 238 | int Dump(LoadedApk* apk) override; |
| 239 | }; |
| 240 | |
| 241 | /** Prints the tree of a compiled xml in an APK. */ |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 242 | class DumpXmlTreeCommand : public DumpApkCommand { |
| 243 | public: |
| 244 | explicit DumpXmlTreeCommand(text::Printer* printer, IDiagnostics* diag) |
| 245 | : DumpApkCommand("xmltree", printer, diag) { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 246 | SetDescription("Print the tree of a compiled xml in an APK."); |
| 247 | AddRequiredFlagList("--file", "A compiled xml file to print", &files_); |
| 248 | } |
| 249 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 250 | int Dump(LoadedApk* apk) override; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 251 | |
| 252 | private: |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 253 | std::vector<std::string> files_; |
| 254 | }; |
| 255 | |
Mårten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 256 | class DumpOverlayableCommand : public DumpApkCommand { |
| 257 | public: |
| 258 | explicit DumpOverlayableCommand(text::Printer* printer, IDiagnostics* diag) |
| 259 | : DumpApkCommand("overlayable", printer, diag) { |
| 260 | SetDescription("Print the <overlayable> resources of an APK."); |
| 261 | } |
| 262 | |
| 263 | int Dump(LoadedApk* apk) override; |
| 264 | }; |
| 265 | |
Todd Kennedy | 908b7fc | 2018-08-24 10:11:21 -0700 | [diff] [blame] | 266 | /** The default dump command. Performs no action because a subcommand is required. */ |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 267 | class DumpCommand : public Command { |
| 268 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 269 | explicit DumpCommand(text::Printer* printer, IDiagnostics* diag) |
| 270 | : Command("dump", "d"), diag_(diag) { |
| 271 | AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(printer, diag_)); |
| 272 | AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(printer, diag_)); |
| 273 | AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(printer, diag_)); |
| 274 | AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(printer, diag_)); |
| 275 | AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(printer, diag_)); |
| 276 | AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(printer, diag_)); |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 277 | AddOptionalSubcommand(util::make_unique<DumpStyleParentCommand>(printer, diag_)); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 278 | AddOptionalSubcommand(util::make_unique<DumpTableCommand>(printer, diag_)); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame^] | 279 | AddOptionalSubcommand(util::make_unique<DumpChunks>(printer, diag_)); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 280 | AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(printer, diag_)); |
| 281 | AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(printer, diag_)); |
Mårten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 282 | AddOptionalSubcommand(util::make_unique<DumpOverlayableCommand>(printer, diag_)); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 283 | AddOptionalSubcommand(util::make_unique<DumpBadgerCommand>(printer), /* hidden */ true); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 286 | int Action(const std::vector<std::string>& args) override { |
| 287 | if (args.size() == 0) { |
| 288 | diag_->Error(DiagMessage() << "no subcommand specified"); |
| 289 | } else { |
| 290 | diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'"); |
| 291 | } |
| 292 | Usage(&std::cerr); |
| 293 | return 1; |
| 294 | } |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 295 | |
| 296 | private: |
| 297 | IDiagnostics* diag_; |
| 298 | }; |
| 299 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 300 | } // namespace aapt |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 301 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 302 | #endif // AAPT2_DUMP_H |