unpack_sha1_header(): detect malformed object header
[git/debian.git] / test-sha1.c
blobe57eae10bf73baac79fd8b95ddb0ff1b4c8c0cd6
1 #include "cache.h"
3 int main(int ac, char **av)
5 git_SHA_CTX ctx;
6 unsigned char sha1[20];
7 unsigned bufsz = 8192;
8 int binary = 0;
9 char *buffer;
11 if (ac == 2) {
12 if (!strcmp(av[1], "-b"))
13 binary = 1;
14 else
15 bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
18 if (!bufsz)
19 bufsz = 8192;
21 while ((buffer = malloc(bufsz)) == NULL) {
22 fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
23 bufsz /= 2;
24 if (bufsz < 1024)
25 die("OOPS");
28 git_SHA1_Init(&ctx);
30 while (1) {
31 ssize_t sz, this_sz;
32 char *cp = buffer;
33 unsigned room = bufsz;
34 this_sz = 0;
35 while (room) {
36 sz = xread(0, cp, room);
37 if (sz == 0)
38 break;
39 if (sz < 0)
40 die_errno("test-sha1");
41 this_sz += sz;
42 cp += sz;
43 room -= sz;
45 if (this_sz == 0)
46 break;
47 git_SHA1_Update(&ctx, buffer, this_sz);
49 git_SHA1_Final(sha1, &ctx);
51 if (binary)
52 fwrite(sha1, 1, 20, stdout);
53 else
54 puts(sha1_to_hex(sha1));
55 exit(0);