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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 19 | #include "androidfw/StringPiece.h" |
| 20 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 21 | #include "Debug.h" |
| 22 | #include "Diagnostics.h" |
| 23 | #include "Flags.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 24 | #include "format/binary/BinaryResourceParser.h" |
| 25 | #include "format/proto/ProtoDeserialize.h" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 26 | #include "io/ZipArchive.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 27 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 28 | #include "util/Files.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 30 | using ::android::StringPiece; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 34 | bool DumpCompiledFile(const pb::internal::CompiledFile& pb_file, const void* data, size_t len, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 35 | const Source& source, IAaptContext* context) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 36 | ResourceFile file; |
| 37 | std::string error; |
| 38 | if (!DeserializeCompiledFileFromPb(pb_file, &file, &error)) { |
| 39 | context->GetDiagnostics()->Warn(DiagMessage(source) |
| 40 | << "failed to read compiled file: " << error); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 41 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 44 | std::cout << "Resource: " << file.name << "\n" |
| 45 | << "Config: " << file.config << "\n" |
| 46 | << "Source: " << file.source << "\n"; |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 47 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 50 | bool TryDumpFile(IAaptContext* context, const std::string& file_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | std::string err; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 52 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(file_path, &err); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | if (zip) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 54 | ResourceTable table; |
| 55 | if (io::IFile* file = zip->FindFile("resources.arsc.flat")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 57 | if (data == nullptr) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 58 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 59 | << "failed to open resources.arsc.flat"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 60 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | pb::ResourceTable pb_table; |
| 64 | if (!pb_table.ParseFromArray(data->data(), data->size())) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 65 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "invalid resources.arsc.flat"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 66 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 68 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 69 | ResourceTable table; |
| 70 | if (!DeserializeTableFromPb(pb_table, &table, &err)) { |
| 71 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 72 | << "failed to parse table: " << err); |
| 73 | return false; |
| 74 | } |
| 75 | } else if (io::IFile* file = zip->FindFile("resources.arsc")) { |
| 76 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 77 | if (!data) { |
| 78 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.arsc"); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | BinaryResourceParser parser(context, &table, Source(file_path), data->data(), data->size()); |
| 83 | if (!parser.Parse()) { |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 84 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 88 | DebugPrintTableOptions options; |
| 89 | options.show_sources = true; |
| 90 | Debug::PrintTable(&table, options); |
| 91 | return true; |
| 92 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 93 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 94 | err.clear(); |
| 95 | |
| 96 | Maybe<android::FileMap> file = file::MmapPath(file_path, &err); |
| 97 | if (!file) { |
| 98 | context->GetDiagnostics()->Error(DiagMessage(file_path) << err); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | android::FileMap* file_map = &file.value(); |
| 103 | |
| 104 | // Check to see if this is a loose ResourceTable. |
| 105 | pb::ResourceTable pb_table; |
| 106 | if (pb_table.ParseFromArray(file_map->getDataPtr(), file_map->getDataLength())) { |
| 107 | ResourceTable table; |
| 108 | if (DeserializeTableFromPb(pb_table, &table, &err)) { |
| 109 | DebugPrintTableOptions options; |
| 110 | options.show_sources = true; |
| 111 | Debug::PrintTable(&table, options); |
| 112 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 116 | // Try as a compiled file. |
| 117 | CompiledFileInputStream input(file_map->getDataPtr(), file_map->getDataLength()); |
| 118 | uint32_t num_files = 0; |
| 119 | if (!input.ReadLittleEndian32(&num_files)) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | for (uint32_t i = 0; i < num_files; i++) { |
| 124 | pb::internal::CompiledFile compiled_file; |
| 125 | if (!input.ReadCompiledFile(&compiled_file)) { |
| 126 | context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 127 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 130 | uint64_t offset, len; |
| 131 | if (!input.ReadDataMetaData(&offset, &len)) { |
| 132 | context->GetDiagnostics()->Warn(DiagMessage() << "failed to read meta data"); |
| 133 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 134 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 136 | const void* data = static_cast<const uint8_t*>(file_map->getDataPtr()) + offset; |
| 137 | if (!DumpCompiledFile(compiled_file, data, len, Source(file_path), context)) { |
| 138 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 141 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | class DumpContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 146 | PackageType GetPackageType() override { |
| 147 | // Doesn't matter. |
| 148 | return PackageType::kApp; |
| 149 | } |
| 150 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 151 | IDiagnostics* GetDiagnostics() override { |
| 152 | return &diagnostics_; |
| 153 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | NameMangler* GetNameMangler() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | abort(); |
| 157 | return nullptr; |
| 158 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 159 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | static std::string empty; |
| 162 | return empty; |
| 163 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 165 | uint8_t GetPackageId() override { |
| 166 | return 0; |
| 167 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | abort(); |
| 171 | return nullptr; |
| 172 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 173 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 174 | bool IsVerbose() override { |
| 175 | return verbose_; |
| 176 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 177 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 178 | void SetVerbose(bool val) { |
| 179 | verbose_ = val; |
| 180 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 181 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 182 | int GetMinSdkVersion() override { |
| 183 | return 0; |
| 184 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | StdErrDiagnostics diagnostics_; |
| 188 | bool verbose_ = false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | /** |
| 192 | * Entry point for dump command. |
| 193 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | int Dump(const std::vector<StringPiece>& args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | bool verbose = false; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 196 | Flags flags = Flags().OptionalSwitch("-v", "increase verbosity of output", &verbose); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | if (!flags.Parse("aapt2 dump", args, &std::cerr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | return 1; |
| 199 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 200 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | DumpContext context; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | context.SetVerbose(verbose); |
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 | for (const std::string& arg : flags.GetArgs()) { |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 205 | if (!TryDumpFile(&context, arg)) { |
| 206 | return 1; |
| 207 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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 | return 0; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | } // namespace aapt |