-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbencode.c
2674 lines (2363 loc) · 53.7 KB
/
bencode.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libbencodetools
*
* Written by Heikki Orsila <[email protected]> and
* Janne Kulmala <[email protected]> in 2011.
*/
#include <bencodetools/bencode.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#define die(fmt, args...) do { fprintf(stderr, "bencode: fatal error: " fmt, ## args); abort(); } while (0)
#define warn(fmt, args...) do { fprintf(stderr, "bencode: warning: " fmt, ## args); } while (0)
#define MAX_ALLOC (((size_t) -1) / sizeof(struct bencode *) / 2)
#define DICT_MAX_ALLOC (((size_t) -1) / sizeof(struct bencode_dict_node) / 2)
struct ben_decode_ctx {
const char *data;
const size_t len;
size_t off;
int error;
int level;
char c;
int line;
struct bencode_type **types;
};
struct ben_encode_ctx {
char *data;
size_t size;
size_t pos;
};
/*
* Buffer size for fitting all unsigned long long and long long integers,
* assuming it is at most 64 bits. If long long is larger than 64 bits,
* an error is produced when too large an integer is converted.
*/
#define LONGLONGSIZE 21
static struct bencode *decode_printed(struct ben_decode_ctx *ctx);
static int resize_dict(struct bencode_dict *d, size_t newalloc);
static int resize_list(struct bencode_list *list, size_t newalloc);
static int unpack(const struct bencode *b, struct ben_decode_ctx *ctx,
va_list *vl);
static struct bencode *pack(struct ben_decode_ctx *ctx, va_list *vl);
static size_t type_size(int type)
{
switch (type) {
case BENCODE_BOOL:
return sizeof(struct bencode_bool);
case BENCODE_DICT:
return sizeof(struct bencode_dict);
case BENCODE_INT:
return sizeof(struct bencode_int);
case BENCODE_LIST:
return sizeof(struct bencode_list);
case BENCODE_STR:
return sizeof(struct bencode_str);
default:
die("Unknown type: %d\n", type);
}
}
static void *alloc(int type)
{
struct bencode *b = calloc(1, type_size(type));
if (b == NULL)
return NULL;
b->type = type;
return b;
}
void *ben_alloc_user(struct bencode_type *type)
{
struct bencode_user *user = calloc(1, type->size);
if (user == NULL)
return NULL;
user->type = BENCODE_USER;
user->info = type;
return user;
}
static int insufficient(struct ben_decode_ctx *ctx)
{
ctx->error = BEN_INSUFFICIENT;
return -1;
}
static int invalid(struct ben_decode_ctx *ctx)
{
ctx->error = BEN_INVALID;
return -1;
}
static int mismatch(struct ben_decode_ctx *ctx)
{
ctx->error = BEN_MISMATCH;
return -1;
}
void *ben_insufficient_ptr(struct ben_decode_ctx *ctx)
{
ctx->error = BEN_INSUFFICIENT;
return NULL;
}
void *ben_invalid_ptr(struct ben_decode_ctx *ctx)
{
ctx->error = BEN_INVALID;
return NULL;
}
void *ben_oom_ptr(struct ben_decode_ctx *ctx)
{
ctx->error = BEN_NO_MEMORY;
return NULL;
}
int ben_need_bytes(const struct ben_decode_ctx *ctx, size_t n)
{
return ((ctx->off + n) <= ctx->len) ? 0 : -1;
}
char ben_current_char(const struct ben_decode_ctx *ctx)
{
return ctx->data[ctx->off];
}
const char *ben_current_buf(const struct ben_decode_ctx *ctx, size_t n)
{
return ben_need_bytes(ctx, n) ? NULL : ctx->data + ctx->off;
}
void ben_skip(struct ben_decode_ctx *ctx, size_t n)
{
ctx->off += n;
}
static struct bencode *internal_blob(void *data, size_t len)
{
struct bencode_str *b = alloc(BENCODE_STR);
if (b == NULL)
return NULL;
b->s = data;
b->len = len;
assert(b->s[len] == 0);
return (struct bencode *) b;
}
static void skip_to_next_line(struct ben_decode_ctx *ctx)
{
for (; ctx->off < ctx->len; ctx->off++) {
if (ben_current_char(ctx) == '\n') {
ctx->line++;
ctx->off++;
break;
}
}
}
static int seek_char(struct ben_decode_ctx *ctx)
{
while (ctx->off < ctx->len) {
char c = ben_current_char(ctx);
if (isspace(c)) {
if (c == '\n')
ctx->line++;
ctx->off++;
} else if (c == '#') {
/* Skip comment */
ctx->off++;
skip_to_next_line(ctx);
} else {
return 0;
}
}
return insufficient(ctx);
}
/*
* Test if string 's' is located at current position.
* Increment current position and return 0 if the string matches.
* Returns -1 otherwise. The function avoids buffer overflow.
*/
static int try_match(struct ben_decode_ctx *ctx, const char *s)
{
size_t n = strlen(s);
if (ben_need_bytes(ctx, n))
return -1;
if (memcmp(ctx->data + ctx->off, s, n) != 0)
return -1;
ctx->off += n;
return 0;
}
static int try_match_with_errors(struct ben_decode_ctx *ctx, const char *s)
{
size_t n = strlen(s);
size_t left = ctx->len - ctx->off;
assert(ctx->off <= ctx->len);
if (left == 0)
return insufficient(ctx);
if (left < n) {
if (memcmp(ctx->data + ctx->off, s, left) != 0)
return invalid(ctx);
return insufficient(ctx);
}
if (memcmp(ctx->data + ctx->off, s, n) != 0)
return invalid(ctx);
ctx->off += n;
return 0;
}
int ben_allocate(struct bencode *b, size_t n)
{
switch (b->type) {
case BENCODE_DICT:
return resize_dict(ben_dict_cast(b), n);
case BENCODE_LIST:
return resize_list(ben_list_cast(b), n);
default:
die("ben_allocate(): Unknown type %d\n", b->type);
}
}
static struct bencode *clone_dict(const struct bencode_dict *d)
{
struct bencode *key;
struct bencode *value;
struct bencode *newkey;
struct bencode *newvalue;
size_t pos;
struct bencode *newdict = ben_dict();
if (newdict == NULL)
return NULL;
ben_dict_for_each(key, value, pos, (const struct bencode *) d) {
newkey = ben_clone(key);
newvalue = ben_clone(value);
if (newkey == NULL || newvalue == NULL) {
ben_free(newkey);
ben_free(newvalue);
goto error;
}
if (ben_dict_set(newdict, newkey, newvalue)) {
ben_free(newkey);
ben_free(newvalue);
goto error;
}
newkey = NULL;
newvalue = NULL;
}
return (struct bencode *) newdict;
error:
ben_free(newdict);
return NULL;
}
static struct bencode *clone_list(const struct bencode_list *list)
{
struct bencode *value;
struct bencode *newvalue;
size_t pos;
struct bencode *newlist = ben_list();
if (newlist == NULL)
return NULL;
ben_list_for_each(value, pos, (const struct bencode *) list) {
newvalue = ben_clone(value);
if (newvalue == NULL)
goto error;
if (ben_list_append(newlist, newvalue)) {
ben_free(newvalue);
goto error;
}
newvalue = NULL;
}
return (struct bencode *) newlist;
error:
ben_free(newlist);
return NULL;
}
static struct bencode *clone_str(const struct bencode_str *s)
{
return ben_blob(s->s, s->len);
}
static struct bencode *share_dict(const struct bencode_dict *d)
{
struct bencode *newdict = ben_dict();
if (newdict == NULL)
return NULL;
memcpy(newdict, d, sizeof(*d));
((struct bencode_dict *) newdict)->shared = 1;
return newdict;
}
static struct bencode *share_list(const struct bencode_list *list)
{
struct bencode *newlist = ben_list();
if (newlist == NULL)
return NULL;
memcpy(newlist, list, sizeof(*list));
((struct bencode_list *) newlist)->shared = 1;
return newlist;
}
struct bencode *ben_clone(const struct bencode *b)
{
switch (b->type) {
case BENCODE_BOOL:
return ben_bool(ben_bool_const_cast(b)->b);
case BENCODE_DICT:
return clone_dict(ben_dict_const_cast(b));
case BENCODE_INT:
return ben_int(ben_int_const_cast(b)->ll);
case BENCODE_LIST:
return clone_list(ben_list_const_cast(b));
case BENCODE_STR:
return clone_str(ben_str_const_cast(b));
default:
die("Invalid type %c\n", b->type);
}
}
struct bencode *ben_shared_clone(const struct bencode *b)
{
switch (b->type) {
case BENCODE_DICT:
return share_dict(ben_dict_const_cast(b));
break;
case BENCODE_LIST:
return share_list(ben_list_const_cast(b));
break;
default:
return ben_clone(b);
}
}
static int cmp_dict(const struct bencode *a, const struct bencode *b)
{
size_t len = ben_dict_len(a);
size_t pos;
struct bencode *key;
struct bencode *va;
struct bencode *vb;
int ret = 0;
struct bencode_keyvalue *pairs;
if (len != ben_dict_len(b)) {
/* Returning any non-zero value is allowed */
return (len < ben_dict_len(b)) ? -1 : 1;
}
pairs = ben_dict_ordered_items(a);
for (pos = 0; pos < len; pos++) {
key = pairs[pos].key;
va = pairs[pos].value;
vb = ben_dict_get(b, key);
if (vb == NULL) {
/* Returning any non-zero value is allowed */
ret = (a < b) ? -1 : 1;
break;
}
ret = ben_cmp(va, vb);
if (ret)
break;
}
free(pairs);
return ret;
}
static int cmp_list(const struct bencode *a, const struct bencode *b)
{
const struct bencode_list *la;
const struct bencode_list *lb;
struct bencode *va;
struct bencode *vb;
size_t cmplen;
size_t i;
int ret;
la = ben_list_const_cast(a);
lb = ben_list_const_cast(b);
cmplen = (la->n <= lb->n) ? la->n : lb->n;
for (i = 0; i < cmplen; ++i) {
va = ben_list_get(a, i);
vb = ben_list_get(b, i);
ret = ben_cmp(va, vb);
if (ret)
return ret;
}
if (la->n != lb->n)
return (la->n < lb->n) ? -1 : 1;
return 0;
}
int ben_cmp(const struct bencode *a, const struct bencode *b)
{
size_t cmplen;
int ret;
const struct bencode_int *ia;
const struct bencode_int *ib;
const struct bencode_str *sa;
const struct bencode_str *sb;
const struct bencode_user *ua;
const struct bencode_user *ub;
if (a->type != b->type)
return (a->type == BENCODE_INT) ? -1 : 1;
switch (a->type) {
case BENCODE_INT:
ia = ben_int_const_cast(a);
ib = ben_int_const_cast(b);
if (ia->ll < ib->ll)
return -1;
if (ib->ll < ia->ll)
return 1;
return 0;
case BENCODE_STR:
sa = ben_str_const_cast(a);
sb = ben_str_const_cast(b);
cmplen = (sa->len <= sb->len) ? sa->len : sb->len;
ret = memcmp(sa->s, sb->s, cmplen);
if (ret)
return ret < 0 ? -1 : 1;
if (sa->len != sb->len)
return (sa->len < sb->len) ? -1 : 1;
return 0;
case BENCODE_DICT:
return cmp_dict(a, b);
case BENCODE_LIST:
return cmp_list(a, b);
case BENCODE_USER:
ua = ben_user_const_cast(a);
ub = ben_user_const_cast(b);
if (ua->info != ub->info)
return (a < b) ? -1 : 1;
return ua->info->cmp(a, b);
default:
die("Invalid type %c\n", b->type);
}
}
int ben_cmp_qsort(const void *a, const void *b)
{
const struct bencode *akey = ((const struct bencode_keyvalue *) a)->key;
const struct bencode *bkey = ((const struct bencode_keyvalue *) b)->key;
return ben_cmp(akey, bkey);
}
static struct bencode *decode_bool(struct ben_decode_ctx *ctx)
{
struct bencode_bool *b;
char value;
char c;
if (ben_need_bytes(ctx, 2))
return ben_insufficient_ptr(ctx);
ctx->off++;
c = ben_current_char(ctx);
if (c != '0' && c != '1')
return ben_invalid_ptr(ctx);
value = (c == '1');
b = alloc(BENCODE_BOOL);
if (b == NULL)
return ben_oom_ptr(ctx);
b->b = value;
ctx->off++;
return (struct bencode *) b;
}
static size_t hash_bucket(long long hash, const struct bencode_dict *d)
{
return hash & (d->alloc - 1);
}
static size_t hash_bucket_head(long long hash, const struct bencode_dict *d)
{
if (d->buckets == NULL)
return -1;
return d->buckets[hash_bucket(hash, d)];
}
static int resize_dict(struct bencode_dict *d, size_t newalloc)
{
size_t *newbuckets;
struct bencode_dict_node *newnodes;;
size_t pos;
if (newalloc == -1) {
if (d->alloc >= DICT_MAX_ALLOC)
return -1;
if (d->alloc == 0)
newalloc = 4;
else
newalloc = d->alloc * 2;
} else {
size_t x;
if (newalloc < d->n || newalloc > DICT_MAX_ALLOC)
return -1;
/* Round to next power of two */
x = 1;
while (x < newalloc)
x <<= 1;
assert(x >= newalloc);
newalloc = x;
if (newalloc > DICT_MAX_ALLOC)
return -1;
}
/* size must be a power of two */
assert((newalloc & (newalloc - 1)) == 0);
newbuckets = realloc(d->buckets, sizeof(newbuckets[0]) * newalloc);
newnodes = realloc(d->nodes, sizeof(newnodes[0]) * newalloc);
if (newnodes == NULL || newbuckets == NULL) {
free(newnodes);
free(newbuckets);
return -1;
}
d->alloc = newalloc;
d->buckets = newbuckets;
d->nodes = newnodes;
/* Clear all buckets */
memset(d->buckets, -1, d->alloc * sizeof(d->buckets[0]));
/* Reinsert nodes into buckets */
for (pos = 0; pos < d->n; pos++) {
struct bencode_dict_node *node = &d->nodes[pos];
size_t bucket = hash_bucket(node->hash, d);
node->next = d->buckets[bucket];
d->buckets[bucket] = pos;
}
return 0;
}
/* The string/binary object hash is copied from Python */
static long long str_hash(const unsigned char *s, size_t len)
{
long long hash;
size_t i;
if (len == 0)
return 0;
hash = s[0] << 7;
for (i = 0; i < len; i++)
hash = (1000003 * hash) ^ s[i];
hash ^= len;
if (hash == -1)
hash = -2;
return hash;
}
long long ben_str_hash(const struct bencode *b)
{
const struct bencode_str *bstr = ben_str_const_cast(b);
const unsigned char *s = (unsigned char *) bstr->s;
return str_hash(s, bstr->len);
}
long long ben_int_hash(const struct bencode *b)
{
long long x = ben_int_const_cast(b)->ll;
return (x == -1) ? -2 : x;
}
long long ben_hash(const struct bencode *b)
{
switch (b->type) {
case BENCODE_INT:
return ben_int_hash(b);
case BENCODE_STR:
return ben_str_hash(b);
default:
die("hash: Invalid type: %d\n", b->type);
}
}
static struct bencode *decode_dict(struct ben_decode_ctx *ctx)
{
struct bencode *key;
struct bencode *lastkey = NULL;
struct bencode *value;
struct bencode_dict *d;
d = alloc(BENCODE_DICT);
if (d == NULL) {
warn("Not enough memory for dict\n");
return ben_oom_ptr(ctx);
}
ctx->off++;
while (ctx->off < ctx->len && ben_current_char(ctx) != 'e') {
key = ben_ctx_decode(ctx);
if (key == NULL)
goto error;
if (key->type != BENCODE_INT && key->type != BENCODE_STR) {
ben_free(key);
key = NULL;
ctx->error = BEN_INVALID;
warn("Invalid dict key type\n");
goto error;
}
if (lastkey != NULL && ben_cmp(lastkey, key) >= 0) {
ben_free(key);
key = NULL;
ctx->error = BEN_INVALID;
goto error;
}
value = ben_ctx_decode(ctx);
if (value == NULL) {
ben_free(key);
key = NULL;
goto error;
}
if (ben_dict_set((struct bencode *) d, key, value)) {
ben_free(key);
ben_free(value);
key = NULL;
value = NULL;
ctx->error = BEN_NO_MEMORY;
goto error;
}
lastkey = key;
}
if (ctx->off >= ctx->len) {
ctx->error = BEN_INSUFFICIENT;
goto error;
}
ctx->off++;
return (struct bencode *) d;
error:
ben_free((struct bencode *) d);
return NULL;
}
static size_t find(const struct ben_decode_ctx *ctx, char c)
{
char *match = memchr(ctx->data + ctx->off, c, ctx->len - ctx->off);
if (match == NULL)
return -1;
return (size_t) (match - ctx->data);
}
/* off is the position of first number in */
static int read_long_long(long long *ll, struct ben_decode_ctx *ctx, int c)
{
char buf[LONGLONGSIZE]; /* fits all 64 bit integers */
char *endptr;
size_t slen;
size_t pos = find(ctx, c);
if (pos == -1)
return insufficient(ctx);
slen = pos - ctx->off;
if (slen == 0 || slen >= sizeof buf)
return invalid(ctx);
assert(slen < sizeof buf);
memcpy(buf, ctx->data + ctx->off, slen);
buf[slen] = 0;
if (buf[0] != '-' && !isdigit(buf[0]))
return invalid(ctx);
errno = 0;
*ll = strtoll(buf, &endptr, 10);
if (errno == ERANGE || *endptr != 0)
return invalid(ctx);
/*
* Demand a unique encoding for all integers.
* Zero may not begin with a (minus) sign.
* Non-zero integers may not have leading zeros in the encoding.
*/
if (buf[0] == '-' && buf[1] == '0')
return invalid(ctx);
if (buf[0] == '0' && pos != (ctx->off + 1))
return invalid(ctx);
ctx->off = pos + 1;
return 0;
}
static struct bencode *decode_int(struct ben_decode_ctx *ctx)
{
struct bencode_int *b;
long long ll;
ctx->off++;
if (read_long_long(&ll, ctx, 'e'))
return NULL;
b = alloc(BENCODE_INT);
if (b == NULL)
return ben_oom_ptr(ctx);
b->ll = ll;
return (struct bencode *) b;
}
static int resize_list(struct bencode_list *list, size_t newalloc)
{
struct bencode **newvalues;
size_t newsize;
if (newalloc == -1) {
if (list->alloc >= MAX_ALLOC)
return -1;
if (list->alloc == 0)
newalloc = 4;
else
newalloc = list->alloc * 2;
} else {
if (newalloc < list->n || newalloc > MAX_ALLOC)
return -1;
}
newsize = sizeof(list->values[0]) * newalloc;
newvalues = realloc(list->values, newsize);
if (newvalues == NULL)
return -1;
list->alloc = newalloc;
list->values = newvalues;
return 0;
}
static struct bencode *decode_list(struct ben_decode_ctx *ctx)
{
struct bencode_list *l = alloc(BENCODE_LIST);
if (l == NULL)
return ben_oom_ptr(ctx);
ctx->off++;
while (ctx->off < ctx->len && ben_current_char(ctx) != 'e') {
struct bencode *b = ben_ctx_decode(ctx);
if (b == NULL)
goto error;
if (ben_list_append((struct bencode *) l, b)) {
ben_free(b);
ctx->error = BEN_NO_MEMORY;
goto error;
}
}
if (ctx->off >= ctx->len) {
ctx->error = BEN_INSUFFICIENT;
goto error;
}
ctx->off++;
return (struct bencode *) l;
error:
ben_free((struct bencode *) l);
return NULL;
}
static size_t read_size_t(struct ben_decode_ctx *ctx, int c)
{
long long ll;
size_t s;
if (read_long_long(&ll, ctx, c))
return -1;
if (ll < 0)
return invalid(ctx);
/*
* Test that information is not lost when converting from long long
* to size_t
*/
s = (size_t) ll;
if (ll != (long long) s)
return invalid(ctx);
return s;
}
static struct bencode *decode_str(struct ben_decode_ctx *ctx)
{
struct bencode *b;
size_t datalen = read_size_t(ctx, ':'); /* Read the string length */
if (datalen == -1)
return NULL;
if (ben_need_bytes(ctx, datalen))
return ben_insufficient_ptr(ctx);
/* Allocate string structure and copy data into it */
b = ben_blob(ctx->data + ctx->off, datalen);
ctx->off += datalen;
return b;
}
struct bencode *ben_ctx_decode(struct ben_decode_ctx *ctx)
{
char c;
struct bencode_type *type;
struct bencode *b;
ctx->level++;
if (ctx->level > 256)
return ben_invalid_ptr(ctx);
if (ctx->off == ctx->len)
return ben_insufficient_ptr(ctx);
assert (ctx->off < ctx->len);
c = ben_current_char(ctx);
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
b = decode_str(ctx);
break;
case 'b':
b = decode_bool(ctx);
break;
case 'd':
b = decode_dict(ctx);
break;
case 'i':
b = decode_int(ctx);
break;
case 'l':
b = decode_list(ctx);
break;
default:
if (ctx->types && (unsigned char) c < 128) {
type = ctx->types[(unsigned char) c];
if (type) {
ctx->off++;
b = type->decode(ctx);
} else
return ben_invalid_ptr(ctx);
} else
return ben_invalid_ptr(ctx);
}
ctx->level--;
return b;
}
struct bencode *ben_decode(const void *data, size_t len)
{
struct ben_decode_ctx ctx = {.data = data, .len = len};
struct bencode *b = ben_ctx_decode(&ctx);
if (b != NULL && ctx.off != len) {
ben_free(b);
return NULL;
}
return b;
}
struct bencode *ben_decode2(const void *data, size_t len, size_t *off, int *error)
{
struct ben_decode_ctx ctx = {.data = data, .len = len, .off = *off};
struct bencode *b = ben_ctx_decode(&ctx);
*off = ctx.off;
if (error != NULL) {
assert((b != NULL) ^ (ctx.error != 0));
*error = ctx.error;
}
return b;
}
struct bencode *ben_decode3(const void *data, size_t len, size_t *off, int *error, struct bencode_type *types[128])
{
struct ben_decode_ctx ctx = {.data = data, .len = len, .off = *off,
.types = types};
struct bencode *b = ben_ctx_decode(&ctx);
*off = ctx.off;
if (error != NULL) {
assert((b != NULL) ^ (ctx.error != 0));
*error = ctx.error;
}
return b;
}
static struct bencode *decode_printed_bool(struct ben_decode_ctx *ctx)
{
struct bencode *b;
int bval = -1;
if (try_match(ctx, "True")) {
if (ben_need_bytes(ctx, 4))
return ben_insufficient_ptr(ctx);
} else {
bval = 1;
}
if (bval < 0) {
/* It's not 'True', so it can only be 'False'. Verify it. */
if (try_match_with_errors(ctx, "False"))
return NULL;
bval = 0;
}
assert(bval == 0 || bval == 1);
b = ben_bool(bval);
if (b == NULL)
return ben_oom_ptr(ctx);
return b;
}
static struct bencode *decode_printed_dict(struct ben_decode_ctx *ctx)
{
struct bencode *d = ben_dict();
struct bencode *key = NULL;
struct bencode *value = NULL;
if (d == NULL)
return ben_oom_ptr(ctx);
ctx->off++;
while (1) {
if (seek_char(ctx))
goto nullpath;
if (ben_current_char(ctx) == '}') {
ctx->off++;
break;
}
key = decode_printed(ctx);
if (key == NULL)
goto nullpath;
if (seek_char(ctx))
goto nullpath;
if (ben_current_char(ctx) != ':')
goto invalidpath;
ctx->off++;
value = decode_printed(ctx);
if (value == NULL)
goto nullpath;
if (ben_dict_set(d, key, value)) {
ben_free(key);
ben_free(value);
ben_free(d);
return ben_oom_ptr(ctx);
}
key = NULL;
value = NULL;
if (seek_char(ctx))
goto nullpath;
if (ben_current_char(ctx) == ',')
ctx->off++;
else if (ben_current_char(ctx) != '}')
goto invalidpath;
}
return d;
invalidpath:
ben_free(key);
ben_free(value);
ben_free(d);
return ben_invalid_ptr(ctx);
nullpath:
ben_free(key);
ben_free(value);
ben_free(d);