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
#![no_std]

use core::str;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[repr(u8)]
/// A wire type that provides just enough information to find the length of the following value.
pub enum WireType {
    /// int32, int64, uint32, uint64, sint32, sint64, bool, enum
    Varint = 0,
    /// fixed64, sfixed64, double
    Bit64 = 1,
    /// string, bytes, embedded messages, packed repeated fields
    Bytes = 2,
    /// fixed32, sfixed32, float
    Bit32 = 5,
}

impl WireType {
    pub fn from_u8(b: u8) -> Option<Self> {
        match b {
            0 => Some(WireType::Varint),
            1 => Some(WireType::Bit64),
            2 => Some(WireType::Bytes),
            5 => Some(WireType::Bit32),
            _ => None,
        }
    }
}

/// The Error type.
#[derive(Debug)]
pub enum Error {
    /// End of parsing, normally this is a safe boundary.
    Eof,
    /// No enough bytes for parsing.
    UnexpectedEof,
    /// Invalid wire type.
    InvalidWireType(u8),
    /// Invalid field number, over 0b11111.
    InvalidFieldNumber(u8),
    /// Overflow a 64bit varint.
    VarintOverflow,
    /// Invalid UTF8 encoding, should use the bytes type.
    InvalidUtf8String,
    /// Buffer overflow while encoding.
    BufferOverflow,
}

impl Error {
    /// Is it a Error::Eof.
    pub fn is_eof(&self) -> bool {
        match self {
            Error::Eof => true,
            _ => false,
        }
    }
}

/// Decode from raw bytes.
pub struct PbReader<'a> {
    buf: &'a [u8],
    pos: usize,
}

impl PbReader<'_> {
    /// Create from raw bytes.
    pub fn new<'a>(buf: &'a [u8]) -> PbReader<'a> {
        PbReader { buf, pos: 0 }
    }

    /// Is the parsing finished and EOF.
    pub fn is_eof(&self) -> bool {
        self.pos == self.buf.len()
    }

    /// Peek next key(field_number, wire_type).
    pub fn peek_next_key(&self) -> Result<(u8, WireType), Error> {
        let key = self.peek_next_u8().ok_or(Error::Eof)?;
        match WireType::from_u8(key & 0b111) {
            Some(wt) => Ok((key >> 3, wt)),
            None => Err(Error::InvalidWireType(key & 0b111)),
        }
    }

    /// Parse next key(field_number, wire_type).
    pub fn next_key(&mut self) -> Result<(u8, WireType), Error> {
        let key = self.next_u8().ok_or(Error::Eof)?;
        match WireType::from_u8(key & 0b111) {
            Some(wt) => Ok((key >> 3, wt)),
            None => Err(Error::InvalidWireType(key & 0b111)),
        }
    }

    /// Skip next field, including key and filed value.
    pub fn skip_next_field(&mut self) -> Result<(), Error> {
        match self.next_key()? {
            (_, WireType::Varint) => {
                let _ = self.next_varint()?;
            }
            (_, WireType::Bytes) => {
                let _ = self.next_bytes()?;
            }
            (_, WireType::Bit32) => {
                let _ = self.next_fixed32()?;
            }
            (_, WireType::Bit64) => {
                let _ = self.next_fixed64()?;
            }
        }
        Ok(())
    }

    /// Parse a fixed32.
    pub fn next_fixed32(&mut self) -> Result<[u8; 4], Error> {
        if self.pos + 4 > self.buf.len() {
            Err(Error::UnexpectedEof)
        } else {
            let mut ret = [0u8; 4];
            ret.copy_from_slice(&self.buf[self.pos..self.pos + 4]);
            self.pos += 4;
            Ok(ret)
        }
    }

    /// Parse a fixed64.
    pub fn next_fixed64(&mut self) -> Result<[u8; 8], Error> {
        if self.pos + 8 > self.buf.len() {
            Err(Error::UnexpectedEof)
        } else {
            let mut ret = [0u8; 8];
            ret.copy_from_slice(&self.buf[self.pos..self.pos + 8]);
            self.pos += 8;
            Ok(ret)
        }
    }

    /// Parse a varint.
    pub fn next_varint(&mut self) -> Result<u64, Error> {
        let mut result = 0;
        let mut bitpos = 0;
        loop {
            let b = self.next_u8().ok_or(Error::UnexpectedEof)?;
            let tmp = ((b & 0x7f) as u64)
                .checked_shl(bitpos)
                .ok_or(Error::VarintOverflow)?;
            result += tmp;
            if b & 0x80 == 0 {
                return Ok(result);
            }
            bitpos += 7;
        }
    }

    /// Parse a bytes array.
    pub fn next_bytes(&mut self) -> Result<&[u8], Error> {
        let len = self.next_varint()? as usize;
        if self.pos + len > self.buf.len() {
            Err(Error::UnexpectedEof)
        } else {
            let bytes = &self.buf[self.pos..self.pos + len];
            self.pos += len;
            Ok(bytes)
        }
    }

    /// Parse a string.
    pub fn next_string(&mut self) -> Result<&str, Error> {
        self.next_bytes()
            .and_then(|raw| str::from_utf8(raw).map_err(|_| Error::InvalidUtf8String))
    }

    /// Parse next bytes array as embedded message(sub-field).
    pub fn next_embedded_message(&mut self) -> Result<PbReader<'_>, Error> {
        self.next_bytes().map(PbReader::new)
    }

    /// Parse next svarint.
    pub fn next_svarint(&mut self) -> Result<i64, Error> {
        let val = self.next_varint()?;
        Ok(varint_to_svarint(val))
    }

    fn peek_next_u8(&self) -> Option<u8> {
        if self.has_next() {
            Some(self.buf[self.pos])
        } else {
            None
        }
    }

    fn next_u8(&mut self) -> Option<u8> {
        if self.has_next() {
            let i = self.pos;
            self.pos += 1;
            Some(self.buf[i])
        } else {
            None
        }
    }

    fn has_next(&self) -> bool {
        self.pos < self.buf.len()
    }
}

