-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfspec-hash.c
More file actions
89 lines (81 loc) · 1.75 KB
/
fspec-hash.c
File metadata and controls
89 lines (81 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <blake3.h>
#include "common.h"
static char *argv0;
static void
usage(void)
{
fprintf(stderr, "usage: %s\n", argv0);
exit(1);
}
static void
fspec(char *pos, size_t len)
{
char *source, *end;
int reg = 0, hash = 1;
end = memchr(pos, '\n', len);
assert(end);
if (fwrite(pos, 1, end - pos + 1, stdout) != end - pos + 1)
fatal("write:");
*end = 0;
source = pos + 1;
len -= end + 1 - pos;
pos = end + 1;
while (len > 0) {
end = memchr(pos, '\n', len);
assert(end);
if (fwrite(pos, 1, end - pos + 1, stdout) != end - pos + 1)
fatal("write:");
*end = 0;
if (len >= 5 && memcmp(pos, "type=", 5) == 0) {
reg = strcmp(pos + 5, "reg") == 0;
} else if (len >= 7 && memcmp(pos, "source=", 7) == 0) {
source = pos + 7;
} else if (len >= 7 && memcmp(pos, "blake3=", 7) == 0) {
hash = 0;
}
len -= end + 1 - pos;
pos = end + 1;
}
if (reg && hash) {
FILE *file;
blake3_hasher ctx;
char buf[16384];
size_t len;
unsigned char out[BLAKE3_OUT_LEN];
file = fopen(source, "rb");
if (!file)
fatal("open %s:", source);
blake3_hasher_init(&ctx);
do {
len = fread(buf, 1, sizeof(buf), file);
blake3_hasher_update(&ctx, buf, len);
} while (len == sizeof(buf));
if (ferror(file))
fatal("read %s:", source);
blake3_hasher_finalize(&ctx, out, sizeof(out));
fclose(file);
fputs("blake3=", stdout);
for (size_t i = 0; i < sizeof(out); ++i)
printf("%02x", out[i]);
fputc('\n', stdout);
}
fputc('\n', stdout);
}
int
main(int argc, char *argv[])
{
argv0 = argc ? argv[0] : "fspec-hash";
if (argc)
++argv, --argc;
if (argc)
usage();
parse(stdin, fspec);
fflush(stdout);
if (ferror(stdout))
fatal("write:");
}