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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
void *
erealloc(void *ptr, size_t n)
{
ptr = realloc(ptr, n);
if (!ptr) {
fprintf(stderr, "%s: %s\n", argv0, strerror(errno));
exit(2);
}
return ptr;
}
void *
emalloc(size_t n)
{
void *ptr = malloc(n);
if (!ptr) {
fprintf(stderr, "%s: %s\n", argv0, strerror(errno));
exit(2);
}
return ptr;
}
static int
parse_fd(const char *name)
{
long int num;
char *end;
if (!isdigit(*name))
return -1;
errno = 0;
num = strtol(name, &end, 10);
if (num > INT_MAX || *end || errno)
return -1;
return (int)num;
}
int
open_file(const char *path, int *closep)
{
int fd = -1;
*closep = 0;
if (!strcmp(path, "-"))
fd = STDIN_FILENO;
else if (!strcmp(path, "/dev/stdin"))
fd = STDIN_FILENO;
else if (!strcmp(path, "/dev/stdout"))
fd = STDOUT_FILENO;
else if (!strcmp(path, "/dev/stderr"))
fd = STDERR_FILENO;
else if (!strncmp(path, "/dev/fd/", sizeof("/dev/fd/") - 1))
fd = parse_fd(&path[sizeof("/dev/fd/") - 1]);
else if (!strncmp(path, "/proc/self/fd/", sizeof("/proc/self/fd/") - 1))
fd = parse_fd(&path[sizeof("/proc/self/fd/") - 1]);
if (fd < 0) {
fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
*closep = 1;
}
return fd;
}
static int
check_and_print_file(const char *path, size_t hashlen, int decode_hex, char *expected)
{
unsigned char *hash;
int r, fd, close_fd;
fd = open_file(path, &close_fd);
if (fd < 0) {
if (errno != ENOENT)
fprintf(stderr, "%s: %s: %s\n", argv0, path, strerror(errno));
missing:
printf("%s: Missing\n", path);
return -1;
}
hash = hashlen ? emalloc(hashlen / 8) : NULL;
r = hash_fd(fd, path, decode_hex, hash);
if (close_fd)
close(fd);
if (r < 0) {
free(hash);
goto missing;
}
libblake_decode_hex(expected, hashlen / 4, expected, &(int){0});
if (!memcmp(hash, expected, hashlen / 8)) {
free(hash);
printf("%s: OK\n", path);
return 0;
} else {
free(hash);
printf("%s: Fail\n", path);
return -1;
}
}
int
check_and_print(const char *path, size_t hashlen, int decode_hex, char newline)
{
int fd, close_fd, status = 0;
char *buf = NULL;
size_t size = 0;
size_t len = 0;
ssize_t r;
size_t i, j, k;
fd = open_file(path, &close_fd);
if (fd < 0) {
fprintf(stderr, "%s: %s: %s\n", argv0, path, strerror(errno));
exit(2);
}
for (;;) {
if (len == size)
buf = erealloc(buf, size += 8 << 10);
r = read(fd, &buf[len], size - len);
if (r > 0) {
len += (size_t)r;
} else if (!r) {
break;
} else if (errno == EINTR) {
continue;
} else {
fprintf(stderr, "%s: %s: %s\n", argv0, path, strerror(errno));
exit(2);
}
}
buf = erealloc(buf, len + 1);
buf[len] = '\0';
if (newline) {
for (i = 0; i < len; i = k + 1) {
while (isspace(buf[i]))
i++;
for (j = i; j - i < hashlen / 4; j++)
if (!isxdigit(buf[j]))
goto corrupt;
if (j == len || !isblank(buf[j]))
goto corrupt;
buf[j] = '\0';
j++;
while (isblank(buf[j]))
j++;
if (!buf[j])
goto corrupt;
for (k = j; buf[k] && buf[k] != newline;)
k++;
buf[k] = '\0';
status |= check_and_print_file(&buf[j], hashlen, decode_hex, &buf[i]);
}
} else {
for (i = 0; i < len; i = k + 1) {
for (j = i; j - i < hashlen / 4; j++)
if (!isxdigit(buf[j]))
goto corrupt;
if (buf[j + 0] != ' ' || buf[j + 1] != ' ')
goto corrupt;
buf[j] = '\0';
j += 2;
k = j + strlen(&buf[j]);
status |= check_and_print_file(&buf[j], hashlen, decode_hex, &buf[i]);
}
}
if (close_fd)
close(fd);
return status;
corrupt:
fprintf(stderr, "%s: %s: invalid file content\n", argv0, path);
exit(2);
}
static int
hash_file(const char *path, int decode_hex, unsigned char hash[])
{
int ret, fd, close_fd;
fd = open_file(path, &close_fd);
if (fd < 0) {
fprintf(stderr, "%s: %s: %s\n", argv0, path, strerror(errno));
return -1;
}
ret = hash_fd(fd, path, decode_hex, hash);
if (close_fd)
close(fd);
return ret;
}
int
hash_and_print(const char *path, size_t hashlen, int decode_hex, char newline, int output_case)
{
unsigned char *hash;
char *hex;
hash = hashlen ? emalloc(hashlen / 8) : 0;
hex = emalloc(hashlen / 4 + 1);
if (hash_file(path, decode_hex, hash)) {
free(hash);
free(hex);
return -1;
}
if (output_case < 0) {
fwrite(hash, 1, hashlen / 8, stdout);
} else {
libblake_encode_hex(hash, hashlen / 8, hex, output_case);
printf("%s %s%c", hex, path, newline);
}
free(hash);
free(hex);
return 0;
}
|