blob: d8c541d96a05e63d108f949dd799078dd385354a [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
Adam Lesinski8780eb62017-10-31 17:44:39 -070021#include "LoadedApk.h"
22#include "ValueVisitor.h"
Iurii Makhno549938a2022-04-26 19:36:51 +000023#include "android-base/macros.h"
24#include "androidfw/StringPiece.h"
Adam Lesinski8780eb62017-10-31 17:44:39 -070025#include "cmd/Util.h"
26#include "format/binary/TableFlattener.h"
27#include "format/binary/XmlFlattener.h"
28#include "format/proto/ProtoDeserialize.h"
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000029#include "format/proto/ProtoSerialize.h"
Adam Lesinski8780eb62017-10-31 17:44:39 -070030#include "io/BigBufferStream.h"
31#include "io/Util.h"
32#include "process/IResourceTableConsumer.h"
33#include "process/SymbolTable.h"
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000034#include "util/Util.h"
Adam Lesinski8780eb62017-10-31 17:44:39 -070035
36using ::android::StringPiece;
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000037using ::android::base::StringPrintf;
Adam Lesinski8780eb62017-10-31 17:44:39 -070038using ::std::unique_ptr;
39using ::std::vector;
40
41namespace aapt {
42
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000043class IApkSerializer {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000044 public:
Ryan Mitchell90b7a082019-02-15 17:39:58 +000045 IApkSerializer(IAaptContext* context, const Source& source) : context_(context),
46 source_(source) {}
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000047
48 virtual bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
Ryan Mitchell05aebf42018-10-09 15:08:41 -070049 IArchiveWriter* writer, uint32_t compression_flags) = 0;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000050 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 +000060class BinaryApkSerializer : public IApkSerializer {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000061 public:
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000062 BinaryApkSerializer(IAaptContext* context, const Source& source,
Ryan Mitchell479fa392019-01-02 17:15:39 -080063 const TableFlattenerOptions& table_flattener_options,
64 const XmlFlattenerOptions& xml_flattener_options)
65 : IApkSerializer(context, source),
66 table_flattener_options_(table_flattener_options),
67 xml_flattener_options_(xml_flattener_options) {}
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000068
69 bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
Ryan Mitchell05aebf42018-10-09 15:08:41 -070070 IArchiveWriter* writer, uint32_t compression_flags) override {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000071 BigBuffer buffer(4096);
Ryan Mitchell479fa392019-01-02 17:15:39 -080072 xml_flattener_options_.use_utf16 = utf16;
73 XmlFlattener flattener(&buffer, xml_flattener_options_);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000074 if (!flattener.Consume(context_, xml)) {
75 return false;
76 }
77
78 io::BigBufferInputStream input_stream(&buffer);
Ryan Mitchell05aebf42018-10-09 15:08:41 -070079 return io::CopyInputStreamToArchive(context_, &input_stream, path, compression_flags, writer);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000080 }
81
Pierre Lecesned8b4bea2017-11-10 23:50:17 +000082 bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) override {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000083 BigBuffer buffer(4096);
Ryan Mitchell479fa392019-01-02 17:15:39 -080084 TableFlattener table_flattener(table_flattener_options_, &buffer);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000085 if (!table_flattener.Consume(context_, table)) {
86 return false;
87 }
88
89 io::BigBufferInputStream input_stream(&buffer);
90 return io::CopyInputStreamToArchive(context_, &input_stream, kApkResourceTablePath,
91 ArchiveEntry::kAlign, writer);
92 }
93
David Chaloupkab66db4e2018-01-15 12:35:41 +000094 bool SerializeFile(FileReference* file, IArchiveWriter* writer) override {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +000095 if (file->type == ResourceFile::Type::kProtoXml) {
96 unique_ptr<io::InputStream> in = file->file->OpenInputStream();
97 if (in == nullptr) {
98 context_->GetDiagnostics()->Error(DiagMessage(source_)
99 << "failed to open file " << *file->path);
100 return false;
101 }
102
103 pb::XmlNode pb_node;
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700104 io::ProtoInputStreamReader proto_reader(in.get());
105 if (!proto_reader.ReadMessage(&pb_node)) {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000106 context_->GetDiagnostics()->Error(DiagMessage(source_)
107 << "failed to parse proto XML " << *file->path);
108 return false;
109 }
110
111 std::string error;
112 unique_ptr<xml::XmlResource> xml = DeserializeXmlResourceFromPb(pb_node, &error);
113 if (xml == nullptr) {
114 context_->GetDiagnostics()->Error(DiagMessage(source_)
115 << "failed to deserialize proto XML "
116 << *file->path << ": " << error);
117 return false;
118 }
119
Ryan Mitchell05aebf42018-10-09 15:08:41 -0700120 if (!SerializeXml(xml.get(), *file->path, false /*utf16*/, writer,
121 file->file->WasCompressed() ? ArchiveEntry::kCompress : 0u)) {
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000122 context_->GetDiagnostics()->Error(DiagMessage(source_)
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000123 << "failed to serialize to binary XML: " << *file->path);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000124 return false;
125 }
David Chaloupkab66db4e2018-01-15 12:35:41 +0000126
127 file->type = ResourceFile::Type::kBinaryXml;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000128 } else {
Pierre Lecesned55bef72017-11-10 22:31:01 +0000129 if (!io::CopyFileToArchivePreserveCompression(context_, file->file, *file->path, writer)) {
130 context_->GetDiagnostics()->Error(DiagMessage(source_)
131 << "failed to copy file " << *file->path);
132 return false;
133 }
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000134 }
135
136 return true;
137 }
138
139 private:
Ryan Mitchell479fa392019-01-02 17:15:39 -0800140 TableFlattenerOptions table_flattener_options_;
141 XmlFlattenerOptions xml_flattener_options_;
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000142
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000143 DISALLOW_COPY_AND_ASSIGN(BinaryApkSerializer);
144};
145
146class ProtoApkSerializer : public IApkSerializer {
147 public:
148 ProtoApkSerializer(IAaptContext* context, const Source& source)
149 : IApkSerializer(context, source) {}
150
151 bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
Ryan Mitchell05aebf42018-10-09 15:08:41 -0700152 IArchiveWriter* writer, uint32_t compression_flags) override {
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000153 pb::XmlNode pb_node;
154 SerializeXmlResourceToPb(*xml, &pb_node);
Ryan Mitchell05aebf42018-10-09 15:08:41 -0700155 return io::CopyProtoToArchive(context_, &pb_node, path, compression_flags, writer);
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000156 }
157
158 bool SerializeTable(ResourceTable* table, IArchiveWriter* writer) override {
159 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700160 SerializeTableToPb(*table, &pb_table, context_->GetDiagnostics());
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000161 return io::CopyProtoToArchive(context_, &pb_table, kProtoResourceTablePath,
162 ArchiveEntry::kCompress, writer);
163 }
164
David Chaloupkab66db4e2018-01-15 12:35:41 +0000165 bool SerializeFile(FileReference* file, IArchiveWriter* writer) override {
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000166 if (file->type == ResourceFile::Type::kBinaryXml) {
167 std::unique_ptr<io::IData> data = file->file->OpenAsData();
168 if (!data) {
169 context_->GetDiagnostics()->Error(DiagMessage(source_)
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000170 << "failed to open file " << *file->path);
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000171 return false;
172 }
173
174 std::string error;
175 std::unique_ptr<xml::XmlResource> xml = xml::Inflate(data->data(), data->size(), &error);
176 if (xml == nullptr) {
177 context_->GetDiagnostics()->Error(DiagMessage(source_) << "failed to parse binary XML: "
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000178 << error);
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000179 return false;
180 }
181
Ryan Mitchell05aebf42018-10-09 15:08:41 -0700182 if (!SerializeXml(xml.get(), *file->path, false /*utf16*/, writer,
183 file->file->WasCompressed() ? ArchiveEntry::kCompress : 0u)) {
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000184 context_->GetDiagnostics()->Error(DiagMessage(source_)
185 << "failed to serialize to proto XML: " << *file->path);
186 return false;
187 }
David Chaloupkab66db4e2018-01-15 12:35:41 +0000188
189 file->type = ResourceFile::Type::kProtoXml;
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000190 } else {
191 if (!io::CopyFileToArchivePreserveCompression(context_, file->file, *file->path, writer)) {
192 context_->GetDiagnostics()->Error(DiagMessage(source_)
193 << "failed to copy file " << *file->path);
194 return false;
195 }
196 }
197
198 return true;
199 }
200
201 private:
202 DISALLOW_COPY_AND_ASSIGN(ProtoApkSerializer);
Pierre Lecesne7e8549d2017-11-10 01:22:12 +0000203};
204
Adam Lesinski8780eb62017-10-31 17:44:39 -0700205class Context : public IAaptContext {
206 public:
207 Context() : mangler_({}), symbols_(&mangler_) {
208 }
209
210 PackageType GetPackageType() override {
211 return PackageType::kApp;
212 }
213
214 SymbolTable* GetExternalSymbols() override {
215 return &symbols_;
216 }
217
218 IDiagnostics* GetDiagnostics() override {
219 return &diag_;
220 }
221
222 const std::string& GetCompilationPackage() override {
223 return package_;
224 }
225
226 uint8_t GetPackageId() override {
227 // Nothing should call this.
228 UNIMPLEMENTED(FATAL) << "PackageID should not be necessary";
229 return 0;
230 }
231
232 NameMangler* GetNameMangler() override {
233 UNIMPLEMENTED(FATAL);
234 return nullptr;
235 }
236
237 bool IsVerbose() override {
238 return verbose_;
239 }
240
241 int GetMinSdkVersion() override {
Iurii Makhno549938a2022-04-26 19:36:51 +0000242 return min_sdk_;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700243 }
244
Udam Sainib228df32019-06-18 16:50:34 -0700245 const std::set<std::string>& GetSplitNameDependencies() override {
246 UNIMPLEMENTED(FATAL) << "Split Name Dependencies should not be necessary";
247 static std::set<std::string> empty;
248 return empty;
249 }
250
Adam Lesinski8780eb62017-10-31 17:44:39 -0700251 bool verbose_ = false;
252 std::string package_;
Iurii Makhno549938a2022-04-26 19:36:51 +0000253 int32_t min_sdk_ = 0;
Adam Lesinski8780eb62017-10-31 17:44:39 -0700254
255 private:
256 DISALLOW_COPY_AND_ASSIGN(Context);
257
258 NameMangler mangler_;
259 SymbolTable symbols_;
260 StdErrDiagnostics diag_;
261};
262
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800263int Convert(IAaptContext* context, LoadedApk* apk, IArchiveWriter* output_writer,
Ryan Mitchell479fa392019-01-02 17:15:39 -0800264 ApkFormat output_format, TableFlattenerOptions table_flattener_options,
265 XmlFlattenerOptions xml_flattener_options) {
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800266 unique_ptr<IApkSerializer> serializer;
267 if (output_format == ApkFormat::kBinary) {
Ryan Mitchell479fa392019-01-02 17:15:39 -0800268 serializer.reset(new BinaryApkSerializer(context, apk->GetSource(), table_flattener_options,
269 xml_flattener_options));
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800270 } else if (output_format == ApkFormat::kProto) {
271 serializer.reset(new ProtoApkSerializer(context, apk->GetSource()));
272 } else {
273 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
274 << "Cannot convert APK to unknown format");
275 return 1;
276 }
277
278 io::IFile* manifest = apk->GetFileCollection()->FindFile(kAndroidManifestPath);
279 if (!serializer->SerializeXml(apk->GetManifest(), kAndroidManifestPath, true /*utf16*/,
280 output_writer, (manifest != nullptr && manifest->WasCompressed())
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000281 ? ArchiveEntry::kCompress : 0u)) {
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800282 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
283 << "failed to serialize AndroidManifest.xml");
284 return 1;
285 }
286
287 if (apk->GetResourceTable() != nullptr) {
288 // The table might be modified by below code.
289 auto converted_table = apk->GetResourceTable();
290
Winsonf54c9a12019-01-23 12:39:40 -0800291 std::unordered_set<std::string> files_written;
292
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800293 // Resources
294 for (const auto& package : converted_table->packages) {
295 for (const auto& type : package->types) {
296 for (const auto& entry : type->entries) {
297 for (const auto& config_value : entry->values) {
298 FileReference* file = ValueCast<FileReference>(config_value->value.get());
299 if (file != nullptr) {
300 if (file->file == nullptr) {
301 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
302 << "no file associated with " << *file);
303 return 1;
304 }
305
Winsonf54c9a12019-01-23 12:39:40 -0800306 // Only serialize if we haven't seen this file before
307 if (files_written.insert(*file->path).second) {
308 if (!serializer->SerializeFile(file, output_writer)) {
309 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000310 << "failed to serialize file " << *file->path);
Winsonf54c9a12019-01-23 12:39:40 -0800311 return 1;
312 }
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800313 }
314 } // file
315 } // config_value
316 } // entry
317 } // type
318 } // package
319
320 // Converted resource table
321 if (!serializer->SerializeTable(converted_table, output_writer)) {
322 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
323 << "failed to serialize the resource table");
324 return 1;
325 }
326 }
327
328 // Other files
329 std::unique_ptr<io::IFileCollectionIterator> iterator = apk->GetFileCollection()->Iterator();
330 while (iterator->HasNext()) {
331 io::IFile* file = iterator->Next();
332 std::string path = file->GetSource().path;
333
334 // Manifest, resource table and resources have already been taken care of.
335 if (path == kAndroidManifestPath ||
336 path == kApkResourceTablePath ||
337 path == kProtoResourceTablePath ||
338 path.find("res/") == 0) {
339 continue;
340 }
341
342 if (!io::CopyFileToArchivePreserveCompression(context, file, path, output_writer)) {
343 context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000344 << "failed to copy file " << path);
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800345 return 1;
346 }
347 }
348
349 return 0;
350}
351
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700352const char* ConvertCommand::kOutputFormatProto = "proto";
353const char* ConvertCommand::kOutputFormatBinary = "binary";
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000354
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700355int ConvertCommand::Action(const std::vector<std::string>& args) {
356 if (args.size() != 1) {
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800357 std::cerr << "must supply a single APK\n";
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700358 Usage(&std::cerr);
359 return 1;
360 }
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000361
Adam Lesinski8780eb62017-10-31 17:44:39 -0700362 Context context;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700363 const StringPiece& path = args[0];
Adam Lesinski8780eb62017-10-31 17:44:39 -0700364 unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(path, context.GetDiagnostics());
365 if (apk == nullptr) {
366 context.GetDiagnostics()->Error(DiagMessage(path) << "failed to load APK");
367 return 1;
368 }
369
Ryan Mitchell4382e442021-07-14 12:53:01 -0700370 auto app_info = ExtractAppInfoFromBinaryManifest(*apk->GetManifest(), context.GetDiagnostics());
Adam Lesinski8780eb62017-10-31 17:44:39 -0700371 if (!app_info) {
372 return 1;
373 }
374
375 context.package_ = app_info.value().package;
Iurii Makhno549938a2022-04-26 19:36:51 +0000376 context.min_sdk_ = app_info.value().min_sdk_version.value_or(0);
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800377 unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(context.GetDiagnostics(),
378 output_path_);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700379 if (writer == nullptr) {
380 return 1;
381 }
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000382
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800383 ApkFormat format;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700384 if (!output_format_ || output_format_.value() == ConvertCommand::kOutputFormatBinary) {
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800385 format = ApkFormat::kBinary;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700386 } else if (output_format_.value() == ConvertCommand::kOutputFormatProto) {
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800387 format = ApkFormat::kProto;
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000388 } else {
Ryan Mitchell4e9a9222018-11-13 10:40:07 -0800389 context.GetDiagnostics()->Error(DiagMessage(path) << "Invalid value for flag --output-format: "
390 << output_format_.value());
Pierre Lecesned8b4bea2017-11-10 23:50:17 +0000391 return 1;
392 }
393
Ryan Mitchell479fa392019-01-02 17:15:39 -0800394 return Convert(&context, apk.get(), writer.get(), format, table_flattener_options_,
395 xml_flattener_options_);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700396}
397
398} // namespace aapt