mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-23 19:11:46 +01:00
Share SQLite record header splitting (#2846)
This commit is contained in:
1 parent
c42c34bfa1
commit
2be8991cb5
2 files changed
+64
-1
No files matched your search
@@ -55,6 +55,8 @@ pub fn local_payload_len(usable: u32, payload_len: u64, kind: PayloadKind) -> Op
|
||||
pub enum Error {
|
||||
/// Input ended before the integer was complete.
|
||||
TruncatedVarint,
|
||||
/// The record's declared header length excludes its prefix or extends beyond the payload.
|
||||
InvalidRecordHeaderLength(u64),
|
||||
/// The serial type is reserved and has no defined field width.
|
||||
ReservedSerialType(u64),
|
||||
/// The decoded database page size is not a power of two in `512..=65_536`.
|
||||
@@ -77,6 +79,7 @@ impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::TruncatedVarint => f.write_str("truncated SQLite varint"),
|
||||
Self::InvalidRecordHeaderLength(length) => write!(f, "invalid SQLite record header length {length}"),
|
||||
Self::ReservedSerialType(code) => write!(f, "reserved SQLite serial type {code}"),
|
||||
Self::InvalidPageSize(size) => write!(f, "invalid SQLite page size {size}"),
|
||||
Self::UsablePageSizeTooSmall(usable) => write!(f, "usable SQLite page size {usable} is too small"),
|
||||
@@ -120,6 +123,31 @@ pub fn read_varint(input: &[u8]) -> Result<(u64, usize), Error> {
|
||||
Err(Error::TruncatedVarint)
|
||||
}
|
||||
|
||||
/// Split a SQLite record payload into its serial-type bytes and body.
|
||||
///
|
||||
/// The [record header length](https://www.sqlite.org/fileformat.html#record_format) includes
|
||||
/// the leading length varint. The returned serial-type slice excludes that varint and ends
|
||||
/// before the body. The caller decodes serial types from that slice and checks field lengths.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns [`Error::TruncatedVarint`] for an incomplete length and
|
||||
/// [`Error::InvalidRecordHeaderLength`] for a size outside the supplied payload's bounds.
|
||||
///
|
||||
/// ```
|
||||
/// use craft_sqlite::split_record;
|
||||
///
|
||||
/// assert_eq!(split_record(&[2, 1, 42]), Ok((&[1][..], &[42][..])));
|
||||
/// ```
|
||||
pub fn split_record(payload: &[u8]) -> Result<(&[u8], &[u8]), Error> {
|
||||
let (length, length_bytes) = read_varint(payload)?;
|
||||
let invalid_length = Error::InvalidRecordHeaderLength(length);
|
||||
let header_end = usize::try_from(length).map_err(|_| invalid_length)?;
|
||||
let (header, body) = payload.split_at_checked(header_end).ok_or(invalid_length)?;
|
||||
let serial_types = header.get(length_bytes..).ok_or(invalid_length)?;
|
||||
Ok((serial_types, body))
|
||||
}
|
||||
|
||||
/// Return the encoded body width in bytes for a SQLite record serial type.
|
||||
///
|
||||
/// The width is independent of the host's pointer size. The caller checks it against the
|
||||
|
||||
@@ -1,8 +1,43 @@
|
||||
use craft_sqlite::{
|
||||
BtreePageHeader, Error, PayloadKind, btree_cell_offset, decode_signed_be_int, local_payload_len, page_size_from_raw, parse_btree_page_header,
|
||||
read_varint, serial_type_len, usable_page_size,
|
||||
read_varint, serial_type_len, split_record, usable_page_size,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn record_split_excludes_the_length_encoding() {
|
||||
assert_eq!(split_record(&[3, 1, 15, 42, b'A']), Ok((&[1, 15][..], &[42, b'A'][..])));
|
||||
assert_eq!(split_record(&[1]), Ok((&[][..], &[][..])));
|
||||
assert_eq!(split_record(&[3, 0, 9]), Ok((&[0, 9][..], &[][..])));
|
||||
|
||||
let mut payload = [0; 129];
|
||||
payload[..3].copy_from_slice(&[0x81, 0, 1]);
|
||||
payload[128] = 42;
|
||||
let (serial_types, body) = split_record(&payload).unwrap();
|
||||
assert_eq!(serial_types.len(), 126);
|
||||
assert_eq!(serial_types[0], 1);
|
||||
assert_eq!(body, [42]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_split_limits_a_serial_type_to_its_header() {
|
||||
let (serial_types, body) = split_record(&[2, 0x81, 0]).unwrap();
|
||||
assert_eq!(body, [0]);
|
||||
assert_eq!(read_varint(serial_types), Err(Error::TruncatedVarint));
|
||||
|
||||
let (serial_types, _) = split_record(&[3, 0x81, 0]).unwrap();
|
||||
assert_eq!(read_varint(serial_types), Ok((128, 2)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_split_rejects_incomplete_and_out_of_range_lengths() {
|
||||
for payload in [&[][..], &[0x80][..]] {
|
||||
assert_eq!(split_record(payload), Err(Error::TruncatedVarint));
|
||||
}
|
||||
for (payload, length) in [(&[0][..], 0), (&[0x80, 1][..], 1), (&[2][..], 2), (&[0xff; 9][..], u64::MAX)] {
|
||||
assert_eq!(split_record(payload), Err(Error::InvalidRecordHeaderLength(length)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_payload_preserves_inline_and_overflow_boundaries() {
|
||||
// Original literal cases calculated from SQLite file format section 1.6, including
|
||||
|
||||
Reference in new issue
Block a user