Skip to main content

mysql_replication/
frame.rs

1use crate::{Error, Result, error::invalid, event_type as event, events::EventHeader};
2pub const MAX_EVENT_BYTES: usize = 64 * 1024 * 1024;
3
4/// A validated frame. Body excludes the original checksum trailer; raw_data does not.
5#[derive(Debug, Clone, Copy)]
6pub struct BinlogFrame<'a> {
7    pub raw_data: &'a [u8],
8    pub header: EventHeader,
9    pub body: &'a [u8],
10}
11#[derive(Debug, Default)]
12pub struct FrameDecoder {
13    crc32: Option<bool>,
14}
15impl FrameDecoder {
16    pub fn new() -> Self {
17        Self::default()
18    }
19    pub(crate) fn set_checksum(&mut self, crc32: bool) {
20        self.crc32 = Some(crc32);
21    }
22    pub fn has_format(&self) -> bool {
23        self.crc32.is_some()
24    }
25    pub fn decode<'a>(&mut self, raw: &'a [u8]) -> Result<BinlogFrame<'a>> {
26        let header_bytes = raw.get(..EventHeader::LEN).ok_or(Error::Truncated {
27            need: EventHeader::LEN,
28            have: raw.len(),
29        })?;
30        let header = EventHeader::parse(header_bytes)?;
31        let len = header.event_size as usize;
32        if !(EventHeader::LEN..=MAX_EVENT_BYTES).contains(&len) || raw.len() != len {
33            return Err(invalid("event size does not match bounded raw frame"));
34        }
35        let is_fde = header.event_type == event::FORMAT_DESCRIPTION_EVENT;
36        let crc32 = if is_fde {
37            if raw.len() < 19 + 57 + 32 + 5 {
38                return Err(invalid("short FORMAT_DESCRIPTION"));
39            }
40            match raw[raw.len() - 5] {
41                0 => false,
42                1 => true,
43                n => return Err(Error::Unsupported(format!("checksum algorithm {n}"))),
44            }
45        } else if let Some(value) = self.crc32 {
46            value
47        } else if header.event_type == event::ROTATE_EVENT {
48            false
49        } else {
50            return Err(invalid("event before FORMAT_DESCRIPTION"));
51        };
52        let trailer = if is_fde {
53            5
54        } else if crc32 {
55            4
56        } else {
57            0
58        };
59        let end = raw
60            .len()
61            .checked_sub(trailer)
62            .filter(|&end| end >= 19)
63            .ok_or_else(|| invalid("missing checksum"))?;
64        if crc32 {
65            let mut hash = crc32fast::Hasher::new();
66            let mut checksum_header = [0; 19];
67            checksum_header.copy_from_slice(header_bytes);
68            if is_fde {
69                checksum_header[17] &= !1;
70            }
71            hash.update(&checksum_header);
72            hash.update(&raw[19..raw.len() - 4]);
73            let mut expected = [0; 4];
74            expected.copy_from_slice(&raw[raw.len() - 4..]);
75            if hash.finalize() != u32::from_le_bytes(expected) {
76                return Err(Error::Checksum);
77            }
78        }
79        if is_fde {
80            self.crc32 = Some(crc32);
81        }
82        Ok(BinlogFrame {
83            raw_data: raw,
84            header,
85            body: &raw[19..end],
86        })
87    }
88}
89
90/// A body view for consumers that do not retain the complete event. Network
91/// readers borrow the driver buffer directly, without assembling a second copy.
92#[derive(Debug, Clone, Copy)]
93pub struct EventView<'a> {
94    pub header: EventHeader,
95    pub body: &'a [u8],
96}