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
use std::ops::{BitOr, Shl};

use crate::decoding::*;
use crate::encoding::*;
use crate::traits::*;
use crate::wire_types::*;

use bytes::{Buf, BufMut};

/// A VarInt type.
pub trait VarInt:
    private::Sealed + Copy + Shl<usize, Output = Self> + From<u8> + BitOr<Output = Self> + 'static
{
    fn write(self, buf: &mut impl bytes::BufMut);
    fn read<B: Buf>(buf: &mut Deserializer<B>) -> crate::decoding::Result<Self>;
    fn read_field_tag<B: Buf>(
        buf: &mut Deserializer<B>,
    ) -> Result<Self, crate::decoding::Result<WireTypes>>;
    fn size(self) -> usize;
}

#[cold]
#[inline]
fn eof<T>() -> Result<T> {
    Err(DecodingError::Eof)
}

#[cold]
#[inline]
fn overflow<T>() -> Result<T> {
    Err(DecodingError::VarIntOverflow)
}

macro_rules! varint {
    (common($intty:ident)) => {
        impl Encodable for $intty {
            type Wire = VarIntWire;

            fn encoded_size<V: VarInt>(&self, field_number: V) -> usize {
                self.size() + field_number.size()
            }
            fn encode(&self, s: &mut crate::encoding::ProtobufSerializer<impl BufMut>) {
                s.write_varint(*self)
            }
        }
        impl Decodable<'_> for $intty {
            type Wire = VarIntWire;
            fn decode<B: Buf>(s: &mut Deserializer<B>) -> Result<$intty> {
                s.read_varint()
            }
        }
    };
    ($($intty:ident),*) => {
        $(
            impl VarInt for $intty {
                fn write(mut self, buf: &mut impl bytes::BufMut) {
                    while self > 0b1000_0000 {
                        // truncate to the last eight bits and set the
                        // most significant bit to 1.
                        buf.put_u8(self as u8 | 0b1000_0000);
                        self >>= 7;
                    }
                    buf.put_u8(self as u8);
                }

                fn read_field_tag<B: Buf>(buf: &mut Deserializer<B>) -> Result<Self, Result<WireTypes>> {
                    if !buf.has_remaining() {
                        return Err(eof());
                    }

                    let mut storage = 0;

                    let mut byte = buf.get_u8();
                    let mut shift = 0;
                    while byte > 0b0111_1111 {
                        storage |= ((byte & 0b0111_1111) as $intty) << shift;
                        shift += 7;

                        macro_rules! overflow {
                            () => {{
                                while byte > 0b0111_1111 {
                                    byte = buf.get_u8();
                                }
                                return Err(WireTypes::new(byte & 0b111))
                            }}
                        }

                        if shift > <$intty>::BITS {
                            // overflow
                            overflow!()
                        }

                        if !buf.has_remaining() {
                            return Err(eof());
                        }

                        byte = buf.get_u8();

                        let bits_left = <$intty>::BITS - shift;
                        if bits_left < 8 {
                            // more bits than we can fit
                            if (8 - byte.leading_zeros()) > bits_left {
                                // overflow
                                overflow!()
                            }
                        }
                    }

                    storage |= (byte as $intty) << shift;

                    Ok(storage)
                }

                fn read<B: Buf>(buf: &mut Deserializer<B>) -> Result<$intty> {
                    if !buf.has_remaining() {
                        return eof();
                    }

                    let mut storage = 0;

                    let mut byte = buf.get_u8();
                    let mut shift = 0;
                    while byte > 0b0111_1111 {
                        storage |= ((byte & 0b0111_1111) as $intty) << shift;
                        shift += 7;

                        if shift > <$intty>::BITS {
                            // overflow
                            return overflow()
                        }

                        if !buf.has_remaining() {
                            return eof();
                        }

                        byte = buf.get_u8();
                        let bits_left = <$intty>::BITS - shift;
                        if bits_left < 8 {
                            // more bits than we can fit
                            if (8 - byte.leading_zeros()) > bits_left {
                                // overflow
                                return overflow();
                            }
                        }
                    }

                    storage |= (byte as $intty) << shift;

                    Ok(storage)
                }

                fn size(self) -> usize {
                    const BITS_M1: u32 = <$intty>::BITS - 1;

                    fn log2_floor_nonzero(n: $intty) -> u32 {
                        BITS_M1 ^ n.leading_zeros()
                    }

                    // https://github.com/protocolbuffers/protobuf/blob/3.3.x/src/google/protobuf/io/coded_stream.h#L1301-L1309

                    let log2_value = log2_floor_nonzero(self | 0x1);

                    ((log2_value * 9 + 73) / 64) as usize
                }
            }
            varint!(common($intty));
        )*
    };
}

macro_rules! varint_forward {
    ($($selfty:ident as $otherty:ty),*) => {$(
        impl VarInt for $selfty {
            #[inline]
            fn write(self, buf: &mut impl bytes::BufMut) {
                VarInt::write(self as $otherty, buf)
            }
            #[inline]
            fn read<B: Buf>(buf: &mut Deserializer<B>) -> Result<$selfty> {
                <$otherty as VarInt>::read(buf).map(|n| n as $selfty)
            }
            fn read_field_tag<B: Buf>(buf: &mut Deserializer<B>) -> Result<$selfty, Result<WireTypes>> {
                <$otherty as VarInt>::read_field_tag(buf).map(|n| n as $selfty)
            }
            #[inline]
            fn size(self) -> usize {
                VarInt::size(self as $otherty)
            }
        }
        varint!(common($selfty));
    )*};
}

varint!(u64, u32, u16, u8, usize);
varint_forward!(i32 as u32, i64 as u64);