-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreehash.c
84 lines (75 loc) · 1.7 KB
/
treehash.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
struct node_sha {
unsigned char sha[SHA256_DIGEST_LENGTH];
struct node_sha *next;
};
typedef struct node_sha sha;
size_t min(size_t a, size_t b) {
return a < b ? a : b;
}
void combine(sha *a, sha *b) {
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, a->sha, SHA256_DIGEST_LENGTH);
SHA256_Update(&ctx, b->sha, SHA256_DIGEST_LENGTH);
SHA256_Final(a->sha, &ctx);
}
void read_one_meg(unsigned char *sha) {
unsigned char buf[BUFSIZ];
size_t mb = 1024 * 1024;
size_t total_amount_read = 0;
size_t amount_read = 0;
size_t to_read;
int eof = 0;
SHA256_CTX ctx;
SHA256_Init(&ctx);
while(!eof && total_amount_read < mb) {
to_read = min(mb - total_amount_read, BUFSIZ);
amount_read = fread(buf, 1, to_read, stdin);
eof = amount_read != to_read;
total_amount_read += amount_read;
SHA256_Update(&ctx, buf, amount_read);
}
SHA256_Final(sha, &ctx);
}
void printbuf(unsigned char *buf) {
int len;
for (len = 0; len < SHA256_DIGEST_LENGTH; ++len)
printf("%02x", buf[len]);
printf("\n");
}
void reduce(sha *head) {
sha *a, *b;
while (head->next) {
a = head;
while (a && a->next) {
b = a->next;
combine(a,b);
a->next = b->next;
a = b->next;
free(b);
}
}
}
int stdin_has_data() {
int c;
c = fgetc(stdin);
ungetc(c, stdin);
return c != EOF;
}
int main(int argc, char **argv) {
sha* head;
sha* cur;
cur = head = (sha *) calloc(sizeof(sha), 1);
while (stdin_has_data()) {
cur->next = (sha *) calloc(sizeof(sha), 1);
cur = cur->next;
read_one_meg(cur->sha);
}
reduce(head->next);
printbuf(head->next->sha);
return 0;
}