[libziparchive] add an option to start iteration with functor
To reduce the seeks for local file headers in large APK files, we can
specify entry prefix/suffix when we call StartIteration(). However,
some use cases need additional name matches that is outside the
prefix/suffix matches.
Adding a new option to StartIteration, which allows additional functor
that restricts the iteration to customized name matching schemes.
Test: atest ziparchive-tests
BUG: 151676293
Change-Id: Iff45e083b334602f183c05cb39ba521e7070252c
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index afbc5d8..6b6502b 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -32,6 +32,7 @@
#include <unistd.h>
#include <memory>
+#include <optional>
#include <vector>
#if defined(__APPLE__)
@@ -675,31 +676,40 @@
struct IterationHandle {
ZipArchive* archive;
- std::string prefix;
- std::string suffix;
+ std::function<bool(std::string_view)> matcher;
uint32_t position = 0;
- IterationHandle(ZipArchive* archive, std::string_view in_prefix, std::string_view in_suffix)
- : archive(archive), prefix(in_prefix), suffix(in_suffix) {}
+ IterationHandle(ZipArchive* archive, std::function<bool(std::string_view)> in_matcher)
+ : archive(archive), matcher(std::move(in_matcher)) {}
+
+ bool Match(std::string_view entry_name) const { return matcher(entry_name); }
};
int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
const std::string_view optional_prefix,
const std::string_view optional_suffix) {
- if (archive == nullptr || archive->cd_entry_map == nullptr) {
- ALOGW("Zip: Invalid ZipArchiveHandle");
- return kInvalidHandle;
- }
-
if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
ALOGW("Zip: prefix/suffix too long");
return kInvalidEntryName;
}
+ auto matcher = [prefix = std::string(optional_prefix),
+ suffix = std::string(optional_suffix)](std::string_view name) mutable {
+ return android::base::StartsWith(name, prefix) && android::base::EndsWith(name, suffix);
+ };
+ return StartIteration(archive, cookie_ptr, std::move(matcher));
+}
+
+int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
+ std::function<bool(std::string_view)> matcher) {
+ if (archive == nullptr || archive->cd_entry_map == nullptr) {
+ ALOGW("Zip: Invalid ZipArchiveHandle");
+ return kInvalidHandle;
+ }
archive->cd_entry_map->ResetIteration();
- *cookie_ptr = new IterationHandle(archive, optional_prefix, optional_suffix);
+ *cookie_ptr = new IterationHandle(archive, matcher);
return 0;
}
@@ -749,8 +759,7 @@
auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
while (entry != std::pair<std::string_view, uint64_t>()) {
const auto [entry_name, offset] = entry;
- if (android::base::StartsWith(entry_name, handle->prefix) &&
- android::base::EndsWith(entry_name, handle->suffix)) {
+ if (handle->Match(entry_name)) {
const int error = FindEntry(archive, entry_name, offset, data);
if (!error && name) {
*name = entry_name;