2017-02-01 00:29:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LoadedApk.h"
|
|
|
|
|
2017-02-01 22:47:03 +00:00
|
|
|
#include "ResourceValues.h"
|
|
|
|
#include "ValueVisitor.h"
|
2017-09-29 14:49:15 -07:00
|
|
|
#include "format/Archive.h"
|
|
|
|
#include "format/binary/TableFlattener.h"
|
|
|
|
#include "format/binary/XmlFlattener.h"
|
2017-03-14 18:52:13 -07:00
|
|
|
#include "io/BigBufferInputStream.h"
|
2017-04-03 18:12:45 -07:00
|
|
|
#include "io/Util.h"
|
2017-09-01 14:34:22 -07:00
|
|
|
#include "xml/XmlDom.h"
|
2017-02-01 22:47:03 +00:00
|
|
|
|
2017-02-01 00:29:25 +00:00
|
|
|
namespace aapt {
|
|
|
|
|
2017-09-01 14:34:22 -07:00
|
|
|
using xml::XmlResource;
|
|
|
|
|
2017-02-01 22:47:03 +00:00
|
|
|
std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(IAaptContext* context,
|
|
|
|
const android::StringPiece& path) {
|
2017-02-01 00:29:25 +00:00
|
|
|
Source source(path);
|
|
|
|
std::string error;
|
2017-03-14 18:52:13 -07:00
|
|
|
std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::Create(path, &error);
|
2017-02-01 00:29:25 +00:00
|
|
|
if (!apk) {
|
|
|
|
context->GetDiagnostics()->Error(DiagMessage(source) << error);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
io::IFile* file = apk->FindFile("resources.arsc");
|
|
|
|
if (!file) {
|
2017-03-14 18:52:13 -07:00
|
|
|
context->GetDiagnostics()->Error(DiagMessage(source) << "no resources.arsc found");
|
2017-02-01 00:29:25 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<io::IData> data = file->OpenAsData();
|
|
|
|
if (!data) {
|
2017-03-14 18:52:13 -07:00
|
|
|
context->GetDiagnostics()->Error(DiagMessage(source) << "could not open resources.arsc");
|
2017-02-01 00:29:25 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
|
2017-04-03 18:12:45 -07:00
|
|
|
BinaryResourceParser parser(context, table.get(), source, data->data(), data->size(), apk.get());
|
2017-02-01 00:29:25 +00:00
|
|
|
if (!parser.Parse()) {
|
|
|
|
return {};
|
|
|
|
}
|
2017-09-01 14:34:22 -07:00
|
|
|
|
2017-02-01 00:29:25 +00:00
|
|
|
return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table));
|
|
|
|
}
|
|
|
|
|
2017-02-21 14:22:30 -08:00
|
|
|
bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
|
|
|
|
IArchiveWriter* writer) {
|
2017-06-19 12:52:04 -07:00
|
|
|
FilterChain empty;
|
2017-06-22 12:24:12 -07:00
|
|
|
return WriteToArchive(context, table_.get(), options, &empty, writer);
|
2017-06-19 12:52:04 -07:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:24:12 -07:00
|
|
|
bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table,
|
|
|
|
const TableFlattenerOptions& options, FilterChain* filters,
|
2017-09-01 14:34:22 -07:00
|
|
|
IArchiveWriter* writer, XmlResource* manifest) {
|
2017-02-01 22:47:03 +00:00
|
|
|
std::set<std::string> referenced_resources;
|
|
|
|
// List the files being referenced in the resource table.
|
2017-06-22 12:24:12 -07:00
|
|
|
for (auto& pkg : split_table->packages) {
|
2017-02-01 22:47:03 +00:00
|
|
|
for (auto& type : pkg->types) {
|
|
|
|
for (auto& entry : type->entries) {
|
|
|
|
for (auto& config_value : entry->values) {
|
|
|
|
FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
|
|
|
|
if (file_ref) {
|
|
|
|
referenced_resources.insert(*file_ref->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<io::IFileCollectionIterator> iterator = apk_->Iterator();
|
|
|
|
while (iterator->HasNext()) {
|
|
|
|
io::IFile* file = iterator->Next();
|
|
|
|
|
|
|
|
std::string path = file->GetSource().path;
|
|
|
|
// The name of the path has the format "<zip-file-name>@<path-to-file>".
|
2017-08-03 16:28:10 -07:00
|
|
|
path = path.substr(path.find('@') + 1);
|
2017-02-01 22:47:03 +00:00
|
|
|
|
|
|
|
// Skip resources that are not referenced if requested.
|
|
|
|
if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) {
|
|
|
|
if (context->IsVerbose()) {
|
|
|
|
context->GetDiagnostics()->Note(DiagMessage()
|
2017-02-03 19:15:03 +00:00
|
|
|
<< "Removing resource '" << path << "' from APK.");
|
2017-02-01 22:47:03 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-19 12:52:04 -07:00
|
|
|
if (!filters->Keep(path)) {
|
|
|
|
if (context->IsVerbose()) {
|
|
|
|
context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK.");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-14 18:52:13 -07:00
|
|
|
// The resource table needs to be re-serialized since it might have changed.
|
2017-02-01 22:47:03 +00:00
|
|
|
if (path == "resources.arsc") {
|
2017-03-14 18:52:13 -07:00
|
|
|
BigBuffer buffer(4096);
|
2017-02-08 07:03:50 -08:00
|
|
|
// TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
|
|
|
|
// with sparse entries) b/35389232.
|
2017-02-21 14:22:30 -08:00
|
|
|
TableFlattener flattener(options, &buffer);
|
2017-06-22 12:24:12 -07:00
|
|
|
if (!flattener.Consume(context, split_table)) {
|
2017-02-01 22:47:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-14 18:52:13 -07:00
|
|
|
io::BigBufferInputStream input_stream(&buffer);
|
2017-04-03 18:12:45 -07:00
|
|
|
if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign,
|
|
|
|
writer)) {
|
2017-02-01 22:47:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:34:22 -07:00
|
|
|
} else if (manifest != nullptr && path == "AndroidManifest.xml") {
|
|
|
|
BigBuffer buffer(8192);
|
|
|
|
XmlFlattener xml_flattener(&buffer, {});
|
|
|
|
if (!xml_flattener.Consume(context, manifest)) {
|
|
|
|
context->GetDiagnostics()->Error(DiagMessage(path) << "flattening failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
|
|
|
|
io::BigBufferInputStream manifest_buffer_in(&buffer);
|
|
|
|
if (!io::CopyInputStreamToArchive(context, &manifest_buffer_in, path, compression_flags,
|
|
|
|
writer)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-03 18:12:45 -07:00
|
|
|
} else {
|
|
|
|
uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
|
|
|
|
if (!io::CopyFileToArchive(context, file, path, compression_flags, writer)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-01 22:47:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:34:22 -07:00
|
|
|
std::unique_ptr<xml::XmlResource> LoadedApk::InflateManifest(IAaptContext* context) {
|
|
|
|
IDiagnostics* diag = context->GetDiagnostics();
|
|
|
|
|
|
|
|
io::IFile* manifest_file = GetFileCollection()->FindFile("AndroidManifest.xml");
|
|
|
|
if (manifest_file == nullptr) {
|
|
|
|
diag->Error(DiagMessage(source_) << "no AndroidManifest.xml found");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<io::IData> manifest_data = manifest_file->OpenAsData();
|
|
|
|
if (manifest_data == nullptr) {
|
|
|
|
diag->Error(DiagMessage(manifest_file->GetSource()) << "could not open AndroidManifest.xml");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<xml::XmlResource> manifest =
|
|
|
|
xml::Inflate(manifest_data->data(), manifest_data->size(), diag, manifest_file->GetSource());
|
|
|
|
if (manifest == nullptr) {
|
|
|
|
diag->Error(DiagMessage() << "failed to read binary AndroidManifest.xml");
|
|
|
|
}
|
|
|
|
return manifest;
|
|
|
|
}
|
2017-02-01 00:29:25 +00:00
|
|
|
} // namespace aapt
|