blob: 33b5a8b2686db97516c0e7fc390a880a110e22f0 [file] [log] [blame]
Pierre Lecesneff759e62017-02-01 00:29:25 +00001/*
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 Lecesne2599aa42017-02-01 22:47:03 +000019#include "ResourceValues.h"
20#include "ValueVisitor.h"
Adam Lesinski46708052017-09-29 14:49:15 -070021#include "format/Archive.h"
22#include "format/binary/TableFlattener.h"
23#include "format/binary/XmlFlattener.h"
Adam Lesinski8780eb62017-10-31 17:44:39 -070024#include "format/proto/ProtoDeserialize.h"
Adam Lesinski00451162017-10-03 07:44:08 -070025#include "io/BigBufferStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070026#include "io/Util.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070027#include "xml/XmlDom.h"
Pierre Lecesne2599aa42017-02-01 22:47:03 +000028
Adam Lesinski8780eb62017-10-31 17:44:39 -070029using ::aapt::io::IFile;
30using ::aapt::io::IFileCollection;
31using ::aapt::xml::XmlResource;
32using ::android::StringPiece;
33using ::std::unique_ptr;
34
Pierre Lecesneff759e62017-02-01 00:29:25 +000035namespace aapt {
36
Adam Lesinski8780eb62017-10-31 17:44:39 -070037std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(const StringPiece& path, IDiagnostics* diag) {
Pierre Lecesneff759e62017-02-01 00:29:25 +000038 Source source(path);
39 std::string error;
Adam Lesinski06460ef2017-03-14 18:52:13 -070040 std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::Create(path, &error);
Adam Lesinski8780eb62017-10-31 17:44:39 -070041 if (apk == nullptr) {
42 diag->Error(DiagMessage(path) << "failed opening zip: " << error);
Pierre Lecesneff759e62017-02-01 00:29:25 +000043 return {};
44 }
45
Pierre Lecesnef267a402017-12-01 11:39:01 +000046 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 Lesinski8780eb62017-10-31 17:44:39 -070055 }
Adam Lesinski8780eb62017-10-31 17:44:39 -070056}
57
58std::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 Lecesneff759e62017-02-01 00:29:25 +000063 return {};
64 }
65
Adam Lesinski8780eb62017-10-31 17:44:39 -070066 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
116std::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 Lecesneff759e62017-02-01 00:29:25 +0000128 return {};
129 }
130
131 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
Adam Lesinski8780eb62017-10-31 17:44:39 -0700132 BinaryResourceParser parser(diag, table.get(), source, data->data(), data->size(),
133 collection.get());
Pierre Lecesneff759e62017-02-01 00:29:25 +0000134 if (!parser.Parse()) {
135 return {};
136 }
Shane Farmer3edd4722017-09-01 14:34:22 -0700137
Adam Lesinski8780eb62017-10-31 17:44:39 -0700138 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 Lecesneff759e62017-02-01 00:29:25 +0000160}
161
Adam Lesinskid48944a2017-02-21 14:22:30 -0800162bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
163 IArchiveWriter* writer) {
Shane Farmer57669432017-06-19 12:52:04 -0700164 FilterChain empty;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700165 return WriteToArchive(context, table_.get(), options, &empty, writer);
Shane Farmer57669432017-06-19 12:52:04 -0700166}
167
Shane Farmer0a5b2012017-06-22 12:24:12 -0700168bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table,
169 const TableFlattenerOptions& options, FilterChain* filters,
Shane Farmer3edd4722017-09-01 14:34:22 -0700170 IArchiveWriter* writer, XmlResource* manifest) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000171 std::set<std::string> referenced_resources;
172 // List the files being referenced in the resource table.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700173 for (auto& pkg : split_table->packages) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000174 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 Hsieh4dc58122017-08-03 16:28:10 -0700192 path = path.substr(path.find('@') + 1);
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000193
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 Lecesnefa131d52017-02-03 19:15:03 +0000198 << "Removing resource '" << path << "' from APK.");
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000199 }
200 continue;
201 }
202
Shane Farmer57669432017-06-19 12:52:04 -0700203 if (!filters->Keep(path)) {
204 if (context->IsVerbose()) {
205 context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK.");
206 }
207 continue;
208 }
209
Adam Lesinski06460ef2017-03-14 18:52:13 -0700210 // The resource table needs to be re-serialized since it might have changed.
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000211 if (path == "resources.arsc") {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700212 BigBuffer buffer(4096);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800213 // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
214 // with sparse entries) b/35389232.
Adam Lesinskid48944a2017-02-21 14:22:30 -0800215 TableFlattener flattener(options, &buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700216 if (!flattener.Consume(context, split_table)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000217 return false;
218 }
219
Adam Lesinski06460ef2017-03-14 18:52:13 -0700220 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700221 if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign,
222 writer)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000223 return false;
224 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000225
Shane Farmer3edd4722017-09-01 14:34:22 -0700226 } 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 Lesinskid0f492d2017-04-03 18:12:45 -0700240 } else {
Pierre Lecesned55bef72017-11-10 22:31:01 +0000241 if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700242 return false;
243 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000244 }
245 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000246 return true;
247}
248
Pierre Lecesnef267a402017-12-01 11:39:01 +0000249ApkFormat 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 Lecesneff759e62017-02-01 00:29:25 +0000286} // namespace aapt