btrfs-progs: image: use CRC32C reversing instead of brute force to find collisions

Author: Piotr Pawlow <pp@siedziba.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Piotr Pawlow 2017-08-11 15:47:35 +02:00 committed by David Sterba
parent 334d83053a
commit 0e88d6d1d3

View File

@ -447,17 +447,26 @@ static int find_collision_is_suffix_valid(const char *suffix)
return 1;
}
static int find_collision_brute_force(struct name *val, u32 name_len)
static int find_collision_reverse_crc32c(struct name *val, u32 name_len)
{
unsigned long checksum;
unsigned long current_checksum;
int found = 0;
int i;
/* There are no same length collisions of 4 or less bytes */
if (name_len <= 4)
return 0;
checksum = crc32c(~1, val->val, name_len);
name_len -= 4;
memset(val->sub, ' ', name_len);
i = 0;
while (1) {
if (crc32c(~1, val->sub, name_len) == checksum &&
current_checksum = crc32c(~1, val->sub, name_len);
find_collision_calc_suffix(current_checksum,
checksum,
val->sub + name_len);
if (find_collision_is_suffix_valid(val->sub + name_len) &&
memcmp(val->sub, val->val, val->len)) {
found = 1;
break;
@ -524,7 +533,7 @@ static char *find_collision(struct metadump_struct *md, char *name,
return NULL;
}
found = find_collision_brute_force(val, name_len);
found = find_collision_reverse_crc32c(val, name_len);
if (!found) {
warning(