Skip to content

Commit f9221e2

Browse files
ttaylorrgitster
authored andcommitted
csum-file: introduce checksum_valid()
Introduce a new function which checks the validity of a file's trailing checksum. This is similar to hashfd_check(), but different since it is intended to be used by callers who aren't writing the same data (like `git index-pack --verify`), but who instead want to validate the integrity of data that they are reading. Rewrite the first of two callers which could benefit from this new function in pack-check.c. Subsequent callers will be added in the following patches. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ebf3c04 commit f9221e2

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

csum-file.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,19 @@ uint32_t crc32_end(struct hashfile *f)
187187
f->do_crc = 0;
188188
return f->crc32;
189189
}
190+
191+
int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
192+
{
193+
unsigned char got[GIT_MAX_RAWSZ];
194+
git_hash_ctx ctx;
195+
size_t data_len = total_len - the_hash_algo->rawsz;
196+
197+
if (total_len < the_hash_algo->rawsz)
198+
return 0; /* say "too short"? */
199+
200+
the_hash_algo->init_fn(&ctx);
201+
the_hash_algo->update_fn(&ctx, data, data_len);
202+
the_hash_algo->final_fn(got, &ctx);
203+
204+
return hasheq(got, data + data_len);
205+
}

csum-file.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ void hashflush(struct hashfile *f);
4242
void crc32_begin(struct hashfile *);
4343
uint32_t crc32_end(struct hashfile *);
4444

45+
/* Verify checksum validity while reading. Returns non-zero on success. */
46+
int hashfile_checksum_valid(const unsigned char *data, size_t len);
47+
4548
/*
4649
* Returns the total number of bytes fed to the hashfile so far (including ones
4750
* that have not been written out to the descriptor yet).

pack-check.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,13 @@ static int verify_packfile(struct repository *r,
164164

165165
int verify_pack_index(struct packed_git *p)
166166
{
167-
size_t len;
168-
const unsigned char *index_base;
169-
git_hash_ctx ctx;
170-
unsigned char hash[GIT_MAX_RAWSZ];
171167
int err = 0;
172168

173169
if (open_pack_index(p))
174170
return error("packfile %s index not opened", p->pack_name);
175-
index_base = p->index_data;
176-
len = p->index_size - the_hash_algo->rawsz;
177171

178172
/* Verify SHA1 sum of the index file */
179-
the_hash_algo->init_fn(&ctx);
180-
the_hash_algo->update_fn(&ctx, index_base, len);
181-
the_hash_algo->final_fn(hash, &ctx);
182-
if (!hasheq(hash, index_base + len))
173+
if (!hashfile_checksum_valid(p->index_data, p->index_size))
183174
err = error("Packfile index for %s hash mismatch",
184175
p->pack_name);
185176
return err;

0 commit comments

Comments
 (0)