blob: 5981401195a4462e846bc9cac3a07792d9fa26ba [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
Adam Lesinski8780eb62017-10-31 17:44:39 -070046 if (apk->FindFile("resources.arsc") != nullptr) {
47 return LoadBinaryApkFromFileCollection(source, std::move(apk), diag);
48 } else if (apk->FindFile("resources.pb") != nullptr) {
49 return LoadProtoApkFromFileCollection(source, std::move(apk), diag);
50 }
51 diag->Error(DiagMessage(path) << "no resource table found");
52 return {};
53}
54
55std::unique_ptr<LoadedApk> LoadedApk::LoadProtoApkFromFileCollection(
56 const Source& source, unique_ptr<io::IFileCollection> collection, IDiagnostics* diag) {
57 io::IFile* table_file = collection->FindFile(kProtoResourceTablePath);
58 if (table_file == nullptr) {
59 diag->Error(DiagMessage(source) << "failed to find " << kProtoResourceTablePath);
Pierre Lecesneff759e62017-02-01 00:29:25 +000060 return {};
61 }
62
Adam Lesinski8780eb62017-10-31 17:44:39 -070063 std::unique_ptr<io::InputStream> in = table_file->OpenInputStream();
64 if (in == nullptr) {
65 diag->Error(DiagMessage(source) << "failed to open " << kProtoResourceTablePath);
66 return {};
67 }
68
69 pb::ResourceTable pb_table;
70 io::ZeroCopyInputAdaptor adaptor(in.get());
71 if (!pb_table.ParseFromZeroCopyStream(&adaptor)) {
72 diag->Error(DiagMessage(source) << "failed to read " << kProtoResourceTablePath);
73 return {};
74 }
75
76 std::string error;
77 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
78 if (!DeserializeTableFromPb(pb_table, collection.get(), table.get(), &error)) {
79 diag->Error(DiagMessage(source)
80 << "failed to deserialize " << kProtoResourceTablePath << ": " << error);
81 return {};
82 }
83
84 io::IFile* manifest_file = collection->FindFile(kAndroidManifestPath);
85 if (manifest_file == nullptr) {
86 diag->Error(DiagMessage(source) << "failed to find " << kAndroidManifestPath);
87 return {};
88 }
89
90 std::unique_ptr<io::InputStream> manifest_in = manifest_file->OpenInputStream();
91 if (manifest_in == nullptr) {
92 diag->Error(DiagMessage(source) << "failed to open " << kAndroidManifestPath);
93 return {};
94 }
95
96 pb::XmlNode pb_node;
97 io::ZeroCopyInputAdaptor manifest_adaptor(manifest_in.get());
98 if (!pb_node.ParseFromZeroCopyStream(&manifest_adaptor)) {
99 diag->Error(DiagMessage(source) << "failed to read proto " << kAndroidManifestPath);
100 return {};
101 }
102
103 std::unique_ptr<xml::XmlResource> manifest = DeserializeXmlResourceFromPb(pb_node, &error);
104 if (manifest == nullptr) {
105 diag->Error(DiagMessage(source)
106 << "failed to deserialize proto " << kAndroidManifestPath << ": " << error);
107 return {};
108 }
109 return util::make_unique<LoadedApk>(source, std::move(collection), std::move(table),
110 std::move(manifest));
111}
112
113std::unique_ptr<LoadedApk> LoadedApk::LoadBinaryApkFromFileCollection(
114 const Source& source, unique_ptr<io::IFileCollection> collection, IDiagnostics* diag) {
115 io::IFile* table_file = collection->FindFile(kApkResourceTablePath);
116 if (table_file == nullptr) {
117 diag->Error(DiagMessage(source) << "failed to find " << kApkResourceTablePath);
118
119 return {};
120 }
121
122 std::unique_ptr<io::IData> data = table_file->OpenAsData();
123 if (data == nullptr) {
124 diag->Error(DiagMessage(source) << "failed to open " << kApkResourceTablePath);
Pierre Lecesneff759e62017-02-01 00:29:25 +0000125 return {};
126 }
127
128 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
Adam Lesinski8780eb62017-10-31 17:44:39 -0700129 BinaryResourceParser parser(diag, table.get(), source, data->data(), data->size(),
130 collection.get());
Pierre Lecesneff759e62017-02-01 00:29:25 +0000131 if (!parser.Parse()) {
132 return {};
133 }
Shane Farmer3edd4722017-09-01 14:34:22 -0700134
Adam Lesinski8780eb62017-10-31 17:44:39 -0700135 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 Lecesneff759e62017-02-01 00:29:25 +0000157}
158
Adam Lesinskid48944a2017-02-21 14:22:30 -0800159bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
160 IArchiveWriter* writer) {
Shane Farmer57669432017-06-19 12:52:04 -0700161 FilterChain empty;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700162 return WriteToArchive(context, table_.get(), options, &empty, writer);
Shane Farmer57669432017-06-19 12:52:04 -0700163}
164
Shane Farmer0a5b2012017-06-22 12:24:12 -0700165bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table,
166 const TableFlattenerOptions& options, FilterChain* filters,
Shane Farmer3edd4722017-09-01 14:34:22 -0700167 IArchiveWriter* writer, XmlResource* manifest) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000168 std::set<std::string> referenced_resources;
169 // List the files being referenced in the resource table.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700170 for (auto& pkg : split_table->packages) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000171 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 Hsieh4dc58122017-08-03 16:28:10 -0700189 path = path.substr(path.find('@') + 1);
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000190
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 Lecesnefa131d52017-02-03 19:15:03 +0000195 << "Removing resource '" << path << "' from APK.");
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000196 }
197 continue;
198 }
199
Shane Farmer57669432017-06-19 12:52:04 -0700200 if (!filters->Keep(path)) {
201 if (context->IsVerbose()) {
202 context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK.");
203 }
204 continue;
205 }
206
Adam Lesinski06460ef2017-03-14 18:52:13 -0700207 // The resource table needs to be re-serialized since it might have changed.
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000208 if (path == "resources.arsc") {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700209 BigBuffer buffer(4096);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800210 // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
211 // with sparse entries) b/35389232.
Adam Lesinskid48944a2017-02-21 14:22:30 -0800212 TableFlattener flattener(options, &buffer);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700213 if (!flattener.Consume(context, split_table)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000214 return false;
215 }
216
Adam Lesinski06460ef2017-03-14 18:52:13 -0700217 io::BigBufferInputStream input_stream(&buffer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700218 if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign,
219 writer)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000220 return false;
221 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000222
Shane Farmer3edd4722017-09-01 14:34:22 -0700223 } else if (manifest != nullptr && path == "AndroidManifest.xml") {
224 BigBuffer buffer(8192);
225 XmlFlattener xml_flattener(&buffer, {});
226 if (!xml_flattener.Consume(context, manifest)) {
227 context->GetDiagnostics()->Error(DiagMessage(path) << "flattening failed");
228 return false;
229 }
230
231 uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
232 io::BigBufferInputStream manifest_buffer_in(&buffer);
233 if (!io::CopyInputStreamToArchive(context, &manifest_buffer_in, path, compression_flags,
234 writer)) {
235 return false;
236 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700237 } else {
Pierre Lecesned55bef72017-11-10 22:31:01 +0000238 if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700239 return false;
240 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000241 }
242 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000243 return true;
244}
245
Pierre Lecesneff759e62017-02-01 00:29:25 +0000246} // namespace aapt