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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use crate::{Error, MlsDecode, MlsEncode, MlsSize};
use alloc::vec::Vec;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VarInt(pub u32);

impl VarInt {
    pub const MAX: VarInt = VarInt((1 << 30) - 1);
}

impl From<VarInt> for u32 {
    #[inline]
    fn from(n: VarInt) -> u32 {
        n.0
    }
}

impl TryFrom<u32> for VarInt {
    type Error = Error;

    fn try_from(n: u32) -> Result<Self, Error> {
        (n <= u32::from(VarInt::MAX))
            .then_some(VarInt(n))
            .ok_or(Error::VarIntOutOfRange)
    }
}

impl TryFrom<usize> for VarInt {
    type Error = Error;

    fn try_from(n: usize) -> Result<Self, Error> {
        u32::try_from(n)
            .map_err(|_| Error::VarIntOutOfRange)?
            .try_into()
    }
}

impl MlsSize for VarInt {
    #[inline]
    fn mls_encoded_len(&self) -> usize {
        count_bytes_to_encode_int(*self) as usize
    }
}

impl MlsEncode for VarInt {
    fn mls_encode(&self, writer: &mut Vec<u8>) -> Result<(), Error> {
        let mut bytes = self.0.to_be_bytes();

        let bytes = match count_bytes_to_encode_int(*self) {
            LengthEncoding::One => &bytes[3..],
            LengthEncoding::Two => {
                bytes[2] |= 0x40;
                &bytes[2..]
            }
            LengthEncoding::Four => {
                bytes[0] |= 0x80;
                &bytes
            }
        };

        writer.extend_from_slice(bytes);
        Ok(())
    }
}

impl MlsDecode for VarInt {
    fn mls_decode(reader: &mut &[u8]) -> Result<Self, Error> {
        let first = u8::mls_decode(reader)?;

        let prefix = first >> 6;

        let count = (prefix < 3)
            .then_some(1 << prefix)
            .ok_or(Error::InvalidVarIntPrefix(prefix))?;

        let n = (1..count).try_fold(u32::from(first & 0x3f), |n, _| {
            u8::mls_decode(reader).map(|b| n << 8 | u32::from(b))
        })?;

        let n = VarInt(n);

        if count_bytes_to_encode_int(n) as usize == count {
            Ok(n)
        } else {
            Err(Error::VarIntMinimumLengthEncoding)
        }
    }
}

/// Number of bytes to encode a variable-size integer.
#[derive(Debug)]
enum LengthEncoding {
    One = 1,
    Two = 2,
    Four = 4,
}

fn count_bytes_to_encode_int(n: VarInt) -> LengthEncoding {
    let used_bits = 32 - n.0.leading_zeros();
    match used_bits {
        0..=6 => LengthEncoding::One,
        7..=14 => LengthEncoding::Two,
        15..=30 => LengthEncoding::Four,
        _ => panic!("Such a large VarInt cannot be instantiated"),
    }
}

#[cfg(test)]
mod tests {
    use super::VarInt;
    use crate::{Error, MlsDecode, MlsEncode};
    use assert_matches::assert_matches;

    #[cfg(target_arch = "wasm32")]
    use wasm_bindgen_test::wasm_bindgen_test as test;

    #[test]
    fn zero_is_convertible_to_varint() {
        assert_matches!(VarInt::try_from(0u32).map(u32::from), Ok(0));
    }

    #[test]
    fn successor_of_max_varint_is_not_convertible_to_varint() {
        let n = u32::from(VarInt::MAX) + 1;
        assert_matches!(VarInt::try_from(n), Err(Error::VarIntOutOfRange));
    }

    #[test]
    fn zero_serializes_as_single_null_byte() {
        assert_eq!(
            VarInt::try_from(0u32).unwrap().mls_encode_to_vec().unwrap(),
            [0]
        );
    }

    #[test]
    fn zero_roundtrips() {
        let n = VarInt::try_from(0u32).unwrap();

        let serialized = n.mls_encode_to_vec().unwrap();
        let restored = VarInt::mls_decode(&mut &*serialized).unwrap();

        assert_eq!(restored, n);
    }

    #[test]
    fn varint_max_roundtrips() {
        let n = VarInt::MAX;

        let serialized = n.mls_encode_to_vec().unwrap();
        let restored = VarInt::mls_decode(&mut &*serialized).unwrap();

        assert_eq!(restored, n);
    }

    fn decoding_matches_rfc(encoded: u32, decoded: u32) {
        let bytes = encoded.to_be_bytes();

        let start = bytes
            .iter()
            .position(|&b| b != 0)
            .unwrap_or(bytes.len() - 1);

        let bytes = &bytes[start..];

        assert_eq!(
            VarInt::mls_decode(&mut &*bytes).unwrap(),
            VarInt::try_from(decoded).unwrap()
        );
    }

    #[test]
    fn decoding_0x25_matches_rfc_result() {
        decoding_matches_rfc(0x25, 37);
    }

    #[test]
    fn decoding_0x7bbd_matches_rfc_result() {
        decoding_matches_rfc(0x7bbd, 15293);
    }

    #[test]
    fn decoding_0x9d7f3e7d_matches_rfc_result() {
        decoding_matches_rfc(0x9d7f3e7d, 494878333);
    }
}