-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbencode.c
More file actions
513 lines (462 loc) · 13.4 KB
/
bencode.c
File metadata and controls
513 lines (462 loc) · 13.4 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* set ts=4 sw=4 enc=utf-8: -*- Mode: c; tab-width: 4; c-basic-offset:4; coding: utf-8 -*- */
/*
* Copyright 2017 Chul-Woong Yang (cwyang@gmail.com)
* Licensed under the Apache License, Version 2.0;
* See LICENSE for details.
*
* bencode.c
* 14 January 2017, cwyang@gmail.com
*
* Bittorrent bencode reader and writer module
*
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bencode.h"
#define EAT(BUF,LEN) (BUF)++,(LEN)--
#define EAT_N(BUF,LEN,N) (BUF)+=(N),(LEN)-=(N)
//#define BE_DEBUG
be_node_t *be_alloc(enum be_type type) {
be_node_t *ret = BE_CALLOC(1, sizeof(be_node_t));
if (ret) {
ret->type = type;
init_list_head(&ret->link);
if (type == LIST)
init_list_head(&ret->x.list_head);
else if (type == DICT)
init_list_head(&ret->x.dict_head);
}
return ret;
}
void be_free(be_node_t *node) {
list_t *l, *tmp;
if (node == NULL)
return;
list_del(&node->link);
switch (node->type) {
case STR:
BE_FREE(node->x.str.buf);
break;
case NUM:
break;
case LIST:
list_for_each_safe(l, tmp, &node->x.list_head) {
be_node_t *entry = list_entry(l, be_node_t, link);
be_free(entry);
}
break;
case DICT:
list_for_each_safe(l, tmp, &node->x.dict_head) {
be_dict_t *entry = list_entry(l, be_dict_t, link);
BE_FREE(entry->key.buf);
be_free(entry->val);
BE_FREE(entry);
}
break;
default:
assert(0);
break;
}
BE_FREE(node);
}
/* Parse until non-digit marker occurs.
'e' = '-e' = '00e' = ':' = '-:' = '00:'= 0
'999999999999999999999999999999999999999999999999999999999e' = LLONG_MAX
'-99999999999999999999999999999999999999999999999999999999e' = LLONG_MIN
'--e' = error (leaves '-e')
'123' = error (consumes all)
*/
static long long int be_decode_int(const char *buf, size_t len, size_t *rx) {
size_t orglen = len;
long long int ret = 0;
int sign = 1, overflowed = 0;
#ifdef BE_DEBUG
printf("%s: %s %d\n", __FUNCTION__, buf, (int) len);
#endif
if (*buf == '-') {
sign = -1;
EAT(buf,len);
}
while (len > 0) {
if (!isdigit(*buf))
break;
if (!overflowed) {
long long int tmp = ret;
ret = ret * 10;
ret += (*buf - '0') * sign;
if (sign == 1 && ret < tmp) { // overflow
overflowed = 1;
ret = LLONG_MAX;
} else if (sign == -1 && ret > tmp) { // underflow
overflowed = 1;
ret = LLONG_MIN;
}
}
EAT(buf,len);
}
*rx = orglen - len;
return ret;
}
static be_str_t be_decode_str(const char *buf, size_t len, size_t *rx) {
char *ret;
size_t orglen = len, n;
be_str_t str = { .buf = NULL, .len = 0 };
#ifdef BE_DEBUG
printf("%s: %s %d\n", __FUNCTION__, buf, (int) len);
#endif
long long int slen = be_decode_int(buf, len, &n);
EAT_N(buf,len,n);
if ((len == 0) ||
(slen < 0 || slen > len - 1) ||
(*buf != ':'))
goto out;
if ((ret = BE_MALLOC(slen + 1)) == NULL)
goto out;
str.len = slen;
str.buf = ret;
memcpy(ret, buf + 1, slen);
ret[slen] = '\0';
EAT_N(buf,len,slen+1);
out:
*rx = orglen - len;
return str;
}
#define DO_ERR(CODE) do { errno = CODE; goto out; } while (0)
#define ALLOC(T) do { \
ret = be_alloc(T); \
if (ret == NULL) DO_ERR(ENOMEM); \
} while (0)
#define CHECK2(COND, CODE) do { \
if (COND) { \
be_free(ret); \
ret = NULL; \
DO_ERR(CODE); \
} \
} while (0)
#define CHECK(COND) CHECK2(COND,EINVAL)
#define EAT_CHECK(BUF,LEN) do { \
EAT(BUF,LEN); \
CHECK(((LEN) == 0)); \
} while (0)
static be_node_t *be_decode1(const char *buf, size_t len, size_t *rx, int depth) {
size_t orglen = len, n;
be_node_t *ret = NULL, *entry;
#ifdef BE_DEBUG
printf("%s: %s %d %d\n", __FUNCTION__, buf, (int) len, depth);
#endif
if (depth > BE_MAX_DEPTH) { // recursion threshold exceeded
errno = ELOOP;
goto out;
}
if (len == 0) {
errno = EINVAL;
goto out;
}
switch (*buf) {
case 'i':
ALLOC(NUM);
EAT_CHECK(buf, len);
ret->x.num = be_decode_int(buf, len, &n); // we parse "i-0e" as 0
EAT_N(buf,len,n);
CHECK((*buf != 'e'));
EAT(buf, len);
break;
case '0'...'9':
ALLOC(STR);
ret->x.str = be_decode_str(buf, len, &n); // "0" should be return ""
EAT_N(buf,len,n);
CHECK((ret->x.str.buf == NULL));
break;
case 'l':
ALLOC(LIST);
EAT_CHECK(buf,len);
while (*buf != 'e') { // "le" return empty list
entry = be_decode1(buf, len, &n, depth+1);
EAT_N(buf,len,n);
CHECK((entry == NULL));
list_add_tail(&entry->link, &ret->x.list_head);
CHECK((len == 0));
}
EAT(buf,len);
break;
case 'd':
ALLOC(DICT);
EAT_CHECK(buf,len);
while (*buf != 'e') { // "de" return empty dictionary
be_dict_t *dict_entry = BE_CALLOC(1, sizeof(be_dict_t));
CHECK2((dict_entry == NULL), ENOMEM);
init_list_head(&dict_entry->link);
list_add_tail(&dict_entry->link, &ret->x.dict_head); // early link
dict_entry->key = be_decode_str(buf, len, &n);
EAT_N(buf,len,n);
CHECK((dict_entry->key.buf == NULL));
dict_entry->val = be_decode1(buf, len, &n, depth+1);
EAT_N(buf,len,n);
CHECK((dict_entry->val == NULL));
CHECK((len == 0));
}
EAT(buf,len);
break;
default: // error
errno = EINVAL;
goto out;
}
out:
*rx = orglen - len;
return ret;
}
/* when error, errno is set:
ENOMEM: malloc failed
ELOOP: recursion threshold met
EINVAL: bad format bencode file
*/
be_node_t *be_decode(const char *inBuf, size_t inBufLen, size_t *readAmount) {
return be_decode1(inBuf, inBufLen, readAmount, 1);
}
static void newline(int indent) {
int i;
putchar('\n');
for (i = 0; i < indent; i++)
putchar(' ');
}
static int be_str_dump(be_str_t *str)
{
int i;
printf("\"");
for (i = 0; i < str->len; i++) {
if (isprint(str->buf[i]))
printf("%c", str->buf[i]);
else
printf(".");
}
printf("\"");
return 2 + str->len;
}
static void be_dump1(be_node_t *node, int indent) {
int sz, first = 1;
list_t *l;
switch (node->type) {
case NUM:
printf("%lld", node->x.num);
break;
case STR:
be_str_dump(&node->x.str);
break;
case LIST:
list_for_each(l, &node->x.list_head) {
be_node_t *entry = list_entry(l, be_node_t, link);
if (first) {
printf("[ ");
first = 0;
} else {
newline(indent);
printf (", ");
}
be_dump1(entry, indent+2);
}
printf("]");
break;
case DICT:
list_for_each(l, &node->x.list_head) {
be_dict_t *entry = list_entry(l, be_dict_t, link);
if (first) {
printf("{ ");
first = 0;
} else {
newline(indent);
printf (", ");
}
sz = be_str_dump(&entry->key);
printf(": ");
be_dump1(entry->val, indent+sz+4);
}
printf("}");
}
}
void be_dump(be_node_t *node) {
be_dump1(node, 0);
printf("\n");
}
#define TMPBUFLEN 32 /* enough to hold LLONG_MAX and some chars */
static ssize_t be_encode_str(const be_str_t *str, char *outBuf, size_t outBufLen)
{
char tmpBuf[TMPBUFLEN];
int sz = snprintf(tmpBuf, TMPBUFLEN, "%lld:", str->len);
if (outBuf == NULL)
return sz + str->len;
if (sz + str->len > outBufLen)
return -1;
memcpy(outBuf, tmpBuf, sz);
memcpy(outBuf+sz, str->buf, str->len);
return sz + str->len;
}
/*
when outBuf == NULL, be_encode returns outBufLen needed
*/
ssize_t be_encode(const be_node_t *node, char *outBuf, size_t outBufLen) {
char tmpBuf[TMPBUFLEN];
ssize_t r;
int sz = 1;
list_t *l;
if (outBuf && outBufLen == 0)
return -1;
switch (node->type) {
case NUM:
sz = snprintf(tmpBuf, TMPBUFLEN, "i%llde", node->x.num);
if (outBuf != NULL) {
if (sz > outBufLen)
return -1;
strncpy(outBuf, tmpBuf, sz);
}
break;
case STR:
sz = be_encode_str(&node->x.str, outBuf, outBufLen);
if (sz < 0)
return -1;
break;
case LIST:
if (outBuf) {
*outBuf = 'l';
EAT(outBuf, outBufLen);
}
list_for_each(l, &node->x.list_head) {
be_node_t *entry = list_entry(l, be_node_t, link);
r = be_encode(entry, outBuf, outBufLen);
#define CHECK_AND_UPDATE do { \
if (r < 0) return -1; \
if (outBuf) EAT_N(outBuf, outBufLen, r); \
sz += r; \
} while (0)
CHECK_AND_UPDATE;
}
if (outBuf) {
if (outBufLen == 0)
return -1;
*outBuf = 'e';
}
sz++;
break;
case DICT:
if (outBuf) {
*outBuf = 'd';
EAT(outBuf, outBufLen);
}
list_for_each(l, &node->x.list_head) {
be_dict_t *entry = list_entry(l, be_dict_t, link);
r = be_encode_str(&entry->key, outBuf, outBufLen);
CHECK_AND_UPDATE;
r = be_encode(entry->val, outBuf, outBufLen);
CHECK_AND_UPDATE;
}
if (outBuf) {
if (outBufLen == 0)
return -1;
*outBuf = 'e';
}
sz++;
break;
}
return sz;
}
/*************************/
void be_dict_free(be_dict_t *dict) {
if (dict == NULL)
return;
list_del(&dict->link);
BE_FREE(dict->key.buf);
be_free(dict->val);
BE_FREE(dict);
}
be_node_t *be_dict_lookup(be_node_t *node, const char *key, be_dict_t **dict_entry) {
list_t *l;
if (node->type != DICT)
return NULL;
list_for_each(l, &node->x.dict_head) {
be_dict_t *entry = list_entry(l, be_dict_t, link);
if (entry->key.buf && (strcmp(key, entry->key.buf) == 0)) {
if (dict_entry) *dict_entry = entry;
return entry->val;
}
}
return NULL;
}
long long int be_dict_lookup_num(be_node_t *node, const char *key) {
be_node_t *entry;
entry = be_dict_lookup(node, key, NULL);
if (entry == NULL || entry->type != NUM)
return -1;
return entry->x.num;
}
char *be_dict_lookup_cstr(be_node_t *node, const char *key) {
be_node_t *entry;
entry = be_dict_lookup(node, key, NULL);
if (entry == NULL || entry->type != STR)
return NULL;
return entry->x.str.buf;
}
char *be_dict_lookup_cstr_size(be_node_t *node, const char *key, int *size) {
be_node_t *entry;
entry = be_dict_lookup(node, key, NULL);
if (entry == NULL || entry->type != STR)
return NULL;
if (size) *size = entry->x.str.len;
return entry->x.str.buf;
}
int be_dict_add(be_node_t *dict, const char *keystr, be_node_t *val) {
be_dict_t *dict_entry = BE_CALLOC(1, sizeof(be_dict_t));
if (dict_entry == NULL)
return -1;
init_list_head(&dict_entry->link);
be_str_t *key = &dict_entry->key;
key->buf = BE_STRDUP(keystr);
key->len = strlen(keystr);
if (key->buf == NULL) {
BE_FREE(dict_entry);
return -1;
}
dict_entry->val = val;
list_add_tail(&dict_entry->link, &dict->x.dict_head);
return 0;
}
int be_dict_add_str(be_node_t *dict, const char *keystr, char *valstr) {
be_node_t *val = be_alloc(STR);
if (val == NULL)
return -1;
val->x.str.buf = BE_STRDUP(valstr);
val->x.str.len = strlen(valstr);
return be_dict_add(dict, keystr, val);
}
int be_dict_add_str_with_len(be_node_t *dict, const char *keystr, char *valstr, int len) {
be_node_t *val = be_alloc(STR);
if (val == NULL)
return -1;
char *c = BE_MALLOC(len);
if (c == NULL) {
BE_FREE(c);
return -1;
}
memcpy(c, valstr, len);
val->x.str.buf = c;
val->x.str.len = len;
return be_dict_add(dict, keystr, val);
}
int be_dict_add_num(be_node_t *dict, const char *keystr, long long int valnum) {
be_node_t *val = be_alloc(NUM);
if (val == NULL)
return -1;
val->x.num = valnum;
return be_dict_add(dict, keystr, val);
}
be_dict_t *be_dict_entry_alloc(void)
{
be_dict_t *dict_entry = BE_CALLOC(1, sizeof(be_dict_t));
if (dict_entry == NULL)
return NULL;
init_list_head(&dict_entry->link);
return dict_entry;
}