Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [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 | |
| 17 | #include "LoadedApk.h" |
| 18 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 19 | #include "ResourceValues.h" |
| 20 | #include "ValueVisitor.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 21 | #include "format/Archive.h" |
| 22 | #include "format/binary/TableFlattener.h" |
| 23 | #include "format/binary/XmlFlattener.h" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 24 | #include "format/proto/ProtoDeserialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 25 | #include "io/BigBufferStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 26 | #include "io/Util.h" |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 27 | #include "xml/XmlDom.h" |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 28 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 29 | using ::aapt::io::IFile; |
| 30 | using ::aapt::io::IFileCollection; |
| 31 | using ::aapt::xml::XmlResource; |
| 32 | using ::android::StringPiece; |
| 33 | using ::std::unique_ptr; |
| 34 | |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 35 | namespace aapt { |
| 36 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 37 | std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(const StringPiece& path, IDiagnostics* diag) { |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 38 | Source source(path); |
| 39 | std::string error; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 40 | std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::Create(path, &error); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 41 | if (apk == nullptr) { |
| 42 | diag->Error(DiagMessage(path) << "failed opening zip: " << error); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 43 | return {}; |
| 44 | } |
| 45 | |
Pierre Lecesne | f267a40 | 2017-12-01 11:39:01 +0000 | [diff] [blame] | 46 | ApkFormat apkFormat = DetermineApkFormat(apk.get()); |
| 47 | switch (apkFormat) { |
| 48 | case ApkFormat::kBinary: |
| 49 | return LoadBinaryApkFromFileCollection(source, std::move(apk), diag); |
| 50 | case ApkFormat::kProto: |
| 51 | return LoadProtoApkFromFileCollection(source, std::move(apk), diag); |
| 52 | default: |
| 53 | diag->Error(DiagMessage(path) << "could not identify format of APK"); |
| 54 | return {}; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 55 | } |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | std::unique_ptr<LoadedApk> LoadedApk::LoadProtoApkFromFileCollection( |
| 59 | const Source& source, unique_ptr<io::IFileCollection> collection, IDiagnostics* diag) { |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 60 | std::unique_ptr<ResourceTable> table; |
| 61 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 62 | io::IFile* table_file = collection->FindFile(kProtoResourceTablePath); |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 63 | if (table_file != nullptr) { |
| 64 | pb::ResourceTable pb_table; |
| 65 | std::unique_ptr<io::InputStream> in = table_file->OpenInputStream(); |
| 66 | if (in == nullptr) { |
| 67 | diag->Error(DiagMessage(source) << "failed to open " << kProtoResourceTablePath); |
| 68 | return {}; |
| 69 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 70 | |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 71 | io::ZeroCopyInputAdaptor adaptor(in.get()); |
| 72 | if (!pb_table.ParseFromZeroCopyStream(&adaptor)) { |
| 73 | diag->Error(DiagMessage(source) << "failed to read " << kProtoResourceTablePath); |
| 74 | return {}; |
| 75 | } |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 76 | |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 77 | std::string error; |
| 78 | table = util::make_unique<ResourceTable>(); |
| 79 | if (!DeserializeTableFromPb(pb_table, collection.get(), table.get(), &error)) { |
| 80 | diag->Error(DiagMessage(source) |
| 81 | << "failed to deserialize " << kProtoResourceTablePath << ": " << error); |
| 82 | return {}; |
| 83 | } |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | io::IFile* manifest_file = collection->FindFile(kAndroidManifestPath); |
| 87 | if (manifest_file == nullptr) { |
| 88 | diag->Error(DiagMessage(source) << "failed to find " << kAndroidManifestPath); |
| 89 | return {}; |
| 90 | } |
| 91 | |
| 92 | std::unique_ptr<io::InputStream> manifest_in = manifest_file->OpenInputStream(); |
| 93 | if (manifest_in == nullptr) { |
| 94 | diag->Error(DiagMessage(source) << "failed to open " << kAndroidManifestPath); |
| 95 | return {}; |
| 96 | } |
| 97 | |
| 98 | pb::XmlNode pb_node; |
| 99 | io::ZeroCopyInputAdaptor manifest_adaptor(manifest_in.get()); |
| 100 | if (!pb_node.ParseFromZeroCopyStream(&manifest_adaptor)) { |
| 101 | diag->Error(DiagMessage(source) << "failed to read proto " << kAndroidManifestPath); |
| 102 | return {}; |
| 103 | } |
| 104 | |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 105 | std::string error; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 106 | std::unique_ptr<xml::XmlResource> manifest = DeserializeXmlResourceFromPb(pb_node, &error); |
| 107 | if (manifest == nullptr) { |
| 108 | diag->Error(DiagMessage(source) |
| 109 | << "failed to deserialize proto " << kAndroidManifestPath << ": " << error); |
| 110 | return {}; |
| 111 | } |
| 112 | return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table), |
| 113 | std::move(manifest)); |
| 114 | } |
| 115 | |
| 116 | std::unique_ptr<LoadedApk> LoadedApk::LoadBinaryApkFromFileCollection( |
| 117 | const Source& source, unique_ptr<io::IFileCollection> collection, IDiagnostics* diag) { |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 118 | std::unique_ptr<ResourceTable> table; |
| 119 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 120 | io::IFile* table_file = collection->FindFile(kApkResourceTablePath); |
Tom Dobek | 725fb12 | 2017-12-08 14:19:01 +0000 | [diff] [blame] | 121 | if (table_file != nullptr) { |
| 122 | table = util::make_unique<ResourceTable>(); |
| 123 | std::unique_ptr<io::IData> data = table_file->OpenAsData(); |
| 124 | if (data == nullptr) { |
| 125 | diag->Error(DiagMessage(source) << "failed to open " << kApkResourceTablePath); |
| 126 | return {}; |
| 127 | } |
| 128 | BinaryResourceParser parser(diag, table.get(), source, data->data(), data->size(), |
| 129 | collection.get()); |
| 130 | if (!parser.Parse()) { |
| 131 | return {}; |
| 132 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 133 | } |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 134 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 135 | io::IFile* manifest_file = collection->FindFile(kAndroidManifestPath); |
| 136 | if (manifest_file == nullptr) { |
| 137 | diag->Error(DiagMessage(source) << "failed to find " << kAndroidManifestPath); |
| 138 | return {}; |
| 139 | } |
| 140 | |
| 141 | std::unique_ptr<io::IData> manifest_data = manifest_file->OpenAsData(); |
| 142 | if (manifest_data == nullptr) { |
| 143 | diag->Error(DiagMessage(source) << "failed to open " << kAndroidManifestPath); |
| 144 | return {}; |
| 145 | } |
| 146 | |
| 147 | std::string error; |
| 148 | std::unique_ptr<xml::XmlResource> manifest = |
| 149 | xml::Inflate(manifest_data->data(), manifest_data->size(), &error); |
| 150 | if (manifest == nullptr) { |
| 151 | diag->Error(DiagMessage(source) |
| 152 | << "failed to parse binary " << kAndroidManifestPath << ": " << error); |
| 153 | return {}; |
| 154 | } |
| 155 | return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table), |
| 156 | std::move(manifest)); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 159 | bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options, |
| 160 | IArchiveWriter* writer) { |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 161 | FilterChain empty; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 162 | return WriteToArchive(context, table_.get(), options, &empty, writer); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 165 | bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table, |
| 166 | const TableFlattenerOptions& options, FilterChain* filters, |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 167 | IArchiveWriter* writer, XmlResource* manifest) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 168 | std::set<std::string> referenced_resources; |
| 169 | // List the files being referenced in the resource table. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 170 | for (auto& pkg : split_table->packages) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 171 | for (auto& type : pkg->types) { |
| 172 | for (auto& entry : type->entries) { |
| 173 | for (auto& config_value : entry->values) { |
| 174 | FileReference* file_ref = ValueCast<FileReference>(config_value->value.get()); |
| 175 | if (file_ref) { |
| 176 | referenced_resources.insert(*file_ref->path); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | std::unique_ptr<io::IFileCollectionIterator> iterator = apk_->Iterator(); |
| 184 | while (iterator->HasNext()) { |
| 185 | io::IFile* file = iterator->Next(); |
| 186 | |
| 187 | std::string path = file->GetSource().path; |
| 188 | // The name of the path has the format "<zip-file-name>@<path-to-file>". |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 189 | path = path.substr(path.find('@') + 1); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 190 | |
| 191 | // Skip resources that are not referenced if requested. |
| 192 | if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) { |
| 193 | if (context->IsVerbose()) { |
| 194 | context->GetDiagnostics()->Note(DiagMessage() |
Pierre Lecesne | fa131d5 | 2017-02-03 19:15:03 +0000 | [diff] [blame] | 195 | << "Removing resource '" << path << "' from APK."); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 196 | } |
| 197 | continue; |
| 198 | } |
| 199 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 200 | if (!filters->Keep(path)) { |
| 201 | if (context->IsVerbose()) { |
| 202 | context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK."); |
| 203 | } |
| 204 | continue; |
| 205 | } |
| 206 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 207 | // The resource table needs to be re-serialized since it might have changed. |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 208 | if (path == "resources.arsc") { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 209 | BigBuffer buffer(4096); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 210 | // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode |
| 211 | // with sparse entries) b/35389232. |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 212 | TableFlattener flattener(options, &buffer); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 213 | if (!flattener.Consume(context, split_table)) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 217 | io::BigBufferInputStream input_stream(&buffer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 218 | if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign, |
| 219 | writer)) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 222 | |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 223 | } else if (manifest != nullptr && path == "AndroidManifest.xml") { |
| 224 | BigBuffer buffer(8192); |
Shane Farmer | d05b913 | 2018-02-14 15:40:35 -0800 | [diff] [blame^] | 225 | XmlFlattenerOptions xml_flattener_options; |
| 226 | xml_flattener_options.use_utf16 = true; |
| 227 | XmlFlattener xml_flattener(&buffer, xml_flattener_options); |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 228 | if (!xml_flattener.Consume(context, manifest)) { |
| 229 | context->GetDiagnostics()->Error(DiagMessage(path) << "flattening failed"); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u; |
| 234 | io::BigBufferInputStream manifest_buffer_in(&buffer); |
| 235 | if (!io::CopyInputStreamToArchive(context, &manifest_buffer_in, path, compression_flags, |
| 236 | writer)) { |
| 237 | return false; |
| 238 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 239 | } else { |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 240 | if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
Pierre Lecesne | f267a40 | 2017-12-01 11:39:01 +0000 | [diff] [blame] | 248 | ApkFormat LoadedApk::DetermineApkFormat(io::IFileCollection* apk) { |
| 249 | if (apk->FindFile("resources.arsc") != nullptr) { |
| 250 | return ApkFormat::kBinary; |
| 251 | } else if (apk->FindFile("resources.pb") != nullptr) { |
| 252 | return ApkFormat::kProto; |
| 253 | } else { |
| 254 | // If the resource table is not present, attempt to read the manifest. |
| 255 | io::IFile* manifest_file = apk->FindFile(kAndroidManifestPath); |
| 256 | if (manifest_file == nullptr) { |
| 257 | return ApkFormat::kUnknown; |
| 258 | } |
| 259 | |
| 260 | // First try in proto format. |
| 261 | std::unique_ptr<io::InputStream> manifest_in = manifest_file->OpenInputStream(); |
| 262 | if (manifest_in != nullptr) { |
| 263 | pb::XmlNode pb_node; |
| 264 | io::ZeroCopyInputAdaptor manifest_adaptor(manifest_in.get()); |
| 265 | if (pb_node.ParseFromZeroCopyStream(&manifest_adaptor)) { |
| 266 | return ApkFormat::kProto; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // If it didn't work, try in binary format. |
| 271 | std::unique_ptr<io::IData> manifest_data = manifest_file->OpenAsData(); |
| 272 | if (manifest_data != nullptr) { |
| 273 | std::string error; |
| 274 | std::unique_ptr<xml::XmlResource> manifest = |
| 275 | xml::Inflate(manifest_data->data(), manifest_data->size(), &error); |
| 276 | if (manifest != nullptr) { |
| 277 | return ApkFormat::kBinary; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return ApkFormat::kUnknown; |
| 282 | } |
| 283 | } |
| 284 | |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 285 | } // namespace aapt |