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
use std::collections::VecDeque;

use binrw::binrw;

use super::Bytes;

/// A `mpint` as defined in the SSH protocol.
///
/// see <https://datatracker.ietf.org/doc/html/rfc4251#section-5>.
#[binrw]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[brw(big)]
pub struct MpInt(Bytes);

impl MpInt {
    /// Create new [`MpInt`] from a [`Vec`].
    pub fn new(value: impl Into<VecDeque<u8>>) -> Self {
        let mut vec = value.into();

        match vec.front() {
            Some(byte) if *byte >= 0x80 => {
                vec.push_front(0);
            }
            _ => (),
        };

        Self(Bytes::new(vec))
    }
}

impl std::ops::Deref for MpInt {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

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

impl From<Vec<u8>> for MpInt {
    fn from(value: Vec<u8>) -> Self {
        Self::new(value)
    }
}