cloudburst 0.0.5

A library to help with the implementation of torrent based protocols and algorithms.
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
// Copyright 2022 Bryant Luk
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Information about what the torrent is for and how to join the peer to peer swarm.

use crate::piece;
use core::{borrow::Borrow, fmt, slice::ChunksExact};
use serde_derive::{Deserialize, Serialize};

#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{string::String, vec, vec::Vec};
#[cfg(feature = "std")]
use std::{path::PathBuf, string::String, vec, vec::Vec};

pub mod validation;

/// The version which the metainfo is compatible with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetaVersion {
    /// Version 1
    V1,
    /// Version 2
    V2,
    /// An unknown version
    Unknown(u64),
}

impl Default for MetaVersion {
    fn default() -> Self {
        Self::V1
    }
}

impl From<u64> for MetaVersion {
    fn from(value: u64) -> Self {
        match value {
            1 => MetaVersion::V1,
            2 => MetaVersion::V2,
            n => MetaVersion::Unknown(n),
        }
    }
}

fn de_announce_list<'de, 'a, D>(deserializer: D) -> Result<Option<Vec<Vec<&'a str>>>, D::Error>
where
    D: serde::de::Deserializer<'de>,
    'de: 'a,
{
    use serde::de::{self, Visitor};

    struct AnnounceListOptionalVisitor;

    impl<'de> Visitor<'de> for AnnounceListOptionalVisitor {
        type Value = Option<Vec<Vec<&'de str>>>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("an announce list")
        }

        fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            match core::str::from_utf8(v) {
                Ok(s) => {
                    let urls = s.split(',').map(str::trim).collect();
                    Ok(Some(vec![urls]))
                }
                Err(_) => Err(E::custom(String::from(
                    "announce list was not a valid UTF-8 string",
                ))),
            }
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: de::SeqAccess<'de>,
        {
            let mut v = Vec::with_capacity(seq.size_hint().unwrap_or(0));
            while let Some(value) = seq.next_element()? {
                v.push(value);
            }
            Ok(Some(v))
        }
    }

    deserializer.deserialize_any(AnnounceListOptionalVisitor)
}

const PIECE_HASH_LEN: usize = 20;

/// Describes how to join a torrent and how to verify data from the torrent.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Metainfo<'a> {
    /// URL of tracker
    #[serde(skip_serializing_if = "Option::is_none", borrow)]
    pub announce: Option<&'a str>,
    /// A multi-tier list of trackers.
    ///
    /// Optional extension described in [BEP 0012][bep_0012].
    ///
    /// [bep_0012]: http://bittorrent.org/beps/bep_0012.html
    #[serde(default)]
    #[serde(
        rename = "announce-list",
        deserialize_with = "de_announce_list",
        borrow
    )]
    pub announce_list: Option<Vec<Vec<&'a str>>>,
    /// An optional free-form comment about the torrent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<&'a str>,
    /// An optional string about what program created the torrent.
    #[serde(rename = "created by")]
    #[serde(skip_serializing_if = "Option::is_none", borrow)]
    pub created_by: Option<&'a str>,
    /// The number of seconds since UNIX epoch time to indicate when the torrent was created.
    #[serde(rename = "creation date", skip_serializing_if = "Option::is_none")]
    pub creation_date: Option<u64>,
    /// Information about the torrent's data.
    #[serde(borrow)]
    pub info: Info<'a>,
}

impl<'a> Metainfo<'a> {}

/// Information about the data exchanged in the torrent.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Info<'a> {
    /// Name of the suggested file/folder to save as.
    #[serde(borrow)]
    pub name: &'a str,
    /// The number of bytes for each piece of a file, except the last one which is the leftover bytes.
    #[serde(rename = "piece length")]
    pub piece_length: u64,
    /// If a single file, then the length of the file. If multiple files, None.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub length: Option<u64>,
    /// If multiple files, a list of file information.
    #[serde(skip_serializing_if = "Option::is_none", borrow)]
    pub files: Option<Vec<File<'a>>>,
    /// The SHA1 hashes of each piece
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none", borrow)]
    pub pieces: Option<&'a [u8]>,
    /// The version of the specification used.
    #[serde(rename = "meta version", skip_serializing_if = "Option::is_none")]
    pub meta_version: Option<u64>,
}

impl<'a> Info<'a> {
    /// The number of bytes for each piece of a file, except the last one which is the leftover bytes.
    ///
    /// # Panics
    ///
    /// Panics if the piece length is greater than a [u32].
    #[must_use]
    pub fn piece_length(&self) -> piece::Length {
        piece::Length::from(u32::try_from(self.piece_length).unwrap())
    }

