/
bdecode.cpp
1130 lines (972 loc) · 29.9 KB
/
bdecode.cpp
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
/*
Copyright (c) 2015-2016, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/bdecode.hpp"
#include "libtorrent/aux_/alloca.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
#include <limits>
#include <cstring> // for memset
#include <cstdio> // for snprintf
#include <cinttypes> // for PRId64 et.al.
#ifndef BOOST_SYSTEM_NOEXCEPT
#define BOOST_SYSTEM_NOEXCEPT throw()
#endif
namespace libtorrent {
using detail::bdecode_token;
namespace {
bool numeric(char c) { return c >= '0' && c <= '9'; }
// finds the end of an integer and verifies that it looks valid this does
// not detect all overflows, just the ones that are an order of magnitude
// beyond. Exact overflow checking is done when the integer value is queried
// from a bdecode_node.
char const* check_integer(char const* start, char const* end
, bdecode_errors::error_code_enum& e)
{
if (start == end)
{
e = bdecode_errors::unexpected_eof;
return start;
}
if (*start == '-')
{
++start;
if (start == end)
{
e = bdecode_errors::unexpected_eof;
return start;
}
}
int digits = 0;
do
{
if (!numeric(*start))
{
e = bdecode_errors::expected_digit;
break;
}
++start;
++digits;
if (start == end)
{
e = bdecode_errors::unexpected_eof;
break;
}
}
while (*start != 'e');
if (digits > 20)
{
e = bdecode_errors::overflow;
}
return start;
}
struct stack_frame
{
stack_frame() : token(0), state(0) {}
explicit stack_frame(int const t): token(std::uint32_t(t)), state(0) {}
// this is an index into m_tokens
std::uint32_t token:31;
// this is used for dictionaries to indicate whether we're
// reading a key or a vale. 0 means key 1 is value
std::uint32_t state:1;
};
// diff between current and next item offset
// should only be called for non last item in array
int token_source_span(bdecode_token const& t)
{
return (&t)[1].offset - t.offset;
}
} // anonymous namespace
// reads the string between start and end, or up to the first occurrance of
// 'delimiter', whichever comes first. This string is interpreted as an
// integer which is assigned to 'val'. If there's a non-delimiter and
// non-digit in the range, a parse error is reported in 'ec'. If the value
// cannot be represented by the variable 'val' and overflow error is reported
// by 'ec'.
char const* parse_int(char const* start, char const* end, char delimiter
, std::int64_t& val, bdecode_errors::error_code_enum& ec)
{
while (start < end && *start != delimiter)
{
if (!numeric(*start))
{
ec = bdecode_errors::expected_digit;
return start;
}
if (val > std::numeric_limits<std::int64_t>::max() / 10)
{
ec = bdecode_errors::overflow;
return start;
}
val *= 10;
int digit = *start - '0';
if (val > std::numeric_limits<std::int64_t>::max() - digit)
{
ec = bdecode_errors::overflow;
return start;
}
val += digit;
++start;
}
return start;
}
struct bdecode_error_category : boost::system::error_category
{
const char* name() const BOOST_SYSTEM_NOEXCEPT override;
std::string message(int ev) const override;
boost::system::error_condition default_error_condition(
int ev) const BOOST_SYSTEM_NOEXCEPT override
{ return {ev, *this}; }
};
const char* bdecode_error_category::name() const BOOST_SYSTEM_NOEXCEPT
{
return "bdecode error";
}
std::string bdecode_error_category::message(int ev) const
{
static char const* msgs[] =
{
"no error",
"expected digit in bencoded string",
"expected colon in bencoded string",
"unexpected end of file in bencoded string",
"expected value (list, dict, int or string) in bencoded string",
"bencoded nesting depth exceeded",
"bencoded item count limit exceeded",
"integer overflow",
};
if (ev < 0 || ev >= int(sizeof(msgs)/sizeof(msgs[0])))
return "Unknown error";
return msgs[ev];
}
boost::system::error_category& bdecode_category()
{
static bdecode_error_category bdecode_category;
return bdecode_category;
}
namespace bdecode_errors
{
boost::system::error_code make_error_code(error_code_enum e)
{
return {e, bdecode_category()};
}
}
bdecode_node::bdecode_node(bdecode_node const& n)
: m_tokens(n.m_tokens)
, m_root_tokens(n.m_root_tokens)
, m_buffer(n.m_buffer)
, m_buffer_size(n.m_buffer_size)
, m_token_idx(n.m_token_idx)
, m_last_index(n.m_last_index)
, m_last_token(n.m_last_token)
, m_size(n.m_size)
{
(*this) = n;
}
bdecode_node& bdecode_node::operator=(bdecode_node const& n)
{
if (&n == this) return *this;
m_tokens = n.m_tokens;
m_root_tokens = n.m_root_tokens;
m_buffer = n.m_buffer;
m_buffer_size = n.m_buffer_size;
m_token_idx = n.m_token_idx;
m_last_index = n.m_last_index;
m_last_token = n.m_last_token;
m_size = n.m_size;
if (!m_tokens.empty())
{
// if this is a root, make the token pointer
// point to our storage
m_root_tokens = &m_tokens[0];
}
return *this;
}
bdecode_node::bdecode_node(bdecode_node&&) noexcept = default;
bdecode_node::bdecode_node(bdecode_token const* tokens, char const* buf
, int len, int idx)
: m_root_tokens(tokens)
, m_buffer(buf)
, m_buffer_size(len)
, m_token_idx(idx)
, m_last_index(-1)
, m_last_token(-1)
, m_size(-1)
{
TORRENT_ASSERT(tokens != nullptr);
TORRENT_ASSERT(idx >= 0);
}
bdecode_node bdecode_node::non_owning() const
{
// if we're not a root, just return a copy of ourself
if (m_tokens.empty()) return *this;
// otherwise, return a reference to this node, but without
// being an owning root node
return bdecode_node(&m_tokens[0], m_buffer, m_buffer_size, m_token_idx);
}
void bdecode_node::clear()
{
m_tokens.clear();
m_root_tokens = nullptr;
m_token_idx = -1;
m_size = -1;
m_last_index = -1;
m_last_token = -1;
}
void bdecode_node::switch_underlying_buffer(char const* buf) noexcept
{
TORRENT_ASSERT(!m_tokens.empty());
if (m_tokens.empty()) return;
m_buffer = buf;
}
bool bdecode_node::has_soft_error(span<char> error) const
{
if (type() == none_t) return false;
bdecode_token const* tokens = m_root_tokens;
int token = m_token_idx;
// we don't know what the original depth_limit was
// so this has to go on the heap
std::vector<int> stack;
// make the initial allocation the default depth_limit
stack.reserve(100);
do
{
switch (tokens[token].type)
{
case bdecode_token::integer:
if (m_buffer[tokens[token].offset + 1] == '0'
&& m_buffer[tokens[token].offset + 2] != 'e')
{
std::snprintf(error.data(), error.size(), "leading zero in integer");
return true;
}
break;
case bdecode_token::string:
if (m_buffer[tokens[token].offset] == '0'
&& m_buffer[tokens[token].offset + 1] != ':')
{
std::snprintf(error.data(), error.size(), "leading zero in string length");
return true;
}
break;
case bdecode_token::dict:
case bdecode_token::list:
stack.push_back(token);
break;
case bdecode_token::end:
auto const parent = stack.back();
stack.pop_back();
if (tokens[parent].type == bdecode_token::dict
&& token != parent + 1)
{
// this is the end of a non-empty dict
// check the sort order of the keys
int k1 = parent + 1;
for (;;)
{
// skip to the first key's value
int const v1 = k1 + tokens[k1].next_item;
// then to the next key
int const k2 = v1 + tokens[v1].next_item;
// check if k1 was the last key in the dict
if (k2 == token)
break;
int const v2 = k2 + tokens[k2].next_item;
int const k1_start = tokens[k1].offset + tokens[k1].start_offset();
int const k1_len = tokens[v1].offset - k1_start;
int const k2_start = tokens[k2].offset + tokens[k2].start_offset();
int const k2_len = tokens[v2].offset - k2_start;
int const min_len = std::min(k1_len, k2_len);
int cmp = std::memcmp(m_buffer + k1_start, m_buffer + k2_start, std::size_t(min_len));
if (cmp > 0 || (cmp == 0 && k1_len > k2_len))
{
std::snprintf(error.data(), error.size(), "unsorted dictionary key");
return true;
}
else if (cmp == 0 && k1_len == k2_len)
{
std::snprintf(error.data(), error.size(), "duplicate dictionary key");
return true;
}
k1 = k2;
}
}
break;
}
++token;
} while (!stack.empty());
return false;
}
bdecode_node::type_t bdecode_node::type() const noexcept
{
if (m_token_idx == -1) return none_t;
return static_cast<bdecode_node::type_t>(m_root_tokens[m_token_idx].type);
}
bdecode_node::operator bool() const noexcept
{ return m_token_idx != -1; }
span<char const> bdecode_node::data_section() const noexcept
{
if (m_token_idx == -1) return {};
TORRENT_ASSERT(m_token_idx != -1);
bdecode_token const& t = m_root_tokens[m_token_idx];
bdecode_token const& next = m_root_tokens[m_token_idx + t.next_item];
return {m_buffer + t.offset, std::size_t(next.offset - t.offset)};
}
bdecode_node bdecode_node::list_at(int i) const
{
TORRENT_ASSERT(type() == list_t);
TORRENT_ASSERT(i >= 0);
// make sure this is a list.
bdecode_token const* tokens = m_root_tokens;
// this is the first item
int token = m_token_idx + 1;
int item = 0;
// do we have a lookup cached?
if (m_last_index <= i && m_last_index != -1)
{
token = m_last_token;
item = m_last_index;
}
while (item < i)
{
token += tokens[token].next_item;
++item;
// index 'i' out of range
TORRENT_ASSERT(tokens[token].type != bdecode_token::end);
}
m_last_token = token;
m_last_index = i;
return bdecode_node(tokens, m_buffer, m_buffer_size, token);
}
string_view bdecode_node::list_string_value_at(int i
, string_view default_val) const
{
bdecode_node const n = list_at(i);
if (n.type() != bdecode_node::string_t) return default_val;
return n.string_value();
}
std::int64_t bdecode_node::list_int_value_at(int i
, std::int64_t default_val) const
{
bdecode_node const n = list_at(i);
if (n.type() != bdecode_node::int_t) return default_val;
return n.int_value();
}
int bdecode_node::list_size() const
{
TORRENT_ASSERT(type() == list_t);
if (m_size != -1) return m_size;
// make sure this is a list.
bdecode_token const* tokens = m_root_tokens;
TORRENT_ASSERT(tokens[m_token_idx].type == bdecode_token::list);
// this is the first item
int token = m_token_idx + 1;
int ret = 0;
// do we have a lookup cached?
if (m_last_index != -1)
{
token = m_last_token;
ret = m_last_index;
}
while (tokens[token].type != bdecode_token::end)
{
token += tokens[token].next_item;
++ret;
}
m_size = ret;
return ret;
}
std::pair<string_view, bdecode_node> bdecode_node::dict_at(int i) const
{
TORRENT_ASSERT(type() == dict_t);
TORRENT_ASSERT(m_token_idx != -1);
bdecode_token const* tokens = m_root_tokens;
TORRENT_ASSERT(tokens[m_token_idx].type == bdecode_token::dict);
int token = m_token_idx + 1;
int item = 0;
// do we have a lookup cached?
if (m_last_index <= i && m_last_index != -1)
{
token = m_last_token;
item = m_last_index;
}
while (item < i)
{
TORRENT_ASSERT(tokens[token].type == bdecode_token::string);
// skip the key
token += tokens[token].next_item;
TORRENT_ASSERT(tokens[token].type != bdecode_token::end);
// skip the value
token += tokens[token].next_item;
++item;
// index 'i' out of range
TORRENT_ASSERT(tokens[token].type != bdecode_token::end);
}
// there's no point in caching the first item
if (i > 0)
{
m_last_token = token;
m_last_index = i;
}
int value_token = token + tokens[token].next_item;
TORRENT_ASSERT(tokens[token].type != bdecode_token::end);
return std::make_pair(
bdecode_node(tokens, m_buffer, m_buffer_size, token).string_value()
, bdecode_node(tokens, m_buffer, m_buffer_size, value_token));
}
int bdecode_node::dict_size() const
{
TORRENT_ASSERT(type() == dict_t);
TORRENT_ASSERT(m_token_idx != -1);
if (m_size != -1) return m_size;
bdecode_token const* tokens = m_root_tokens;
TORRENT_ASSERT(tokens[m_token_idx].type == bdecode_token::dict);
// this is the first item
int token = m_token_idx + 1;
int ret = 0;
if (m_last_index != -1)
{
ret = m_last_index * 2;
token = m_last_token;
}
while (tokens[token].type != bdecode_token::end)
{
token += tokens[token].next_item;
++ret;
}
// a dictionary must contain full key-value pairs. which means
// the number of entries is divisible by 2
TORRENT_ASSERT((ret % 2) == 0);
// each item is one key and one value, so divide by 2
ret /= 2;
m_size = ret;
return ret;
}
bdecode_node bdecode_node::dict_find(string_view key) const
{
TORRENT_ASSERT(type() == dict_t);
bdecode_token const* const tokens = m_root_tokens;
// this is the first item
int token = m_token_idx + 1;
while (tokens[token].type != bdecode_token::end)
{
bdecode_token const& t = tokens[token];
TORRENT_ASSERT(t.type == bdecode_token::string);
int const size = token_source_span(t) - t.start_offset();
if (int(key.size()) == size
&& std::equal(key.data(), key.data() + size, m_buffer
+ t.offset + t.start_offset()))
{
// skip key
token += t.next_item;
TORRENT_ASSERT(tokens[token].type != bdecode_token::end);
return bdecode_node(tokens, m_buffer, m_buffer_size, token);
}
// skip key
token += t.next_item;
TORRENT_ASSERT(tokens[token].type != bdecode_token::end);
// skip value
token += tokens[token].next_item;
}
return bdecode_node();
}
bdecode_node bdecode_node::dict_find_list(string_view key) const
{
bdecode_node ret = dict_find(key);
if (ret.type() == bdecode_node::list_t)
return ret;
return bdecode_node();
}
bdecode_node bdecode_node::dict_find_dict(string_view key) const
{
bdecode_node ret = dict_find(key);
if (ret.type() == bdecode_node::dict_t)
return ret;
return bdecode_node();
}
bdecode_node bdecode_node::dict_find_string(string_view key) const
{
bdecode_node ret = dict_find(key);
if (ret.type() == bdecode_node::string_t)
return ret;
return bdecode_node();
}
bdecode_node bdecode_node::dict_find_int(string_view key) const
{
bdecode_node ret = dict_find(key);
if (ret.type() == bdecode_node::int_t)
return ret;
return bdecode_node();
}
string_view bdecode_node::dict_find_string_value(string_view key
, string_view default_value) const
{
bdecode_node n = dict_find(key);
if (n.type() != bdecode_node::string_t) return default_value;
return n.string_value();
}
std::int64_t bdecode_node::dict_find_int_value(string_view key
, std::int64_t default_val) const
{
bdecode_node n = dict_find(key);
if (n.type() != bdecode_node::int_t) return default_val;
return n.int_value();
}
std::int64_t bdecode_node::int_value() const
{
TORRENT_ASSERT(type() == int_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
int const size = token_source_span(t);
TORRENT_ASSERT(t.type == bdecode_token::integer);
// +1 is to skip the 'i'
char const* ptr = m_buffer + t.offset + 1;
std::int64_t val = 0;
bool const negative = (*ptr == '-');
bdecode_errors::error_code_enum ec = bdecode_errors::no_error;
char const* end = parse_int(ptr + int(negative)
, ptr + size, 'e', val, ec);
if (ec) return 0;
TORRENT_UNUSED(end);
TORRENT_ASSERT(end < ptr + size);
if (negative) val = -val;
return val;
}
string_view bdecode_node::string_value() const
{
TORRENT_ASSERT(type() == string_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
std::size_t const size = aux::numeric_cast<std::size_t>(token_source_span(t) - t.start_offset());
TORRENT_ASSERT(t.type == bdecode_token::string);
return string_view(m_buffer + t.offset + t.start_offset(), size);
}
char const* bdecode_node::string_ptr() const
{
TORRENT_ASSERT(type() == string_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
TORRENT_ASSERT(t.type == bdecode_token::string);
return m_buffer + t.offset + t.start_offset();
}
int bdecode_node::string_length() const
{
TORRENT_ASSERT(type() == string_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
TORRENT_ASSERT(t.type == bdecode_token::string);
return token_source_span(t) - t.start_offset();
}
void bdecode_node::reserve(int tokens)
{ m_tokens.reserve(aux::numeric_cast<std::size_t>(tokens)); }
void bdecode_node::swap(bdecode_node& n)
{
/*
bool lhs_is_root = (m_root_tokens == &m_tokens);
bool rhs_is_root = (n.m_root_tokens == &n.m_tokens);
// swap is only defined between non-root nodes
// and between root-nodes. They may not be mixed!
// note that when swapping root nodes, all bdecode_node
// entries that exist in those subtrees are invalidated!
TORRENT_ASSERT(lhs_is_root == rhs_is_root);
// if both are roots, m_root_tokens always point to
// its own vector, and should not get swapped (the
// underlying vectors are swapped already)
if (!lhs_is_root && !rhs_is_root)
{
// if neither is a root, we just swap the pointers
// to the token vectors, switching their roots
std::swap(m_root_tokens, n.m_root_tokens);
}
*/
m_tokens.swap(n.m_tokens);
std::swap(m_root_tokens, n.m_root_tokens);
std::swap(m_buffer, n.m_buffer);
std::swap(m_buffer_size, n.m_buffer_size);
std::swap(m_token_idx, n.m_token_idx);
std::swap(m_last_index, n.m_last_index);
std::swap(m_last_token, n.m_last_token);
std::swap(m_size, n.m_size);
}
#define TORRENT_FAIL_BDECODE(code) do { \
ec = code; \
if (error_pos) *error_pos = int(start - orig_start); \
goto done; \
} TORRENT_WHILE_0
int bdecode(char const* start, char const* end, bdecode_node& ret
, error_code& ec, int* error_pos, int const depth_limit, int token_limit)
{
ret = bdecode({start, static_cast<size_t>(end - start)}, ec, error_pos, depth_limit, token_limit);
return ec ? -1 : 0;
}
bdecode_node bdecode(span<char const> buffer
, error_code& ec, int* error_pos, int depth_limit, int token_limit)
{
bdecode_node ret;
ec.clear();
if (buffer.size() > bdecode_token::max_offset)
{
if (error_pos) *error_pos = 0;
ec = bdecode_errors::limit_exceeded;
return ret;
}
// this is the stack of bdecode_token indices, into m_tokens.
// sp is the stack pointer, as index into the array, stack
int sp = 0;
TORRENT_ALLOCA(stack, stack_frame, depth_limit);
// TODO: 2 attempt to simplify this implementation by embracing the span
char const* start = buffer.data();
char const* end = start + buffer.size();
char const* const orig_start = start;
if (start == end)
TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
while (start <= end)
{
if (start >= end) TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
if (sp >= depth_limit)
TORRENT_FAIL_BDECODE(bdecode_errors::depth_exceeded);
--token_limit;
if (token_limit < 0)
TORRENT_FAIL_BDECODE(bdecode_errors::limit_exceeded);
// look for a new token
char const t = *start;
int const current_frame = sp;
// if we're currently parsing a dictionary, assert that
// every other node is a string.
if (current_frame > 0
&& ret.m_tokens[stack[current_frame - 1].token].type == bdecode_token::dict)
{
if (stack[current_frame - 1].state == 0)
{
// the current parent is a dict and we are parsing a key.
// only allow a digit (for a string) or 'e' to terminate
if (!numeric(t) && t != 'e')
TORRENT_FAIL_BDECODE(bdecode_errors::expected_digit);
}
}
switch (t)
{
case 'd':
stack[sp++] = stack_frame(int(ret.m_tokens.size()));
// we push it into the stack so that we know where to fill
// in the next_node field once we pop this node off the stack.
// i.e. get to the node following the dictionary in the buffer
ret.m_tokens.push_back({start - orig_start, bdecode_token::dict});
++start;
break;
case 'l':
stack[sp++] = stack_frame(int(ret.m_tokens.size()));
// we push it into the stack so that we know where to fill
// in the next_node field once we pop this node off the stack.
// i.e. get to the node following the list in the buffer
ret.m_tokens.push_back({start - orig_start, bdecode_token::list});
++start;
break;
case 'i':
{
char const* const int_start = start;
bdecode_errors::error_code_enum e = bdecode_errors::no_error;
// +1 here to point to the first digit, rather than 'i'
start = check_integer(start + 1, end, e);
if (e)
{
// in order to gracefully terminate the tree,
// make sure the end of the previous token is set correctly
if (error_pos) *error_pos = int(start - orig_start);
error_pos = nullptr;
start = int_start;
TORRENT_FAIL_BDECODE(e);
}
ret.m_tokens.push_back({int_start - orig_start
, 1, bdecode_token::integer, 1});
TORRENT_ASSERT(*start == 'e');
// skip 'e'
++start;
break;
}
case 'e':
{
// this is the end of a list or dict
if (sp == 0)
TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
if (sp > 0
&& ret.m_tokens[stack[sp - 1].token].type == bdecode_token::dict
&& stack[sp - 1].state == 1)
{
// this means we're parsing a dictionary and about to parse a
// value associated with a key. Instead, we got a termination
TORRENT_FAIL_BDECODE(bdecode_errors::expected_value);
}
// insert the end-of-sequence token
ret.m_tokens.push_back({start - orig_start, 1, bdecode_token::end});
// and back-patch the start of this sequence with the offset
// to the next token we'll insert
int const top = stack[sp - 1].token;
// subtract the token's own index, since this is a relative
// offset
if (int(ret.m_tokens.size()) - top > bdecode_token::max_next_item)
TORRENT_FAIL_BDECODE(bdecode_errors::limit_exceeded);
ret.m_tokens[std::size_t(top)].next_item = std::uint32_t(int(ret.m_tokens.size()) - top);
// and pop it from the stack.
TORRENT_ASSERT(sp > 0);
--sp;
++start;
break;
}
default:
{
// this is the case for strings. The start character is any
// numeric digit
if (!numeric(t))
TORRENT_FAIL_BDECODE(bdecode_errors::expected_value);
std::int64_t len = t - '0';
char const* const str_start = start;
++start;
if (start >= end) TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
bdecode_errors::error_code_enum e = bdecode_errors::no_error;
start = parse_int(start, end, ':', len, e);
if (e)
TORRENT_FAIL_BDECODE(e);
if (start == end)
TORRENT_FAIL_BDECODE(bdecode_errors::expected_colon);
// remaining buffer size excluding ':'
ptrdiff_t const buff_size = end - start - 1;
if (len > buff_size)
TORRENT_FAIL_BDECODE(bdecode_errors::unexpected_eof);
if (len < 0)
TORRENT_FAIL_BDECODE(bdecode_errors::overflow);
// skip ':'
++start;
// no need to range check start here
// the check above ensures that the buffer is long enough to hold
// the string's length which guarantees that start <= end
// the bdecode_token only has 8 bits to keep the header size
// in. If it overflows, fail!
if (start - str_start - 2 > detail::bdecode_token::max_header)
TORRENT_FAIL_BDECODE(bdecode_errors::limit_exceeded);
ret.m_tokens.push_back({str_start - orig_start
, 1, bdecode_token::string, std::uint8_t(start - str_start)});
start += len;
break;
}
}
if (current_frame > 0
&& ret.m_tokens[stack[current_frame - 1].token].type == bdecode_token::dict)
{
// the next item we parse is the opposite
stack[current_frame - 1].state = ~stack[current_frame - 1].state;
}
// this terminates the top level node, we're done!
if (sp == 0) break;
}
done:
// if parse failed, sp will be greater than 1
// unwind the stack by inserting terminator to make whatever we have
// so far valid
while (sp > 0) {
TORRENT_ASSERT(ec);
--sp;
// we may need to insert a dummy token to properly terminate the tree,
// in case we just parsed a key to a dict and failed in the value
if (ret.m_tokens[stack[sp].token].type == bdecode_token::dict
&& stack[sp].state == 1)
{
// insert an empty dictionary as the value
ret.m_tokens.push_back({start - orig_start, 2, bdecode_token::dict});
ret.m_tokens.push_back({start - orig_start, bdecode_token::end});
}
int const top = stack[sp].token;
TORRENT_ASSERT(int(ret.m_tokens.size()) - top <= bdecode_token::max_next_item);
ret.m_tokens[std::size_t(top)].next_item = std::uint32_t(int(ret.m_tokens.size()) - top);
ret.m_tokens.push_back({start - orig_start, 1, bdecode_token::end});
}
ret.m_tokens.push_back({start - orig_start, 0, bdecode_token::end});
ret.m_token_idx = 0;
ret.m_buffer = orig_start;
ret.m_buffer_size = int(start - orig_start);
ret.m_root_tokens = ret.m_tokens.data();
return ret;
}
namespace {
int line_longer_than(bdecode_node const& e, int limit)
{
int line_len = 0;
switch (e.type())
{
case bdecode_node::list_t:
line_len += 4;
if (line_len > limit) return -1;
for (int i = 0; i < e.list_size(); ++i)
{
int const ret = line_longer_than(e.list_at(i), limit - line_len);
if (ret == -1) return -1;
line_len += ret + 2;
}
break;
case bdecode_node::dict_t:
line_len += 4;
if (line_len > limit) return -1;
for (int i = 0; i < e.dict_size(); ++i)
{
line_len += 4 + int(e.dict_at(i).first.size());
if (line_len > limit) return -1;
int const ret = line_longer_than(e.dict_at(i).second, limit - line_len);
if (ret == -1) return -1;
line_len += ret + 1;
}
break;
case bdecode_node::string_t:
line_len += 3 + e.string_length();
break;
case bdecode_node::int_t:
{
std::int64_t val = e.int_value();
while (val > 0)
{
++line_len;
val /= 10;
}
line_len += 2;
}
break;