prost_reflect/dynamic/
unknown.rs

1use std::{fmt, slice, vec};
2
3use prost::{
4    bytes::{Buf, BufMut, Bytes},
5    encoding::{self, DecodeContext, WireType},
6    DecodeError, Message,
7};
8
9use crate::dynamic::text_format;
10
11/// An unknown field found when deserializing a protobuf message.
12///
13/// A field is unknown if the message descriptor does not contain a field with the given number. This is often the
14/// result of a new field being added to the message definition.
15///
16/// The [`Message`](prost::Message) implementation of [`DynamicMessage`](crate::DynamicMessage) will preserve any unknown
17/// fields.
18#[derive(Debug, Clone, PartialEq)]
19pub struct UnknownField {
20    number: u32,
21    value: UnknownFieldValue,
22}
23
24/// The vaalue of an unknown field in a protobuf message.
25#[derive(Debug, Clone, PartialEq)]
26pub(crate) enum UnknownFieldValue {
27    /// An unknown field with the `Varint` wire type.
28    Varint(u64),
29    /// An unknown field with the `SixtyFourBit` wire type.
30    SixtyFourBit([u8; 8]),
31    /// An unknown field with the `LengthDelimited` wire type.
32    LengthDelimited(Bytes),
33    /// An unknown field with the group wire type.
34    Group(UnknownFieldSet),
35    /// An unknown field with the `ThirtyTwoBit` wire type.
36    ThirtyTwoBit([u8; 4]),
37}
38
39#[derive(Debug, Default, Clone, PartialEq)]
40pub(crate) struct UnknownFieldSet {
41    fields: Vec<UnknownField>,
42}
43
44impl UnknownField {
45    /// The number of this field as found during decoding.
46    pub fn number(&self) -> u32 {
47        self.number
48    }
49
50    /// The wire type of this field as found during decoding.
51    pub fn wire_type(&self) -> WireType {
52        match &self.value {
53            UnknownFieldValue::Varint(_) => WireType::Varint,
54            UnknownFieldValue::SixtyFourBit(_) => WireType::SixtyFourBit,
55            UnknownFieldValue::LengthDelimited(_) => WireType::LengthDelimited,
56            UnknownFieldValue::Group(_) => WireType::StartGroup,
57            UnknownFieldValue::ThirtyTwoBit(_) => WireType::ThirtyTwoBit,
58        }
59    }
60
61    pub(crate) fn value(&self) -> &UnknownFieldValue {
62        &self.value
63    }
64
65    /// Encodes this field into its byte representation.
66    pub fn encode<B>(&self, buf: &mut B)
67    where
68        B: BufMut,
69    {
70        match &self.value {
71            UnknownFieldValue::Varint(value) => {
72                encoding::encode_key(self.number, WireType::Varint, buf);
73                encoding::encode_varint(*value, buf);
74            }
75            UnknownFieldValue::SixtyFourBit(value) => {
76                encoding::encode_key(self.number, WireType::SixtyFourBit, buf);
77                buf.put_slice(value);
78            }
79            UnknownFieldValue::LengthDelimited(value) => {
80                encoding::bytes::encode(self.number, value, buf);
81            }
82            UnknownFieldValue::Group(value) => {
83                encoding::group::encode(self.number, value, buf);
84            }
85            UnknownFieldValue::ThirtyTwoBit(value) => {
86                encoding::encode_key(self.number, WireType::ThirtyTwoBit, buf);
87                buf.put_slice(value);
88            }
89        }
90    }
91
92    /// Decodes an unknown field from the given buffer.
93    ///
94    /// This method will read the field number and wire type from the buffer. Normally, it is useful to know
95    /// the field number before deciding whether to treat a field as unknown. See [`decode_value`](UnknownField::decode_value)
96    /// if you have already read the number.
97    ///
98    /// # Examples
99    ///
100    /// ```
101    /// # use prost_reflect::{DescriptorPool, UnknownField};
102    /// # use prost::encoding::{DecodeContext, WireType};
103    /// # let pool = DescriptorPool::decode(include_bytes!("../file_descriptor_set.bin").as_ref()).unwrap();
104    /// # let message_descriptor = pool.get_message_by_name("google.protobuf.Empty").unwrap();
105    /// let unknown_field = UnknownField::decode(&mut b"\x1a\x02\x10\x42".as_ref(), DecodeContext::default()).unwrap();
106    /// assert_eq!(unknown_field.number(), 3);
107    /// assert_eq!(unknown_field.wire_type(), WireType::LengthDelimited);
108    /// ```
109    pub fn decode<B>(buf: &mut B, ctx: DecodeContext) -> Result<Self, DecodeError>
110    where
111        B: Buf,
112    {
113        let (number, wire_type) = encoding::decode_key(buf)?;
114        Self::decode_value(number, wire_type, buf, ctx)
115    }
116
117    /// Given a field number and wire type, decodes the value of an unknown field.
118    ///
119    /// This method assumes the field number and wire type have already been read from the buffer.
120    /// See also [`decode`](UnknownField::decode).
121    ///
122    /// # Examples
123    ///
124    /// ```
125    /// # use prost_reflect::{DescriptorPool, UnknownField};
126    /// # use prost::encoding::{DecodeContext, WireType};
127    /// # let pool = DescriptorPool::decode(include_bytes!("../file_descriptor_set.bin").as_ref()).unwrap();
128    /// # let message_descriptor = pool.get_message_by_name("google.protobuf.Empty").unwrap();
129    /// let unknown_field = UnknownField::decode_value(3, WireType::LengthDelimited, &mut b"\x02\x10\x42".as_ref(), DecodeContext::default()).unwrap();
130    /// assert_eq!(unknown_field.number(), 3);
131    /// assert_eq!(unknown_field.wire_type(), WireType::LengthDelimited);
132    ///
133    /// let mut buf = Vec::new();
134    /// unknown_field.encode(&mut buf);
135    /// assert_eq!(buf, b"\x1a\x02\x10\x42");
136    /// ```
137    pub fn decode_value<B>(
138        number: u32,
139        wire_type: WireType,
140        buf: &mut B,
141        ctx: DecodeContext,
142    ) -> Result<Self, DecodeError>
143    where
144        B: Buf,
145    {
146        let value = match wire_type {
147            WireType::Varint => {
148                let value = encoding::decode_varint(buf)?;
149                UnknownFieldValue::Varint(value)
150            }
151            WireType::SixtyFourBit => {
152                let mut value = [0; 8];
153                if buf.remaining() < value.len() {
154                    return Err(DecodeError::new("buffer underflow"));
155                }
156                buf.copy_to_slice(&mut value);
157                UnknownFieldValue::SixtyFourBit(value)
158            }
159            WireType::LengthDelimited => {
160                let mut value = Bytes::default();
161                encoding::bytes::merge(wire_type, &mut value, buf, ctx)?;
162                UnknownFieldValue::LengthDelimited(value)
163            }
164            WireType::StartGroup => {
165                let mut value = UnknownFieldSet::default();
166                encoding::group::merge(number, wire_type, &mut value, buf, ctx)?;
167                UnknownFieldValue::Group(value)
168            }
169            WireType::EndGroup => return Err(DecodeError::new("unexpected end group tag")),
170            WireType::ThirtyTwoBit => {
171                let mut value = [0; 4];
172                if buf.remaining() < value.len() {
173                    return Err(DecodeError::new("buffer underflow"));
174                }
175                buf.copy_to_slice(&mut value);
176                UnknownFieldValue::ThirtyTwoBit(value)
177            }
178        };
179
180        Ok(UnknownField { number, value })
181    }
182
183    /// Gets the length of this field when encoded to its byte representation.
184    pub fn encoded_len(&self) -> usize {
185        match &self.value {
186            UnknownFieldValue::Varint(value) => {
187                encoding::key_len(self.number) + encoding::encoded_len_varint(*value)
188            }
189            UnknownFieldValue::SixtyFourBit(value) => encoding::key_len(self.number) + value.len(),
190            UnknownFieldValue::LengthDelimited(value) => {
191                encoding::bytes::encoded_len(self.number, value)
192            }
193            UnknownFieldValue::Group(value) => encoding::group::encoded_len(self.number, value),
194            UnknownFieldValue::ThirtyTwoBit(value) => encoding::key_len(self.number) + value.len(),
195        }
196    }
197}
198
199impl fmt::Display for UnknownField {
200    /// Formats this unknown field using the protobuf text format.
201    ///
202    /// The protobuf format does not include type information, so the formatter will attempt to infer types.
203    ///
204    /// # Examples
205    ///
206    /// ```
207    /// # use prost_reflect::{DescriptorPool, UnknownField};
208    /// # use prost::encoding::DecodeContext;
209    /// # let pool = DescriptorPool::decode(include_bytes!("../file_descriptor_set.bin").as_ref()).unwrap();
210    /// # let message_descriptor = pool.get_message_by_name("google.protobuf.Empty").unwrap();
211    /// let unknown_field = UnknownField::decode(&mut b"\x1a\x02\x10\x42".as_ref(), DecodeContext::default()).unwrap();
212    /// assert_eq!(format!("{}", unknown_field), "3{2:66}");
213    /// // The alternate format specifier may be used to indent the output
214    /// assert_eq!(format!("{:#}", unknown_field), "3 {\n  2: 66\n}");
215    /// ```
216    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217        text_format::Writer::new(text_format::FormatOptions::new().pretty(f.alternate()), f)
218            .fmt_unknown_field(self)
219    }
220}
221
222impl UnknownFieldSet {
223    pub(crate) fn is_empty(&self) -> bool {
224        self.fields.is_empty()
225    }
226
227    pub(crate) fn iter(&self) -> slice::Iter<'_, UnknownField> {
228        self.fields.iter()
229    }
230
231    pub(crate) fn into_iter(self) -> vec::IntoIter<UnknownField> {
232        self.fields.into_iter()
233    }
234
235    pub(crate) fn insert(&mut self, unknown: UnknownField) {
236        self.fields.push(unknown);
237    }
238}
239
240impl Message for UnknownFieldSet {
241    fn encode_raw(&self, buf: &mut impl BufMut)
242    where
243        Self: Sized,
244    {
245        for field in &self.fields {
246            field.encode(buf)
247        }
248    }
249
250    fn merge_field(
251        &mut self,
252        number: u32,
253        wire_type: WireType,
254        buf: &mut impl Buf,
255        ctx: DecodeContext,
256    ) -> Result<(), DecodeError>
257    where
258        Self: Sized,
259    {
260        let field = UnknownField::decode_value(number, wire_type, buf, ctx)?;
261        self.fields.push(field);
262        Ok(())
263    }
264
265    fn encoded_len(&self) -> usize {
266        let mut len = 0;
267        for field in &self.fields {
268            len += field.encoded_len();
269        }
270        len
271    }
272
273    fn clear(&mut self) {
274        self.fields.clear();
275    }
276}
277
278impl FromIterator<UnknownField> for UnknownFieldSet {
279    fn from_iter<T>(iter: T) -> Self
280    where
281        T: IntoIterator<Item = UnknownField>,
282    {
283        UnknownFieldSet {
284            fields: Vec::from_iter(iter),
285        }
286    }
287}
288
289#[cfg(test)]
290mod tests {
291    use prost::{
292        bytes::Bytes,
293        encoding::{DecodeContext, WireType},
294    };
295
296    use super::{UnknownField, UnknownFieldSet, UnknownFieldValue};
297
298    fn assert_roundtrip(expected: &[u8], value: &UnknownField) {
299        assert_eq!(expected.len(), value.encoded_len());
300
301        let mut actual = Vec::with_capacity(expected.len());
302        value.encode(&mut actual);
303        assert_eq!(expected, actual.as_slice());
304    }
305
306    #[test]
307    fn sixty_four_bit() {
308        let bytes = b"\x09\x9a\x99\x99\x99\x99\x99\xf1\x3ftail";
309        let mut buf = bytes.as_ref();
310
311        let value = UnknownField::decode(&mut buf, DecodeContext::default()).unwrap();
312
313        assert_eq!(value.number(), 1);
314        assert_eq!(value.wire_type(), WireType::SixtyFourBit);
315        assert_eq!(
316            value.value(),
317            &UnknownFieldValue::SixtyFourBit(*b"\x9a\x99\x99\x99\x99\x99\xf1\x3f")
318        );
319        assert_eq!(buf, b"tail");
320
321        assert_roundtrip(bytes.strip_suffix(buf).unwrap(), &value);
322    }
323
324    #[test]
325    fn thirty_two_bit() {
326        let bytes = b"\x15\xcd\xcc\x0c\x40tail";
327        let mut buf = bytes.as_ref();
328
329        let value = UnknownField::decode(&mut buf, DecodeContext::default()).unwrap();
330
331        assert_eq!(value.number(), 2);
332        assert_eq!(value.wire_type(), WireType::ThirtyTwoBit);
333        assert_eq!(
334            value.value(),
335            &UnknownFieldValue::ThirtyTwoBit(*b"\xcd\xcc\x0c\x40")
336        );
337        assert_eq!(buf, b"tail");
338
339        assert_roundtrip(bytes.strip_suffix(buf).unwrap(), &value);
340    }
341
342    #[test]
343    fn varint() {
344        let bytes = b"\x18\x03tail";
345        let mut buf = bytes.as_ref();
346
347        let value = UnknownField::decode(&mut buf, DecodeContext::default()).unwrap();
348
349        assert_eq!(value.number(), 3);
350        assert_eq!(value.wire_type(), WireType::Varint);
351        assert_eq!(value.value(), &UnknownFieldValue::Varint(3));
352        assert_eq!(buf, b"tail");
353
354        assert_roundtrip(bytes.strip_suffix(buf).unwrap(), &value);
355    }
356
357    #[test]
358    fn length_delimited() {
359        let bytes = b"\x7a\x07\x69\xa6\xbe\x6d\xb6\xff\x58tail";
360        let mut buf = bytes.as_ref();
361
362        let value = UnknownField::decode(&mut buf, DecodeContext::default()).unwrap();
363
364        assert_eq!(value.number(), 15);
365        assert_eq!(value.wire_type(), WireType::LengthDelimited);
366        assert_eq!(
367            value.value(),
368            &UnknownFieldValue::LengthDelimited(Bytes::from_static(
369                b"\x69\xa6\xbe\x6d\xb6\xff\x58"
370            ))
371        );
372        assert_eq!(buf, b"tail");
373
374        assert_roundtrip(bytes.strip_suffix(buf).unwrap(), &value);
375    }
376
377    #[test]
378    fn group() {
379        let bytes = b"\x1b\x0a\x05\x68\x65\x6c\x6c\x6f\x10\x0a\x10\x0b\x1ctail";
380        let mut buf = bytes.as_ref();
381
382        let value = UnknownField::decode(&mut buf, DecodeContext::default()).unwrap();
383
384        assert_eq!(value.number(), 3);
385        assert_eq!(value.wire_type(), WireType::StartGroup);
386        assert_eq!(
387            value.value(),
388            &UnknownFieldValue::Group(UnknownFieldSet::from_iter([
389                UnknownField {
390                    number: 1,
391                    value: UnknownFieldValue::LengthDelimited(Bytes::from_static(b"hello"))
392                },
393                UnknownField {
394                    number: 2,
395                    value: UnknownFieldValue::Varint(10)
396                },
397                UnknownField {
398                    number: 2,
399                    value: UnknownFieldValue::Varint(11)
400                },
401            ]))
402        );
403        assert_eq!(buf, b"tail");
404
405        assert_roundtrip(bytes.strip_suffix(buf).unwrap(), &value);
406    }
407}