use super::map::merge_map;
use super::scalar::Sc;
use super::{
Bool, Double, Entry, EnumVt, Fixed32, Fixed64, Float, Int32, Int64, Kind, MessageTable,
OneofVt, Sfixed32, Sfixed64, Sint32, Sint64, Uint32, Uint64, IMPLICIT, NO_UNKNOWN, OPTIONAL,
PACKED, REPEATED, REQUIRED,
};
use crate::alloc::{string::String, vec::Vec};
use crate::bytes::Buf;
use crate::encoding::{
check_wire_type, decode_unknown_field, decode_varint, wire_type_mismatch, Tag, WireType,
};
use crate::message::MAX_MESSAGE_BYTES;
use crate::{types, DecodeContext, DecodeError, UnknownField, UnknownFieldData, UnknownFields};
pub(super) unsafe fn merge_to_limit<B: Buf>(
table: &MessageTable,
base: *mut u8,
buf: &mut B,
ctx: DecodeContext<'_>,
limit: usize,
) -> Result<(), DecodeError> {
let n = buf.remaining().saturating_sub(limit);
let ctx = ctx.with_message_end(0);
let chunk = buf.chunk();
if chunk.len() >= n {
let mut payload = &chunk[..n];
unsafe { merge_slice(table, base, &mut payload, ctx)? };
buf.advance(n);
Ok(())
} else {
let gathered = buf.copy_to_bytes(n);
let mut payload = &gathered[..];
unsafe { merge_slice(table, base, &mut payload, ctx) }
}
}
pub(super) unsafe fn merge_length_delimited<B: Buf>(
table: &MessageTable,
base: *mut u8,
buf: &mut B,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let ctx = ctx.descend()?;
let len = decode_varint(buf)?;
if len > u64::from(MAX_MESSAGE_BYTES) {
return Err(DecodeError::MessageTooLarge);
}
let len = usize::try_from(len).map_err(|_| DecodeError::MessageTooLarge)?;
ctx.check_len(buf.remaining(), len)?;
let limit = buf.remaining() - len;
unsafe { merge_to_limit(table, base, buf, ctx, limit) }
}
pub(super) unsafe fn merge_field<B: Buf>(
table: &MessageTable,
base: *mut u8,
tag: Tag,
buf: &mut B,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let remaining = buf.remaining();
let chunk = buf.chunk();
if chunk.len() < remaining {
return Err(DecodeError::UnexpectedEof);
}
let Some(span) = remaining.checked_sub(ctx.message_end()) else {
return Err(DecodeError::UnexpectedEof);
};
let mut payload = &chunk[..span];
unsafe { merge_one(table, base, tag, &mut payload, ctx.with_message_end(0))? };
let consumed = span - payload.len();
buf.advance(consumed);
Ok(())
}
unsafe fn merge_slice(
table: &MessageTable,
base: *mut u8,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
while !buf.is_empty() {
let tag = Tag::decode(buf)?;
unsafe { merge_one(table, base, tag, buf, ctx)? };
}
Ok(())
}
pub(super) unsafe fn merge_sub(
table: &MessageTable,
base: *mut u8,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let ctx = ctx.descend()?;
let len = decode_varint(buf)?;
if len > u64::from(MAX_MESSAGE_BYTES) {
return Err(DecodeError::MessageTooLarge);
}
let len = usize::try_from(len).map_err(|_| DecodeError::MessageTooLarge)?;
if buf.len() < len {
return Err(DecodeError::UnexpectedEof);
}
let (mut payload, rest) = buf.split_at(len);
*buf = rest;
unsafe { merge_slice(table, base, &mut payload, ctx.with_message_end(0)) }
}
#[inline]
unsafe fn merge_one(
table: &MessageTable,
base: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let Some(e) = table.find(tag.field_number()) else {
return unsafe { merge_unknown(table, base, tag, buf, ctx) };
};
let slot = unsafe { base.add(e.offset as usize) };
unsafe {
if e.kind == Kind::Map {
merge_map(table, e, base, slot, tag, buf, ctx)
} else {
merge_kind(table, e, base, slot, tag, buf, ctx)
}
}
}
#[cold]
unsafe fn merge_unknown(
table: &MessageTable,
base: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
if table.unknown == NO_UNKNOWN {
return crate::encoding::skip_unknown_field(tag, buf, ctx);
}
let field = decode_unknown_field(tag, buf, ctx)?;
unsafe { (*base.add(table.unknown as usize).cast::<UnknownFields>()).push(field) };
Ok(())
}
macro_rules! merge_dispatch {
($($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
#[inline]
pub(super) unsafe fn merge_kind(
table: &MessageTable,
e: &Entry,
base: *mut u8,
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
unsafe {
match e.kind {
$(Kind::$name => merge_dispatch!(@arm $fam $ty $card table e base slot tag buf ctx),)*
}
}
}
};
(@arm Scalar $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
merge_scalar::<$ty, $card>($slot, $tag, $buf)
};
(@arm Str $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
merge_str::<$card>($slot, $tag, $buf, $ctx)
};
(@arm Bytes $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
merge_bytes::<$card>($slot, $tag, $buf, $ctx)
};
(@arm Enum $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
merge_enum::<$card>($table, $e, $base, $slot, $tag, $buf, $ctx)
};
(@arm Msg $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
merge_msg::<$card>($table, $e, $slot, $tag, $buf, $ctx)
};
(@arm Oneof $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
merge_oneof($table, $e, $base, $slot, $tag, $buf, $ctx)
};
(@arm Map $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
unreachable!("a map field is merged by `merge_one`")
};
}
kind_table!(merge_dispatch);
#[inline(never)]
unsafe fn merge_oneof(
table: &MessageTable,
e: &Entry,
base: *mut u8,
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let m = table.member(e);
let payload_entry = Entry {
kind: m.kind,
aux: m.aux,
..*e
};
let oneof = OneofSlot {
group: table.group(m),
slot,
};
unsafe { merge_payload(table, &payload_entry, &oneof, base, tag, buf, ctx) }
}
struct OneofSlot<'a> {
group: &'a OneofVt,
slot: *mut u8,
}
impl OneofSlot<'_> {
#[inline]
unsafe fn place(&self, number: u32) -> *mut u8 {
unsafe { (self.group.place)(self.slot, number) }
}
#[inline]
unsafe fn place_with(
&self,
number: u32,
f: &mut dyn FnMut(*mut u8) -> Result<(), DecodeError>,
) -> Result<(), DecodeError> {
unsafe { (self.group.place_with)(self.slot, number, f) }
}
}
macro_rules! merge_payload_dispatch {
($fname:ident; $($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
#[inline]
unsafe fn $fname(
table: &MessageTable,
payload_entry: &Entry,
oneof: &OneofSlot<'_>,
base: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let number = payload_entry.number();
unsafe {
match payload_entry.kind {
$(Kind::$name => merge_payload_dispatch!(
@arm $fam $ty table payload_entry oneof number base tag buf ctx
),)*
#[allow(unreachable_patterns)]
_ => unreachable!("`Table::new` checked the payload kinds"),
}
}
}
};
(@arm Scalar $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
merge_oneof_scalar::<$ty>($o, $n, $tag, $buf)
};
(@arm Str $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
merge_oneof_str($o, $n, $tag, $buf)
};
(@arm Bytes $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
merge_oneof_bytes($o, $n, $tag, $buf)
};
(@arm Enum $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
merge_oneof_enum($table, $pe, $o, $base, $tag, $buf, $ctx)
};
(@arm Msg $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
merge_oneof_msg($table, $pe, $o, $tag, $buf, $ctx)
};
}
payload_kind_table!(merge_payload_dispatch, merge_payload);
#[inline]
unsafe fn merge_oneof_scalar<S: Sc>(
oneof: &OneofSlot<'_>,
number: u32,
tag: Tag,
buf: &mut &[u8],
) -> Result<(), DecodeError> {
check_wire_type(tag, S::WIRE)?;
let value = S::read(buf)?;
unsafe { *oneof.place(number).cast::<S::V>() = value };
Ok(())
}
#[inline]
unsafe fn merge_oneof_str(
oneof: &OneofSlot<'_>,
number: u32,
tag: Tag,
buf: &mut &[u8],
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::LengthDelimited)?;
let value = types::decode_string(buf)?;
unsafe { *oneof.place(number).cast::<String>() = value };
Ok(())
}
#[inline]
unsafe fn merge_oneof_bytes(
oneof: &OneofSlot<'_>,
number: u32,
tag: Tag,
buf: &mut &[u8],
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::LengthDelimited)?;
let value = types::decode_bytes(buf)?;
unsafe { *oneof.place(number).cast::<Vec<u8>>() = value };
Ok(())
}
#[inline]
unsafe fn merge_oneof_enum(
table: &MessageTable,
payload_entry: &Entry,
oneof: &OneofSlot<'_>,
base: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::Varint)?;
let raw = types::decode_int32(buf)?;
let vt = table.enum_vt(payload_entry);
if !(vt.accepts)(raw) {
return unsafe { enum_reject(table, payload_entry, base, raw, ctx) };
}
let stored = unsafe { (vt.set)(oneof.place(payload_entry.number()), raw) };
debug_assert!(stored, "`accepts` said the enum stores {raw}");
Ok(())
}
#[inline]
unsafe fn merge_oneof_msg(
table: &MessageTable,
payload_entry: &Entry,
oneof: &OneofSlot<'_>,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::LengthDelimited)?;
let vt = table.msg_vt(payload_entry);
unsafe {
oneof.place_with(payload_entry.number(), &mut |child| {
vt.child.merge_sub(child, buf, ctx)
})
}
}
#[inline]
unsafe fn merge_scalar<S: Sc, const C: u8>(
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
) -> Result<(), DecodeError> {
unsafe {
match C {
IMPLICIT | REQUIRED => {
check_wire_type(tag, S::WIRE)?;
*slot.cast::<S::V>() = S::read(buf)?;
}
OPTIONAL => {
check_wire_type(tag, S::WIRE)?;
*slot.cast::<Option<S::V>>() = Some(S::read(buf)?);
}
_ => {
let out = &mut *slot.cast::<Vec<S::V>>();
let wire = tag.wire_type();
if wire == WireType::LengthDelimited {
let payload = take_len_delimited(buf)?;
S::extend(payload, out)?;
} else if wire == S::WIRE {
out.push(S::read(buf)?);
} else {
return Err(wire_type_mismatch(tag, WireType::LengthDelimited));
}
}
}
}
Ok(())
}
#[inline]
pub(super) fn take_len_delimited<'a>(buf: &mut &'a [u8]) -> Result<&'a [u8], DecodeError> {
let len = decode_varint(buf)?;
let len = usize::try_from(len).map_err(|_| DecodeError::MessageTooLarge)?;
if buf.len() < len {
return Err(DecodeError::UnexpectedEof);
}
let (payload, rest) = buf.split_at(len);
*buf = rest;
Ok(payload)
}
#[inline]
unsafe fn merge_str<const C: u8>(
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::LengthDelimited)?;
unsafe {
match C {
IMPLICIT | REQUIRED => types::merge_string(&mut *slot.cast::<String>(), buf),
OPTIONAL => types::merge_string(
(*slot.cast::<Option<String>>()).get_or_insert_with(String::new),
buf,
),
_ => {
let elem = types::decode_string(buf)?;
ctx.register_element_memory(core::mem::size_of::<String>())?;
(*slot.cast::<Vec<String>>()).push(elem);
Ok(())
}
}
}
}
#[inline]
unsafe fn merge_bytes<const C: u8>(
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::LengthDelimited)?;
unsafe {
match C {
IMPLICIT | REQUIRED => types::merge_bytes(&mut *slot.cast::<Vec<u8>>(), buf),
OPTIONAL => types::merge_bytes(
(*slot.cast::<Option<Vec<u8>>>()).get_or_insert_with(Vec::new),
buf,
),
_ => {
let elem = types::decode_bytes(buf)?;
ctx.register_element_memory(core::mem::size_of::<Vec<u8>>())?;
(*slot.cast::<Vec<Vec<u8>>>()).push(elem);
Ok(())
}
}
}
}
#[inline]
unsafe fn merge_msg<const C: u8>(
table: &MessageTable,
e: &Entry,
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
check_wire_type(tag, WireType::LengthDelimited)?;
unsafe {
if C == REPEATED {
let vt = table.rep_vt(e);
ctx.register_element_memory(vt.size)?;
let elem = (vt.push)(slot);
let decoded = vt.child.merge_sub(elem, buf, ctx);
if decoded.is_err() {
(vt.pop)(slot);
}
decoded
} else {
let vt = table.msg_vt(e);
let child = (vt.place)(slot);
vt.child.merge_sub(child, buf, ctx)
}
}
}
#[inline]
unsafe fn merge_enum<const C: u8>(
table: &MessageTable,
e: &Entry,
base: *mut u8,
slot: *mut u8,
tag: Tag,
buf: &mut &[u8],
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let vt = table.enum_vt(e);
unsafe {
match C {
REPEATED | PACKED => {
let wire = tag.wire_type();
if wire == WireType::LengthDelimited {
let mut payload = take_len_delimited(buf)?;
while !payload.is_empty() {
let raw = types::decode_int32_packed(&mut payload)?;
enum_store(table, e, base, vt, slot, raw, ctx)?;
}
Ok(())
} else if wire == WireType::Varint {
let raw = types::decode_int32(buf)?;
enum_store(table, e, base, vt, slot, raw, ctx)
} else {
Err(wire_type_mismatch(tag, WireType::LengthDelimited))
}
}
_ => {
check_wire_type(tag, WireType::Varint)?;
let raw = types::decode_int32(buf)?;
enum_store(table, e, base, vt, slot, raw, ctx)
}
}
}
}
#[inline]
unsafe fn enum_store(
table: &MessageTable,
e: &Entry,
base: *mut u8,
vt: &EnumVt,
slot: *mut u8,
raw: i32,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
if unsafe { (vt.set)(slot, raw) } {
return Ok(());
}
unsafe { enum_reject(table, e, base, raw, ctx) }
}
#[inline]
unsafe fn enum_reject(
table: &MessageTable,
e: &Entry,
base: *mut u8,
raw: i32,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
if table.unknown == NO_UNKNOWN {
return ctx.check_unknown_field();
}
ctx.register_unknown_field()?;
unsafe {
(*base.add(table.unknown as usize).cast::<UnknownFields>()).push(UnknownField {
number: e.tag >> 3,
data: UnknownFieldData::Varint(raw as u64),
});
}
Ok(())
}