blob: d57eaa1ba145e1c82843e3f1ef3469c639b72368 [file] [log] [blame]
Adam Lesinski8780eb62017-10-31 17:44:39 -07001/*
2 * Copyright (C) 2017 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
Ryan Mitchell833a1a62018-07-10 13:51:36 -070017#include "Convert.h"
18
Adam Lesinski8780eb62017-10-31 17:44:39 -070019#include <vector>
20
21#include "android-base/macros.h"
22#include "androidfw/StringPiece.h"
23
Adam Lesinski8780eb62017-10-31 17:44:39 -070024#include "LoadedApk.h"
25#include "ValueVisitor.h"
26#include "cmd/Util.h"
27#include "format/binary/TableFlattener.h"
28#include "format/binary/XmlFlattener.h"
29#include "format/proto/ProtoDeserialize.h"
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000030#include "format/proto/ProtoSerialize.h"
Adam Lesinski8780eb62017-10-31 17:44:39 -070031#include "io/BigBufferStream.h"
32#include "io/Util.h"
33#include "process/IResourceTableConsumer.h"
34#include "process/SymbolTable.h"
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000035#include "util/Util.h"
Adam Lesinski8780eb62017-10-31 17:44:39 -070036
37using ::android::StringPiece;
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000038using ::android::base::StringPrintf;
Adam Lesinski8780eb62017-10-31 17:44:39 -070039using ::std::unique_ptr;
40using ::std::vector;
41
42namespace aapt {
43
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000044class IApkSerializer {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000045 public:
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000046 IApkSerializer(IAaptContext* context, const Source& source) : context_(context), source_(source) {}
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000047
48 virtual bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
49 IArchiveWriter* writer) = 0;
50 virtual bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) = 0;
David Chaloupkab66db4e2018-01-15 12:35:41 +000051 virtual bool SerializeFile(FileReference* file, IArchiveWriter* writer) = 0;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000052
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000053 virtual ~IApkSerializer() = default;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000054
55 protected:
56 IAaptContext* context_;
57 Source source_;
58};
Adam Lesinski8780eb62017-10-31 17:44:39 -070059
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000060bool ConvertApk(IAaptContext* context, unique_ptr<LoadedApk> apk, IApkSerializer* serializer,
61 IArchiveWriter* writer) {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000062 if (!serializer->SerializeXml(apk->GetManifest(), kAndroidManifestPath, true /*utf16*/, writer)) {
63 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
64 << "failed to serialize AndroidManifest.xml");
Adam Lesinski8780eb62017-10-31 17:44:39 -070065 return false;
66 }
67
Tom Dobek725fb122017-12-08 14:19:01 +000068 if (apk->GetResourceTable() != nullptr) {
David Chaloupkab66db4e2018-01-15 12:35:41 +000069 // The table might be modified by below code.
70 auto converted_table = apk->GetResourceTable();
Tom Dobek725fb122017-12-08 14:19:01 +000071
72 // Resources
David Chaloupkab66db4e2018-01-15 12:35:41 +000073 for (const auto& package : converted_table->packages) {
Tom Dobek725fb122017-12-08 14:19:01 +000074 for (const auto& type : package->types) {
75 for (const auto& entry : type->entries) {
76 for (const auto& config_value : entry->values) {
David Chaloupkab66db4e2018-01-15 12:35:41 +000077 FileReference* file = ValueCast<FileReference>(config_value->value.get());
Tom Dobek725fb122017-12-08 14:19:01 +000078 if (file != nullptr) {
79 if (file->file == nullptr) {
80 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
81 << "no file associated with " << *file);
82 return false;
83 }
84
85 if (!serializer->SerializeFile(file, writer)) {
86 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
87 << "failed to serialize file " << *file->path);
88 return false;
89 }
90 } // file
91 } // config_value
92 } // entry
93 } // type
94 } // package
David Chaloupkab66db4e2018-01-15 12:35:41 +000095
96 // Converted resource table
97 if (!serializer->SerializeTable(converted_table, writer)) {
98 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
99 << "failed to serialize the resource table");
100 return false;
101 }
Adam Lesinski8780eb62017-10-31 17:44:39 -0700102 }
103
Pierre Lecesned55bef72017-11-10 22:31:01 +0000104 // Other files
105 std::unique_ptr<io::IFileCollectionIterator> iterator = apk->GetFileCollection()->Iterator();
106 while (iterator->HasNext()) {
107 io::IFile* file = iterator->Next();
108
109 std::string path = file->GetSource().path;
110 // The name of the path has the format "<zip-file-name>@<path-to-file>".
111 path = path.substr(path.find('@') + 1);
112
113 // Manifest, resource table and resources have already been taken care of.
114 if (path == kAndroidManifestPath ||
115 path == kApkResourceTablePath ||
116 path == kProtoResourceTablePath ||
117 path.find("res/") == 0) {
118 continue;
119 }
120
121 if (!io::CopyFileToArchivePreserveCompression(context, file, path, writer)) {
122 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
123 << "failed to copy file " << path);
124 return false;
125 }
126 }
127
Adam Lesinski8780eb62017-10-31 17:44:39 -0700128 return true;
129}
130
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000131
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000132class BinaryApkSerializer : public IApkSerializer {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000133 public:
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000134 BinaryApkSerializer(IAaptContext* context, const Source& source,
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000135 const TableFlattenerOptions& options)
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000136 : IApkSerializer(context, source), tableFlattenerOptions_(options) {}
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000137
138 bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000139 IArchiveWriter* writer) override {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000140 BigBuffer buffer(4096);
141 XmlFlattenerOptions options = {};
142 options.use_utf16 = utf16;
Adam Lesinskibbf42972018-02-14 13:36:09 -0800143 options.keep_raw_values = true;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000144 XmlFlattener flattener(&buffer, options);
145 if (!flattener.Consume(context_, xml)) {
146 return false;
147 }
148
149 io::BigBufferInputStream input_stream(&buffer);
150 return io::CopyInputStreamToArchive(context_, &input_stream, path, ArchiveEntry::kCompress,
151 writer);
152 }
153
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000154 bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) override {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000155 BigBuffer buffer(4096);
156 TableFlattener table_flattener(tableFlattenerOptions_, &buffer);
157 if (!table_flattener.Consume(context_, table)) {
158 return false;
159 }
160
161 io::BigBufferInputStream input_stream(&buffer);
162 return io::CopyInputStreamToArchive(context_, &input_stream, kApkResourceTablePath,
163 ArchiveEntry::kAlign, writer);
164 }
165
David Chaloupkab66db4e2018-01-15 12:35:41 +0000166 bool SerializeFile(FileReference* file, IArchiveWriter* writer) override {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000167 if (file->type == ResourceFile::Type::kProtoXml) {
168 unique_ptr<io::InputStream> in = file->file->OpenInputStream();
169 if (in == nullptr) {
170 context_->GetDiagnostics()->Error(DiagMessage(source_)
171 << "failed to open file " << *file->path);
172 return false;
173 }
174
175 pb::XmlNode pb_node;
176 io::ZeroCopyInputAdaptor adaptor(in.get());
177 if (!pb_node.ParseFromZeroCopyStream(&adaptor)) {
178 context_->GetDiagnostics()->Error(DiagMessage(source_)
179 << "failed to parse proto XML " << *file->path);
180 return false;
181 }
182
183 std::string error;
184 unique_ptr<xml::XmlResource> xml = DeserializeXmlResourceFromPb(pb_node, &error);
185 if (xml == nullptr) {
186 context_->GetDiagnostics()->Error(DiagMessage(source_)
187 << "failed to deserialize proto XML "
188 << *file->path << ": " << error);
189 return false;
190 }
191
192 if (!SerializeXml(xml.get(), *file->path, false /*utf16*/, writer)) {
193 context_->GetDiagnostics()->Error(DiagMessage(source_)
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000194 << "failed to serialize to binary XML: " << *file->path);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000195 return false;
196 }
David Chaloupkab66db4e2018-01-15 12:35:41 +0000197
198 file->type = ResourceFile::Type::kBinaryXml;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000199 } else {
Pierre Lecesned55bef72017-11-10 22:31:01 +0000200 if (!io::CopyFileToArchivePreserveCompression(context_, file->file, *file->path, writer)) {
201 context_->GetDiagnostics()->Error(DiagMessage(source_)
202 << "failed to copy file " << *file->path);
203 return false;
204 }
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000205 }
206
207 return true;
208 }
209
210 private:
211 TableFlattenerOptions tableFlattenerOptions_;
212
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000213 DISALLOW_COPY_AND_ASSIGN(BinaryApkSerializer);
214};
215
216class ProtoApkSerializer : public IApkSerializer {
217 public:
218 ProtoApkSerializer(IAaptContext* context, const Source& source)
219 : IApkSerializer(context, source) {}
220
221 bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
222 IArchiveWriter* writer) override {
223 pb::XmlNode pb_node;
224 SerializeXmlResourceToPb(*xml, &pb_node);
225 return io::CopyProtoToArchive(context_, &pb_node, path, ArchiveEntry::kCompress, writer);
226 }
227
228 bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) override {
229 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700230 SerializeTableToPb(*table, &pb_table, context_->GetDiagnostics());
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000231 return io::CopyProtoToArchive(context_, &pb_table, kProtoResourceTablePath,
232 ArchiveEntry::kCompress, writer);
233 }
234
David Chaloupkab66db4e2018-01-15 12:35:41 +0000235 bool SerializeFile(FileReference* file, IArchiveWriter* writer) override {
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000236 if (file->type == ResourceFile::Type::kBinaryXml) {
237 std::unique_ptr<io::IData> data = file->file->OpenAsData();
238 if (!data) {
239 context_->GetDiagnostics()->Error(DiagMessage(source_)
240 << "failed to open file " << *file->path);
241 return false;
242 }
243
244 std::string error;
245 std::unique_ptr<xml::XmlResource> xml = xml::Inflate(data->data(), data->size(), &error);
246 if (xml == nullptr) {
247 context_->GetDiagnostics()->Error(DiagMessage(source_) << "failed to parse binary XML: "
248 << error);
249 return false;
250 }
251
252 if (!SerializeXml(xml.get(), *file->path, false /*utf16*/, writer)) {
253 context_->GetDiagnostics()->Error(DiagMessage(source_)
254 << "failed to serialize to proto XML: " << *file->path);
255 return false;
256 }
David Chaloupkab66db4e2018-01-15 12:35:41 +0000257
258 file->type = ResourceFile::Type::kProtoXml;
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000259 } else {
260 if (!io::CopyFileToArchivePreserveCompression(context_, file->file, *file->path, writer)) {
261 context_->GetDiagnostics()->Error(DiagMessage(source_)
262 << "failed to copy file " << *file->path);
263 return false;
264 }
265 }
266
267 return true;
268 }
269
270 private:
271 DISALLOW_COPY_AND_ASSIGN(ProtoApkSerializer);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000272};
273
Adam Lesinski8780eb62017-10-31 17:44:39 -0700274class Context : public IAaptContext {
275 public:
276 Context() : mangler_({}), symbols_(&mangler_) {
277 }
278
279 PackageType GetPackageType() override {
280 return PackageType::kApp;
281 }
282
283 SymbolTable* GetExternalSymbols() override {
284 return &symbols_;
285 }
286
287 IDiagnostics* GetDiagnostics() override {
288 return &diag_;
289 }
290
291 const std::string& GetCompilationPackage() override {
292 return package_;
293 }
294
295 uint8_t GetPackageId() override {
296 // Nothing should call this.
297 UNIMPLEMENTED(FATAL) << "PackageID should not be necessary";
298 return 0;
299 }
300
301 NameMangler* GetNameMangler() override {
302 UNIMPLEMENTED(FATAL);
303 return nullptr;
304 }
305
306 bool IsVerbose() override {
307 return verbose_;
308 }
309
310 int GetMinSdkVersion() override {
311 return 0u;
312 }
313
314 bool verbose_ = false;
315 std::string package_;
316
317 private:
318 DISALLOW_COPY_AND_ASSIGN(Context);
319
320 NameMangler mangler_;
321 SymbolTable symbols_;
322 StdErrDiagnostics diag_;
323};
324
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700325const char* ConvertCommand::kOutputFormatProto = "proto";
326const char* ConvertCommand::kOutputFormatBinary = "binary";
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000327
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700328int ConvertCommand::Action(const std::vector<std::string>& args) {
329 if (args.size() != 1) {
330 std::cerr << "must supply a single proto APK\n";
331 Usage(&std::cerr);
332 return 1;
333 }
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000334
Adam Lesinski8780eb62017-10-31 17:44:39 -0700335 Context context;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700336 const StringPiece& path = args[0];
Adam Lesinski8780eb62017-10-31 17:44:39 -0700337 unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(path, context.GetDiagnostics());
338 if (apk == nullptr) {
339 context.GetDiagnostics()->Error(DiagMessage(path) << "failed to load APK");
340 return 1;
341 }
342
343 Maybe<AppInfo> app_info =
344 ExtractAppInfoFromBinaryManifest(*apk->GetManifest(), context.GetDiagnostics());
345 if (!app_info) {
346 return 1;
347 }
348
349 context.package_ = app_info.value().package;
350
351 unique_ptr<IArchiveWriter> writer =
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700352 CreateZipFileArchiveWriter(context.GetDiagnostics(), output_path_);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700353 if (writer == nullptr) {
354 return 1;
355 }
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000356
357 unique_ptr<IApkSerializer> serializer;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700358 if (!output_format_ || output_format_.value() == ConvertCommand::kOutputFormatBinary) {
359
360 serializer.reset(new BinaryApkSerializer(&context, apk->GetSource(), options_));
361 } else if (output_format_.value() == ConvertCommand::kOutputFormatProto) {
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000362 serializer.reset(new ProtoApkSerializer(&context, apk->GetSource()));
363 } else {
364 context.GetDiagnostics()->Error(DiagMessage(path)
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700365 << "Invalid value for flag --output-format: "
366 << output_format_.value());
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000367 return 1;
368 }
369
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000370 return ConvertApk(&context, std::move(apk), serializer.get(), writer.get()) ? 0 : 1;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700371}
372
373} // namespace aapt