use std::result;
use crate::error::PbfFormatError;
type Result<T> = result::Result<T, PbfFormatError>;
#[inline]
pub(crate) fn varint(buf: &[u8], pos: &mut usize) -> Option<u64> {
let mut value = 0u64;
for shift in (0..64).step_by(7) {
let byte = *buf.get(*pos)?;
*pos += 1;
if shift == 63 && byte > 1 {
return None;
}
value |= u64::from(byte & 0x7f) << shift;
if byte < 0x80 {
return Some(value);
}
}
None
}
#[inline]
fn for_each(packed: &[u8], what: &'static str, mut f: impl FnMut(u64) -> Result<()>) -> Result<()> {
let mut pos = 0;
while pos < packed.len() {
f(varint(packed, &mut pos).ok_or(PbfFormatError::Packed(what))?)?;
}
Ok(())
}
#[derive(Clone, Copy)]
pub(crate) enum Value<'a> {
Varint(u64),
Bytes(&'a [u8]),
Fixed,
}
pub(crate) fn fields<'a>(
msg: &'a [u8],
what: &'static str,
mut f: impl FnMut(u32, Value<'a>) -> Result<()>,
) -> Result<()> {
let err = || PbfFormatError::Message(what);
let mut pos = 0;
while pos < msg.len() {
let key = varint(msg, &mut pos).ok_or_else(err)?;
let field = u32::try_from(key >> 3)
.ok()
.filter(|&n| n != 0)
.ok_or_else(err)?;
let skip = match key & 7 {
0 => {
f(field, Value::Varint(varint(msg, &mut pos).ok_or_else(err)?))?;
continue;
}
2 => {
let len = varint(msg, &mut pos)
.and_then(|l| usize::try_from(l).ok())
.ok_or_else(err)?;
let bytes = msg
.get(pos..)
.and_then(|rest| rest.get(..len))
.ok_or_else(err)?;
f(field, Value::Bytes(bytes))?;
len
}
1 | 5 => {
f(field, Value::Fixed)?;
if key & 7 == 1 { 8 } else { 4 }
}
_ => return Err(err()),
};
pos = pos
.checked_add(skip)
.filter(|&p| p <= msg.len())
.ok_or_else(err)?;
}
Ok(())
}
pub(crate) fn values(
value: Value<'_>,
what: &'static str,
mut f: impl FnMut(u64) -> Result<()>,
) -> Result<()> {
match value {
Value::Bytes(bytes) => for_each(bytes, what, f),
Value::Varint(v) => f(v),
Value::Fixed => Err(PbfFormatError::Packed(what)),
}
}
pub(crate) fn count(packed: &[u8]) -> Option<usize> {
let (mut count, mut continued) = (0, 0);
for &byte in packed {
if continued == 9 && byte > 1 {
return None;
}
if byte < 0x80 {
count += 1;
continued = 0;
} else {
continued += 1;
}
}
(continued == 0).then_some(count)
}
pub(crate) fn push_varint(out: &mut Vec<u8>, mut v: u64) {
let low = |v: u64| u8::try_from(v & 0x7f).unwrap_or_default();
while v >= 0x80 {
out.push(low(v) | 0x80);
v >>= 7;
}
out.push(low(v));
}
#[inline]
pub(crate) fn zigzag(v: u64) -> i64 {
(v >> 1).cast_signed() ^ -(v & 1).cast_signed()
}
#[cfg(test)]
#[allow(
clippy::cast_possible_truncation,
reason = "a test encoder truncates on purpose"
)]
mod tests {
use super::*;
fn encode(values: &[u64]) -> Vec<u8> {
let mut out = Vec::new();
for &v in values {
push_varint(&mut out, v);
}
out
}
fn all(packed: &[u8]) -> Result<Vec<u64>> {
let mut out = Vec::new();
for_each(packed, "t", |v| {
out.push(v);
Ok(())
})?;
Ok(out)
}
#[test]
fn varint_edges() {
let values = [
0,
1,
127,
128,
16_383,
16_384,
u64::from(u32::MAX),
u64::MAX - 1,
u64::MAX,
];
let bytes = encode(&values);
assert_eq!(all(&bytes).unwrap(), values);
}
#[test]
fn rejects_bad_varints() {
all(&[0x80]).unwrap_err();
all(&[0x01, 0x80]).unwrap_err();
let mut eleven = vec![0xff; 10];
eleven.push(0x01);
all(&eleven).unwrap_err();
let mut ten = vec![0xff; 9];
ten.push(0x02);
all(&ten).unwrap_err();
let mut ok = vec![0xff; 9];
ok.push(0x01);
assert_eq!(all(&ok).unwrap(), [u64::MAX]);
}
#[test]
fn walks_fields() {
let msg = [
0x08, 0x96, 0x01, 0x12, 2, b'a', b'b', 0x19, 0, 0, 0, 0, 0, 0, 0, 0, 0x25, 0, 0, 0, 0,
0x28, 1,
];
let mut seen = Vec::new();
fields(&msg, "t", |f, v| {
seen.push(match v {
Value::Varint(v) => (f, Some(v), None),
Value::Bytes(b) => (f, None, Some(b.to_vec())),
Value::Fixed => (f, None, None),
});
Ok(())
})
.unwrap();
assert_eq!(
seen,
[
(1, Some(150), None),
(2, None, Some(b"ab".to_vec())),
(3, None, None),
(4, None, None),
(5, Some(1), None),
]
);
for bad in [&[0x12, 5, b'a'][..], &[0x0b], &[0x00, 1], &[0x09, 0, 0]] {
assert!(fields(bad, "t", |_, _| Ok(())).is_err(), "{bad:?}");
}
}
#[test]
fn count_checks_every_varint() {
let mut packed = Vec::new();
for v in [0, 1, 127, 128, u64::MAX] {
push_varint(&mut packed, v);
}
assert_eq!(packed, encode(&[0, 1, 127, 128, u64::MAX]));
assert_eq!(count(&packed), Some(5));
assert_eq!(count(&[]), Some(0));
assert_eq!(count(&[0x80]), None, "cut off");
assert_eq!(
count(&[0xff; 9].iter().copied().chain([2]).collect::<Vec<_>>()),
None,
"past 64 bits"
);
assert_eq!(count(&[0x80; 10]), None, "11 bytes");
}
#[test]
fn values_packed_or_not() {
let mut out = Vec::new();
values(Value::Bytes(&encode(&[1, 300])), "t", |v| {
out.push(v);
Ok(())
})
.unwrap();
values(Value::Varint(7), "t", |v| {
out.push(v);
Ok(())
})
.unwrap();
assert_eq!(out, [1, 300, 7]);
assert!(values(Value::Fixed, "t", |_| Ok(())).is_err());
}
}