typebit 0.1.2

TypeBit: A universal download engine for torrent (v1/v2, DHT, PEX, web seeds), eD2k/eMule, Xunlei, FlashGet, QQDL, IPFS, Kad and direct HTTP(S)/FTP, with provable-download receipts and a semantic utility scheduler.
Documentation
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
//! Bencode codec (BEP-3) with strict resource limits, for `.torrent`
//! metainfo, tracker announce/scrape and DHT KRPC. Exposes a raw-range
//! walker to capture the exact `info` dict for infohash computation.

use crate::error::{Error, Result};
use alloc::collections::BTreeMap;
use alloc::string::ToString;
use alloc::vec::Vec;

/// Maximum nesting depth.
pub const MAX_DEPTH: usize = 64;
/// Maximum byte-string length.
pub const MAX_STR_LEN: usize = 16 * 1024 * 1024;
/// Maximum number of dict keys / list items.
pub const MAX_ITEMS: usize = 1_000_000;

/// A decoded bencode value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BVal {
    /// Integer.
    Int(i64),
    /// Byte string (may contain arbitrary bytes).
    Bytes(Vec<u8>),
    /// List.
    List(Vec<BVal>),
    /// Dictionary (keys are raw byte strings, kept in byte order).
    Dict(BTreeMap<Vec<u8>, BVal>),
}

impl BVal {
    /// Parse `input` completely (trailing bytes rejected).
    pub fn parse(input: &[u8]) -> Result<BVal> {
        let mut p = Parser::new(input);
        let v = p.value(0)?;
        if p.pos != input.len() {
            return Err(Error::Bencode);
        }
        Ok(v)
    }

    /// Look up a key in a dict; `None` if not a dict or missing.
    pub fn get(&self, key: &[u8]) -> Option<&BVal> {
        match self {
            BVal::Dict(m) => m.get(key),
            _ => None,
        }
    }

    /// Interpret as integer.
    pub fn as_int(&self) -> Option<i64> {
        match self {
            BVal::Int(i) => Some(*i),
            _ => None,
        }
    }

    /// Interpret as bytes.
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            BVal::Bytes(b) => Some(b),
            _ => None,
        }
    }

    /// Interpret as string (lossy for non-UTF-8; bencode keys are usually ASCII).
    pub fn as_str(&self) -> Option<&str> {
        self.as_bytes().and_then(|b| core::str::from_utf8(b).ok())
    }

    /// Interpret as list.
    pub fn as_list(&self) -> Option<&[BVal]> {
        match self {
            BVal::List(l) => Some(l),
            _ => None,
        }
    }

    /// Interpret as dict.
    pub fn as_dict(&self) -> Option<&BTreeMap<Vec<u8>, BVal>> {
        match self {
            BVal::Dict(d) => Some(d),
            _ => None,
        }
    }

    /// Encode back to bencode bytes.
    pub fn encode(&self, out: &mut Vec<u8>) {
        encode_value(self, out, 0);
    }

    /// Convenience: `b"key"`-style literal keys.
    pub fn dict_get_int(&self, key: &[u8]) -> Option<i64> {
        self.get(key).and_then(|v| v.as_int())
    }
}

fn encode_value(v: &BVal, out: &mut Vec<u8>, depth: usize) {
    debug_assert!(depth <= MAX_DEPTH);
    match v {
        BVal::Int(i) => {
            out.push(b'i');
            out.extend_from_slice(i.to_string().as_bytes());
            out.push(b'e');
        }
        BVal::Bytes(b) => {
            out.extend_from_slice(b.len().to_string().as_bytes());
            out.push(b':');
            out.extend_from_slice(b);
        }
        BVal::List(l) => {
            out.push(b'l');
            for item in l {
                encode_value(item, out, depth + 1);
            }
            out.push(b'e');
        }
        BVal::Dict(m) => {
            out.push(b'd');
            for (k, val) in m {
                out.extend_from_slice(k.len().to_string().as_bytes());
                out.push(b':');
                out.extend_from_slice(k);
                encode_value(val, out, depth + 1);
            }
            out.push(b'e');
        }
    }
}

/// Convenience constructor for byte strings.
pub fn bytes(b: impl Into<Vec<u8>>) -> BVal {
    BVal::Bytes(b.into())
}

/// Convenience constructor for integers.
pub fn int(i: i64) -> BVal {
    BVal::Int(i)
}

/// Convenience constructor for lists.
pub fn list(items: Vec<BVal>) -> BVal {
    BVal::List(items)
}

/// Convenience constructor for dicts.
pub fn dict(entries: Vec<(&[u8], BVal)>) -> BVal {
    let mut m = BTreeMap::new();
    for (k, v) in entries {
        m.insert(k.to_vec(), v);
    }
    BVal::Dict(m)
}

/// Result of a raw walk: the start/end offsets of a value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RawRange {
    /// Start offset (inclusive).
    pub start: usize,
    /// End offset (exclusive).
    pub end: usize,
    /// Whether the value is a container (list/dict).
    pub is_container: bool,
}