/// Encode to raw bytes.
pub struct PbWriter<'a> {
    buf: &'a mut [u8],
    pos: usize,
}

impl PbWriter<'_> {
    /// Create a PbWriter from raw mut bytes.
    pub fn new<'a>(buf: &'a mut [u8]) -> PbWriter<'a> {
        PbWriter { buf, pos: 0 }
    }

    /// Is the buffer full?
    pub fn is_eof(&self) -> bool {
        self.pos == self.buf.len()
    }

    /// Encode a raw varint.
    pub fn write_varint(&mut self, value: u64) -> Result<(), Error> {
        let mut value = value;
        let fallback_pos = self.pos;
        if value == 0 {
            return self.write_u8(0x00);
        }
        while value > 0 {
            self.write_u8(((value & 0x7f) as u8) | 0x80)
                .map_err(|err| {
                    self.pos = fallback_pos;
                    err
                })?;
            value >>= 7;
        }
        // clean msb of last byte
        *self.last_u8_mut() &= 0x7f;
        Ok(())
    }

    /// Encode a varint field.
    pub fn encode_varint_field(&mut self, field_number: u8, value: u64) -> Result<(), Error> {
        // if value == 0 {
        //     return Ok(());
        // }
        let key = field_number
            .checked_shl(3)
            .ok_or(Error::InvalidFieldNumber(field_number))?
            + WireType::Varint as u8;
        self.write_u8(key)?;
        self.write_varint(value)
    }

    /// Encode a svarint field.
    pub fn encode_svarint_field(&mut self, field_number: u8, value: i64) -> Result<(), Error> {
        self.encode_varint_field(field_number, svarint_to_varint(value))
    }

    /// Encode a bytes field.
    pub fn encode_bytes_field(&mut self, field_number: u8, value: &[u8]) -> Result<(), Error> {
        // if value.is_empty() {
        //     return Ok(())
        // }
        let key = field_number
            .checked_shl(3)
            .ok_or(Error::InvalidFieldNumber(field_number))?
            + WireType::Bytes as u8;
        self.write_u8(key)?;
        self.write_varint(value.len() as _)?;
        self.write_bytes(value)
    }

    /// Encode a string field.
    pub fn encode_string_field(&mut self, field_number: u8, value: &str) -> Result<(), Error> {
        self.encode_bytes_field(field_number, value.as_bytes())
    }

    /// The raw protobuf bytes encoded.
    pub fn as_bytes(&self) -> &[u8] {
        &self.buf[..self.pos]
    }

    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
        if self.pos < self.buf.len() {
            self.buf[self.pos] = val;
            self.pos += 1;
            Ok(())
        } else {
            Err(Error::BufferOverflow)
        }
    }

    fn write_bytes(&mut self, val: &[u8]) -> Result<(), Error> {
        if self.pos + val.len() < self.buf.len() {
            self.buf[self.pos..self.pos + val.len()].copy_from_slice(val);
            self.pos += val.len();
            Ok(())
        } else {
            Err(Error::BufferOverflow)
        }
    }

    // Require: buf is not empty.
    fn last_u8_mut(&mut self) -> &mut u8 {
        &mut self.buf[self.pos - 1]
    }
}

#[inline]
fn varint_to_svarint(n: u64) -> i64 {
    if n & 0b1 == 1 {
        -((n >> 1) as i64 + 1)
    } else {
        (n >> 1) as i64
    }
}

#[inline]
fn svarint_to_varint(n: i64) -> u64 {
    ((n << 1) ^ (n >> 63)) as u64
}