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
use std::{collections::BTreeMap, io::Read, result::Result};
// Bencoding spec
// https://wiki.theory.org/index.php/BitTorrentSpecification#Bencoding

#[derive(PartialEq, Ord, PartialOrd, Eq, Debug, Clone)]
pub enum Value {
    ByteString(Vec<u8>),
    Integer(i64),
    List(Vec<Value>),
    Dictionary(BTreeMap<Vec<u8>, Value>),
}

#[derive(PartialEq, Debug)]
pub enum ParseResult {
    ValueType(Value),
    ListStart,
    DictStart,
    End,
    EOF,
}

/// Constructs a `Parser` for bencoded data from a reader implementing
/// `std::io::read`. The only exposed interface is an iterator, which
/// will emit parsed tokens `ParseResult` up until (but not including)
/// EOF.
///
/// ```
/// use bencode_decode::Parser;
/// let input = std::io::Cursor::new(
///            "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee"
///                .to_string()
///                .into_bytes(),
///        );
/// let mut parser = Parser::new(input);
/// for item in parser {
///     println!("{:?}", item);
/// }
/// ```
pub struct Parser<R: Read> {
    reader: R,
}
impl<R: Read> Parser<R> {
    pub fn new(reader: R) -> Self {
        Self { reader }
    }
}

impl<R: Read> Iterator for Parser<R> {
    type Item = ParseResult;
    fn next(&mut self) -> Option<Self::Item> {
        let res = parse(&mut self.reader).ok();
        if res == Some(ParseResult::EOF) {
            None
        } else {
            res
        }
    }
}

use ParseResult::*;
use Value::*;
/// Given a token parser `parser`, will try to decode `ParseResult` into
/// `Value`s. This function does obviously not attempt to drain the passed
/// reader instance, but rather expects one top-level value to parse form.
///
/// ```
/// use bencode_decode::{Parser, decode};
/// use std::fs::File;
///
/// let f = File::open("./test/ubuntu-18.04.4-live-server-amd64.iso.torrent").unwrap();
/// let mut parser = Parser::new(f);
/// let res = decode(&mut parser, None).unwrap();
/// ```
pub fn decode<R: Read>(parser: &mut Parser<R>, current: Option<ParseResult>) -> Option<Value> {
    match current.or_else(|| parser.next()) {
        Some(ValueType(val)) => Some(val),
        Some(t @ DictStart) | Some(t @ ListStart) => {
            let mut data = vec![];
            let mut next = parser.next().expect("Unexpected EOF");
            while next != End {
                data.push(decode(parser, Some(next)).unwrap());
                next = parser.next().expect("Unexpected EOF");
            }
            if t == ListStart {
                Some(Value::List(data))
            } else {
                let mut map = BTreeMap::new();
                let mut input = data.into_iter();
                while let (Some(ByteString(key)), Some(value)) = (input.next(), input.next()) {
                    map.insert(key, value);
                }
                Some(Dictionary(map))
            }
        }
        Some(End) => unreachable!(),
        Some(EOF) => unreachable!(),
        None => None,
    }
}

fn parse<R: Read>(reader: &mut R) -> Result<ParseResult, Box<dyn std::error::Error>> {
    let mut buf = [0; 1];
    let mut vec = vec![];
    loop {
        let read_bytes = reader.read(&mut buf)?;
        if read_bytes == 0 {
            return Ok(EOF);
        }
        match buf[0] {
            n @ b'0'..=b'9' => vec.push(n),
            b':' => {
                let size = String::from_utf8(vec)?.parse()?;
                let mut str = vec![0; size];
                reader.read_exact(&mut str)?;
                return Ok(ValueType(ByteString(str)));
            }
            b'i' => {
                let mut b = [0; 1];
                reader.read_exact(&mut b)?;
                let mut vec = vec![];
                while b[0] != b'e' {
                    vec.push(b[0]);
                    reader.read_exact(&mut b)?;
                }
                let int: i64 = String::from_utf8(vec)?.parse()?;
                return Ok(ValueType(Integer(int)));
            }
            b'e' => return Ok(End),
            b'l' => return Ok(ListStart),
            b'd' => return Ok(DictStart),
            _ => unreachable!("unexpected token"),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::fs::File;
    #[test]
    fn torrent() {
        let f = File::open("./test/ubuntu-18.04.4-live-server-amd64.iso.torrent").unwrap();
        let mut parser = Parser::new(f);
        let res = decode(&mut parser, None).unwrap();
        if let Value::Dictionary(x) = res {
            if let Value::Dictionary(y) = x.get(&b"info".to_vec()).unwrap() {
                let path = y.get(&b"name".to_vec()).unwrap();
                let length = y.get(&b"length".to_vec()).unwrap();
                if let (Value::ByteString(path), Value::Integer(length)) = (path, length) {
                    let path = String::from_utf8_lossy(path);
                    println!("{} -> {} bytes", path, length);
                    assert_eq!(path, "ubuntu-18.04.4-live-server-amd64.iso");
                    assert_eq!(*length, 912_261_120);
                }
            }
        }
    }

    #[test]
    fn spec() {
        let input = std::io::Cursor::new(
            "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee"
                .to_string()
                .into_bytes(),
        );
        let mut parser = Parser::new(input);
        let res = decode(&mut parser, None).unwrap();
        let mut map = BTreeMap::new();
        vec![
            ("publisher", "bob"),
            ("publisher-webpage", "www.example.com"),
            ("publisher.location", "home"),
        ]
        .into_iter()
        .for_each(|(k, v)| {
            map.insert(
                k.as_bytes().to_vec(),
                Value::ByteString(v.as_bytes().to_vec()),
            );
        });

        assert_eq!(res, Value::Dictionary(map));
    }
}