blob: 4a4260d1d3231aad71cd4c5b7f434a7209cf83e2 [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"
corysmith@google.comf7db43e2018-03-13 12:04:10 -040025#include "format/proto/ProtoSerialize.h"
Adam Lesinski00451162017-10-03 07:44:08 -070026#include "io/BigBufferStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070027#include "io/Util.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070028#include "xml/XmlDom.h"
Pierre Lecesne2599aa42017-02-01 22:47:03 +000029
Adam Lesinski8780eb62017-10-31 17:44:39 -070030using ::aapt::io::IFile;
31using ::aapt::io::IFileCollection;
32using ::aapt::xml::XmlResource;
33using ::android::StringPiece;
34using ::std::unique_ptr;
35
Pierre Lecesneff759e62017-02-01 00:29:25 +000036namespace aapt {
37
Adam Lesinski8780eb62017-10-31 17:44:39 -070038std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(const StringPiece& path, IDiagnostics* diag) {
Pierre Lecesneff759e62017-02-01 00:29:25 +000039 Source source(path);
40 std::string error;
Adam Lesinski06460ef2017-03-14 18:52:13 -070041 std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::Create(path, &error);
Adam Lesinski8780eb62017-10-31 17:44:39 -070042 if (apk == nullptr) {
43 diag->Error(DiagMessage(path) << "failed opening zip: " << error);
Pierre Lecesneff759e62017-02-01 00:29:25 +000044 return {};
45 }
46
Pierre Lecesnef267a402017-12-01 11:39:01 +000047 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 Lesinski8780eb62017-10-31 17:44:39 -070056 }
Adam Lesinski8780eb62017-10-31 17:44:39 -070057}
58
59std::unique_ptr<LoadedApk> LoadedApk::LoadProtoApkFromFileCollection(
60 const Source& source, unique_ptr<io::IFileCollection> collection, IDiagnostics* diag) {
Tom Dobek725fb122017-12-08 14:19:01 +000061 std::unique_ptr<ResourceTable> table;
62
Adam Lesinski8780eb62017-10-31 17:44:39 -070063 io::IFile* table_file = collection->FindFile(kProtoResourceTablePath);
Tom Dobek725fb122017-12-08 14:19:01 +000064 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 Lecesneff759e62017-02-01 00:29:25 +000071
Tom Dobek725fb122017-12-08 14:19:01 +000072 io::ZeroCopyInputAdaptor adaptor(in.get());
73 if (!pb_table.ParseFromZeroCopyStream(&adaptor)) {
74 diag->Error(DiagMessage(source) << "failed to read " << kProtoResourceTablePath);
75 return {};
76 }
Adam Lesinski8780eb62017-10-31 17:44:39 -070077
Tom Dobek725fb122017-12-08 14:19:01 +000078 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 Lesinski8780eb62017-10-31 17:44:39 -070085 }
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 Dobek725fb122017-12-08 14:19:01 +0000106 std::string error;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700107 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.comf7db43e2018-03-13 12:04:10 -0400114 std::move(manifest), ApkFormat::kProto);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700115}
116
117std::unique_ptr<LoadedApk> LoadedApk::LoadBinaryApkFromFileCollection(
118 const Source& source, unique_ptr<io::IFileCollection> collection, IDiagnostics* diag) {
Tom Dobek725fb122017-12-08 14:19:01 +0000119 std::unique_ptr<ResourceTable> table;
120
Adam Lesinski8780eb62017-10-31 17:44:39 -0700121 io::IFile* table_file = collection->FindFile(kApkResourceTablePath);
Tom Dobek725fb122017-12-08 14:19:01 +0000122 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 Lecesneff759e62017-02-01 00:29:25 +0000134 }
Shane Farmer3edd4722017-09-01 14:34:22 -0700135
Adam Lesinski8780eb62017-10-31 17:44:39 -0700136 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.comf7db43e2018-03-13 12:04:10 -0400157 std::move(manifest), ApkFormat::kBinary);
Pierre Lecesneff759e62017-02-01 00:29:25 +0000158}
159
Adam Lesinskid48944a2017-02-21 14:22:30 -0800160bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
161 IArchiveWriter* writer) {
Shane Farmer57669432017-06-19 12:52:04 -0700162 FilterChain empty;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700163 return WriteToArchive(context, table_.get(), options, &empty, writer);
Shane Farmer57669432017-06-19 12:52:04 -0700164}
165
Shane Farmer0a5b2012017-06-22 12:24:12 -0700166bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table,
167 const TableFlattenerOptions& options, FilterChain* filters,
Shane Farmer3edd4722017-09-01 14:34:22 -0700168 IArchiveWriter* writer, XmlResource* manifest) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000169 std::set<std::string> referenced_resources;
170 // List the files being referenced in the resource table.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700171 for (auto& pkg : split_table->packages) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000172 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 Hsieh4dc58122017-08-03 16:28:10 -0700190 path = path.substr(path.find('@') + 1);
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000191
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 Lecesnefa131d52017-02-03 19:15:03 +0000196 << "Removing resource '" << path << "' from APK.");
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000197 }
198 continue;
199 }
200
Shane Farmer57669432017-06-19 12:52:04 -0700201 if (!filters->Keep(path)) {
202 if (context->IsVerbose()) {
203 context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK.");
204 }
205 continue;
206 }
207
Adam Lesinski06460ef2017-03-14 18:52:13 -0700208 // The resource table needs to be re-serialized since it might have changed.
corysmith@google.comf7db43e2018-03-13 12:04:10 -0400209 if (format_ == ApkFormat::kBinary && path == kApkResourceTablePath) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700210 BigBuffer buffer(4096);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800211 // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
212 // with sparse entries) b/35389232.
Adam Lesinskid48944a2017-02-21 14:22:30 -0800213 TableFlattener flattener(options, &buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700214 if (!flattener.Consume(context, split_table)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000215 return false;
216 }
217
Adam Lesinski06460ef2017-03-14 18:52:13 -0700218 io::BigBufferInputStream input_stream(&buffer);
corysmith@google.comf7db43e2018-03-13 12:04:10 -0400219 if (!io::CopyInputStreamToArchive(context,
220 &input_stream,
221 path,
222 ArchiveEntry::kAlign,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700223 writer)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000224 return false;
225 }
corysmith@google.comf7db43e2018-03-13 12:04:10 -0400226 } else if (format_ == ApkFormat::kProto && path == kProtoResourceTablePath) {
227 pb::ResourceTable pb_table;
228 SerializeTableToPb(*split_table, &pb_table);
229 if (!io::CopyProtoToArchive(context,
230 &pb_table,
231 path,
232 ArchiveEntry::kAlign, writer)) {
233 return false;
234 }
Shane Farmer3edd4722017-09-01 14:34:22 -0700235 } else if (manifest != nullptr && path == "AndroidManifest.xml") {
236 BigBuffer buffer(8192);
Shane Farmerd05b9132018-02-14 15:40:35 -0800237 XmlFlattenerOptions xml_flattener_options;
238 xml_flattener_options.use_utf16 = true;
239 XmlFlattener xml_flattener(&buffer, xml_flattener_options);
Shane Farmer3edd4722017-09-01 14:34:22 -0700240 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 Lesinskid0f492d2017-04-03 18:12:45 -0700251 } else {
Pierre Lecesned55bef72017-11-10 22:31:01 +0000252 if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700253 return false;
254 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000255 }
256 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000257 return true;
258}
259
Pierre Lecesnef267a402017-12-01 11:39:01 +0000260ApkFormat LoadedApk::DetermineApkFormat(io::IFileCollection* apk) {
corysmith@google.comf7db43e2018-03-13 12:04:10 -0400261 if (apk->FindFile(kApkResourceTablePath) != nullptr) {
Pierre Lecesnef267a402017-12-01 11:39:01 +0000262 return ApkFormat::kBinary;
corysmith@google.comf7db43e2018-03-13 12:04:10 -0400263 } else if (apk->FindFile(kProtoResourceTablePath) != nullptr) {
Pierre Lecesnef267a402017-12-01 11:39:01 +0000264 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 Lecesneff759e62017-02-01 00:29:25 +0000297} // namespace aapt