    /// Returns the last piece index.
    #[inline]
    #[must_use]
    fn last_piece_index(&self) -> usize {
        self.pieces.unwrap().len() / PIECE_HASH_LEN - 1
    }

    /// The length of a specific piece.
    ///
    /// # Panics
    ///
    /// Panics if the piece length is greater than a [u32] or if there is no piece data.
    #[must_use]
    pub fn length_for_piece(&self, index: piece::Index) -> piece::Length {
        if u32::from(index) == u32::try_from(self.last_piece_index()).unwrap() {
            let remainder = self.total_size() % self.piece_length;
            if remainder == 0 {
                self.piece_length()
            } else {
                piece::Length::from(u32::try_from(remainder).unwrap())
            }
        } else {
            self.piece_length()
        }
    }

    /// The number of blocks for a specific piece.
    #[must_use]
    pub fn block_count_for_piece(&self, index: piece::Index) -> u32 {
        let piece_len = self.length_for_piece(index);
        (u32::from(piece_len) / piece::BlockLength::MAX)
            + u32::from(u32::from(piece_len) % piece::BlockLength::MAX != 0)
    }

    /// The SHA1 hashes of each piece
    #[must_use]
    pub fn pieces(&self) -> Option<ChunksExact<'_, u8>> {
        self.pieces
            .map(|pieces| pieces.chunks_exact(PIECE_HASH_LEN))
    }

    /// The maximum piece index.
    ///
    /// # Panics
    ///
    /// Panics if the number of pieces is greater than a [u32].
    #[must_use]
    pub fn max_index(&self) -> Option<piece::Index> {
        self.pieces
            .map(|pieces| piece::Index::from(u32::try_from(pieces.len() / PIECE_HASH_LEN).unwrap()))
    }

    /// The total size of all the files.
    #[must_use]
    pub fn total_size(&self) -> u64 {
        self.files
            .as_ref()
            .map(|f| f.iter().map(|f| f.length).sum())
            .or(self.length)
            .unwrap_or_default()
    }

    /// The version of the specification used.
    #[must_use]
    pub fn meta_version(&self) -> MetaVersion {
        self.meta_version.map(MetaVersion::from).unwrap_or_default()
    }
}

/// File specific information in the torrent.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct File<'a> {
    length: u64,
    #[serde(borrow)]
    path: Vec<&'a str>,
}

impl<'a> File<'a> {
    /// The length of the file.
    #[must_use]
    pub fn length(&self) -> u64 {
        self.length
    }

    /// The path of the file.
    #[must_use]
    pub fn path(&self) -> &[&str] {
        self.path.as_ref()
    }

    /// The path as a standard `PathBuf`
    #[cfg(feature = "std")]
    #[must_use]
    pub fn to_path_buf(&self) -> PathBuf {
        self.path.iter().collect::<PathBuf>()
    }
}

/// A 160-bit value which is used to identify a torrent.
#[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct InfoHash(pub [u8; 20]);

impl InfoHash {
    /// Instantiate with the `info` as a Bencode [Value][bt_bencode::Value] and the expected metainfo version.
    ///
    /// # Errors
    ///
    /// Returns an error if the `info` value cannot be serialized.
    pub fn with_value_and_meta_version(
        info: &bt_bencode::Value,
        meta_version: MetaVersion,
    ) -> Result<Self, bt_bencode::Error> {
        let bytes = bt_bencode::to_vec(info)?;
        Ok(Self::with_bytes_and_meta_version(bytes, meta_version))
    }

    /// Instantiate with the `info` value's raw bytes and the expected metainfo version.
    ///
    /// # Panics
    ///
    /// Panics if the [`MetaVersion`] is unknown.
    pub fn with_bytes_and_meta_version<T: AsRef<[u8]>>(
        bytes: T,
        meta_version: MetaVersion,
    ) -> Self {
        match meta_version {
            MetaVersion::V1 => {
                use sha1::{Digest, Sha1};

                let mut hasher = Sha1::new();
                hasher.update(bytes.as_ref());
                let result = hasher.finalize();
                InfoHash(result.into())
            }
            MetaVersion::V2 | MetaVersion::Unknown(_) => {
                todo!()
            }
        }
    }
}