/// A position-aware parser over `input`.
pub struct Parser<'a> {
    input: &'a [u8],
    /// Current offset.
    pub pos: usize,
}

impl<'a> Parser<'a> {
    /// Create a parser at offset 0.
    pub fn new(input: &'a [u8]) -> Self {
        Parser { input, pos: 0 }
    }

    /// Parse the value at the current position (advancing `pos`).
    pub fn value(&mut self, depth: usize) -> Result<BVal> {
        if depth > MAX_DEPTH {
            return Err(Error::Depth);
        }
        let b = *self.input.get(self.pos).ok_or(Error::Bencode)?;
        match b {
            b'i' => self.int(),
            b'l' => self.list(depth),
            b'd' => self.dict(depth),
            b'0'..=b'9' => self.string(),
            _ => Err(Error::Bencode),
        }
    }

    /// Parse a value and return its raw byte range without building a tree.
    pub fn raw_range(&mut self, depth: usize) -> Result<RawRange> {
        let start = self.pos;
        let b = *self.input.get(self.pos).ok_or(Error::Bencode)?;
        let is_container = matches!(b, b'l' | b'd');
        self.skip_value(depth)?;
        Ok(RawRange {
            start,
            end: self.pos,
            is_container,
        })
    }

    /// Skip a value, advancing `pos` past it.
    pub fn skip_value(&mut self, depth: usize) -> Result<()> {
        if depth > MAX_DEPTH {
            return Err(Error::Depth);
        }
        let b = *self.input.get(self.pos).ok_or(Error::Bencode)?;
        match b {
            b'i' => {
                self.pos += 1;
                let mut saw_digit = false;
                loop {
                    let c = *self.input.get(self.pos).ok_or(Error::Bencode)?;
                    self.pos += 1;
                    if c == b'e' {
                        break;
                    }
                    if c == b'-' {
                        continue;
                    }
                    if c.is_ascii_digit() {
                        saw_digit = true;
                    } else {
                        return Err(Error::Bencode);
                    }
                }
                if !saw_digit {
                    return Err(Error::Bencode);
                }
            }
            b'l' => {
                self.pos += 1;
                loop {
                    if *self.input.get(self.pos).ok_or(Error::Bencode)? == b'e' {
                        self.pos += 1;
                        break;
                    }
                    self.skip_value(depth + 1)?;
                }
            }
            b'd' => {
                self.pos += 1;
                let mut items = 0usize;
                loop {
                    if *self.input.get(self.pos).ok_or(Error::Bencode)? == b'e' {
                        self.pos += 1;
                        break;
                    }
                    // dict key must be a string
                    let b = *self.input.get(self.pos).ok_or(Error::Bencode)?;
                    if !b.is_ascii_digit() {
                        return Err(Error::Bencode);
                    }
                    self.skip_string()?;
                    self.skip_value(depth + 1)?;
                    items += 1;
                    if items > MAX_ITEMS {
                        return Err(Error::Full);
                    }
                }
            }
            b'0'..=b'9' => self.skip_string()?,
            _ => return Err(Error::Bencode),
        }
        Ok(())
    }

    /// Current position.
    pub fn position(&self) -> usize {
        self.pos
    }

    fn int(&mut self) -> Result<BVal> {
        self.pos += 1; // 'i'
        let start = self.pos;
        loop {
            let c = *self.input.get(self.pos).ok_or(Error::Bencode)?;
            if c == b'e' {
                let s = core::str::from_utf8(&self.input[start..self.pos])
                    .map_err(|_| Error::Bencode)?;
                self.pos += 1;
                let i = parse_bencoded_int(s)?;
                return Ok(BVal::Int(i));
            }
            self.pos += 1;
            if self.pos - start > 24 {
                return Err(Error::Bencode);
            }
        }
    }

    fn skip_string(&mut self) -> Result<()> {
        let len = self.string_len()?;
        self.pos = self.pos.checked_add(len).ok_or(Error::Bencode)?;
        if self.pos > self.input.len() {
            return Err(Error::Bencode);
        }
        Ok(())
    }

    /// Parse `N:` length prefix, leaving `pos` at the payload start.
    pub fn string(&mut self) -> Result<BVal> {
        let len = self.string_len()?;
        if len > MAX_STR_LEN {
            return Err(Error::TooLarge);
        }
        let end = self.pos.checked_add(len).ok_or(Error::Bencode)?;
        if end > self.input.len() {
            return Err(Error::Bencode);
        }
        let out = self.input[self.pos..end].to_vec();
        self.pos = end;
        Ok(BVal::Bytes(out))
    }

