Skip to main content

buffa/
message_set.rs

1//! MessageSet wire format helpers (legacy Google-internal encoding).
2//!
3//! Enabled per-message via `option message_set_wire_format = true` on a
4//! proto2 message with no fields, only `extensions N to M;`. Each extension
5//! is encoded as a group at field 1 containing `type_id = 2` (the extension's
6//! field number) and `message = 3` (the extension's payload bytes):
7//!
8//! ```text
9//! tag(1, SGROUP) | tag(2, Varint) | <type_id> | tag(3, LD) | len | <payload> | tag(1, EGROUP)
10//! ```
11//!
12//! instead of the regular `tag(type_id, LD) | len | <payload>`.
13//!
14//! Storage is **non-MessageSet**: an unknown item with `type_id = T` and
15//! payload `V` is stored in [`UnknownFields`](crate::UnknownFields) as
16//! `{ number: T, data: LengthDelimited(V) }`, not as a group. Decode unwraps
17//! the group into that form; encode rewraps it. This mirrors protobuf-go.
18
19use crate::encoding::{decode_varint, skip_field_depth, varint_len, Tag, WireType};
20use crate::DecodeError;
21use alloc::vec::Vec;
22use bytes::Buf;
23
24/// Field 1, wire type `StartGroup` (3): `(1 << 3) | 3`.
25pub const ITEM_START_TAG: u64 = (1 << 3) | 3;
26/// Field 1, wire type `EndGroup` (4): `(1 << 3) | 4`.
27pub const ITEM_END_TAG: u64 = (1 << 3) | 4;
28/// Field 2, wire type `Varint` (0): `(2 << 3) | 0`.
29pub const TYPE_ID_TAG: u64 = 2 << 3;
30/// Field 3, wire type `LengthDelimited` (2): `(3 << 3) | 2`.
31pub const MESSAGE_TAG: u64 = (3 << 3) | 2;
32
33/// The largest MessageSet type ID, `i32::MAX`.
34///
35/// `type_id` is a `required int32`, so an item's type ID, and the number of
36/// an extension of a MessageSet, can pass
37/// [`MAX_FIELD_NUMBER`](crate::encoding::MAX_FIELD_NUMBER).
38pub(crate) const MAX_TYPE_ID: u32 = i32::MAX as u32;
39
40/// Parse one MessageSet `Item` group body (between `SGROUP` and `EGROUP`).
41///
42/// The caller must have already consumed the `ITEM_START_TAG`; this function
43/// reads until the matching `EGROUP` (field 1). The `type_id` and `message`
44/// fields may arrive in either order. Unknown fields inside the group are
45/// skipped. Repeated `message` fields are concatenated (proto merge
46/// semantics); repeated `type_id` fields take the last value.
47///
48/// Returns `(type_id, message_bytes)`. `message_bytes` is empty if no
49/// `message` field was present (valid: an empty sub-message).
50///
51/// `ctx` carries the remaining recursion depth for skipping unknown group
52/// fields **inside** the Item group, plus the shared unknown-field
53/// allowance. The caller should pass `caller_ctx.descend()?` (the Item
54/// group itself consumes one level). The item consumes one slot of the
55/// unknown-field allowance — the caller stores it as one
56/// [`UnknownField`](crate::UnknownField).
57///
58/// # Errors
59///
60/// Returns [`DecodeError::InvalidMessageSet`] if `type_id` is missing or out
61/// of the valid range `[1, i32::MAX]`. Returns other decode errors on
62/// malformed input (truncated varint, buffer underrun, mismatched end-group)
63/// or when the unknown-field limit is exceeded.
64pub fn merge_item(
65    buf: &mut impl Buf,
66    ctx: crate::DecodeContext<'_>,
67) -> Result<(u32, Vec<u8>), DecodeError> {
68    // The caller stores the result as one UnknownField slot — consume an
69    // allowance slot here so MessageSet items draw from the same limit as
70    // regular unknowns.
71    ctx.register_unknown_field()?;
72    let mut type_id: Option<u32> = None;
73    let mut message: Vec<u8> = Vec::new();
74
75    loop {
76        let tag = Tag::decode(buf)?;
77        // Item group terminator: field 1, EndGroup.
78        if tag.field_number() == 1 && tag.wire_type() == WireType::EndGroup {
79            break;
80        }
81        match (tag.field_number(), tag.wire_type()) {
82            (2, WireType::Varint) => {
83                let v = decode_varint(buf)?;
84                // type_id is `required int32`; valid protobuf field numbers
85                // are [1, 2^29-1], but MessageSet historically allows the
86                // full positive int32 range.
87                if v < 1 || v > u64::from(MAX_TYPE_ID) {
88                    return Err(DecodeError::InvalidMessageSet("type_id out of range"));
89                }
90                type_id = Some(v as u32);
91            }
92            (3, WireType::LengthDelimited) => {
93                let len = decode_varint(buf)?;
94                // Compare as u64 so a 32-bit build rejects a length that
95                // overflows usize before the cast truncates it.
96                if len > buf.remaining() as u64 {
97                    return Err(DecodeError::UnexpectedEof);
98                }
99                let len = len as usize;
100                let start = message.len();
101                message.resize(start + len, 0);
102                buf.copy_to_slice(&mut message[start..]);
103            }
104            (_, WireType::EndGroup) => {
105                // EndGroup for a different field number — the stream is
106                // malformed (we never opened that group).
107                return Err(DecodeError::InvalidEndGroup(tag.field_number()));
108            }
109            _ => {
110                // Unknown field inside the Item group: skip. Generated code
111                // passes `ctx.descend()?` into `merge_item`, so nested groups
112                // here share the caller's recursion budget.
113                skip_field_depth(tag, buf, ctx.depth())?;
114            }
115        }
116    }
117
118    let type_id = type_id.ok_or(DecodeError::InvalidMessageSet("missing type_id"))?;
119    Ok((type_id, message))
120}
121
122/// Encoded length of one MessageSet `Item` wrapping `payload_len` bytes at
123/// extension field `number`.
124///
125/// Four single-byte tags (fields 1, 2, 3 all fit in one byte) + the `type_id`
126/// varint + the length-prefix varint + the payload.
127#[inline]
128pub const fn item_encoded_len(number: u32, payload_len: usize) -> usize {
129    // ITEM_START_TAG (1) + TYPE_ID_TAG (1) + varint(number)
130    //   + MESSAGE_TAG (1) + varint(payload_len) + payload_len
131    //   + ITEM_END_TAG (1)
132    4 + varint_len(number as u64) + varint_len(payload_len as u64) + payload_len
133}
134
135#[cfg(test)]
136mod tests {
137    use super::*;
138
139    /// Call `merge_item` with a fresh default-limit context at `depth`.
140    fn merge_item_d(buf: &mut impl Buf, depth: u32) -> Result<(u32, Vec<u8>), DecodeError> {
141        let limit = core::cell::Cell::new(crate::DEFAULT_UNKNOWN_FIELD_LIMIT);
142        merge_item(buf, crate::DecodeContext::new(depth, &limit))
143    }
144    use crate::encoding::encode_varint;
145
146    /// Build a MessageSet Item group body (no SGROUP tag — `merge_item`
147    /// assumes the caller consumed it).
148    fn item_body(parts: &[&[u8]]) -> Vec<u8> {
149        let mut buf = Vec::new();
150        for p in parts {
151            buf.extend_from_slice(p);
152        }
153        buf
154    }
155
156    fn type_id_field(id: u64) -> Vec<u8> {
157        let mut buf = Vec::new();
158        encode_varint(TYPE_ID_TAG, &mut buf);
159        encode_varint(id, &mut buf);
160        buf
161    }
162
163    fn message_field(payload: &[u8]) -> Vec<u8> {
164        let mut buf = Vec::new();
165        encode_varint(MESSAGE_TAG, &mut buf);
166        encode_varint(payload.len() as u64, &mut buf);
167        buf.extend_from_slice(payload);
168        buf
169    }
170
171    fn end_group() -> Vec<u8> {
172        let mut buf = Vec::new();
173        encode_varint(ITEM_END_TAG, &mut buf);
174        buf
175    }
176
177    #[test]
178    fn tag_constants_match_wire_bytes() {
179        // Field 1-3 all produce single-byte tags.
180        assert_eq!(ITEM_START_TAG, 0x0B);
181        assert_eq!(ITEM_END_TAG, 0x0C);
182        assert_eq!(TYPE_ID_TAG, 0x10);
183        assert_eq!(MESSAGE_TAG, 0x1A);
184    }
185
186    #[test]
187    fn merge_item_type_id_then_message() {
188        let body = item_body(&[&type_id_field(1000), &message_field(b"hello"), &end_group()]);
189        let (tid, msg) = merge_item_d(&mut body.as_slice(), 50).expect("merge");
190        assert_eq!(tid, 1000);
191        assert_eq!(msg, b"hello");
192    }
193
194    #[test]
195    fn merge_item_message_then_type_id() {
196        let body = item_body(&[&message_field(b"world"), &type_id_field(42), &end_group()]);
197        let (tid, msg) = merge_item_d(&mut body.as_slice(), 50).expect("merge");
198        assert_eq!(tid, 42);
199        assert_eq!(msg, b"world");
200    }
201
202    #[test]
203    #[allow(clippy::identity_op)] // `| 0` makes the Varint wire type explicit
204    fn merge_item_skips_unknown_fields() {
205        // Junk field 99 (varint) between type_id and message.
206        let mut junk = Vec::new();
207        encode_varint((99 << 3) | 0, &mut junk); // tag(99, Varint)
208        encode_varint(12345, &mut junk);
209        let body = item_body(&[
210            &type_id_field(7),
211            &junk,
212            &message_field(b"ok"),
213            &end_group(),
214        ]);
215        let (tid, msg) = merge_item_d(&mut body.as_slice(), 50).expect("merge");
216        assert_eq!(tid, 7);
217        assert_eq!(msg, b"ok");
218    }
219
220    #[test]
221    #[allow(clippy::identity_op)] // `| 0` makes the Varint wire type explicit
222    fn merge_item_skips_nested_group_respecting_depth() {
223        // Junk field 50, wire type StartGroup, containing a varint then EndGroup.
224        let mut junk = Vec::new();
225        encode_varint((50 << 3) | 3, &mut junk); // tag(50, SGROUP)
226        encode_varint((8 << 3) | 0, &mut junk); // tag(8, Varint)
227        encode_varint(1, &mut junk);
228        encode_varint((50 << 3) | 4, &mut junk); // tag(50, EGROUP)
229
230        let body = item_body(&[&type_id_field(5), &junk, &message_field(b"x"), &end_group()]);
231
232        // With depth budget: succeeds.
233        let (tid, msg) = merge_item_d(&mut body.as_slice(), 10).expect("merge");
234        assert_eq!(tid, 5);
235        assert_eq!(msg, b"x");
236
237        // With depth exhausted: fails.
238        let err = merge_item_d(&mut body.as_slice(), 0).unwrap_err();
239        assert_eq!(err, DecodeError::RecursionLimitExceeded);
240    }
241
242    #[test]
243    fn merge_item_missing_type_id_errors() {
244        let body = item_body(&[&message_field(b"orphan"), &end_group()]);
245        let err = merge_item_d(&mut body.as_slice(), 50).unwrap_err();
246        assert_eq!(err, DecodeError::InvalidMessageSet("missing type_id"));
247    }
248
249    #[test]
250    fn merge_item_missing_message_yields_empty() {
251        // Missing `message` is valid — it's an empty sub-message.
252        let body = item_body(&[&type_id_field(3), &end_group()]);
253        let (tid, msg) = merge_item_d(&mut body.as_slice(), 50).expect("merge");
254        assert_eq!(tid, 3);
255        assert_eq!(msg, b"");
256    }
257
258    #[test]
259    fn merge_item_multiple_messages_concatenate() {
260        let body = item_body(&[
261            &type_id_field(9),
262            &message_field(b"ab"),
263            &message_field(b"cd"),
264            &end_group(),
265        ]);
266        let (tid, msg) = merge_item_d(&mut body.as_slice(), 50).expect("merge");
267        assert_eq!(tid, 9);
268        assert_eq!(msg, b"abcd");
269    }
270
271    #[test]
272    fn merge_item_repeated_type_id_last_wins() {
273        // Two type_id fields in one Item: the second overwrites the first.
274        let body = item_body(&[
275            &type_id_field(5),
276            &type_id_field(99),
277            &message_field(b"x"),
278            &end_group(),
279        ]);
280        let (tid, msg) = merge_item_d(&mut body.as_slice(), 50).expect("merge");
281        assert_eq!(tid, 99);
282        assert_eq!(msg, b"x");
283    }
284
285    #[test]
286    fn merge_item_type_id_out_of_range() {
287        #[rustfmt::skip]
288        let cases: &[(u64, bool)] = &[
289            (0,                     false), // zero → error
290            (1,                     true),  // minimum valid
291            (i32::MAX as u64,       true),  // maximum valid
292            (i32::MAX as u64 + 1,   false), // overflows int32
293        ];
294        for &(id, ok) in cases {
295            let body = item_body(&[&type_id_field(id), &message_field(b""), &end_group()]);
296            let result = merge_item_d(&mut body.as_slice(), 50);
297            assert_eq!(result.is_ok(), ok, "type_id = {id}");
298        }
299    }
300
301    #[test]
302    fn merge_item_mismatched_end_group_errors() {
303        // EndGroup for field 7 instead of field 1.
304        let mut bad_end = Vec::new();
305        encode_varint((7 << 3) | 4, &mut bad_end);
306        let body = item_body(&[&type_id_field(1), &bad_end]);
307        let err = merge_item_d(&mut body.as_slice(), 50).unwrap_err();
308        assert_eq!(err, DecodeError::InvalidEndGroup(7));
309    }
310
311    #[test]
312    fn merge_item_truncated_message_errors() {
313        // Length prefix claims 100 bytes, only 2 available.
314        let mut body = Vec::new();
315        encode_varint(TYPE_ID_TAG, &mut body);
316        encode_varint(5, &mut body);
317        encode_varint(MESSAGE_TAG, &mut body);
318        encode_varint(100, &mut body);
319        body.extend_from_slice(b"xy");
320        let err = merge_item_d(&mut body.as_slice(), 50).unwrap_err();
321        assert_eq!(err, DecodeError::UnexpectedEof);
322    }
323
324    #[test]
325    #[allow(clippy::identity_op)] // `+ 0` keeps the byte-count breakdown columns aligned
326    fn item_encoded_len_matches_manual_count() {
327        #[rustfmt::skip]
328        let cases: &[(u32, usize, usize)] = &[
329            // (number, payload_len, expected)
330            (1,       0,   4 + 1 + 1 + 0),  // 1-byte number, 1-byte len
331            (127,     5,   4 + 1 + 1 + 5),  // 127 = 1-byte varint
332            (128,     5,   4 + 2 + 1 + 5),  // 128 = 2-byte varint
333            (1000,    10,  4 + 2 + 1 + 10), // 1000 = 2-byte varint
334            (1000,    200, 4 + 2 + 2 + 200),// 200 = 2-byte varint
335        ];
336        for &(number, payload_len, expected) in cases {
337            assert_eq!(
338                item_encoded_len(number, payload_len),
339                expected,
340                "number={number} payload_len={payload_len}"
341            );
342        }
343    }
344
345    #[test]
346    fn item_encoded_len_matches_actual_encoding() {
347        // Cross-check: hand-build an Item and compare its length.
348        let number = 1000u32;
349        let payload = b"hello world";
350        let mut buf = Vec::new();
351        encode_varint(ITEM_START_TAG, &mut buf);
352        encode_varint(TYPE_ID_TAG, &mut buf);
353        encode_varint(number as u64, &mut buf);
354        encode_varint(MESSAGE_TAG, &mut buf);
355        encode_varint(payload.len() as u64, &mut buf);
356        buf.extend_from_slice(payload);
357        encode_varint(ITEM_END_TAG, &mut buf);
358
359        assert_eq!(item_encoded_len(number, payload.len()), buf.len());
360    }
361}