pack-check: convert various uses of SHA-1 to abstract forms
Convert various explicit calls to use SHA-1 functions and constants to references to the_hash_algo. Make several strings more generic with respect to the hash algorithm used. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 1, 2018 at 02:18 UTC
ccc12e06764e551c86ff458b16db35958957645f
1 file changed
+16
-16
pack-check.c
+16
-16
@@ -41,7 +41,7 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
41
} while (len);
42
43
index_crc = p->index_data;
44
- index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
44
+ index_crc += 2 + 256 + p->num_objects * (the_hash_algo->rawsz/4) + nr;
45
46
return data_crc != ntohl(*index_crc);
47
}
@@ -54,7 +54,7 @@ static int verify_packfile(struct packed_git *p,
54
{
55
off_t index_size = p->index_size;
56
const unsigned char *index_base = p->index_data;
57
- git_SHA_CTX ctx;
57
+ git_hash_ctx ctx;
58
unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
59
off_t offset = 0, pack_sig_ofs = 0;
60
uint32_t nr_objects, i;
@@ -64,24 +64,24 @@ static int verify_packfile(struct packed_git *p,
64
if (!is_pack_valid(p))
65
return error("packfile %s cannot be accessed", p->pack_name);
66
67
- git_SHA1_Init(&ctx);
67
+ the_hash_algo->init_fn(&ctx);
68
do {
69
unsigned long remaining;
70
unsigned char *in = use_pack(p, w_curs, offset, &remaining);
71
offset += remaining;
72
if (!pack_sig_ofs)
73
- pack_sig_ofs = p->pack_size - 20;
73
+ pack_sig_ofs = p->pack_size - the_hash_algo->rawsz;
74
if (offset > pack_sig_ofs)
75
remaining -= (unsigned int)(offset - pack_sig_ofs);
76
- git_SHA1_Update(&ctx, in, remaining);
76
+ the_hash_algo->update_fn(&ctx, in, remaining);
77
} while (offset < pack_sig_ofs);
78
- git_SHA1_Final(hash, &ctx);
78
+ the_hash_algo->final_fn(hash, &ctx);
79
pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
80
if (hashcmp(hash, pack_sig))
81
- err = error("%s SHA1 checksum mismatch",
81
+ err = error("%s pack checksum mismatch",
82
p->pack_name);
83
- if (hashcmp(index_base + index_size - 40, pack_sig))
84
- err = error("%s SHA1 does not match its index",
83
+ if (hashcmp(index_base + index_size - the_hash_algo->hexsz, pack_sig))
84
+ err = error("%s pack checksum does not match its index",
85
p->pack_name);
86
unuse_pack(w_curs);
87
@@ -165,8 +165,8 @@ int verify_pack_index(struct packed_git *p)
165
{
166
off_t index_size;
167
const unsigned char *index_base;
168
- git_SHA_CTX ctx;
169
- unsigned char sha1[20];
168
+ git_hash_ctx ctx;
169
+ unsigned char hash[GIT_MAX_RAWSZ];
170
int err = 0;
171
172
if (open_pack_index(p))
@@ -175,11 +175,11 @@ int verify_pack_index(struct packed_git *p)
175
index_base = p->index_data;
176
177
/* Verify SHA1 sum of the index file */
178
- git_SHA1_Init(&ctx);
179
- git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
180
- git_SHA1_Final(sha1, &ctx);
181
- if (hashcmp(sha1, index_base + index_size - 20))
182
- err = error("Packfile index for %s SHA1 mismatch",
178
+ the_hash_algo->init_fn(&ctx);
179
+ the_hash_algo->update_fn(&ctx, index_base, (unsigned int)(index_size - the_hash_algo->rawsz));
180
+ the_hash_algo->final_fn(hash, &ctx);
181
+ if (hashcmp(hash, index_base + index_size - the_hash_algo->rawsz))
182
+ err = error("Packfile index for %s hash mismatch",
183
p->pack_name);
184
return err;
185
}