/// Additional information passed to every decode/merge function. /// /// The context should be passed by value and can be freely cloned. When passing /// to a function which is decoding a nested object, then use `enter_recursion`. #[derive(Clone, Debug)] #[cfg_attr(feature = "no-recursion-limit", derive(Default))] pubstruct DecodeContext { /// How many times we can recurse in the current decode stack before we hit /// the recursion limit. /// /// The recursion limit is defined by `RECURSION_LIMIT` and cannot be /// customized. The recursion limit can be ignored by building the Prost /// crate with the `no-recursion-limit` feature. #[cfg(not(feature = "no-recursion-limit"))]
recurse_count: u32,
}
impl DecodeContext { /// Call this function before recursively decoding. /// /// There is no `exit` function since this function creates a new `DecodeContext` /// to be used at the next level of recursion. Continue to use the old context // at the previous level of recursion. #[cfg(not(feature = "no-recursion-limit"))] #[inline] pub(crate) fn enter_recursion(&self) -> DecodeContext {
DecodeContext {
recurse_count: self.recurse_count - 1,
}
}
/// Checks whether the recursion limit has been reached in the stack of /// decodes described by the `DecodeContext` at `self.ctx`. /// /// Returns `Ok<()>` if it is ok to continue recursing. /// Returns `Err<DecodeError>` if the recursion limit has been reached. #[cfg(not(feature = "no-recursion-limit"))] #[inline] pub(crate) fn limit_reached(&self) -> Result<(), DecodeError> { ifself.recurse_count == 0 {
Err(DecodeError::new("recursion limit reached"))
} else {
Ok(())
}
}
#[cfg(feature = "no-recursion-limit")] #[inline] #[allow(clippy::unnecessary_wraps)] // needed in other features pub(crate) fn limit_reached(&self) -> Result<(), DecodeError> {
Ok(())
}
}
/// Encodes a Protobuf field key, which consists of a wire type designator and /// the field tag. #[inline] pubfn encode_key(tag: u32, wire_type: WireType, buf: &mutimpl BufMut) {
debug_assert!((MIN_TAG..=MAX_TAG).contains(&tag)); let key = (tag << 3) | wire_type as u32;
encode_varint(u64::from(key), buf);
}
/// Decodes a Protobuf field key, which consists of a wire type designator and /// the field tag. #[inline(always)] pubfn decode_key(buf: &mutimpl Buf) -> Result<(u32, WireType), DecodeError> { let key = decode_varint(buf)?; if key > u64::from(u32::MAX) { return Err(DecodeError::new(format!("invalid key value: {}", key)));
} let wire_type = WireType::try_from(key & 0x07)?; let tag = key as u32 >> 3;
if tag < MIN_TAG { return Err(DecodeError::new("invalid tag value: 0"));
}
Ok((tag, wire_type))
}
/// Returns the width of an encoded Protobuf field key with the given tag. /// The returned width will be between 1 and 5 bytes (inclusive). #[inline] pubconstfn key_len(tag: u32) -> usize {
encoded_len_varint((tag << 3) as u64)
}
/// Helper function which abstracts reading a length delimiter prefix followed /// by decoding values until the length of bytes is exhausted. pubfn merge_loop<T, M, B>(
value: &mut T,
buf: &mut B,
ctx: DecodeContext, mut merge: M,
) -> Result<(), DecodeError> where
M: FnMut(&mut T, &mut B, DecodeContext) -> Result<(), DecodeError>,
B: Buf,
{ let len = decode_varint(buf)?; let remaining = buf.remaining(); if len > remaining as u64 { return Err(DecodeError::new("buffer underflow"));
}
let limit = remaining - len as usize; while buf.remaining() > limit {
merge(value, buf, ctx.clone())?;
}
pubfn skip_field(
wire_type: WireType,
tag: u32,
buf: &mutimpl Buf,
ctx: DecodeContext,
) -> Result<(), DecodeError> {
ctx.limit_reached()?; let len = match wire_type {
WireType::Varint => decode_varint(buf).map(|_| 0)?,
WireType::ThirtyTwoBit => 4,
WireType::SixtyFourBit => 8,
WireType::LengthDelimited => decode_varint(buf)?,
WireType::StartGroup => loop { let (inner_tag, inner_wire_type) = decode_key(buf)?; match inner_wire_type {
WireType::EndGroup => { if inner_tag != tag { return Err(DecodeError::new("unexpected end group tag"));
} break0;
}
_ => skip_field(inner_wire_type, inner_tag, buf, ctx.enter_recursion())?,
}
},
WireType::EndGroup => return Err(DecodeError::new("unexpected end group tag")),
};
if len > buf.remaining() as u64 { return Err(DecodeError::new("buffer underflow"));
}
buf.advance(len as usize);
Ok(())
}
/// Helper macro which emits an `encode_repeated` function for the type.
macro_rules! encode_repeated {
($ty:ty) => { pubfn encode_repeated(tag: u32, values: &[$ty], buf: &mutimpl BufMut) { for value in values {
encode(tag, value, buf);
}
}
};
}
/// Macro which emits a module containing a set of encoding functions for a /// variable width numeric type.
macro_rules! varint {
($ty:ty,
$proto_ty:ident) => (
varint!($ty,
$proto_ty,
to_uint64(value) { *value as u64 },
from_uint64(value) { value as $ty });
);
proptest! { #[test] fn check(value: $ty, tag in MIN_TAG..=MAX_TAG) {
check_type(value, tag, WireType::Varint,
encode, merge, encoded_len)?;
} #[test] fn check_repeated(value: Vec<$ty>, tag in MIN_TAG..=MAX_TAG) {
check_collection_type(value, tag, WireType::Varint,
encode_repeated, merge_repeated,
encoded_len_repeated)?;
} #[test] fn check_packed(value: Vec<$ty>, tag in MIN_TAG..=MAX_TAG) {
check_type(value, tag, WireType::LengthDelimited,
encode_packed, merge_repeated,
encoded_len_packed)?;
}
}
}
}
);
}
varint!(bool, bool,
to_uint64(value) u64::from(*value),
from_uint64(value) value != 0);
varint!(i32, int32);
varint!(i64, int64);
varint!(u32, uint32);
varint!(u64, uint64);
varint!(i32, sint32,
to_uint64(value) {
((value << 1) ^ (value >> 31)) as u32 as u64
},
from_uint64(value) { let value = value as u32;
((value >> 1) as i32) ^ (-((value & 1) as i32))
});
varint!(i64, sint64,
to_uint64(value) {
((value << 1) ^ (value >> 63)) as u64
},
from_uint64(value) {
((value >> 1) as i64) ^ (-((value & 1) as i64))
});
/// Macro which emits a module containing a set of encoding functions for a /// fixed width numeric type.
macro_rules! fixed_width {
($ty:ty,
$width:expr,
$wire_type:expr,
$proto_ty:ident,
$put:ident,
$get:ident) => { pubmod $proto_ty { usecrate::encoding::*;
pubfn merge(
wire_type: WireType,
value: &mut String,
buf: &mutimpl Buf,
ctx: DecodeContext,
) -> Result<(), DecodeError> { // ## Unsafety // // `string::merge` reuses `bytes::merge`, with an additional check of utf-8 // well-formedness. If the utf-8 is not well-formed, or if any other error occurs, then the // string is cleared, so as to avoid leaking a string field with invalid data. // // This implementation uses the unsafe `String::as_mut_vec` method instead of the safe // alternative of temporarily swapping an empty `String` into the field, because it results // in up to 10% better performance on the protobuf message decoding benchmarks. // // It's required when using `String::as_mut_vec` that invalid utf-8 data not be leaked into // the backing `String`. To enforce this, even in the event of a panic in `bytes::merge` or // in the buf implementation, a drop guard is used. unsafe { struct DropGuard<'a>(&'a mut Vec<u8>); impl Drop for DropGuard<'_> { #[inline] fn drop(&mutself) { self.0.clear();
}
}
let drop_guard = DropGuard(value.as_mut_vec());
bytes::merge_one_copy(wire_type, drop_guard.0, buf, ctx)?; match str::from_utf8(drop_guard.0) {
Ok(_) => { // Success; do not clear the bytes.
mem::forget(drop_guard);
Ok(())
}
Err(_) => Err(DecodeError::new( "invalid string value: data is not UTF-8 encoded",
)),
}
}
}
pubfn merge(
wire_type: WireType,
value: &mutimpl BytesAdapter,
buf: &mutimpl Buf,
_ctx: DecodeContext,
) -> Result<(), DecodeError> {
check_wire_type(WireType::LengthDelimited, wire_type)?; let len = decode_varint(buf)?; if len > buf.remaining() as u64 { return Err(DecodeError::new("buffer underflow"));
} let len = len as usize;
// Clear the existing value. This follows from the following rule in the encoding guide[1]: // // > Normally, an encoded message would never have more than one instance of a non-repeated // > field. However, parsers are expected to handle the case in which they do. For numeric // > types and strings, if the same field appears multiple times, the parser accepts the // > last value it sees. // // [1]: https://developers.google.com/protocol-buffers/docs/encoding#optional // // This is intended for A and B both being Bytes so it is zero-copy. // Some combinations of A and B types may cause a double-copy, // in which case merge_one_copy() should be used instead.
value.replace_with(buf.copy_to_bytes(len));
Ok(())
}
pub(super) fn merge_one_copy(
wire_type: WireType,
value: &mutimpl BytesAdapter,
buf: &mutimpl Buf,
_ctx: DecodeContext,
) -> Result<(), DecodeError> {
check_wire_type(WireType::LengthDelimited, wire_type)?; let len = decode_varint(buf)?; if len > buf.remaining() as u64 { return Err(DecodeError::new("buffer underflow"));
} let len = len as usize;
// If we must copy, make sure to copy only once.
value.replace_with(buf.take(len));
Ok(())
}
proptest! { #[test] fn check_vec(value: Vec<u8>, tag in MIN_TAG..=MAX_TAG) { super::test::check_type::<Vec<u8>, Vec<u8>>(value, tag, WireType::LengthDelimited,
encode, merge, encoded_len)?;
}
#[test] fn check_bytes(value: Vec<u8>, tag in MIN_TAG..=MAX_TAG) { let value = Bytes::from(value); super::test::check_type::<Bytes, Bytes>(value, tag, WireType::LengthDelimited,
encode, merge, encoded_len)?;
}
#[test] fn check_repeated_vec(value: Vec<Vec<u8>>, tag in MIN_TAG..=MAX_TAG) { super::test::check_collection_type(value, tag, WireType::LengthDelimited,
encode_repeated, merge_repeated,
encoded_len_repeated)?;
}
#[test] fn check_repeated_bytes(value: Vec<Vec<u8>>, tag in MIN_TAG..=MAX_TAG) { let value = value.into_iter().map(Bytes::from).collect(); super::test::check_collection_type(value, tag, WireType::LengthDelimited,
encode_repeated, merge_repeated,
encoded_len_repeated)?;
}
}
}
}
#[inline] pubfn encoded_len<M>(tag: u32, msg: &M) -> usize where
M: Message,
{ let len = msg.encoded_len();
key_len(tag) + encoded_len_varint(len as u64) + len
}
#[inline] pubfn encoded_len_repeated<M>(tag: u32, messages: &[M]) -> usize where
M: Message,
{
key_len(tag) * messages.len()
+ messages
.iter()
.map(Message::encoded_len)
.map(|len| len + encoded_len_varint(len as u64))
.sum::<usize>()
}
}
ctx.limit_reached()?; loop { let (field_tag, field_wire_type) = decode_key(buf)?; if field_wire_type == WireType::EndGroup { if field_tag != tag { return Err(DecodeError::new("unexpected end group tag"));
} return Ok(());
}
/// Rust doesn't have a `Map` trait, so macros are currently the best way to be /// generic over `HashMap` and `BTreeMap`.
macro_rules! map {
($map_ty:ident) => { usecrate::encoding::*; use core::hash::Hash;
/// Generic protobuf map encode function with an overridden value default. /// /// This is necessary because enumeration values can have a default value other /// than 0 in proto2. pubfn encode_with_default<K, V, B, KE, KL, VE, VL>(
key_encode: KE,
key_encoded_len: KL,
val_encode: VE,
val_encoded_len: VL,
val_default: &V,
tag: u32,
values: &$map_ty<K, V>,
buf: &mut B,
) where
K: Default + Eq + Hash + Ord,
V: PartialEq,
B: BufMut,
KE: Fn(u32, &K, &mut B),
KL: Fn(u32, &K) -> usize,
VE: Fn(u32, &V, &mut B),
VL: Fn(u32, &V) -> usize,
{ for (key, val) in values.iter() { let skip_key = key == &K::default(); let skip_val = val == val_default;
#[test] fn string_merge_invalid_utf8() { letmut s = String::new(); let buf = b"\x02\x80\x80";
let r = string::merge(
WireType::LengthDelimited,
&mut s,
&mut &buf[..],
DecodeContext::default(),
);
r.expect_err("must be an error");
assert!(s.is_empty());
}
/// This big bowl o' macro soup generates an encoding property test for each combination of map /// type, scalar map key, and value type. /// TODO: these tests take a long time to compile, can this be improved? #[cfg(feature = "std")]
macro_rules! map_tests {
(keys: $keys:tt,
vals: $vals:tt) => { mod hash_map {
map_tests!(@private HashMap, hash_map, $keys, $vals);
} mod btree_map {
map_tests!(@private BTreeMap, btree_map, $keys, $vals);
}
};
(@private $map_type:ident,
$mod_name:ident,
[$(($key_ty:ty, $key_proto:ident)),*],
$vals:tt) => {
$( mod $key_proto { use std::collections::$map_type;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.