Skip to content

Commit ccc12e0

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f89428 commit ccc12e0

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

pack-check.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
4141
} while (len);
4242

4343
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;
4545

4646
return data_crc != ntohl(*index_crc);
4747
}
@@ -54,7 +54,7 @@ static int verify_packfile(struct packed_git *p,
5454
{
5555
off_t index_size = p->index_size;
5656
const unsigned char *index_base = p->index_data;
57-
git_SHA_CTX ctx;
57+
git_hash_ctx ctx;
5858
unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
5959
off_t offset = 0, pack_sig_ofs = 0;
6060
uint32_t nr_objects, i;
@@ -64,24 +64,24 @@ static int verify_packfile(struct packed_git *p,
6464
if (!is_pack_valid(p))
6565
return error("packfile %s cannot be accessed", p->pack_name);
6666

67-
git_SHA1_Init(&ctx);
67+
the_hash_algo->init_fn(&ctx);
6868
do {
6969
unsigned long remaining;
7070
unsigned char *in = use_pack(p, w_curs, offset, &remaining);
7171
offset += remaining;
7272
if (!pack_sig_ofs)
73-
pack_sig_ofs = p->pack_size - 20;
73+
pack_sig_ofs = p->pack_size - the_hash_algo->rawsz;
7474
if (offset > pack_sig_ofs)
7575
remaining -= (unsigned int)(offset - pack_sig_ofs);
76-
git_SHA1_Update(&ctx, in, remaining);
76+
the_hash_algo->update_fn(&ctx, in, remaining);
7777
} while (offset < pack_sig_ofs);
78-
git_SHA1_Final(hash, &ctx);
78+
the_hash_algo->final_fn(hash, &ctx);
7979
pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
8080
if (hashcmp(hash, pack_sig))
81-
err = error("%s SHA1 checksum mismatch",
81+
err = error("%s pack checksum mismatch",
8282
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",
8585
p->pack_name);
8686
unuse_pack(w_curs);
8787

@@ -165,8 +165,8 @@ int verify_pack_index(struct packed_git *p)
165165
{
166166
off_t index_size;
167167
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];
170170
int err = 0;
171171

172172
if (open_pack_index(p))
@@ -175,11 +175,11 @@ int verify_pack_index(struct packed_git *p)
175175
index_base = p->index_data;
176176

177177
/* 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",
183183
p->pack_name);
184184
return err;
185185
}

0 commit comments

Comments
 (0)