| 1 | //! The bundle envelope: a container of verbatim L0 LTX files, never a |
| 2 | //! format. |
| 3 | //! |
| 4 | //! celld-original, not a Litestream port. Layout: the concatenated raw L0 |
| 5 | //! bytes, a binary footer of rows, the footer length as a little-endian |
| 6 | //! u32, and a four-byte magic. A row is `u16 cell_len | cell utf-8 | |
| 7 | //! u64 cell_epoch | u64 txid | u64 offset | u64 len`, all little-endian. |
| 8 | //! Un-bundling is arithmetic. The inner bytes are exactly what the |
| 9 | //! per-cell writer produces, so the LTX differential oracle keeps |
| 10 | //! validating every byte that matters, and "at rest, the bucket is pure |
| 11 | //! Litestream" stays checkable — a drained bundle leaves no trace this |
| 12 | //! module ever existed. Nothing else in this crate depends on bundles; |
| 13 | //! deleting this file returns the crate to its upstream shape. |
| 14 | |
| 15 | use crate::error::{Error, Result}; |
| 16 | use crate::TXID; |
| 17 | |
| 18 | const MAGIC: &[u8; 4] = b"CLB1"; |
| 19 | |
| 20 | fn malformed(what: &str) -> Error { |
| 21 | Error::Other(format!("bundle: {what}").into()) |
| 22 | } |
| 23 | |
| 24 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 25 | pub struct BundleRow { |
| 26 | pub cell: String, |
| 27 | pub cell_epoch: u64, |
| 28 | pub txid: u64, |
| 29 | pub offset: u64, |
| 30 | pub len: u64, |
| 31 | } |
| 32 | |
| 33 | impl BundleRow { |
| 34 | pub fn txid(&self) -> TXID { |
| 35 | TXID(self.txid) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// One captured L0 segment headed for a bundle. |
| 40 | pub struct BundleEntry { |
| 41 | pub cell: String, |
| 42 | pub cell_epoch: u64, |
| 43 | pub txid: u64, |
| 44 | pub bytes: Vec<u8>, |
| 45 | } |
| 46 | |
| 47 | pub fn encode(entries: &[BundleEntry]) -> Result<Vec<u8>> { |
| 48 | let mut out = Vec::new(); |
| 49 | let mut footer = Vec::new(); |
| 50 | for entry in entries { |
| 51 | let cell = entry.cell.as_bytes(); |
| 52 | let cell_len: u16 = cell |
| 53 | .len() |
| 54 | .try_into() |
| 55 | .map_err(|_| malformed("cell name too long"))?; |
| 56 | footer.extend_from_slice(&cell_len.to_le_bytes()); |
| 57 | footer.extend_from_slice(cell); |
| 58 | for value in [ |
| 59 | entry.cell_epoch, |
| 60 | entry.txid, |
| 61 | out.len() as u64, |
| 62 | entry.bytes.len() as u64, |
| 63 | ] { |
| 64 | footer.extend_from_slice(&value.to_le_bytes()); |
| 65 | } |
| 66 | out.extend_from_slice(&entry.bytes); |
| 67 | } |
| 68 | let len: u32 = footer |
| 69 | .len() |
| 70 | .try_into() |
| 71 | .map_err(|_| malformed("footer too large"))?; |
| 72 | out.extend_from_slice(&footer); |
| 73 | out.extend_from_slice(&len.to_le_bytes()); |
| 74 | out.extend_from_slice(MAGIC); |
| 75 | Ok(out) |
| 76 | } |
| 77 | |
| 78 | /// The rows, from a complete bundle object. |
| 79 | pub fn decode_rows(bundle: &[u8]) -> Result<Vec<BundleRow>> { |
| 80 | if bundle.len() < 8 { |
| 81 | return Err(malformed("shorter than its trailer")); |
| 82 | } |
| 83 | let (rest, magic) = bundle.split_at(bundle.len() - 4); |
| 84 | if magic != MAGIC { |
| 85 | return Err(malformed("bad magic")); |
| 86 | } |
| 87 | let (rest, len_bytes) = rest.split_at(rest.len() - 4); |
| 88 | let len = u32::from_le_bytes(len_bytes.try_into().unwrap()) as usize; |
| 89 | if rest.len() < len { |
| 90 | return Err(malformed("footer overruns the object")); |
| 91 | } |
| 92 | let mut footer = &rest[rest.len() - len..]; |
| 93 | let mut rows = Vec::new(); |
| 94 | while !footer.is_empty() { |
| 95 | if footer.len() < 2 { |
| 96 | return Err(malformed("truncated row")); |
| 97 | } |
| 98 | let cell_len = u16::from_le_bytes(footer[..2].try_into().unwrap()) as usize; |
| 99 | footer = &footer[2..]; |
| 100 | if footer.len() < cell_len + 32 { |
| 101 | return Err(malformed("truncated row")); |
| 102 | } |
| 103 | let cell = std::str::from_utf8(&footer[..cell_len]) |
| 104 | .map_err(|_| malformed("cell name not utf-8"))? |
| 105 | .to_string(); |
| 106 | footer = &footer[cell_len..]; |
| 107 | let mut values = [0_u64; 4]; |
| 108 | for value in &mut values { |
| 109 | *value = u64::from_le_bytes(footer[..8].try_into().unwrap()); |
| 110 | footer = &footer[8..]; |
| 111 | } |
| 112 | rows.push(BundleRow { |
| 113 | cell, |
| 114 | cell_epoch: values[0], |
| 115 | txid: values[1], |
| 116 | offset: values[2], |
| 117 | len: values[3], |
| 118 | }); |
| 119 | } |
| 120 | Ok(rows) |
| 121 | } |
| 122 | |
| 123 | /// One row's verbatim L0 bytes out of a complete bundle object. |
| 124 | pub fn slice<'a>(bundle: &'a [u8], row: &BundleRow) -> Result<&'a [u8]> { |
| 125 | let start = row.offset as usize; |
| 126 | let end = start |
| 127 | .checked_add(row.len as usize) |
| 128 | .ok_or_else(|| malformed("row overflows"))?; |
| 129 | if end > bundle.len() { |
| 130 | return Err(malformed("row overruns the object")); |
| 131 | } |
| 132 | Ok(&bundle[start..end]) |
| 133 | } |
| 134 | |
| 135 | #[cfg(test)] |
| 136 | mod tests { |
| 137 | use super::*; |
| 138 | |
| 139 | #[test] |
| 140 | fn round_trip_returns_byte_identical_segments() { |
| 141 | let entries = vec![ |
| 142 | BundleEntry { |
| 143 | cell: "a".into(), |
| 144 | cell_epoch: 3, |
| 145 | txid: 7, |
| 146 | bytes: vec![1, 2, 3, 4], |
| 147 | }, |
| 148 | BundleEntry { |
| 149 | cell: "b-with-a-longer-name".into(), |
| 150 | cell_epoch: 1, |
| 151 | txid: 9, |
| 152 | bytes: vec![0xde, 0xad], |
| 153 | }, |
| 154 | ]; |
| 155 | let bundle = encode(&entries).unwrap(); |
| 156 | let rows = decode_rows(&bundle).unwrap(); |
| 157 | assert_eq!(rows.len(), 2); |
| 158 | for (row, entry) in rows.iter().zip(&entries) { |
| 159 | assert_eq!( |
| 160 | slice(&bundle, row).unwrap(), |
| 161 | entry.bytes.as_slice(), |
| 162 | "the envelope rule: inner bytes verbatim" |
| 163 | ); |
| 164 | assert_eq!( |
| 165 | (row.cell.as_str(), row.cell_epoch, row.txid), |
| 166 | (entry.cell.as_str(), entry.cell_epoch, entry.txid) |
| 167 | ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn a_lying_footer_is_refused() { |
| 173 | let bundle = encode(&[BundleEntry { |
| 174 | cell: "a".into(), |
| 175 | cell_epoch: 1, |
| 176 | txid: 1, |
| 177 | bytes: vec![9; 8], |
| 178 | }]) |
| 179 | .unwrap(); |
| 180 | let mut rows = decode_rows(&bundle).unwrap(); |
| 181 | rows[0].len = 1 << 30; |
| 182 | assert!(slice(&bundle, &rows[0]).is_err()); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn not_a_bundle_is_refused() { |
| 187 | assert!(decode_rows(b"just some sqlite bytes").is_err()); |
| 188 | assert!(decode_rows(b"").is_err()); |
| 189 | } |
| 190 | } |