am 692dc75d
: Merge "Rename ZipEntryName to ZipString"
* commit '692dc75d9fbf5c256cd8c66219a930ae0fe9f523': Rename ZipEntryName to ZipString
This commit is contained in:
commit
95a37e756e
4 changed files with 90 additions and 79 deletions
|
@ -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)
|
||||
{
|
||||
ZipEntryName zip_entry_name(entry_name);
|
||||
ZipString zip_entry_name(entry_name);
|
||||
ZipEntry zip_entry;
|
||||
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
ZipEntryName zip_entry_name(entry_name);
|
||||
ZipString zip_entry_name(entry_name);
|
||||
ZipEntry zip_entry;
|
||||
if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
|
||||
fprintf(stderr, "archive does not contain '%s'\n", entry_name);
|
||||
|
|
|
@ -33,17 +33,33 @@ enum {
|
|||
kCompressDeflated = 8, // standard deflate
|
||||
};
|
||||
|
||||
struct ZipEntryName {
|
||||
struct ZipString {
|
||||
const uint8_t* name;
|
||||
uint16_t name_length;
|
||||
|
||||
ZipEntryName() {}
|
||||
ZipString() {}
|
||||
|
||||
/*
|
||||
* 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)) {}
|
||||
|
||||
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
|
||||
* has been processed.
|
||||
*/
|
||||
int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
|
||||
int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
|
||||
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
|
||||
* EndIteration to free any allocated memory.
|
||||
*
|
||||
* This method also accepts an optional prefix to restrict iteration to
|
||||
* entry names that start with |optional_prefix|.
|
||||
* This method also accepts optional prefix and suffix to restrict iteration to
|
||||
* entry names that start with |optional_prefix| or end with |optional_suffix|.
|
||||
*
|
||||
* Returns 0 on success and negative values on failure.
|
||||
*/
|
||||
int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
|
||||
const ZipEntryName* optional_prefix,
|
||||
// TODO: Remove the default parameter.
|
||||
const ZipEntryName* optional_suffix = NULL);
|
||||
const ZipString* optional_prefix,
|
||||
const ZipString* optional_suffix);
|
||||
|
||||
/*
|
||||
* 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
|
||||
* 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
|
||||
|
|
|
@ -307,7 +307,7 @@ struct ZipArchive {
|
|||
* ((4 * UINT16_MAX) / 3 + 1) which can safely fit into a uint32_t.
|
||||
*/
|
||||
uint32_t hash_table_size;
|
||||
ZipEntryName* hash_table;
|
||||
ZipString* hash_table;
|
||||
|
||||
ZipArchive(const int fd, bool assume_ownership) :
|
||||
fd(fd),
|
||||
|
@ -343,7 +343,7 @@ static uint32_t RoundUpPower2(uint32_t val) {
|
|||
return val;
|
||||
}
|
||||
|
||||
static uint32_t ComputeHash(const ZipEntryName& name) {
|
||||
static uint32_t ComputeHash(const ZipString& name) {
|
||||
uint32_t hash = 0;
|
||||
uint16_t len = name.name_length;
|
||||
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
|
||||
* 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 ZipEntryName& name) {
|
||||
const ZipString& name) {
|
||||
const uint32_t hash = ComputeHash(name);
|
||||
|
||||
// NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
|
||||
uint32_t ent = hash & (hash_table_size - 1);
|
||||
while (hash_table[ent].name != NULL) {
|
||||
if (hash_table[ent].name_length == name.name_length &&
|
||||
memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
|
||||
if (hash_table[ent] == name) {
|
||||
return ent;
|
||||
}
|
||||
|
||||
|
@ -382,8 +381,8 @@ static int64_t EntryToIndex(const ZipEntryName* hash_table,
|
|||
/*
|
||||
* Add a new entry to the hash table.
|
||||
*/
|
||||
static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
|
||||
const ZipEntryName& name) {
|
||||
static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size,
|
||||
const ZipString& name) {
|
||||
const uint64_t hash = ComputeHash(name);
|
||||
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.
|
||||
*/
|
||||
while (hash_table[ent].name != NULL) {
|
||||
if (hash_table[ent].name_length == name.name_length &&
|
||||
memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
|
||||
if (hash_table[ent] == name) {
|
||||
// We've found a duplicate entry. We don't accept it
|
||||
ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
|
||||
return kDuplicateEntry;
|
||||
|
@ -565,8 +563,8 @@ static int32_t ParseZipArchive(ZipArchive* archive) {
|
|||
* least one unused entry to avoid an infinite loop during creation.
|
||||
*/
|
||||
archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
|
||||
archive->hash_table = reinterpret_cast<ZipEntryName*>(calloc(archive->hash_table_size,
|
||||
sizeof(ZipEntryName)));
|
||||
archive->hash_table = reinterpret_cast<ZipString*>(calloc(archive->hash_table_size,
|
||||
sizeof(ZipString)));
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
ZipEntryName entry_name;
|
||||
ZipString entry_name;
|
||||
entry_name.name = file_name;
|
||||
entry_name.name_length = file_name_length;
|
||||
const int add_result = AddToHash(archive->hash_table,
|
||||
|
@ -851,39 +849,41 @@ struct IterationHandle {
|
|||
uint32_t position;
|
||||
// We're not using vector here because this code is used in the Windows SDK
|
||||
// where the STL is not available.
|
||||
const uint8_t* prefix;
|
||||
const uint16_t prefix_len;
|
||||
const uint8_t* suffix;
|
||||
const uint16_t suffix_len;
|
||||
ZipString prefix;
|
||||
ZipString suffix;
|
||||
ZipArchive* archive;
|
||||
|
||||
IterationHandle(const ZipEntryName* prefix_name,
|
||||
const ZipEntryName* suffix_name)
|
||||
: prefix(NULL),
|
||||
prefix_len(prefix_name ? prefix_name->name_length : 0),
|
||||
suffix(NULL),
|
||||
suffix_len(suffix_name ? suffix_name->name_length : 0) {
|
||||
if (prefix_name) {
|
||||
uint8_t* prefix_copy = new uint8_t[prefix_len];
|
||||
memcpy(prefix_copy, prefix_name->name, prefix_len);
|
||||
prefix = prefix_copy;
|
||||
IterationHandle(const ZipString* in_prefix,
|
||||
const ZipString* in_suffix) {
|
||||
if (in_prefix) {
|
||||
uint8_t* name_copy = new uint8_t[in_prefix->name_length];
|
||||
memcpy(name_copy, in_prefix->name, in_prefix->name_length);
|
||||
prefix.name = name_copy;
|
||||
prefix.name_length = in_prefix->name_length;
|
||||
} else {
|
||||
prefix.name = NULL;
|
||||
prefix.name_length = 0;
|
||||
}
|
||||
if (suffix_name) {
|
||||
uint8_t* suffix_copy = new uint8_t[suffix_len];
|
||||
memcpy(suffix_copy, suffix_name->name, suffix_len);
|
||||
suffix = suffix_copy;
|
||||
if (in_suffix) {
|
||||
uint8_t* name_copy = new uint8_t[in_suffix->name_length];
|
||||
memcpy(name_copy, in_suffix->name, in_suffix->name_length);
|
||||
suffix.name = name_copy;
|
||||
suffix.name_length = in_suffix->name_length;
|
||||
} else {
|
||||
suffix.name = NULL;
|
||||
suffix.name_length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
~IterationHandle() {
|
||||
delete[] prefix;
|
||||
delete[] suffix;
|
||||
delete[] prefix.name;
|
||||
delete[] suffix.name;
|
||||
}
|
||||
};
|
||||
|
||||
int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
|
||||
const ZipEntryName* optional_prefix,
|
||||
const ZipEntryName* optional_suffix) {
|
||||
const ZipString* optional_prefix,
|
||||
const ZipString* optional_suffix) {
|
||||
ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
|
||||
|
||||
if (archive == NULL || archive->hash_table == NULL) {
|
||||
|
@ -903,7 +903,7 @@ void EndIteration(void* 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) {
|
||||
const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
|
||||
if (entryName.name_length == 0) {
|
||||
|
@ -922,7 +922,7 @@ int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
|
|||
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);
|
||||
if (handle == NULL) {
|
||||
return kInvalidHandle;
|
||||
|
@ -936,18 +936,14 @@ int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
|
|||
|
||||
const uint32_t currentOffset = handle->position;
|
||||
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) {
|
||||
if (hash_table[i].name != NULL &&
|
||||
(handle->prefix_len == 0 ||
|
||||
(hash_table[i].name_length >= handle->prefix_len &&
|
||||
memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0)) &&
|
||||
(handle->suffix_len == 0 ||
|
||||
(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->prefix.name_length == 0 ||
|
||||
hash_table[i].StartsWith(handle->prefix)) &&
|
||||
(handle->suffix.name_length == 0 ||
|
||||
hash_table[i].EndsWith(handle->suffix))) {
|
||||
handle->position = (i + 1);
|
||||
const int error = FindEntry(archive, i, data);
|
||||
if (!error) {
|
||||
|
|
|
@ -70,7 +70,7 @@ static int32_t OpenArchiveWrapper(const std::string& name,
|
|||
}
|
||||
|
||||
static void AssertNameEquals(const std::string& name_str,
|
||||
const ZipEntryName& name) {
|
||||
const ZipString& name) {
|
||||
ASSERT_EQ(name_str.size(), 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));
|
||||
|
||||
ZipEntry data;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
|
||||
// b/c.txt
|
||||
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
|
||||
|
@ -151,11 +151,11 @@ TEST(ziparchive, IterationWithPrefix) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
|
||||
|
||||
void* iteration_cookie;
|
||||
ZipEntryName prefix("b/");
|
||||
ZipString prefix("b/");
|
||||
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, NULL));
|
||||
|
||||
ZipEntry data;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
|
||||
// b/c.txt
|
||||
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
|
||||
|
@ -180,11 +180,11 @@ TEST(ziparchive, IterationWithSuffix) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
|
||||
|
||||
void* iteration_cookie;
|
||||
ZipEntryName suffix(".txt");
|
||||
ZipString suffix(".txt");
|
||||
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, NULL, &suffix));
|
||||
|
||||
ZipEntry data;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
|
||||
// b/c.txt
|
||||
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
|
||||
|
@ -213,12 +213,12 @@ TEST(ziparchive, IterationWithPrefixAndSuffix) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
|
||||
|
||||
void* iteration_cookie;
|
||||
ZipEntryName prefix("b");
|
||||
ZipEntryName suffix(".txt");
|
||||
ZipString prefix("b");
|
||||
ZipString suffix(".txt");
|
||||
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix));
|
||||
|
||||
ZipEntry data;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
|
||||
// b/c.txt
|
||||
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
|
||||
|
@ -243,12 +243,12 @@ TEST(ziparchive, IterationWithBadPrefixAndSuffix) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
|
||||
|
||||
void* iteration_cookie;
|
||||
ZipEntryName prefix("x");
|
||||
ZipEntryName suffix("y");
|
||||
ZipString prefix("x");
|
||||
ZipString suffix("y");
|
||||
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix));
|
||||
|
||||
ZipEntry data;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
|
||||
// End of iteration.
|
||||
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
|
||||
|
@ -261,7 +261,7 @@ TEST(ziparchive, FindEntry) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
|
||||
|
||||
ZipEntry data;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
name.name = kATxtName;
|
||||
name.name_length = kATxtNameLength;
|
||||
ASSERT_EQ(0, FindEntry(handle, name, &data));
|
||||
|
@ -274,7 +274,7 @@ TEST(ziparchive, FindEntry) {
|
|||
ASSERT_EQ(0x950821c5, data.crc32);
|
||||
|
||||
// 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_length = kNonexistentTxtNameLength;
|
||||
ASSERT_LT(FindEntry(handle, absent_name, &data), 0);
|
||||
|
@ -287,9 +287,9 @@ TEST(ziparchive, TestInvalidDeclaredLength) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper("declaredlength.zip", &handle));
|
||||
|
||||
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;
|
||||
|
||||
ASSERT_EQ(Next(iteration_cookie, &data, &name), 0);
|
||||
|
@ -304,7 +304,7 @@ TEST(ziparchive, ExtractToMemory) {
|
|||
|
||||
// An entry that's deflated.
|
||||
ZipEntry data;
|
||||
ZipEntryName a_name;
|
||||
ZipString a_name;
|
||||
a_name.name = kATxtName;
|
||||
a_name.name_length = kATxtNameLength;
|
||||
ASSERT_EQ(0, FindEntry(handle, a_name, &data));
|
||||
|
@ -316,7 +316,7 @@ TEST(ziparchive, ExtractToMemory) {
|
|||
delete[] buffer;
|
||||
|
||||
// An entry that's stored.
|
||||
ZipEntryName b_name;
|
||||
ZipString b_name;
|
||||
b_name.name = kBTxtName;
|
||||
b_name.name_length = kBTxtNameLength;
|
||||
ASSERT_EQ(0, FindEntry(handle, b_name, &data));
|
||||
|
@ -403,7 +403,7 @@ TEST(ziparchive, EmptyEntries) {
|
|||
ASSERT_EQ(0, OpenArchiveFd(fd, "EmptyEntriesTest", &handle));
|
||||
|
||||
ZipEntry entry;
|
||||
ZipEntryName empty_name;
|
||||
ZipString empty_name;
|
||||
empty_name.name = kEmptyTxtName;
|
||||
empty_name.name_length = kEmptyTxtNameLength;
|
||||
ASSERT_EQ(0, FindEntry(handle, empty_name, &entry));
|
||||
|
@ -434,7 +434,7 @@ TEST(ziparchive, EntryLargerThan32K) {
|
|||
ASSERT_EQ(0, OpenArchiveFd(fd, "EntryLargerThan32KTest", &handle));
|
||||
|
||||
ZipEntry entry;
|
||||
ZipEntryName ab_name;
|
||||
ZipString ab_name;
|
||||
ab_name.name = kAbTxtName;
|
||||
ab_name.name_length = kAbTxtNameLength;
|
||||
ASSERT_EQ(0, FindEntry(handle, ab_name, &entry));
|
||||
|
@ -502,7 +502,7 @@ TEST(ziparchive, ExtractToFile) {
|
|||
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
|
||||
|
||||
ZipEntry entry;
|
||||
ZipEntryName name;
|
||||
ZipString name;
|
||||
name.name = kATxtName;
|
||||
name.name_length = kATxtNameLength;
|
||||
ASSERT_EQ(0, FindEntry(handle, name, &entry));
|
||||
|
|
Loading…
Reference in a new issue