git/t/helper/test-hash.c
Taylor Blau 3339180b28 t/helper/test-hash.c: use unsafe_hash_algo()
Remove a series of conditionals within the shared cmd_hash_impl() helper
that powers the 'sha1' and 'sha1-unsafe' helpers.

Instead, replace them with a single conditional that transforms the
specified hash algorithm into its unsafe variant. Then all subsequent
calls can directly use whatever function it wants to call without having
to decide between the safe and unsafe variants.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-01-23 10:28:17 -08:00

62 lines
1.1 KiB
C

#include "test-tool.h"
#include "hex.h"
int cmd_hash_impl(int ac, const char **av, int algo, int unsafe)
{
git_hash_ctx ctx;
unsigned char hash[GIT_MAX_HEXSZ];
unsigned bufsz = 8192;
int binary = 0;
char *buffer;
const struct git_hash_algo *algop = &hash_algos[algo];
if (unsafe)
algop = unsafe_hash_algo(algop);
if (ac == 2) {
if (!strcmp(av[1], "-b"))
binary = 1;
else
bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
}
if (!bufsz)
bufsz = 8192;
while ((buffer = malloc(bufsz)) == NULL) {
fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
bufsz /= 2;
if (bufsz < 1024)
die("OOPS");
}
algop->init_fn(&ctx);
while (1) {
ssize_t sz, this_sz;
char *cp = buffer;
unsigned room = bufsz;
this_sz = 0;
while (room) {
sz = xread(0, cp, room);
if (sz == 0)
break;
if (sz < 0)
die_errno("test-hash");
this_sz += sz;
cp += sz;
room -= sz;
}
if (this_sz == 0)
break;
algop->update_fn(&ctx, buffer, this_sz);
}
algop->final_fn(hash, &ctx);
if (binary)
fwrite(hash, 1, algop->rawsz, stdout);
else
puts(hash_to_hex_algop(hash, algop));
free(buffer);
return 0;
}