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 | |
Iurii Makhno | dcead62 | 2022-04-13 18:00:03 +0000 | [diff] [blame^] | 134 | void SetIncludeMetaData(bool value) { |
| 135 | options_.include_meta_data = value; |
| 136 | } |
| 137 | |
| 138 | void SetOnlyPermissions(bool value) { |
| 139 | options_.only_permissions = value; |
| 140 | } |
| 141 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 142 | int Dump(LoadedApk* apk) override { |
| 143 | return DumpManifest(apk, options_, GetPrinter(), GetDiagnostics()); |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | DumpManifestOptions options_; |
| 148 | }; |
| 149 | |
| 150 | class DumpConfigsCommand : public DumpApkCommand { |
| 151 | public: |
| 152 | explicit DumpConfigsCommand(text::Printer* printer, IDiagnostics* diag) |
| 153 | : DumpApkCommand("configurations", printer, diag) { |
| 154 | SetDescription("Print every configuration used by a resource in the APK."); |
| 155 | } |
| 156 | |
| 157 | int Dump(LoadedApk* apk) override; |
| 158 | }; |
| 159 | |
| 160 | class DumpPackageNameCommand : public DumpApkCommand { |
| 161 | public: |
| 162 | explicit DumpPackageNameCommand(text::Printer* printer, IDiagnostics* diag) |
| 163 | : DumpApkCommand("packagename", printer, diag) { |
| 164 | SetDescription("Print the package name of the APK."); |
| 165 | } |
| 166 | |
| 167 | int Dump(LoadedApk* apk) override; |
| 168 | }; |
| 169 | |
| 170 | class DumpPermissionsCommand : public DumpApkCommand { |
| 171 | public: |
| 172 | explicit DumpPermissionsCommand(text::Printer* printer, IDiagnostics* diag) |
| 173 | : DumpApkCommand("permissions", printer, diag) { |
| 174 | SetDescription("Print the permissions extracted from the manifest of the APK."); |
| 175 | } |
| 176 | |
| 177 | int Dump(LoadedApk* apk) override { |
| 178 | DumpManifestOptions options; |
| 179 | options.only_permissions = true; |
| 180 | return DumpManifest(apk, options, GetPrinter(), GetDiagnostics()); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | class DumpStringsCommand : public DumpApkCommand { |
| 185 | public: |
| 186 | explicit DumpStringsCommand(text::Printer* printer, IDiagnostics* diag) |
| 187 | : DumpApkCommand("strings", printer, diag) { |
| 188 | SetDescription("Print the contents of the resource table string pool in the APK."); |
| 189 | } |
| 190 | |
| 191 | int Dump(LoadedApk* apk) override; |
| 192 | }; |
| 193 | |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 194 | /** Prints the graph of parents of a style in an APK. */ |
| 195 | class DumpStyleParentCommand : public DumpApkCommand { |
| 196 | public: |
| 197 | explicit DumpStyleParentCommand(text::Printer* printer, IDiagnostics* diag) |
| 198 | : DumpApkCommand("styleparents", printer, diag) { |
| 199 | SetDescription("Print the parents of a style in an APK."); |
| 200 | AddRequiredFlag("--style", "The name of the style to print", &style_); |
| 201 | } |
| 202 | |
| 203 | int Dump(LoadedApk* apk) override; |
| 204 | |
| 205 | private: |
| 206 | std::string style_; |
| 207 | }; |
| 208 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 209 | class DumpTableCommand : public DumpApkCommand { |
| 210 | public: |
| 211 | explicit DumpTableCommand(text::Printer* printer, IDiagnostics* diag) |
| 212 | : DumpApkCommand("resources", printer, diag) { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 213 | SetDescription("Print the contents of the resource table from the APK."); |
| 214 | AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.", |
| 215 | &no_values_); |
| 216 | AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_); |
| 217 | } |
| 218 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 219 | int Dump(LoadedApk* apk) override; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 220 | |
| 221 | private: |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 222 | bool no_values_ = false; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 223 | bool verbose_ = false; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 226 | class DumpXmlStringsCommand : public DumpApkCommand { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 227 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 228 | explicit DumpXmlStringsCommand(text::Printer* printer, IDiagnostics* diag) |
| 229 | : DumpApkCommand("xmlstrings", printer, diag) { |
| 230 | SetDescription("Print the string pool of a compiled xml in an APK."); |
| 231 | AddRequiredFlagList("--file", "A compiled xml file to print", &files_); |
| 232 | } |
| 233 | |
| 234 | int Dump(LoadedApk* apk) override; |
| 235 | |
| 236 | private: |
| 237 | std::vector<std::string> files_; |
| 238 | }; |
| 239 | |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 240 | class DumpChunks : public DumpApkCommand { |
| 241 | public: |
| 242 | DumpChunks(text::Printer* printer, IDiagnostics* diag) : DumpApkCommand("chunks", printer, diag) { |
| 243 | SetDescription("Print the chunk information of the compiled resources.arsc in the APK."); |
| 244 | } |
| 245 | |
| 246 | int Dump(LoadedApk* apk) override; |
| 247 | }; |
| 248 | |
| 249 | /** Prints the tree of a compiled xml in an APK. */ |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 250 | class DumpXmlTreeCommand : public DumpApkCommand { |
| 251 | public: |
| 252 | explicit DumpXmlTreeCommand(text::Printer* printer, IDiagnostics* diag) |
| 253 | : DumpApkCommand("xmltree", printer, diag) { |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 254 | SetDescription("Print the tree of a compiled xml in an APK."); |
| 255 | AddRequiredFlagList("--file", "A compiled xml file to print", &files_); |
| 256 | } |
| 257 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 258 | int Dump(LoadedApk* apk) override; |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 259 | |
| 260 | private: |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 261 | std::vector<std::string> files_; |
| 262 | }; |
| 263 | |
Mårten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 264 | class DumpOverlayableCommand : public DumpApkCommand { |
| 265 | public: |
| 266 | explicit DumpOverlayableCommand(text::Printer* printer, IDiagnostics* diag) |
| 267 | : DumpApkCommand("overlayable", printer, diag) { |
| 268 | SetDescription("Print the <overlayable> resources of an APK."); |
| 269 | } |
| 270 | |
| 271 | int Dump(LoadedApk* apk) override; |
| 272 | }; |
| 273 | |
Todd Kennedy | 908b7fc | 2018-08-24 10:11:21 -0700 | [diff] [blame] | 274 | /** The default dump command. Performs no action because a subcommand is required. */ |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 275 | class DumpCommand : public Command { |
| 276 | public: |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 277 | explicit DumpCommand(text::Printer* printer, IDiagnostics* diag) |
| 278 | : Command("dump", "d"), diag_(diag) { |
| 279 | AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(printer, diag_)); |
| 280 | AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(printer, diag_)); |
| 281 | AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(printer, diag_)); |
| 282 | AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(printer, diag_)); |
| 283 | AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(printer, diag_)); |
| 284 | AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(printer, diag_)); |
Aurimas Liutikas | 13e6a1d | 2018-09-25 16:11:40 -0700 | [diff] [blame] | 285 | AddOptionalSubcommand(util::make_unique<DumpStyleParentCommand>(printer, diag_)); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 286 | AddOptionalSubcommand(util::make_unique<DumpTableCommand>(printer, diag_)); |
Ryan Mitchell | 19b2709 | 2018-08-13 11:36:27 -0700 | [diff] [blame] | 287 | AddOptionalSubcommand(util::make_unique<DumpChunks>(printer, diag_)); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 288 | AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(printer, diag_)); |
| 289 | AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(printer, diag_)); |
Mårten Kongstad | 1d3b6485 | 2019-09-17 13:02:32 +0200 | [diff] [blame] | 290 | AddOptionalSubcommand(util::make_unique<DumpOverlayableCommand>(printer, diag_)); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 291 | AddOptionalSubcommand(util::make_unique<DumpBadgerCommand>(printer), /* hidden */ true); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 294 | int Action(const std::vector<std::string>& args) override { |
| 295 | if (args.size() == 0) { |
| 296 | diag_->Error(DiagMessage() << "no subcommand specified"); |
| 297 | } else { |
| 298 | diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'"); |
| 299 | } |
| 300 | Usage(&std::cerr); |
| 301 | return 1; |
| 302 | } |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 303 | |
| 304 | private: |
| 305 | IDiagnostics* diag_; |
| 306 | }; |
| 307 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 308 | } // namespace aapt |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 309 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 310 | #endif // AAPT2_DUMP_H |