| 2 | #include "hex.h" |
| 3 | |
| 4 | int cmd_hash_impl(int ac, const char **av, int algo, int unsafe) |
| 5 | { |
| 6 | struct git_hash_ctx ctx; |
| 7 | unsigned char hash[GIT_MAX_HEXSZ]; |
| 8 | unsigned bufsz = 8192; |
| 9 | int binary = 0; |
| 10 | char *buffer; |
| 11 | const struct git_hash_algo *algop = &hash_algos[algo]; |
| 12 | if (unsafe) |
| 13 | algop = unsafe_hash_algo(algop); |
| 14 | |
| 15 | if (ac == 2) { |
| 16 | if (!strcmp(av[1], "-b")) |
| 17 | binary = 1; |
| 18 | else |
| 19 | bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024; |
| 20 | } |
| 21 | |
| 22 | if (!bufsz) |
| 23 | bufsz = 8192; |
| 24 | |
| 25 | while ((buffer = malloc(bufsz)) == NULL) { |
| 26 | fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz); |
| 27 | bufsz /= 2; |
| 28 | if (bufsz < 1024) |
| 29 | die("OOPS"); |
| 30 | } |
| 31 | |
| 32 | algop->init_fn(&ctx); |
| 33 | |
| 34 | while (1) { |
| 35 | ssize_t sz, this_sz; |
| 36 | char *cp = buffer; |
| 37 | unsigned room = bufsz; |
| 38 | this_sz = 0; |
| 39 | while (room) { |
| 40 | sz = xread(0, cp, room); |
| 41 | if (sz == 0) |
| 42 | break; |
| 43 | if (sz < 0) |
| 44 | die_errno("test-hash"); |
| 45 | this_sz += sz; |
| 46 | cp += sz; |
| 47 | room -= sz; |
| 48 | } |
| 49 | if (this_sz == 0) |
| 50 | break; |
| 51 | git_hash_update(&ctx, buffer, this_sz); |
| 52 | } |
| 53 | git_hash_final(hash, &ctx); |
| 54 | |
| 55 | if (binary) |
| 56 | fwrite(hash, 1, algop->rawsz, stdout); |
| 57 | else |
| 58 | puts(hash_to_hex_algop(hash, algop)); |
| 59 | free(buffer); |
| 60 | return 0; |
| 61 | } |
no test coverage detected