Skip to main content

turnloop_mongodb/
wire.rs

1//! MongoDB specifications: message/OP_MSG.md ยงยง OP_MSG, flagBits, sections;
2//! compression/OP_COMPRESSED.md. Integer arithmetic is checked before slicing.
3use crate::{Error, Result};
4use bson::raw::RawDocument;
5use std::ops::Range;
6pub const OP_MSG: i32 = 2013;
7pub const OP_COMPRESSED: i32 = 2012;
8pub const CHECKSUM: u32 = 1;
9pub const MORE_TO_COME: u32 = 2;
10pub const EXHAUST_ALLOWED: u32 = 1 << 16;
11pub const DEFAULT_MAX_MESSAGE: usize = 48_000_000;
12pub fn i32_at(bytes: &[u8], at: usize) -> Result<i32> {
13    Ok(i32::from_le_bytes(
14        bytes
15            .get(at..at + 4)
16            .ok_or_else(|| Error::protocol("Truncated int32"))?
17            .try_into()
18            .unwrap(),
19    ))
20}
21fn length(bytes: &[u8], at: usize, min: usize) -> Result<usize> {
22    let n = i32_at(bytes, at)?;
23    if n < min as i32 {
24        return Err(Error::protocol("Invalid BSON or message length"));
25    }
26    Ok(n as usize)
27}
28pub fn crc32c(bytes: &[u8]) -> u32 {
29    let mut c = !0u32;
30    for &v in bytes {
31        c ^= v as u32;
32        for _ in 0..8 {
33            c = (c >> 1) ^ (0x82f63b78u32.wrapping_mul(c & 1));
34        }
35    }
36    !c
37}
38#[derive(Debug)]
39pub struct Message<'a> {
40    pub request_id: i32,
41    pub response_to: i32,
42    pub flags: u32,
43    pub body: &'a RawDocument,
44    sections: &'a [u8],
45}
46impl<'a> Message<'a> {
47    pub fn parse(bytes: &'a [u8], max: usize) -> Result<Self> {
48        let n = length(bytes, 0, 26)?;
49        if n != bytes.len() || n > max {
50            return Err(Error::protocol("Invalid OP_MSG length"));
51        }
52        if i32_at(bytes, 12)? != OP_MSG {
53            return Err(Error::protocol("Expected OP_MSG"));
54        }
55        let flags = i32_at(bytes, 16)? as u32;
56        if flags & 0xffff & !3 != 0 {
57            return Err(Error::protocol("Unknown required OP_MSG flag"));
58        }
59        let end = if flags & CHECKSUM != 0 {
60            if n < 30 {
61                return Err(Error::protocol("Missing checksum"));
62            }
63            let end = n - 4;
64            if crc32c(&bytes[..end]) != i32_at(bytes, end)? as u32 {
65                return Err(Error::protocol("OP_MSG checksum mismatch"));
66            }
67            end
68        } else {
69            n
70        };
71        let sections = &bytes[20..end];
72        let mut body = None;
73        let mut at = 0;
74        while at < sections.len() {
75            let start = at;
76            let kind = sections[at];
77            at += 1;
78            let size = length(sections, at, 5)?;
79            let stop = at
80                .checked_add(size)
81                .filter(|&s| s <= sections.len())
82                .ok_or_else(|| Error::protocol("Truncated section"))?;
83            match kind {
84                0 => {
85                    if body.is_some() {
86                        return Err(Error::protocol("Duplicate OP_MSG body"));
87                    }
88                    let d = RawDocument::from_bytes(&sections[at..stop])
89                        .map_err(|_| Error::protocol("Invalid BSON body"))?;
90                    validate(d, 0)?;
91                    body = Some(d);
92                }
93                1 => {
94                    let name_end = sections[at + 4..stop]
95                        .iter()
96                        .position(|&v| v == 0)
97                        .map(|p| p + at + 4)
98                        .ok_or_else(|| Error::protocol("Unterminated sequence identifier"))?;
99                    let name = std::str::from_utf8(&sections[at + 4..name_end])
100                        .map_err(|_| Error::protocol("Invalid sequence identifier"))?;
101                    // No per-section identifier allocation: bounded re-scan of previous sections.
102                    let mut prev = 0;
103                    while prev < start {
104                        let k = sections[prev];
105                        prev += 1;
106                        let sz = length(sections, prev, 5)?;
107                        if k == 1 {
108                            let e = sections[prev + 4..prev + sz]
109                                .iter()
110                                .position(|&b| b == 0)
111                                .unwrap()
112                                + prev
113                                + 4;
114                            if &sections[prev + 4..e] == name.as_bytes() {
115                                return Err(Error::protocol("Duplicate sequence identifier"));
116                            }
117                        }
118                        prev += sz;
119                    }
120                    let mut p = name_end + 1;
121                    while p < stop {
122                        let len = length(sections, p, 5)?;
123                        let e = p
124                            .checked_add(len)
125                            .filter(|&e| e <= stop)
126                            .ok_or_else(|| Error::protocol("Truncated sequence BSON"))?;
127                        let d = RawDocument::from_bytes(&sections[p..e])
128                            .map_err(|_| Error::protocol("Invalid sequence BSON"))?;
129                        validate(d, 0)?;
130                        p = e;
131                    }
132                }
133                _ => return Err(Error::protocol("Unknown OP_MSG section type")),
134            }
135            at = stop;
136        }
137        let body = body.ok_or_else(|| Error::protocol("Missing OP_MSG body"))?;
138        Ok(Self {
139            request_id: i32_at(bytes, 4)?,
140            response_to: i32_at(bytes, 8)?,
141            flags,
142            body,
143            sections,
144        })
145    }
146    /// Replaces only the body; document sequences retain their original bytes/order.
147    pub fn encode_with_body(
148        &self,
149        out: &mut Vec<u8>,
150        body: &RawDocument,
151        max: usize,
152    ) -> Result<()> {
153        encode(out, self.request_id, 0, 0, body, &[], max)?;
154        let mut at = 0;
155        while at < self.sections.len() {
156            let start = at;
157            let kind = self.sections[at];
158            at += 1;
159            let n = i32_at(self.sections, at)? as usize;
160            at += n;
161            if kind == 1 {
162                out.extend_from_slice(&self.sections[start..at]);
163            }
164        }
165        if out.len() > max || out.len() > i32::MAX as usize {
166            out.clear();
167            return Err(Error::protocol("Message exceeds maxMessageSizeBytes"));
168        }
169        let n = out.len() as i32;
170        out[..4].copy_from_slice(&n.to_le_bytes());
171        Ok(())
172    }
173    pub fn sequences(&self) -> Sequences<'a> {
174        Sequences {
175            bytes: self.sections,
176            at: 0,
177        }
178    }
179}
180fn validate(doc: &RawDocument, depth: usize) -> Result<()> {
181    if depth > 100 {
182        return Err(Error::protocol("BSON nesting exceeds 100"));
183    }
184    for el in doc {
185        let (_, v) = el.map_err(|_| Error::protocol("Malformed BSON element"))?;
186        match v {
187            bson::raw::RawBsonRef::Document(d) => validate(d, depth + 1)?,
188            bson::raw::RawBsonRef::Array(a) => {
189                let d = RawDocument::from_bytes(a.as_bytes())
190                    .map_err(|_| Error::protocol("Malformed BSON array"))?;
191                validate(d, depth + 1)?;
192            }
193            _ => {}
194        }
195    }
196    Ok(())
197}
198pub struct Sequence<'a> {
199    pub identifier: &'a str,
200    bytes: &'a [u8],
201}
202impl<'a> Sequence<'a> {
203    pub fn documents(&self) -> impl Iterator<Item = &'a RawDocument> {
204        let mut b = self.bytes;
205        std::iter::from_fn(move || {
206            if b.is_empty() {
207                return None;
208            }
209            let n = i32_at(b, 0).unwrap() as usize;
210            let d = RawDocument::from_bytes(&b[..n]).unwrap();
211            b = &b[n..];
212            Some(d)
213        })
214    }
215}
216pub struct Sequences<'a> {
217    bytes: &'a [u8],
218    at: usize,
219}
220impl<'a> Iterator for Sequences<'a> {
221    type Item = Sequence<'a>;
222    fn next(&mut self) -> Option<Self::Item> {
223        while self.at < self.bytes.len() {
224            let k = self.bytes[self.at];
225            self.at += 1;
226            let n = i32_at(self.bytes, self.at).unwrap() as usize;
227            let b = &self.bytes[self.at..self.at + n];
228            self.at += n;
229            if k == 1 {
230                let e = b[4..].iter().position(|&v| v == 0).unwrap() + 4;
231                return Some(Sequence {
232                    identifier: std::str::from_utf8(&b[4..e]).unwrap(),
233                    bytes: &b[e + 1..],
234                });
235            }
236        }
237        None
238    }
239}
240/// Encodes raw documents into a reusable output buffer. No allocations after capacity warms.
241pub fn encode(
242    out: &mut Vec<u8>,
243    request: i32,
244    response: i32,
245    flags: u32,
246    body: &RawDocument,
247    sequences: &[(&str, &[&RawDocument])],
248    max: usize,
249) -> Result<()> {
250    out.clear();
251    out.extend_from_slice(&[0; 16]);
252    out[4..8].copy_from_slice(&request.to_le_bytes());
253    out[8..12].copy_from_slice(&response.to_le_bytes());
254    out[12..16].copy_from_slice(&OP_MSG.to_le_bytes());
255    out.extend_from_slice(&flags.to_le_bytes());
256    out.push(0);
257    out.extend_from_slice(body.as_bytes());
258    for (name, docs) in sequences {
259        if name.as_bytes().contains(&0) {
260            out.clear();
261            return Err(Error::protocol("NUL in sequence identifier"));
262        }
263        out.push(1);
264        let at = out.len();
265        out.extend_from_slice(&[0; 4]);
266        out.extend_from_slice(name.as_bytes());
267        out.push(0);
268        for doc in *docs {
269            out.extend_from_slice(doc.as_bytes());
270        }
271        let n = out.len() - at;
272        if n > i32::MAX as usize {
273            out.clear();
274            return Err(Error::protocol("Sequence too large"));
275        }
276        out[at..at + 4].copy_from_slice(&(n as i32).to_le_bytes());
277    }
278    let n = out.len() + if flags & CHECKSUM != 0 { 4 } else { 0 };
279    if n > max || n > i32::MAX as usize {
280        out.clear();
281        return Err(Error::protocol("Message exceeds maxMessageSizeBytes"));
282    }
283    out[..4].copy_from_slice(&(n as i32).to_le_bytes());
284    if flags & CHECKSUM != 0 {
285        let c = crc32c(out);
286        out.extend_from_slice(&c.to_le_bytes());
287    }
288    Ok(())
289}
290/// Bounded incremental frame buffer; the host drains a reply before feeding another.
291#[derive(Debug)]
292pub struct Decoder {
293    bytes: Vec<u8>,
294    max: usize,
295}
296impl Decoder {
297    pub fn new(max: usize) -> Self {
298        Self {
299            bytes: Vec::with_capacity(8192),
300            max,
301        }
302    }
303    pub fn feed(&mut self, input: &[u8]) -> Result<usize> {
304        let target = if self.bytes.len() < 4 {
305            4
306        } else {
307            length(&self.bytes, 0, 16)?
308        };
309        if target > self.max {
310            return Err(Error::protocol("Message exceeds maxMessageSizeBytes"));
311        }
312        let n = input.len().min(target - self.bytes.len());
313        self.bytes.extend_from_slice(&input[..n]);
314        if self.bytes.len() >= 4 && length(&self.bytes, 0, 16)? > self.max {
315            return Err(Error::protocol("Message exceeds maxMessageSizeBytes"));
316        }
317        Ok(n)
318    }
319    pub fn complete(&self) -> bool {
320        self.bytes.len() >= 4
321            && i32_at(&self.bytes, 0).is_ok_and(|n| n as usize == self.bytes.len())
322    }
323    pub fn bytes(&self) -> &[u8] {
324        &self.bytes
325    }
326    pub fn clear(&mut self) {
327        self.bytes.clear();
328    }
329    pub fn set_max(&mut self, max: usize) {
330        self.max = max;
331    }
332}
333/// Raw BSON command builder with a retained backing buffer. Nesting uses offsets,
334/// so arrays/documents can be built without temporary Documents or key Strings.
335#[derive(Debug, Default)]
336pub struct BsonWriter {
337    bytes: Vec<u8>,
338}
339impl BsonWriter {
340    pub fn new() -> Self {
341        Self {
342            bytes: Vec::with_capacity(1024),
343        }
344    }
345    pub fn clear(&mut self) {
346        self.bytes.clear();
347        self.bytes.extend_from_slice(&[0; 4]);
348    }
349    fn key(&mut self, kind: u8, key: &str) -> Result<()> {
350        if key.as_bytes().contains(&0) {
351            return Err(Error::protocol("NUL in BSON key"));
352        }
353        self.bytes.push(kind);
354        self.bytes.extend_from_slice(key.as_bytes());
355        self.bytes.push(0);
356        Ok(())
357    }
358    pub fn string(&mut self, k: &str, v: &str) -> Result<()> {
359        self.key(2, k)?;
360        let n = i32::try_from(v.len() + 1).map_err(|_| Error::protocol("BSON string too large"))?;
361        self.bytes.extend_from_slice(&n.to_le_bytes());
362        self.bytes.extend_from_slice(v.as_bytes());
363        self.bytes.push(0);
364        Ok(())
365    }
366    pub fn int32(&mut self, k: &str, v: i32) -> Result<()> {
367        self.key(16, k)?;
368        self.bytes.extend_from_slice(&v.to_le_bytes());
369        Ok(())
370    }
371    pub fn int64(&mut self, k: &str, v: i64) -> Result<()> {
372        self.key(18, k)?;
373        self.bytes.extend_from_slice(&v.to_le_bytes());
374        Ok(())
375    }
376    pub fn boolean(&mut self, k: &str, v: bool) -> Result<()> {
377        self.key(8, k)?;
378        self.bytes.push(u8::from(v));
379        Ok(())
380    }
381    pub fn document(&mut self, k: &str, d: &RawDocument) -> Result<()> {
382        self.key(3, k)?;
383        self.bytes.extend_from_slice(d.as_bytes());
384        Ok(())
385    }
386    pub fn object_id(&mut self, key: &str, id: bson::oid::ObjectId) -> Result<()> {
387        self.key(7, key)?;
388        self.bytes.extend_from_slice(&id.bytes());
389        Ok(())
390    }
391    pub fn timestamp(&mut self, key: &str, t: bson::Timestamp) -> Result<()> {
392        self.key(17, key)?;
393        self.bytes.extend_from_slice(&t.increment.to_le_bytes());
394        self.bytes.extend_from_slice(&t.time.to_le_bytes());
395        Ok(())
396    }
397    pub fn binary(&mut self, k: &str, subtype: u8, b: &[u8]) -> Result<()> {
398        self.key(5, k)?;
399        let n = i32::try_from(b.len()).map_err(|_| Error::protocol("Binary too large"))?;
400        self.bytes.extend_from_slice(&n.to_le_bytes());
401        self.bytes.push(subtype);
402        self.bytes.extend_from_slice(b);
403        Ok(())
404    }
405    pub fn start_document(&mut self, k: &str, array: bool) -> Result<usize> {
406        self.key(if array { 4 } else { 3 }, k)?;
407        let p = self.bytes.len();
408        self.bytes.extend_from_slice(&[0; 4]);
409        Ok(p)
410    }
411    pub fn end_document(&mut self, at: usize) -> Result<()> {
412        if at.checked_add(4).is_none_or(|end| end > self.bytes.len()) {
413            return Err(Error::protocol("Invalid BSON builder offset"));
414        }
415        self.bytes.push(0);
416        let n =
417            i32::try_from(self.bytes.len() - at).map_err(|_| Error::protocol("BSON too large"))?;
418        self.bytes[at..at + 4].copy_from_slice(&n.to_le_bytes());
419        Ok(())
420    }
421    pub fn finish(&mut self) -> Result<&RawDocument> {
422        self.end_document(0)?;
423        self.as_raw()
424    }
425    pub fn as_raw(&self) -> Result<&RawDocument> {
426        RawDocument::from_bytes(&self.bytes).map_err(|_| Error::protocol("Unfinished BSON builder"))
427    }
428    pub fn append_fields(&mut self, doc: &RawDocument, omit: &[&str]) -> Result<()> {
429        let b = doc.as_bytes();
430        let mut start = 4;
431        for el in doc.iter_elements() {
432            let el = el.map_err(|_| Error::protocol("Malformed BSON options"))?;
433            let range: Range<usize> = start..start + 1 + el.key().as_str().len() + 1 + el.size();
434            if !omit.contains(&el.key().as_str()) {
435                self.bytes.extend_from_slice(&b[range.clone()]);
436            }
437            start = range.end;
438        }
439        Ok(())
440    }
441}