-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
192 lines (159 loc) · 3.79 KB
/
util.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
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
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#ifdef DEBUG
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "includes.h"
/* prints a string to stderr then exits
* TODO: replace with logging lib
*/
void
error(char *s)
{
fprintf(stderr, "%s\n", s);
exit(1);
}
/* takes a key and a BEncoded dictionary node and searches for
* the corresponding value
*/
struct BEncode
*find_value(char *key, struct BDictNode *d)
{
struct BDictNode *pt = d;
struct BDictNode *next = d;
while (next) {
next = pt->next;
if (strncmp(pt->key, key, strlen(key)) == 0)
return pt->value;
pt = next;
}
return NULL;
}
void
freeBList(struct BListNode *l)
{
struct BListNode *pt = l;
struct BListNode *next = l;
while (next) {
next = pt->next;
freeBEncode(pt->cargo);
free(pt);
pt = next;
}
}
void
freeBDict(struct BDictNode *d)
{
struct BDictNode *pt = d;
struct BDictNode *next = d;
while (next) {
next = pt->next;
free(pt->key);
freeBEncode(pt->value);
free(pt);
pt = next;
}
return;
}
void
freeBEncode(struct BEncode *b)
{
if (b->type == BInt) {
free(b);
return;
}
if (b->type == BString) {
free(b->cargo.bStr);
free(b);
return;
}
if (b->type == BList) {
freeBList(b->cargo.bList);
free(b);
return;
}
if (b->type == BDict) {
freeBDict(b->cargo.bDict);
free(b);
return;
}
}
void
print_sha1(uint8_t *sha1)
{
int i;
for (i = 0; i < 20; i++)
printf("%d ", sha1[i]);
printf("\n");
}
int
mkpath(const char *s, mode_t mode)
{
char *r, *path, *up;
int rv;
rv = -1;
if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
return 0;
if ((path = strdup(s)) == NULL)
exit(EXIT_FAILURE);
if ((r = dirname(strdup(s))) == NULL)
goto out;
if ((up = strdup(r)) == NULL)
exit(EXIT_FAILURE);
if ((mkpath(up, mode) == -1) && (errno != EEXIST))
goto out;
if ((mkdir(path, mode) == -1) && (errno != EEXIST))
rv = -1;
else
rv = 0;
out:
if (up != NULL)
free(up);
free(path);
return rv;
}
#ifdef DEBUG
/* saves piece from known-good file to a file
* for later comparison with the output of
* the file from write_incorrect_piece
*/
void
write_correct_piece (uint32_t piece_length, uint32_t index)
{
FILE* known_good = fopen("known_good.iso", "r+");
FILE* correct_piece = fopen("correct_piece.out", "w+");
/* get file size for MMAPing */
struct stat st;
stat("known_good.iso", &st);
long size = st.st_size;
void* addr = mmap(0,
size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fileno(known_good),
0);
write(fileno(correct_piece), addr + piece_length * index, piece_length);
munmap(addr, size);
fclose(known_good);
fclose(correct_piece);
}
/* saves piece that failed verification to a file
* for later inspection with the likes of hexdump.
* only available if built with -DDEBUG
*/
void
write_incorrect_piece(void *piece, uint32_t piece_length, uint32_t index)
{
FILE *failed_piece = fopen("failed_piece.out", "w+");
write(fileno(failed_piece), piece, piece_length);
fclose(failed_piece);
printf("ERROR: Wrote piece #%d to failed_piece.out for inspection with hexdump.\n",
index);
write_correct_piece(piece_length, index);
exit(1);
}
#endif