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
//! A variable-length 7-bit encoder where each bit indicates if there is a
//! continuation of the sequence or not.

#![allow(unused)]

use crate::int;
use crate::reader::Reader;
use crate::writer::Writer;
use musli::error::Error;

use super::Unsigned;

const MASK_BYTE: u8 = 0b0111_1111;
const CONT_BYTE: u8 = 0b1000_0000;

/// Decode the given length using variable int encoding.
#[inline(never)]
#[cold]
pub fn decode<'de, R, T>(mut r: R) -> Result<T, R::Error>
where
    R: Reader<'de>,
    T: int::Unsigned,
{
    let mut b = r.read_byte()?;

    if b & 0b1000_0000 == 0 {
        return Ok(T::from_byte(b));
    }

    let mut value = T::from_byte(b & MASK_BYTE);
    let mut shift = 0u32;

    while b & CONT_BYTE == CONT_BYTE {
        shift += 7;
        b = r.read_byte()?;

        value = T::from_byte(b & MASK_BYTE)
            .checked_shl(shift)
            .and_then(|add| value.checked_add(add))
            .ok_or_else(|| R::Error::custom("length overflow"))?;
    }

    Ok(value)
}

/// Encode the given length using variable length encoding.
#[inline(never)]
#[cold]
pub fn encode<W, T>(mut w: W, mut value: T) -> Result<(), W::Error>
where
    W: Writer,
    T: int::Unsigned,
{
    let mut b = value.as_byte();

    if value < T::from_byte(0b1000_0000) {
        w.write_byte(b)?;
        return Ok(());
    }

    loop {
        value = value
            .checked_shr(7)
            .ok_or_else(|| W::Error::custom("length underflow"))?;

        if value.is_zero() {
            w.write_byte(b & MASK_BYTE)?;
            break;
        }

        w.write_byte(b | CONT_BYTE)?;
        b = value.as_byte();
    }

    Ok(())
}