Merge "Rename ZipEntryName to ZipString"

This commit is contained in:
Yusuke Sato 2015-06-29 17:49:28 +00:00 committed by Gerrit Code Review
commit 692dc75d9f
4 changed files with 90 additions and 79 deletions

View file

@ -391,7 +391,7 @@ void *load_bootable_image(const char *kernel, const char *ramdisk,
static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz) static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
{ {
ZipEntryName zip_entry_name(entry_name); ZipString zip_entry_name(entry_name);
ZipEntry zip_entry; ZipEntry zip_entry;
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
fprintf(stderr, "archive does not contain '%s'\n", entry_name); fprintf(stderr, "archive does not contain '%s'\n", entry_name);
@ -453,7 +453,7 @@ static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
return -1; return -1;
} }
ZipEntryName zip_entry_name(entry_name); ZipString zip_entry_name(entry_name);
ZipEntry zip_entry; ZipEntry zip_entry;
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
fprintf(stderr, "archive does not contain '%s'\n", entry_name); fprintf(stderr, "archive does not contain '%s'\n", entry_name);

View file

@ -33,17 +33,33 @@ enum {
kCompressDeflated = 8, // standard deflate kCompressDeflated = 8, // standard deflate
}; };
struct ZipEntryName { struct ZipString {
const uint8_t* name; const uint8_t* name;
uint16_t name_length; uint16_t name_length;
ZipEntryName() {} ZipString() {}
/* /*
* entry_name has to be an c-style string with only ASCII characters. * entry_name has to be an c-style string with only ASCII characters.
*/ */
explicit ZipEntryName(const char* entry_name) explicit ZipString(const char* entry_name)
: name(reinterpret_cast<const uint8_t*>(entry_name)), name_length(strlen(entry_name)) {} : name(reinterpret_cast<const uint8_t*>(entry_name)), name_length(strlen(entry_name)) {}
bool operator==(const ZipString& rhs) const {
return name && (name_length == rhs.name_length) &&
(memcmp(name, rhs.name, name_length) == 0);
}
bool StartsWith(const ZipString& prefix) const {
return name && (name_length >= prefix.name_length) &&
(memcmp(name, prefix.name, prefix.name_length) == 0);
}
bool EndsWith(const ZipString& suffix) const {
return name && (name_length >= suffix.name_length) &&
(memcmp(name + name_length - suffix.name_length, suffix.name,
suffix.name_length) == 0);
}
}; };
/* /*
@ -136,7 +152,7 @@ void CloseArchive(ZipArchiveHandle handle);
* and length, a call to VerifyCrcAndLengths must be made after entry data * and length, a call to VerifyCrcAndLengths must be made after entry data
* has been processed. * has been processed.
*/ */
int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName, int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
ZipEntry* data); ZipEntry* data);
/* /*
@ -147,15 +163,14 @@ int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
* calls to Next. All calls to StartIteration must be matched by a call to * calls to Next. All calls to StartIteration must be matched by a call to
* EndIteration to free any allocated memory. * EndIteration to free any allocated memory.
* *
* This method also accepts an optional prefix to restrict iteration to * This method also accepts optional prefix and suffix to restrict iteration to
* entry names that start with |optional_prefix|. * entry names that start with |optional_prefix| or end with |optional_suffix|.
* *
* Returns 0 on success and negative values on failure. * Returns 0 on success and negative values on failure.
*/ */
int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
const ZipEntryName* optional_prefix, const ZipString* optional_prefix,
// TODO: Remove the default parameter. const ZipString* optional_suffix);
const ZipEntryName* optional_suffix = NULL);
/* /*
* Advance to the next element in the zipfile in iteration order. * Advance to the next element in the zipfile in iteration order.
@ -163,7 +178,7 @@ int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
* Returns 0 on success, -1 if there are no more elements in this * Returns 0 on success, -1 if there are no more elements in this
* archive and lower negative values on failure. * archive and lower negative values on failure.
*/ */
int32_t Next(void* cookie, ZipEntry* data, ZipEntryName *name); int32_t Next(void* cookie, ZipEntry* data, ZipString* name);
/* /*
* End iteration over all entries of a zip file and frees the memory allocated * End iteration over all entries of a zip file and frees the memory allocated

View file

@ -307,7 +307,7 @@ struct ZipArchive {
* ((4 * UINT16_MAX) / 3 + 1) which can safely fit into a uint32_t. * ((4 * UINT16_MAX) / 3 + 1) which can safely fit into a uint32_t.
*/ */
uint32_t hash_table_size; uint32_t hash_table_size;
ZipEntryName* hash_table; ZipString* hash_table;
ZipArchive(const int fd, bool assume_ownership) : ZipArchive(const int fd, bool assume_ownership) :
fd(fd), fd(fd),
@ -343,7 +343,7 @@ static uint32_t RoundUpPower2(uint32_t val) {
return val; return val;
} }
static uint32_t ComputeHash(const ZipEntryName& name) { static uint32_t ComputeHash(const ZipString& name) {
uint32_t hash = 0; uint32_t hash = 0;
uint16_t len = name.name_length; uint16_t len = name.name_length;
const uint8_t* str = name.name; const uint8_t* str = name.name;
@ -359,16 +359,15 @@ static uint32_t ComputeHash(const ZipEntryName& name) {
* Convert a ZipEntry to a hash table index, verifying that it's in a * Convert a ZipEntry to a hash table index, verifying that it's in a
* valid range. * valid range.
*/ */
static int64_t EntryToIndex(const ZipEntryName* hash_table, static int64_t EntryToIndex(const ZipString* hash_table,
const uint32_t hash_table_size, const uint32_t hash_table_size,
const ZipEntryName& name) { const ZipString& name) {
const uint32_t hash = ComputeHash(name); const uint32_t hash = ComputeHash(name);
// NOTE: (hash_table_size - 1) is guaranteed to be non-negative. // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
uint32_t ent = hash & (hash_table_size - 1); uint32_t ent = hash & (hash_table_size - 1);
while (hash_table[ent].name != NULL) { while (hash_table[ent].name != NULL) {
if (hash_table[ent].name_length == name.name_length && if (hash_table[ent] == name) {
memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
return ent; return ent;
} }
@ -382,8 +381,8 @@ static int64_t EntryToIndex(const ZipEntryName* hash_table,
/* /*
* Add a new entry to the hash table. * Add a new entry to the hash table.
*/ */
static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size, static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size,
const ZipEntryName& name) { const ZipString& name) {
const uint64_t hash = ComputeHash(name); const uint64_t hash = ComputeHash(name);
uint32_t ent = hash & (hash_table_size - 1); uint32_t ent = hash & (hash_table_size - 1);
@ -392,8 +391,7 @@ static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_siz
* Further, we guarantee that the hashtable size is not 0. * Further, we guarantee that the hashtable size is not 0.
*/ */
while (hash_table[ent].name != NULL) { while (hash_table[ent].name != NULL) {
if (hash_table[ent].name_length == name.name_length && if (hash_table[ent] == name) {
memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
// We've found a duplicate entry. We don't accept it // We've found a duplicate entry. We don't accept it
ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name); ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
return kDuplicateEntry; return kDuplicateEntry;
@ -565,8 +563,8 @@ static int32_t ParseZipArchive(ZipArchive* archive) {
* least one unused entry to avoid an infinite loop during creation. * least one unused entry to avoid an infinite loop during creation.
*/ */
archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3); archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
archive->hash_table = reinterpret_cast<ZipEntryName*>(calloc(archive->hash_table_size, archive->hash_table = reinterpret_cast<ZipString*>(calloc(archive->hash_table_size,
sizeof(ZipEntryName))); sizeof(ZipString)));
/* /*
* Walk through the central directory, adding entries to the hash * Walk through the central directory, adding entries to the hash
@ -605,7 +603,7 @@ static int32_t ParseZipArchive(ZipArchive* archive) {
} }
/* add the CDE filename to the hash table */ /* add the CDE filename to the hash table */
ZipEntryName entry_name; ZipString entry_name;
entry_name.name = file_name; entry_name.name = file_name;
entry_name.name_length = file_name_length; entry_name.name_length = file_name_length;
const int add_result = AddToHash(archive->hash_table, const int add_result = AddToHash(archive->hash_table,
@ -851,39 +849,41 @@ struct IterationHandle {
uint32_t position; uint32_t position;
// We're not using vector here because this code is used in the Windows SDK // We're not using vector here because this code is used in the Windows SDK
// where the STL is not available. // where the STL is not available.
const uint8_t* prefix; ZipString prefix;
const uint16_t prefix_len; ZipString suffix;
const uint8_t* suffix;
const uint16_t suffix_len;
ZipArchive* archive; ZipArchive* archive;
IterationHandle(const ZipEntryName* prefix_name, IterationHandle(const ZipString* in_prefix,
const ZipEntryName* suffix_name) const ZipString* in_suffix) {
: prefix(NULL), if (in_prefix) {
prefix_len(prefix_name ? prefix_name->name_length : 0), uint8_t* name_copy = new uint8_t[in_prefix->name_length];
suffix(NULL), memcpy(name_copy, in_prefix->name, in_prefix->name_length);
suffix_len(suffix_name ? suffix_name->name_length : 0) { prefix.name = name_copy;
if (prefix_name) { prefix.name_length = in_prefix->name_length;
uint8_t* prefix_copy = new uint8_t[prefix_len]; } else {
memcpy(prefix_copy, prefix_name->name, prefix_len); prefix.name = NULL;
prefix = prefix_copy; prefix.name_length = 0;
} }
if (suffix_name) { if (in_suffix) {
uint8_t* suffix_copy = new uint8_t[suffix_len]; uint8_t* name_copy = new uint8_t[in_suffix->name_length];
memcpy(suffix_copy, suffix_name->name, suffix_len); memcpy(name_copy, in_suffix->name, in_suffix->name_length);
suffix = suffix_copy; suffix.name = name_copy;
suffix.name_length = in_suffix->name_length;
} else {
suffix.name = NULL;
suffix.name_length = 0;
} }
} }
~IterationHandle() { ~IterationHandle() {
delete[] prefix; delete[] prefix.name;
delete[] suffix; delete[] suffix.name;
} }
}; };
int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
const ZipEntryName* optional_prefix, const ZipString* optional_prefix,
const ZipEntryName* optional_suffix) { const ZipString* optional_suffix) {
ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle); ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
if (archive == NULL || archive->hash_table == NULL) { if (archive == NULL || archive->hash_table == NULL) {
@ -903,7 +903,7 @@ void EndIteration(void* cookie) {
delete reinterpret_cast<IterationHandle*>(cookie); delete reinterpret_cast<IterationHandle*>(cookie);
} }
int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName, int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
ZipEntry* data) { ZipEntry* data) {
const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle); const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
if (entryName.name_length == 0) { if (entryName.name_length == 0) {
@ -922,7 +922,7 @@ int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
return FindEntry(archive, ent, data); return FindEntry(archive, ent, data);
} }
int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) { int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie); IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
if (handle == NULL) { if (handle == NULL) {
return kInvalidHandle; return kInvalidHandle;
@ -936,18 +936,14 @@ int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
const uint32_t currentOffset = handle->position; const uint32_t currentOffset = handle->position;
const uint32_t hash_table_length = archive->hash_table_size; const uint32_t hash_table_length = archive->hash_table_size;
const ZipEntryName *hash_table = archive->hash_table; const ZipString* hash_table = archive->hash_table;
for (uint32_t i = currentOffset; i < hash_table_length; ++i) { for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
if (hash_table[i].name != NULL && if (hash_table[i].name != NULL &&
(handle->prefix_len == 0 || (handle->prefix.name_length == 0 ||
(hash_table[i].name_length >= handle->prefix_len && hash_table[i].StartsWith(handle->prefix)) &&
memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0)) && (handle->suffix.name_length == 0 ||
(handle->suffix_len == 0 || hash_table[i].EndsWith(handle->suffix))) {
(hash_table[i].name_length >= handle->suffix_len &&
memcmp(handle->suffix,
hash_table[i].name + hash_table[i].name_length - handle->suffix_len,
handle->suffix_len) == 0))) {
handle->position = (i + 1); handle->position = (i + 1);
const int error = FindEntry(archive, i, data); const int error = FindEntry(archive, i, data);
if (!error) { if (!error) {

View file

@ -70,7 +70,7 @@ static int32_t OpenArchiveWrapper(const std::string& name,
} }
static void AssertNameEquals(const std::string& name_str, static void AssertNameEquals(const std::string& name_str,
const ZipEntryName& name) { const ZipString& name) {
ASSERT_EQ(name_str.size(), name.name_length); ASSERT_EQ(name_str.size(), name.name_length);
ASSERT_EQ(0, memcmp(name_str.c_str(), name.name, name.name_length)); ASSERT_EQ(0, memcmp(name_str.c_str(), name.name, name.name_length));
} }
@ -118,7 +118,7 @@ TEST(ziparchive, Iteration) {
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL, NULL)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL, NULL));
ZipEntry data; ZipEntry data;
ZipEntryName name; ZipString name;
// b/c.txt // b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
@ -151,11 +151,11 @@ TEST(ziparchive, IterationWithPrefix) {
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie; void* iteration_cookie;
ZipEntryName prefix("b/"); ZipString prefix("b/");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, NULL)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, NULL));
ZipEntry data; ZipEntry data;
ZipEntryName name; ZipString name;
// b/c.txt // b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
@ -180,11 +180,11 @@ TEST(ziparchive, IterationWithSuffix) {
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie; void* iteration_cookie;
ZipEntryName suffix(".txt"); ZipString suffix(".txt");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL, &suffix)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL, &suffix));
ZipEntry data; ZipEntry data;
ZipEntryName name; ZipString name;
// b/c.txt // b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
@ -213,12 +213,12 @@ TEST(ziparchive, IterationWithPrefixAndSuffix) {
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie; void* iteration_cookie;
ZipEntryName prefix("b"); ZipString prefix("b");
ZipEntryName suffix(".txt"); ZipString suffix(".txt");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix));
ZipEntry data; ZipEntry data;
ZipEntryName name; ZipString name;
// b/c.txt // b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
@ -243,12 +243,12 @@ TEST(ziparchive, IterationWithBadPrefixAndSuffix) {
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie; void* iteration_cookie;
ZipEntryName prefix("x"); ZipString prefix("x");
ZipEntryName suffix("y"); ZipString suffix("y");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix));
ZipEntry data; ZipEntry data;
ZipEntryName name; ZipString name;
// End of iteration. // End of iteration.
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
@ -261,7 +261,7 @@ TEST(ziparchive, FindEntry) {
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
ZipEntry data; ZipEntry data;
ZipEntryName name; ZipString name;
name.name = kATxtName; name.name = kATxtName;
name.name_length = kATxtNameLength; name.name_length = kATxtNameLength;
ASSERT_EQ(0, FindEntry(handle, name, &data)); ASSERT_EQ(0, FindEntry(handle, name, &data));
@ -274,7 +274,7 @@ TEST(ziparchive, FindEntry) {
ASSERT_EQ(0x950821c5, data.crc32); ASSERT_EQ(0x950821c5, data.crc32);
// An entry that doesn't exist. Should be a negative return code. // An entry that doesn't exist. Should be a negative return code.
ZipEntryName absent_name; ZipString absent_name;
absent_name.name = kNonexistentTxtName; absent_name.name = kNonexistentTxtName;
absent_name.name_length = kNonexistentTxtNameLength; absent_name.name_length = kNonexistentTxtNameLength;
ASSERT_LT(FindEntry(handle, absent_name, &data), 0); ASSERT_LT(FindEntry(handle, absent_name, &data), 0);
@ -287,9 +287,9 @@ TEST(ziparchive, TestInvalidDeclaredLength) {
ASSERT_EQ(0, OpenArchiveWrapper("declaredlength.zip", &handle)); ASSERT_EQ(0, OpenArchiveWrapper("declaredlength.zip", &handle));
void* iteration_cookie; void* iteration_cookie;
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL, NULL));
ZipEntryName name; ZipString name;
ZipEntry data; ZipEntry data;
ASSERT_EQ(Next(iteration_cookie, &data, &name), 0); ASSERT_EQ(Next(iteration_cookie, &data, &name), 0);
@ -304,7 +304,7 @@ TEST(ziparchive, ExtractToMemory) {
// An entry that's deflated. // An entry that's deflated.
ZipEntry data; ZipEntry data;
ZipEntryName a_name; ZipString a_name;
a_name.name = kATxtName; a_name.name = kATxtName;
a_name.name_length = kATxtNameLength; a_name.name_length = kATxtNameLength;
ASSERT_EQ(0, FindEntry(handle, a_name, &data)); ASSERT_EQ(0, FindEntry(handle, a_name, &data));
@ -316,7 +316,7 @@ TEST(ziparchive, ExtractToMemory) {
delete[] buffer; delete[] buffer;
// An entry that's stored. // An entry that's stored.
ZipEntryName b_name; ZipString b_name;
b_name.name = kBTxtName; b_name.name = kBTxtName;
b_name.name_length = kBTxtNameLength; b_name.name_length = kBTxtNameLength;
ASSERT_EQ(0, FindEntry(handle, b_name, &data)); ASSERT_EQ(0, FindEntry(handle, b_name, &data));
@ -403,7 +403,7 @@ TEST(ziparchive, EmptyEntries) {
ASSERT_EQ(0, OpenArchiveFd(fd, "EmptyEntriesTest", &handle)); ASSERT_EQ(0, OpenArchiveFd(fd, "EmptyEntriesTest", &handle));
ZipEntry entry; ZipEntry entry;
ZipEntryName empty_name; ZipString empty_name;
empty_name.name = kEmptyTxtName; empty_name.name = kEmptyTxtName;
empty_name.name_length = kEmptyTxtNameLength; empty_name.name_length = kEmptyTxtNameLength;
ASSERT_EQ(0, FindEntry(handle, empty_name, &entry)); ASSERT_EQ(0, FindEntry(handle, empty_name, &entry));
@ -434,7 +434,7 @@ TEST(ziparchive, EntryLargerThan32K) {
ASSERT_EQ(0, OpenArchiveFd(fd, "EntryLargerThan32KTest", &handle)); ASSERT_EQ(0, OpenArchiveFd(fd, "EntryLargerThan32KTest", &handle));
ZipEntry entry; ZipEntry entry;
ZipEntryName ab_name; ZipString ab_name;
ab_name.name = kAbTxtName; ab_name.name = kAbTxtName;
ab_name.name_length = kAbTxtNameLength; ab_name.name_length = kAbTxtNameLength;
ASSERT_EQ(0, FindEntry(handle, ab_name, &entry)); ASSERT_EQ(0, FindEntry(handle, ab_name, &entry));
@ -502,7 +502,7 @@ TEST(ziparchive, ExtractToFile) {
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
ZipEntry entry; ZipEntry entry;
ZipEntryName name; ZipString name;
name.name = kATxtName; name.name = kATxtName;
name.name_length = kATxtNameLength; name.name_length = kATxtNameLength;
ASSERT_EQ(0, FindEntry(handle, name, &entry)); ASSERT_EQ(0, FindEntry(handle, name, &entry));