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