use crate::error::ErrorKind;
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum WireType {
Varint = 0,
I64 = 1,
Len = 2,
SGroup = 3,
EGroup = 4,
I32 = 5,
}
impl WireType {
#[inline]
pub(crate) fn from_bits(bits: u32) -> Option<WireType> {
match bits {
0 => Some(WireType::Varint),
1 => Some(WireType::I64),
2 => Some(WireType::Len),
3 => Some(WireType::SGroup),
4 => Some(WireType::EGroup),
5 => Some(WireType::I32),
_ => None,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Tag {
pub field: u32,
pub wire: WireType,
}
const MAX_FIELD_NUMBER: u32 = (1 << 29) - 1;
const MAX_GROUP_DEPTH: u32 = 100;
#[inline(always)]
pub fn read_varint<'a>(rest: &'a [u8], out: &mut u64) -> Result<&'a [u8], ErrorKind> {
if let Some((&b0, tail)) = rest.split_first() {
if b0 & 0x80 == 0 {
*out = u64::from(b0);
return Ok(tail);
}
}
read_varint_cold(rest, out)
}
#[cold]
#[inline(never)]
fn read_varint_cold<'a>(rest: &'a [u8], out: &mut u64) -> Result<&'a [u8], ErrorKind> {
read_varint_full(rest, out)
}
#[inline(always)]
pub fn read_varint_inline<'a>(rest: &'a [u8], out: &mut u64) -> Result<&'a [u8], ErrorKind> {
if let Some((&b0, tail)) = rest.split_first() {
if b0 & 0x80 == 0 {
*out = u64::from(b0);
return Ok(tail);
}
}
read_varint_full(rest, out)
}
#[inline(always)]
pub(crate) fn read_varint_full<'a>(rest: &'a [u8], out: &mut u64) -> Result<&'a [u8], ErrorKind> {
let mut result: u64 = 0;
let mut shift: u32 = 0;
for (i, &byte) in rest.iter().take(10).enumerate() {
result |= u64::from(byte & 0x7f) << shift;
if byte & 0x80 == 0 {
if i == 9 && byte > 0x01 {
return Err(ErrorKind::VarintOverflow);
}
*out = result;
return Ok(&rest[i + 1..]);
}
shift += 7;
}
if rest.len() >= 10 {
Err(ErrorKind::VarintOverflow)
} else {
Err(ErrorKind::Truncated)
}
}
pub struct PackedVarints<'a> {
run: &'a [u8],
rest: core::slice::Iter<'a, u8>,
}
impl<'a> PackedVarints<'a> {
#[inline(always)]
#[must_use]
pub fn new(run: &'a [u8]) -> Self {
Self {
run,
rest: run.iter(),
}
}
#[inline(always)]
pub fn read(&mut self) -> Result<Option<u64>, ErrorKind> {
let Some(&b0) = self.rest.next() else {
return Ok(None);
};
if b0 & 0x80 == 0 {
return Ok(Some(u64::from(b0)));
}
let mut value = u64::from(b0 & 0x7f);
for i in 1..10 {
let Some(&byte) = self.rest.next() else {
return Err(self.rewind_by(i, ErrorKind::Truncated));
};
value |= u64::from(byte & 0x7f) << (7 * i);
if byte & 0x80 == 0 {
if i == 9 && byte > 0x01 {
return Err(self.rewind_by(10, ErrorKind::VarintOverflow));
}
return Ok(Some(value));
}
}
Err(self.rewind_by(10, ErrorKind::VarintOverflow))
}
#[inline(always)]
#[must_use]
pub fn remaining(&self) -> &'a [u8] {
self.rest.as_slice()
}
#[inline(always)]
fn rewind_by(&mut self, consumed: usize, kind: ErrorKind) -> ErrorKind {
self.rest = rewind(self.run, self.rest.len(), consumed);
kind
}
}
#[cold]
fn rewind(run: &[u8], left: usize, consumed: usize) -> core::slice::Iter<'_, u8> {
run[run.len() - left - consumed..].iter()
}
#[inline]
pub fn read_tag_or_end<'a>(rest: &'a [u8], out: &mut Tag) -> Result<Option<&'a [u8]>, ErrorKind> {
match rest.split_first() {
None => Ok(None),
Some((&first, tail)) => read_tag_from(first, tail, rest, out).map(Some),
}
}
#[inline]
pub fn read_tag<'a>(rest: &'a [u8], out: &mut Tag) -> Result<&'a [u8], ErrorKind> {
match rest.split_first() {
None => Err(ErrorKind::Truncated),
Some((&first, tail)) => read_tag_from(first, tail, rest, out),
}
}
#[inline]
fn read_tag_from<'a>(
first: u8,
tail: &'a [u8],
rest: &'a [u8],
out: &mut Tag,
) -> Result<&'a [u8], ErrorKind> {
if first & 0x80 == 0 {
let field = u32::from(first >> 3);
if field != 0 {
if let Some(wire) = WireType::from_bits(u32::from(first & 0x07)) {
*out = Tag { field, wire };
return Ok(tail);
}
}
} else if let Some((&second, rest2)) = tail.split_first() {
if second.wrapping_sub(1) < 0x7f {
let value = (u32::from(first) & 0x7f) | (u32::from(second) << 7);
if let Some(wire) = WireType::from_bits(value & 0x07) {
*out = Tag {
field: value >> 3,
wire,
};
return Ok(rest2);
}
}
}
read_tag_general(rest, out)
}
#[inline]
fn read_tag_general<'a>(rest: &'a [u8], out: &mut Tag) -> Result<&'a [u8], ErrorKind> {
let mut raw = 0u64;
let rest = read_varint(rest, &mut raw)?;
let field = raw >> 3;
if field == 0 || field > u64::from(MAX_FIELD_NUMBER) {
return Err(ErrorKind::InvalidTag);
}
let wire = WireType::from_bits((raw & 0x07) as u32).ok_or(ErrorKind::InvalidTag)?;
*out = Tag {
field: field as u32,
wire,
};
Ok(rest)
}
#[inline]
pub fn read_fixed32<'a>(rest: &'a [u8], out: &mut u32) -> Result<&'a [u8], ErrorKind> {
match rest.first_chunk::<4>() {
Some(bytes) => {
*out = u32::from_le_bytes(*bytes);
Ok(&rest[4..])
}
None => Err(ErrorKind::Truncated),
}
}
#[inline]
pub fn read_fixed64<'a>(rest: &'a [u8], out: &mut u64) -> Result<&'a [u8], ErrorKind> {
match rest.first_chunk::<8>() {
Some(bytes) => {
*out = u64::from_le_bytes(*bytes);
Ok(&rest[8..])
}
None => Err(ErrorKind::Truncated),
}
}
#[inline]
pub fn read_length_delimited<'a>(
rest: &'a [u8],
out: &mut &'a [u8],
) -> Result<&'a [u8], ErrorKind> {
let mut len = 0u64;
let rest = read_varint(rest, &mut len)?;
let len = usize::try_from(len).map_err(|_| ErrorKind::Truncated)?;
if len > rest.len() {
return Err(ErrorKind::Truncated);
}
let (payload, tail) = rest.split_at(len);
*out = payload;
Ok(tail)
}
pub fn skip(rest: &[u8], tag: Tag) -> Result<&[u8], ErrorKind> {
skip_depth(rest, tag, 0)
}
fn skip_depth(rest: &[u8], tag: Tag, depth: u32) -> Result<&[u8], ErrorKind> {
match tag.wire {
WireType::Varint => {
let mut v = 0;
read_varint(rest, &mut v)
}
WireType::I64 => {
let mut v = 0;
read_fixed64(rest, &mut v)
}
WireType::I32 => {
let mut v = 0;
read_fixed32(rest, &mut v)
}
WireType::Len => {
let mut p: &[u8] = &[];
read_length_delimited(rest, &mut p)
}
WireType::EGroup => Err(ErrorKind::InvalidWireType),
WireType::SGroup => {
if depth >= MAX_GROUP_DEPTH {
return Err(ErrorKind::DepthLimitExceeded);
}
let mut rest = rest;
loop {
let mut inner = Tag {
field: 0,
wire: WireType::Varint,
};
rest = read_tag(rest, &mut inner)?;
if inner.wire == WireType::EGroup {
if inner.field != tag.field {
return Err(ErrorKind::InvalidWireType);
}
break;
}
rest = skip_depth(rest, inner, depth + 1)?;
}
Ok(rest)
}
}
}
#[inline]
pub fn read_group<'a>(
rest: &'a [u8],
field: u32,
out: &mut &'a [u8],
) -> Result<&'a [u8], ErrorKind> {
let body_start = rest;
let mut rest = rest;
loop {
let at_tag = rest;
let mut inner = Tag {
field: 0,
wire: WireType::Varint,
};
rest = read_tag(rest, &mut inner)?;
if inner.wire == WireType::EGroup {
if inner.field != field {
return Err(ErrorKind::InvalidWireType);
}
let body_len = body_start.len() - at_tag.len();
*out = &body_start[..body_len];
break;
}
rest = skip_depth(rest, inner, 1)?;
}
Ok(rest)
}
#[inline(always)]
pub fn skip_field(rest: &[u8], key: u32) -> Result<&[u8], ErrorKind> {
let wire = key & 0x07;
if key >> 3 != 0 {
if wire == 0 {
let mut v = 0;
return read_varint_inline(rest, &mut v);
}
if wire == 2 {
let mut p: &[u8] = &[];
return read_length_delimited(rest, &mut p);
}
if wire == 1 {
return rest.get(8..).ok_or(ErrorKind::Truncated);
}
if wire == 5 {
return rest.get(4..).ok_or(ErrorKind::Truncated);
}
}
skip_field_cold(rest, key)
}
#[cold]
#[inline(never)]
fn skip_field_cold(rest: &[u8], key: u32) -> Result<&[u8], ErrorKind> {
let field = key >> 3;
if field == 0 {
return Err(ErrorKind::InvalidTag);
}
let wire = WireType::from_bits(key & 0x07).ok_or(ErrorKind::InvalidTag)?;
skip(rest, Tag { field, wire })
}
#[inline]
#[must_use]
pub fn zigzag32(value: u32) -> i32 {
((value >> 1) as i32) ^ -((value & 1) as i32)
}
#[inline]
#[must_use]
pub fn zigzag64(value: u64) -> i64 {
((value >> 1) as i64) ^ -((value & 1) as i64)
}
#[cfg(test)]
mod tests {
use super::*;
fn kind(input: &[u8]) -> ErrorKind {
let mut v = 0;
read_varint(input, &mut v).unwrap_err()
}
fn tag(field: u32, wire: WireType) -> Tag {
Tag { field, wire }
}
#[test]
fn varint_values_and_advance() {
let mut v = 0u64;
assert_eq!(read_varint(&[0x00], &mut v).unwrap(), &[] as &[u8]);
assert_eq!(v, 0);
assert_eq!(read_varint(&[0xac, 0x02, 0x7f], &mut v).unwrap(), &[0x7f]);
assert_eq!(v, 300);
let max = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01];
assert_eq!(read_varint(&max, &mut v).unwrap(), &[] as &[u8]);
assert_eq!(v, u64::MAX);
assert_eq!(read_varint(&[0x7f], &mut v).unwrap(), &[] as &[u8]);
assert_eq!(v, 127);
assert_eq!(read_varint(&[0x80, 0x01], &mut v).unwrap(), &[] as &[u8]);
assert_eq!(v, 128);
assert_eq!(read_varint(&[0x7f, 0xaa], &mut v).unwrap(), &[0xaa]);
assert_eq!(v, 127);
}
#[test]
fn varint_rejects_truncated_and_overlong() {
assert_eq!(kind(&[]), ErrorKind::Truncated);
assert_eq!(kind(&[0x80]), ErrorKind::Truncated); assert_eq!(kind(&[0x80; 5]), ErrorKind::Truncated);
assert_eq!(kind(&[0x80; 10]), ErrorKind::VarintOverflow);
assert_eq!(
kind(&[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02]),
ErrorKind::VarintOverflow
);
}
#[test]
fn read_varint_inline_agrees_with_read_varint() {
let mut max = vec![0xff; 9];
max.extend([0x01, 0xaa]);
let overlong_tenth = [0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02];
for input in [
&[][..],
&[0x7f, 0xaa],
&[0x80, 0x01],
&[0xac, 0x02, 0x7f],
&max,
&[0x80],
&[0x80; 10],
&overlong_tenth,
] {
let (mut inline, mut split) = (0u64, 0u64);
assert_eq!(
read_varint_inline(input, &mut inline),
read_varint(input, &mut split),
"{input:02x?}"
);
assert_eq!(inline, split, "{input:02x?}");
}
}
fn packed_varints(run: &[u8]) -> (Vec<u64>, Option<(ErrorKind, usize)>) {
let mut varints = PackedVarints::new(run);
let mut values = Vec::new();
loop {
match varints.read() {
Ok(Some(v)) => values.push(v),
Ok(None) => return (values, None),
Err(k) => return (values, Some((k, run.len() - varints.remaining().len()))),
}
}
}
#[test]
fn packed_varints_values() {
assert_eq!(packed_varints(&[]), (vec![], None));
let mut run = vec![0x7f, 0x80, 0x01, 0xac, 0x02];
run.extend([0xff; 9]);
run.extend([0x01, 0x00]);
assert_eq!(
packed_varints(&run),
(vec![127, 128, 300, u64::MAX, 0], None)
);
}
#[test]
fn packed_varints_reject_as_read_varint_does() {
let overlong_tenth = [0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02];
for bad in [
&[0x80][..],
&[0x80; 5],
&[0x80; 9],
&[0x80; 10],
&[0x80; 11],
&overlong_tenth,
] {
let mut run = vec![0x05];
run.extend_from_slice(bad);
assert_eq!(
packed_varints(&run),
(vec![5], Some((kind(bad), 1))),
"{bad:02x?}"
);
}
}
#[test]
fn tag_decode_and_rejections() {
let read = |input: &[u8]| {
let mut t = tag(0, WireType::Varint);
read_tag(input, &mut t).map(|_| t)
};
assert_eq!(read(&[0x0a]), Ok(tag(1, WireType::Len)));
assert_eq!(read(&[]), Err(ErrorKind::Truncated));
assert_eq!(read(&[0x00]), Err(ErrorKind::InvalidTag));
assert_eq!(read(&[0x0f]), Err(ErrorKind::InvalidTag));
let encode = |value: u64| {
let mut buf = Vec::new();
let mut v = value;
loop {
let byte = (v & 0x7f) as u8;
v >>= 7;
if v == 0 {
buf.push(byte);
break;
}
buf.push(byte | 0x80);
}
buf
};
let max_tag = (u64::from(MAX_FIELD_NUMBER) << 3) | 2;
let over_tag = ((u64::from(MAX_FIELD_NUMBER) + 1) << 3) | 2;
assert_eq!(read(&encode(max_tag)).unwrap().field, MAX_FIELD_NUMBER);
assert_eq!(read(&encode(over_tag)), Err(ErrorKind::InvalidTag));
let mut t = tag(0, WireType::Varint);
assert_eq!(read_tag(&[0x0a, 0xff], &mut t).unwrap(), &[0xff]);
assert_eq!(t, tag(1, WireType::Len));
assert_eq!(read_tag(&[0x90, 0x01, 0x7f], &mut t).unwrap(), &[0x7f]);
assert_eq!(t, tag(18, WireType::Varint));
assert_eq!(read(&[0x96, 0x01]), Err(ErrorKind::InvalidTag));
assert_eq!(read(&[0x88]), Err(ErrorKind::Truncated));
}
#[test]
fn fixed_and_length_delimited() {
let mut x = 0u32;
assert_eq!(
read_fixed32(&[0x78, 0x56, 0x34, 0x12, 0xff], &mut x).unwrap(),
&[0xff]
);
assert_eq!(x, 0x1234_5678);
assert_eq!(read_fixed32(&[0, 0, 0], &mut x), Err(ErrorKind::Truncated));
let mut y = 0u64;
assert_eq!(
read_fixed64(&[1, 2, 3, 4, 5, 6, 7, 8], &mut y).unwrap(),
&[] as &[u8]
);
assert_eq!(y, 0x0807_0605_0403_0201);
assert_eq!(read_fixed64(&[0; 7], &mut y), Err(ErrorKind::Truncated));
let mut payload: &[u8] = &[];
assert_eq!(
read_length_delimited(&[0x03, b'a', b'b', b'c', b'z'], &mut payload).unwrap(),
b"z"
);
assert_eq!(payload, b"abc");
assert_eq!(
read_length_delimited(&[0x05, b'a'], &mut payload),
Err(ErrorKind::Truncated)
);
}
#[test]
fn skip_every_wire_type_including_nested_groups() {
assert_eq!(
skip(&[0xac, 0x02, 0x7f], tag(1, WireType::Varint)),
Ok(&[0x7f][..])
);
assert_eq!(skip(&[0; 9], tag(1, WireType::I64)), Ok(&[0][..]));
assert_eq!(skip(&[0; 5], tag(1, WireType::I32)), Ok(&[0][..]));
assert_eq!(
skip(&[0x02, b'h', b'i', 0x7f], tag(1, WireType::Len)),
Ok(&[0x7f][..])
);
let group = [
0x08, 0x01, 0x0b, 0x0c,
0x0c,
];
assert_eq!(skip(&group, tag(1, WireType::SGroup)), Ok(&[][..]));
assert_eq!(
skip(&[], tag(1, WireType::EGroup)),
Err(ErrorKind::InvalidWireType)
);
assert_eq!(
skip(&[0x14], tag(1, WireType::SGroup)),
Err(ErrorKind::InvalidWireType)
);
assert_eq!(skip(&[0x0c], tag(1, WireType::SGroup)), Ok(&[][..]));
}
#[test]
fn skip_group_depth_is_capped() {
let starts = vec![0x0b_u8; (MAX_GROUP_DEPTH as usize) + 5];
assert_eq!(
skip(&starts, tag(1, WireType::SGroup)),
Err(ErrorKind::DepthLimitExceeded)
);
}
#[test]
fn groups_nest_up_to_max_group_depth() {
let nested = |levels: usize| {
let mut buf = vec![0x0b_u8; levels - 1];
buf.resize(2 * levels - 1, 0x0c);
buf
};
let (at_cap, over_cap) = (
nested(MAX_GROUP_DEPTH as usize),
nested(MAX_GROUP_DEPTH as usize + 1),
);
let mut body: &[u8] = &[];
assert_eq!(skip(&at_cap, tag(1, WireType::SGroup)), Ok(&[][..]));
assert_eq!(read_group(&at_cap, 1, &mut body), Ok(&[][..]));
assert_eq!(
skip(&over_cap, tag(1, WireType::SGroup)),
Err(ErrorKind::DepthLimitExceeded)
);
assert_eq!(
read_group(&over_cap, 1, &mut body),
Err(ErrorKind::DepthLimitExceeded)
);
}
#[test]
fn read_group_returns_the_body_span_and_the_tail() {
let buf = [0x08, 0x01, 0x13, 0x14, 0x0c, 0xde, 0xad];
let mut body: &[u8] = &[];
let tail = read_group(&buf, 1, &mut body).unwrap();
assert_eq!(body, &[0x08, 0x01, 0x13, 0x14]);
assert_eq!(tail, &[0xde, 0xad]);
}
#[test]
fn read_group_rejects_mismatched_end_and_caps_depth() {
let mut body: &[u8] = &[];
assert_eq!(
read_group(&[0x14], 1, &mut body).unwrap_err(),
ErrorKind::InvalidWireType
);
let starts = vec![0x0b_u8; (MAX_GROUP_DEPTH as usize) + 5];
assert_eq!(
read_group(&starts, 1, &mut body).unwrap_err(),
ErrorKind::DepthLimitExceeded
);
}
#[test]
fn skip_field_dispatches_on_the_raw_key() {
let key = |field: u32, wire: WireType| (field << 3) | u32::from(wire as u8);
assert_eq!(
skip_field(&[0xac, 0x02, 0x7f], key(1, WireType::Varint)).unwrap(),
&[0x7f]
);
assert_eq!(
skip_field(&[0x02, b'h', b'i', 0xff], key(2, WireType::Len)).unwrap(),
&[0xff]
);
assert_eq!(
skip_field(&[0; 4], key(3, WireType::I32)).unwrap(),
&[] as &[u8]
);
assert_eq!(
skip_field(&[0; 8], key(4, WireType::I64)).unwrap(),
&[] as &[u8]
);
assert_eq!(skip_field(&[], 0x04).unwrap_err(), ErrorKind::InvalidTag);
assert_eq!(skip_field(&[], 0x0f).unwrap_err(), ErrorKind::InvalidTag);
for key in [0x00, 0x01, 0x02, 0x05] {
assert_eq!(
skip_field(&[0; 16], key),
Err(ErrorKind::InvalidTag),
"{key:#x}"
);
}
let big = key(MAX_FIELD_NUMBER, WireType::Len);
assert_eq!(skip_field(&[0x00], big).unwrap(), &[] as &[u8]);
}
#[test]
fn skip_field_agrees_with_skip() {
let mut max = vec![0xff; 9];
max.extend([0x01, 0xaa]);
let overlong_tenth = [0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02];
let inputs: &[&[u8]] = &[
&[],
&[0x7f, 0xaa],
&[0xac, 0x02, 0x7f],
&max,
&[0x80],
&[0x80; 10],
&overlong_tenth,
&[0x02, 1, 2, 3],
&[0x03, 1, 2],
&[0; 7],
&[0x0c, 0xaa],
];
for wire in [
WireType::Varint,
WireType::I64,
WireType::Len,
WireType::SGroup,
WireType::EGroup,
WireType::I32,
] {
for &input in inputs {
let key = (1 << 3) | u32::from(wire as u8);
assert_eq!(
skip_field(input, key),
skip(input, tag(1, wire)),
"{wire:?} {input:02x?}"
);
}
}
}
#[test]
fn zigzag_round_trips_known_values() {
assert_eq!(zigzag32(0), 0);
assert_eq!(zigzag32(1), -1);
assert_eq!(zigzag32(2), 1);
assert_eq!(zigzag32(4_294_967_294), i32::MAX);
assert_eq!(zigzag32(4_294_967_295), i32::MIN);
assert_eq!(zigzag64(0), 0);
assert_eq!(zigzag64(1), -1);
assert_eq!(zigzag64(u64::MAX), i64::MIN);
}
#[test]
fn never_panics_on_arbitrary_or_truncated_input() {
let samples: &[&[u8]] = &[
&[
0x08, 0xac, 0x02, 0x12, 0x03, b'a', b'b', b'c', 0x1d, 1, 2, 3, 4,
],
&[0xff; 32],
&[0x80; 32],
&[0x0b, 0x0b, 0x0b, 0x12, 0xff],
];
for sample in samples {
for end in 0..=sample.len() {
let mut rest = &sample[..end];
let mut field_tag = tag(0, WireType::Varint);
while let Ok(Some(value)) = read_tag_or_end(rest, &mut field_tag) {
match skip(value, field_tag) {
Ok(next) => rest = next,
Err(_) => break,
}
}
}
}
}
#[test]
fn wire_type_repr_is_the_wire_code() {
assert_eq!(WireType::Varint as u8, 0);
assert_eq!(WireType::I64 as u8, 1);
assert_eq!(WireType::Len as u8, 2);
assert_eq!(WireType::SGroup as u8, 3);
assert_eq!(WireType::EGroup as u8, 4);
assert_eq!(WireType::I32 as u8, 5);
}
#[test]
fn read_tag_or_end_fused_end_one_two_and_three_byte() {
let mut t = tag(0, WireType::Varint);
assert_eq!(read_tag_or_end(&[], &mut t).unwrap(), None);
assert_eq!(
read_tag_or_end(&[0x0a, 0x00], &mut t).unwrap(),
Some(&[0x00][..])
);
assert_eq!(t, tag(1, WireType::Len));
assert_eq!(
read_tag_or_end(&[0x90, 0x01], &mut t).unwrap(),
Some(&[][..])
);
assert_eq!(t, tag(18, WireType::Varint));
assert_eq!(
read_tag_or_end(&[0x80, 0x80, 0x01, 0x42], &mut t).unwrap(),
Some(&[0x42][..])
);
assert_eq!(t, tag(2048, WireType::Varint));
assert_eq!(
read_tag_or_end(&[0x0f], &mut t).unwrap_err(),
ErrorKind::InvalidTag
);
}
#[test]
fn fused_2byte_and_general_paths_agree_for_a_mid_range_field() {
let mut fused = tag(0, WireType::Varint);
let mut general = tag(0, WireType::Varint);
assert!(read_tag(&[0x90, 0x01], &mut fused).unwrap().is_empty());
assert!(read_tag(&[0x90, 0x81, 0x00], &mut general)
.unwrap()
.is_empty());
assert_eq!(fused, general);
assert_eq!(fused, tag(18, WireType::Varint));
}
#[test]
fn varint_overflow_surfaces_through_read_tag() {
let mut t = tag(0, WireType::Varint);
assert_eq!(
read_tag(&[0xff; 11], &mut t).unwrap_err(),
ErrorKind::VarintOverflow
);
}
#[test]
fn length_delimited_rejects_huge_and_overflowing_length() {
let mut p: &[u8] = &[];
assert_eq!(
read_length_delimited(&[0xff, 0xff, 0xff, 0xff, 0x0f, b'x'], &mut p),
Err(ErrorKind::Truncated)
);
assert_eq!(
read_length_delimited(&[0xff; 11], &mut p).unwrap_err(),
ErrorKind::VarintOverflow
);
}
#[test]
fn skip_matches_nested_groups_with_differing_fields() {
assert_eq!(
skip(&[0x13, 0x14, 0x0c], tag(1, WireType::SGroup)),
Ok(&[][..])
);
assert_eq!(
skip(&[0x13, 0x0c, 0x0c], tag(1, WireType::SGroup)),
Err(ErrorKind::InvalidWireType)
);
}
}