1use crate::encoding::{decode_varint, skip_field_depth, varint_len, Tag, WireType};
20use crate::DecodeError;
21use alloc::vec::Vec;
22use bytes::Buf;
23
24pub const ITEM_START_TAG: u64 = (1 << 3) | 3;
26pub const ITEM_END_TAG: u64 = (1 << 3) | 4;
28pub const TYPE_ID_TAG: u64 = 2 << 3;
30pub const MESSAGE_TAG: u64 = (3 << 3) | 2;
32
33pub(crate) const MAX_TYPE_ID: u32 = i32::MAX as u32;
39
40pub fn merge_item(
65 buf: &mut impl Buf,
66 ctx: crate::DecodeContext<'_>,
67) -> Result<(u32, Vec<u8>), DecodeError> {
68 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 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 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 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 return Err(DecodeError::InvalidEndGroup(tag.field_number()));
108 }
109 _ => {
110 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#[inline]
128pub const fn item_encoded_len(number: u32, payload_len: usize) -> usize {
129 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 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 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 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)] fn merge_item_skips_unknown_fields() {
205 let mut junk = Vec::new();
207 encode_varint((99 << 3) | 0, &mut junk); 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)] fn merge_item_skips_nested_group_respecting_depth() {
223 let mut junk = Vec::new();
225 encode_varint((50 << 3) | 3, &mut junk); encode_varint((8 << 3) | 0, &mut junk); encode_varint(1, &mut junk);
228 encode_varint((50 << 3) | 4, &mut junk); let body = item_body(&[&type_id_field(5), &junk, &message_field(b"x"), &end_group()]);
231
232 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 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 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 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), (1, true), (i32::MAX as u64, true), (i32::MAX as u64 + 1, false), ];
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 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 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)] fn item_encoded_len_matches_manual_count() {
327 #[rustfmt::skip]
328 let cases: &[(u32, usize, usize)] = &[
329 (1, 0, 4 + 1 + 1 + 0), (127, 5, 4 + 1 + 1 + 5), (128, 5, 4 + 2 + 1 + 5), (1000, 10, 4 + 2 + 1 + 10), (1000, 200, 4 + 2 + 2 + 200),];
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 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}