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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#include "benc.h"
#include "log.h"
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#define MAX(a, b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
#define MIN(a, b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})
bool readint(const char** loc, int64_t* val) {
char* end = NULL;
errno = 0;
*val = strtol(*loc, &end, 10);
*loc = end;
return errno == 0;
}
bool digit(const char c) {
return c >= '0' && c <= '9';
}
char* nodenames[] = {
"INT",
"STR",
"LIST",
"DICT",
"END",
};
void indent(int depth) {
for(int i = 0; i < depth; i++) {
printf(" ");
}
}
void benc_print(const struct benc_node* stream, size_t stream_len) {
int depth = 0;
const struct benc_node* cursor = stream;
for(; cursor < stream + stream_len; cursor++) {
switch(cursor->type) {
case BNT_INT:
indent(depth);
printf("INT %.*s\n", cursor->size, cursor->loc);
break;
case BNT_STRING:
indent(depth);
bool allprint = true;
for (const char* c = cursor->loc; c < cursor->loc + cursor->size; c++) {
if(!isalnum(*c) && *c != '_') {
allprint = false;
break;
}
}
printf("STR ");
if(allprint) {
printf("%.*s", cursor->size, cursor->loc);
} else {
for (const unsigned char* c = (const unsigned char*)cursor->loc; c < (unsigned char*)(cursor->loc + cursor->size); c++) {
printf("\\x%02X", *c);
}
}
printf("\n");
break;
case BNT_LIST:
indent(depth);
printf("LIST\n");
depth = cursor->depth + 1;
break;
case BNT_DICT:
indent(depth);
printf("DICT\n");
depth = cursor->depth + 1;
break;
case BNT_END:
depth = cursor->depth;
indent(depth);
printf("END\n");
break;
}
}
}
int64_t benc_decode(const char** cursor, const char* end, int* depth, struct benc_node* stream, size_t stream_len) {
size_t cursor_out = 0;
while(cursor_out < stream_len) {
struct benc_node* node = &stream[cursor_out];
if(**cursor == 'i') {
node->type = BNT_INT;
(*cursor)++;
if(*cursor >= end) {
return -BENC_EBADP;
}
node->loc = *cursor;
while(**cursor != 'e') {
if(**cursor != '-' && !digit(**cursor)) {
return -BENC_EBADP;
}
(*cursor)++;
if(*cursor >= end) {
return -BENC_EBADP;
}
}
node->size = *cursor - node->loc;
(*cursor)++;
} else if(**cursor == 'l') {
node->type = BNT_LIST;
node->depth = *depth;
(*depth)++;
node->loc = *cursor;
(*cursor)++;
if(*cursor >= end) {
return -BENC_EBADP;
}
} else if(**cursor == 'd') {
node->type = BNT_DICT;
node->depth = *depth;
(*depth)++;
node->loc = *cursor;
(*cursor)++;
if(*cursor >= end) {
return -BENC_EBADP;
}
} else if(**cursor == 'e') {
node->type = BNT_END;
(*depth)--;
node->depth = *depth;
node->loc = *cursor;
(*cursor)++;
} else if(digit(**cursor)) {
node->type = BNT_STRING;
int64_t val;
bool rc = readint(cursor, &val);
node->size = val;
if(*cursor >= end) {
return -BENC_EBADP;
}
assert(rc);
if(**cursor != ':') {
return -BENC_EBADP;
}
(*cursor)++;
if(*cursor >= end) {
return -BENC_EBADP;
}
node->loc = *cursor;
(*cursor) += node->size;
if(*cursor > end) {
return -BENC_EBADP;
}
} else {
return -BENC_EBADP;
dbg("Failing on char \"%c\"", **cursor);
assert(false);
}
cursor_out++;
if(*depth == 0) break;
}
return cursor_out;
}
int bcur_fill(struct bcursor* cursor, size_t ignoring) {
if(cursor->source == cursor->source_end) {
return BENC_EBADP;
}
int read;
while(true) {
read = benc_decode(&cursor->source, cursor->source_end, &cursor->source_depth, cursor->base, cursor->base_len);
if(read < 0) {
return BENC_EBADP;
}
if(read > ignoring) {
break;
}
ignoring -= read;
}
cursor->readhead = cursor->base + ignoring;
cursor->end = cursor->base + read;
return 0;
}
int bcur_open(struct bcursor* cursor, const char* source, const char* source_end, struct benc_node* buffer, size_t buffer_len) {
cursor->source = source;
(*((const char**)&cursor->source_end)) = source_end;
cursor->source_depth = 0;
*((struct benc_node**)&cursor->base) = buffer;
*((size_t*)&cursor->base_len) = buffer_len;
cursor->readhead = buffer;
return bcur_fill(cursor, 0);
}
int bcur_next(struct bcursor* cursor, uint32_t steps) {
cursor->readhead += steps;
if(cursor->readhead >= cursor->end) {
return bcur_fill(cursor, cursor->readhead - cursor->end);
}
return 0;
}
int bcur_next_sibling(struct bcursor* cursor) {
assert(cursor->readhead->type == BNT_LIST || cursor->readhead->type == BNT_DICT);
if(cursor->readhead->type == BNT_LIST || cursor->readhead->type == BNT_DICT) {
uint32_t tdepth = cursor->readhead->depth;
cursor->readhead++;
while(cursor->readhead->type != BNT_END || cursor->readhead->depth != tdepth) {
if(bcur_next(cursor, 1) != 0) {
return BENC_EBADP;
}
}
}
return bcur_next(cursor, 1);
}
// All the arrays should be equal length
ssize_t bcur_find_key(struct bcursor* cursor, const enum benc_nodetype* keyTypes, const char** keyValues, const size_t* keyLengths, const size_t keys) {
// @ROBUSTNESS: Check if keytypes are anything but strings and ints because that is not supported
int rc;
while(true) {
// Skip lists
if(cursor->readhead->type == BNT_LIST || cursor->readhead->type == BNT_DICT) {
rc = bcur_next_sibling(cursor);
if(rc != 0) {
return -rc;
}
} else {
// Check if the key one of the ones we are looking for
for(size_t i = 0; i < keys; i++) {
if(cursor->readhead->type == keyTypes[i] && memcmp(cursor->readhead->loc, keyValues[i], MIN(cursor->readhead->size, keyLengths[i])) == 0) {
return i;
}
}
}
// Skip the key part
rc = bcur_next(cursor, 1);
if(rc != 0) {
return -rc;
}
if(cursor->readhead->type == BNT_LIST || cursor->readhead->type == BNT_DICT) {
// Skip a multitoken element
rc = bcur_next_sibling(cursor);
if(rc != 0) {
return -rc;
}
} else {
// Skip a single token element
rc = bcur_next(cursor, 1);
if(rc != 0) {
return -rc;
}
}
if(cursor->readhead->type == BNT_END) {
return -BENC_EENDP;
}
}
}
void skip_sibling(const struct benc_node** cursor, const struct benc_node* end) {
assert((*cursor)->type == BNT_LIST || (*cursor)->type == BNT_DICT);
if((*cursor)->type == BNT_LIST || (*cursor)->type == BNT_DICT) {
uint32_t tdepth = (*cursor)->depth;
(*cursor)++;
while(true) {
if(*cursor == end) {
fatal("Invalid dict");
}
if((*cursor)->type == BNT_END && (*cursor)->depth == tdepth) {
break;
}
(*cursor)++;
}
(*cursor)++;
} else {
(*cursor)++;
}
}
// All the arrays should be equal length
ssize_t skip_to_key(const struct benc_node** cursor, const struct benc_node* end, const enum benc_nodetype* keyTypes, const char** keyValues, const size_t* keyLengths, const size_t keys) {
// @ROBUSTNESS: Check if keytypes are anything but strings and ints because that is not supported
while(true) {
if(*cursor > end-1) {
return -1;
}
// Skip lists
if((*cursor)->type == BNT_LIST || (*cursor)->type == BNT_DICT) {
skip_sibling(cursor, end);
} else {
for(size_t i = 0; i < keys; i++) {
if((*cursor)->type == keyTypes[i] && memcmp((*cursor)->loc, keyValues[i], MIN((*cursor)->size, keyLengths[i])) == 0) {
return i;
}
}
}
(*cursor) += 2;
if((*cursor-1)->type == BNT_LIST || (*cursor-1)->type == BNT_DICT) {
(*cursor)--;
skip_sibling(cursor, end);
}
if((*cursor)->type == BNT_END) {
return -1;
}
}
}
|