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) { |
| 60 | io::IFile* table_file = collection->FindFile(kProtoResourceTablePath); |
| 61 | if (table_file == nullptr) { |
| 62 | diag->Error(DiagMessage(source) << "failed to find " << kProtoResourceTablePath); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 63 | return {}; |
| 64 | } |
| 65 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 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 | } |
| 71 | |
| 72 | pb::ResourceTable pb_table; |
| 73 | io::ZeroCopyInputAdaptor adaptor(in.get()); |
| 74 | if (!pb_table.ParseFromZeroCopyStream(&adaptor)) { |
| 75 | diag->Error(DiagMessage(source) << "failed to read " << kProtoResourceTablePath); |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | std::string error; |
| 80 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
| 81 | if (!DeserializeTableFromPb(pb_table, collection.get(), table.get(), &error)) { |
| 82 | diag->Error(DiagMessage(source) |
| 83 | << "failed to deserialize " << kProtoResourceTablePath << ": " << error); |
| 84 | return {}; |
| 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 | |
| 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) { |
| 118 | io::IFile* table_file = collection->FindFile(kApkResourceTablePath); |
| 119 | if (table_file == nullptr) { |
| 120 | diag->Error(DiagMessage(source) << "failed to find " << kApkResourceTablePath); |
| 121 | |
| 122 | return {}; |
| 123 | } |
| 124 | |
| 125 | std::unique_ptr<io::IData> data = table_file->OpenAsData(); |
| 126 | if (data == nullptr) { |
| 127 | diag->Error(DiagMessage(source) << "failed to open " << kApkResourceTablePath); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 128 | return {}; |
| 129 | } |
| 130 | |
| 131 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 132 | BinaryResourceParser parser(diag, table.get(), source, data->data(), data->size(), |
| 133 | collection.get()); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 134 | if (!parser.Parse()) { |
| 135 | return {}; |
| 136 | } |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 138 | io::IFile* manifest_file = collection->FindFile(kAndroidManifestPath); |
| 139 | if (manifest_file == nullptr) { |
| 140 | diag->Error(DiagMessage(source) << "failed to find " << kAndroidManifestPath); |
| 141 | return {}; |
| 142 | } |
| 143 | |
| 144 | std::unique_ptr<io::IData> manifest_data = manifest_file->OpenAsData(); |
| 145 | if (manifest_data == nullptr) { |
| 146 | diag->Error(DiagMessage(source) << "failed to open " << kAndroidManifestPath); |
| 147 | return {}; |
| 148 | } |
| 149 | |
| 150 | std::string error; |
| 151 | std::unique_ptr<xml::XmlResource> manifest = |
| 152 | xml::Inflate(manifest_data->data(), manifest_data->size(), &error); |
| 153 | if (manifest == nullptr) { |
| 154 | diag->Error(DiagMessage(source) |
| 155 | << "failed to parse binary " << kAndroidManifestPath << ": " << error); |
| 156 | return {}; |
| 157 | } |
| 158 | return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table), |
| 159 | std::move(manifest)); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 162 | bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options, |
| 163 | IArchiveWriter* writer) { |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 164 | FilterChain empty; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 165 | return WriteToArchive(context, table_.get(), options, &empty, writer); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 168 | bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table, |
| 169 | const TableFlattenerOptions& options, FilterChain* filters, |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 170 | IArchiveWriter* writer, XmlResource* manifest) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 171 | std::set<std::string> referenced_resources; |
| 172 | // List the files being referenced in the resource table. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 173 | for (auto& pkg : split_table->packages) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 174 | for (auto& type : pkg->types) { |
| 175 | for (auto& entry : type->entries) { |
| 176 | for (auto& config_value : entry->values) { |
| 177 | FileReference* file_ref = ValueCast<FileReference>(config_value->value.get()); |
| 178 | if (file_ref) { |
| 179 | referenced_resources.insert(*file_ref->path); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | std::unique_ptr<io::IFileCollectionIterator> iterator = apk_->Iterator(); |
| 187 | while (iterator->HasNext()) { |
| 188 | io::IFile* file = iterator->Next(); |
| 189 | |
| 190 | std::string path = file->GetSource().path; |
| 191 | // 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] | 192 | path = path.substr(path.find('@') + 1); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 193 | |
| 194 | // Skip resources that are not referenced if requested. |
| 195 | if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) { |
| 196 | if (context->IsVerbose()) { |
| 197 | context->GetDiagnostics()->Note(DiagMessage() |
Pierre Lecesne | fa131d5 | 2017-02-03 19:15:03 +0000 | [diff] [blame] | 198 | << "Removing resource '" << path << "' from APK."); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 199 | } |
| 200 | continue; |
| 201 | } |
| 202 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 203 | if (!filters->Keep(path)) { |
| 204 | if (context->IsVerbose()) { |
| 205 | context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK."); |
| 206 | } |
| 207 | continue; |
| 208 | } |
| 209 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 210 | // 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] | 211 | if (path == "resources.arsc") { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 212 | BigBuffer buffer(4096); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 213 | // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode |
| 214 | // with sparse entries) b/35389232. |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 215 | TableFlattener flattener(options, &buffer); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 216 | if (!flattener.Consume(context, split_table)) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 220 | io::BigBufferInputStream input_stream(&buffer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 221 | if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign, |
| 222 | writer)) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 223 | return false; |
| 224 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 225 | |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 226 | } else if (manifest != nullptr && path == "AndroidManifest.xml") { |
| 227 | BigBuffer buffer(8192); |
| 228 | XmlFlattener xml_flattener(&buffer, {}); |
| 229 | if (!xml_flattener.Consume(context, manifest)) { |
| 230 | context->GetDiagnostics()->Error(DiagMessage(path) << "flattening failed"); |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u; |
| 235 | io::BigBufferInputStream manifest_buffer_in(&buffer); |
| 236 | if (!io::CopyInputStreamToArchive(context, &manifest_buffer_in, path, compression_flags, |
| 237 | writer)) { |
| 238 | return false; |
| 239 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 240 | } else { |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 241 | if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
Pierre Lecesne | f267a40 | 2017-12-01 11:39:01 +0000 | [diff] [blame] | 249 | ApkFormat LoadedApk::DetermineApkFormat(io::IFileCollection* apk) { |
| 250 | if (apk->FindFile("resources.arsc") != nullptr) { |
| 251 | return ApkFormat::kBinary; |
| 252 | } else if (apk->FindFile("resources.pb") != nullptr) { |
| 253 | return ApkFormat::kProto; |
| 254 | } else { |
| 255 | // If the resource table is not present, attempt to read the manifest. |
| 256 | io::IFile* manifest_file = apk->FindFile(kAndroidManifestPath); |
| 257 | if (manifest_file == nullptr) { |
| 258 | return ApkFormat::kUnknown; |
| 259 | } |
| 260 | |
| 261 | // First try in proto format. |
| 262 | std::unique_ptr<io::InputStream> manifest_in = manifest_file->OpenInputStream(); |
| 263 | if (manifest_in != nullptr) { |
| 264 | pb::XmlNode pb_node; |
| 265 | io::ZeroCopyInputAdaptor manifest_adaptor(manifest_in.get()); |
| 266 | if (pb_node.ParseFromZeroCopyStream(&manifest_adaptor)) { |
| 267 | return ApkFormat::kProto; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // If it didn't work, try in binary format. |
| 272 | std::unique_ptr<io::IData> manifest_data = manifest_file->OpenAsData(); |
| 273 | if (manifest_data != nullptr) { |
| 274 | std::string error; |
| 275 | std::unique_ptr<xml::XmlResource> manifest = |
| 276 | xml::Inflate(manifest_data->data(), manifest_data->size(), &error); |
| 277 | if (manifest != nullptr) { |
| 278 | return ApkFormat::kBinary; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return ApkFormat::kUnknown; |
| 283 | } |
| 284 | } |
| 285 | |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 286 | } // namespace aapt |