    /// Parse `N:` length prefix, leaving `pos` at the payload start.
    fn string_len(&mut self) -> Result<usize> {
        let start = self.pos;
        let mut len: usize = 0;
        loop {
            let c = *self.input.get(self.pos).ok_or(Error::Bencode)?;
            match c {
                b'0'..=b'9' => {
                    len = len
                        .checked_mul(10)
                        .and_then(|l| l.checked_add((c - b'0') as usize))
                        .ok_or(Error::Bencode)?;
                    if len > MAX_STR_LEN {
                        return Err(Error::TooLarge);
                    }
                }
                b':' => {
                    if self.pos == start {
                        return Err(Error::Bencode);
                    }
                    self.pos += 1;
                    return Ok(len);
                }
                _ => return Err(Error::Bencode),
            }
            self.pos += 1;
            if self.pos - start > 10 {
                return Err(Error::Bencode);
            }
        }
    }

    fn list(&mut self, depth: usize) -> Result<BVal> {
        self.pos += 1;
        let mut out = Vec::new();
        loop {
            if *self.input.get(self.pos).ok_or(Error::Bencode)? == b'e' {
                self.pos += 1;
                return Ok(BVal::List(out));
            }
            if out.len() >= MAX_ITEMS {
                return Err(Error::Full);
            }
            out.push(self.value(depth + 1)?);
        }
    }

    fn dict(&mut self, depth: usize) -> Result<BVal> {
        self.pos += 1;
        let mut out = BTreeMap::new();
        loop {
            if *self.input.get(self.pos).ok_or(Error::Bencode)? == b'e' {
                self.pos += 1;
                return Ok(BVal::Dict(out));
            }
            if out.len() >= MAX_ITEMS {
                return Err(Error::Full);
            }
            let key = match self.string()? {
                BVal::Bytes(k) => k,
                _ => return Err(Error::Bencode),
            };
            let val = self.value(depth + 1)?;
            out.insert(key, val);
        }
    }
}

fn parse_bencoded_int(s: &str) -> Result<i64> {
    if s.is_empty() {
        return Err(Error::Bencode);
    }
    // reject leading zeros / "-0"
    let body = s.strip_prefix('-').unwrap_or(s);
    if body.len() > 1 && body.starts_with('0') {
        return Err(Error::Bencode);
    }
    if body == "0" && s.starts_with('-') {
        return Err(Error::Bencode);
    }
    s.parse::<i64>().map_err(|_| Error::Bencode)
}

/// Encode helper for building small dicts without allocation churn.
pub fn encode_to_vec(v: &BVal) -> Vec<u8> {
    let mut out = Vec::new();
    v.encode(&mut out);
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_basic() {
        let v = dict(vec![
            (b"announce", bytes("http://tracker.example/announce")),
            (b"pieces", bytes(vec![7u8; 40])),
            (b"length", int(1234)),
            (
                b"nested",
                dict(vec![(b"x", list(vec![int(1), int(2), int(-3)]))]),
            ),
            (b"empty", list(vec![])),
            (b"emptyd", dict(vec![])),
        ]);
        let enc = encode_to_vec(&v);
        let dec = BVal::parse(&enc).unwrap();
        assert_eq!(dec, v);
    }

    #[test]
    fn raw_range_of_info() {
        // simulate a torrent: d8:announce...4:info d...ee
        let v = dict(vec![
            (b"announce", bytes("http://x/a")),
            (
                b"info",
                dict(vec![
                    (b"name", bytes("file.bin")),
                    (b"piece length", int(16384)),
                    (b"pieces", bytes(vec![0xAB; 20])),
                    (b"length", int(16384)),
                ]),
            ),
        ]);
        let enc = encode_to_vec(&v);
        let mut p = Parser::new(&enc);
        // skip 'd'
        assert_eq!(enc[0], b'd');
        p.pos = 1;
        // key "announce"
        p.skip_string().unwrap();
        p.skip_value(0).unwrap();
        // key "info"
        p.skip_string().unwrap();
        let r = p.raw_range(0).unwrap();
        let info_raw = &enc[r.start..r.end];
        // re-parse info_raw and compare to expected info dict
        let info = BVal::parse(info_raw).unwrap();
        assert_eq!(info, v.get(&b"info"[..]).unwrap().clone());
        // infohash-style: SHA-1 of raw info bytes
        let _ = crate::crypto::Sha1::digest(info_raw);
    }

    #[test]
    fn rejects_trailing() {
        let mut enc = encode_to_vec(&int(5));
        enc.push(b'x');
        assert!(BVal::parse(&enc).is_err());
    }

    #[test]
    fn rejects_bad_int() {
        assert!(BVal::parse(b"i05e").is_err());
        assert!(BVal::parse(b"i-e").is_err());
        assert!(BVal::parse(b"i-0e").is_err());
        assert_eq!(BVal::parse(b"i-5e").unwrap(), BVal::Int(-5));
        assert_eq!(BVal::parse(b"i0e").unwrap(), BVal::Int(0));
    }

    #[test]
    fn depth_limit() {
        let mut s = Vec::new();
        s.resize(70, b'l');
        s.push(b'e');
        s.resize(70, b'e');
        assert!(BVal::parse(&s).is_err());
    }
}