impl AsRef<[u8]> for InfoHash {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl Borrow<[u8]> for InfoHash {
    fn borrow(&self) -> &[u8] {
        &self.0
    }
}

impl From<[u8; 20]> for InfoHash {
    fn from(bytes: [u8; 20]) -> Self {
        Self(bytes)
    }
}

impl From<InfoHash> for Vec<u8> {
    fn from(info_hash: InfoHash) -> Self {
        Vec::from(info_hash.0)
    }
}

impl From<InfoHash> for [u8; 20] {
    fn from(info_hash: InfoHash) -> Self {
        info_hash.0
    }
}

fmt_byte_array!(InfoHash);

impl serde::Serialize for InfoHash {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bytes(&self.0)
    }
}

// TODO: Implement std::fmt::UpperHex, std::fmt::LowerHex, std::fmt::Octal and std::fmt::Binary for InfoHash?

impl TryFrom<&[u8]> for InfoHash {
    type Error = core::array::TryFromSliceError;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        <[u8; 20]>::try_from(value).map(InfoHash)
    }
}

/// Errors when reading and validating the `Metainfo`.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[non_exhaustive]
pub enum Error {
    /// A bencode error
    #[cfg_attr(feature = "std", error(transparent))]
    BtBencode(bt_bencode::Error),
    /// A validation error
    #[cfg_attr(feature = "std", error(transparent))]
    Validation(validation::Error),
}

impl From<bt_bencode::Error> for Error {
    fn from(other: bt_bencode::Error) -> Self {
        Self::BtBencode(other)
    }
}

impl From<validation::Error> for Error {
    fn from(other: validation::Error) -> Self {
        Self::Validation(other)
    }
}

#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
    fn from(error: Error) -> Self {
        match error {
            Error::BtBencode(error) => std::io::Error::new(std::io::ErrorKind::Other, error),
            Error::Validation(error) => std::io::Error::from(error),
        }
    }
}

/// Reads `Metainfo`.
///
/// # Important
///
/// Call [`validation::check`] to validate the data.
///
/// # Errors
///
/// Returns an error if there is a deserialization or validation error such as
/// a required field for the [Metainfo] is missing.
pub fn from_slice(buf: &[u8]) -> Result<(InfoHash, Metainfo<'_>), Error> {
    let torrent_value: bt_bencode::Value = bt_bencode::from_slice(buf)?;

    let info: &bt_bencode::Value = torrent_value
        .get("info")
        .ok_or(validation::Error::MissingInfo)?;

    let meta_version = info
        .get("meta_version")
        .unwrap_or(&bt_bencode::Value::Int(
            bt_bencode::value::Number::Unsigned(1),
        ))
        .as_u64()
        .map(MetaVersion::from)
        .ok_or(validation::Error::UnknownMetaversion)?;

    let info_hash = InfoHash::with_value_and_meta_version(info, meta_version)?;

    {
        #[derive(Debug, Deserialize)]
        struct MetainfoBytes<'a> {
            info: &'a [u8],
        }
        let raw_metainfo_bytes: MetainfoBytes<'_> = bt_bencode::from_slice(buf)?;
        let raw_bytes_info_hash =
            InfoHash::with_bytes_and_meta_version(raw_metainfo_bytes.info, meta_version);
        if info_hash != raw_bytes_info_hash {
            return Err(Error::BtBencode(bt_bencode::Error::new(
                bt_bencode::ErrorKind::InvalidDict,
                0,
            )));
        }
    }

    let metainfo: Metainfo<'_> = bt_bencode::from_slice(buf)?;

    Ok((info_hash, metainfo))
}

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

    #[cfg(all(feature = "alloc", not(feature = "std")))]
    use alloc::format;

    #[cfg(feature = "std")]
    use std::format;

    #[test]
    fn test_formats() {
        let info_hash = InfoHash(hex_literal::hex!(
            "8EFA979AD7627693BA91D48E941F025BAE78CB77"
        ));
        assert_eq!(
            format!("{info_hash:X}"),
            "8EFA979AD7627693BA91D48E941F025BAE78CB77"
        );
        assert_eq!(
            format!("{info_hash:#X}"),
            "0x8EFA979AD7627693BA91D48E941F025BAE78CB77"
        );
        assert_eq!(
            format!("{info_hash:x}"),
            "8efa979ad7627693ba91d48e941f025bae78cb77"
        );
        assert_eq!(
            format!("{info_hash:#x}"),
            "0x8efa979ad7627693ba91d48e941f025bae78cb77"
        );
        assert_eq!(
            format!("{info_hash:b}"),
            "10001110111110101001011110011010110101111100010111011010010011101110101001000111010100100011101001010011111101011011101011101111000110010111110111"
        );
        assert_eq!(
            format!("{info_hash:#b}"),
            "0b10001110111110101001011110011010110101111100010111011010010011101110101001000111010100100011101001010011111101011011101011101111000110010111110111"
        );
    }
}