csum-file.c: use unsafe_hash_algo()

Instead of calling the unsafe_ hash function variants directly, make use
of the shared 'algop' pointer by initializing it to:

    f->algop = unsafe_hash_algo(the_hash_algo);

, thus making all calls use the unsafe variants directly.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Taylor Blau
2025-01-23 12:34:33 -05:00
committed by Junio C Hamano
parent 7b081d2f70
commit f0c266af4e

View File

@@ -50,7 +50,7 @@ void hashflush(struct hashfile *f)
if (offset) { if (offset) {
if (!f->skip_hash) if (!f->skip_hash)
f->algop->unsafe_update_fn(&f->ctx, f->buffer, offset); f->algop->update_fn(&f->ctx, f->buffer, offset);
flush(f, f->buffer, offset); flush(f, f->buffer, offset);
f->offset = 0; f->offset = 0;
} }
@@ -73,7 +73,7 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
if (f->skip_hash) if (f->skip_hash)
hashclr(f->buffer, f->algop); hashclr(f->buffer, f->algop);
else else
f->algop->unsafe_final_fn(f->buffer, &f->ctx); f->algop->final_fn(f->buffer, &f->ctx);
if (result) if (result)
hashcpy(result, f->buffer, f->algop); hashcpy(result, f->buffer, f->algop);
@@ -128,7 +128,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
* f->offset is necessarily zero. * f->offset is necessarily zero.
*/ */
if (!f->skip_hash) if (!f->skip_hash)
f->algop->unsafe_update_fn(&f->ctx, buf, nr); f->algop->update_fn(&f->ctx, buf, nr);
flush(f, buf, nr); flush(f, buf, nr);
} else { } else {
/* /*
@@ -175,8 +175,8 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
f->do_crc = 0; f->do_crc = 0;
f->skip_hash = 0; f->skip_hash = 0;
f->algop = the_hash_algo; f->algop = unsafe_hash_algo(the_hash_algo);
f->algop->unsafe_init_fn(&f->ctx); f->algop->init_fn(&f->ctx);
f->buffer_len = buffer_len; f->buffer_len = buffer_len;
f->buffer = xmalloc(buffer_len); f->buffer = xmalloc(buffer_len);
@@ -210,7 +210,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo
{ {
hashflush(f); hashflush(f);
checkpoint->offset = f->total; checkpoint->offset = f->total;
f->algop->unsafe_clone_fn(&checkpoint->ctx, &f->ctx); f->algop->clone_fn(&checkpoint->ctx, &f->ctx);
} }
int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
@@ -221,7 +221,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
lseek(f->fd, offset, SEEK_SET) != offset) lseek(f->fd, offset, SEEK_SET) != offset)
return -1; return -1;
f->total = offset; f->total = offset;
f->algop->unsafe_clone_fn(&f->ctx, &checkpoint->ctx); f->algop->clone_fn(&f->ctx, &checkpoint->ctx);
f->offset = 0; /* hashflush() was called in checkpoint */ f->offset = 0; /* hashflush() was called in checkpoint */
return 0; return 0;
} }
@@ -242,15 +242,15 @@ int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
{ {
unsigned char got[GIT_MAX_RAWSZ]; unsigned char got[GIT_MAX_RAWSZ];
git_hash_ctx ctx; git_hash_ctx ctx;
const struct git_hash_algo *algop = the_hash_algo; const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo);
size_t data_len = total_len - algop->rawsz; size_t data_len = total_len - algop->rawsz;
if (total_len < algop->rawsz) if (total_len < algop->rawsz)
return 0; /* say "too short"? */ return 0; /* say "too short"? */
algop->unsafe_init_fn(&ctx); algop->init_fn(&ctx);
algop->unsafe_update_fn(&ctx, data, data_len); algop->update_fn(&ctx, data, data_len);
algop->unsafe_final_fn(got, &ctx); algop->final_fn(got, &ctx);
return hasheq(got, data + data_len, algop); return hasheq(got, data + data_len, algop);
} }