mod kind;
use std::ops::Range;
use std::sync::Arc;
use crate::error::Error;
use crate::limits::{Meter, MeteredVec};
pub use kind::{ReaderKind, STREAM_READER, StreamRefusal};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Endian {
Little,
Big,
}
pub const STRING_TO_NULL_WINDOW: usize = 32_767;
#[must_use]
pub fn py_read(len: usize, pos: i128, size: i64) -> (Range<usize>, i128) {
if size == 0 {
return (0..0, pos);
}
let l = i128::try_from(len).unwrap_or(i128::MAX);
let size = if size == -1 {
l.saturating_sub(pos)
} else {
i128::from(size)
};
let stop = pos.saturating_add(size);
let index = |i: i128| -> usize {
let i = if i < 0 { i.saturating_add(l) } else { i };
usize::try_from(i.clamp(0, l)).unwrap_or(0)
};
let start = index(pos);
let end = index(stop).max(start);
(start..end, stop)
}
#[must_use]
pub fn py_unpack_from(len: usize, pos: i128, n: usize) -> Option<usize> {
let l = i128::try_from(len).ok()?;
let n = i128::try_from(n).ok()?;
let mut offset = pos;
if offset < 0 {
if offset.saturating_add(n) > 0 || offset.saturating_add(l) < 0 {
return None;
}
offset = offset.saturating_add(l);
}
if l.saturating_sub(offset) < n {
return None;
}
usize::try_from(offset).ok()
}
#[must_use]
pub fn py_align(pos: i128, n: u32) -> i128 {
let n = i128::from(n);
if n == 0 {
return pos;
}
pos.saturating_add((n - pos.rem_euclid(n)) % n)
}
#[must_use]
pub fn py_string_to_null(
data: &[u8],
pos: i128,
max_length: usize,
) -> Option<(Range<usize>, i128)> {
let window = py_string_window(data.len(), pos, max_length);
let nul = data
.get(window.clone())
.and_then(|w| w.iter().position(|b| *b == 0))
.map(|k| window.start + k);
py_string_to_null_found(data.len(), pos, max_length, nul)
}
#[must_use]
pub(crate) fn py_string_window(len: usize, pos: i128, max_length: usize) -> Range<usize> {
let l = i128::try_from(len).unwrap_or(i128::MAX);
let max = i128::try_from(max_length).unwrap_or(i128::MAX);
let clamp = |i: i128| usize::try_from(i.clamp(0, l)).unwrap_or(0);
let start = clamp(pos);
start..clamp(pos.saturating_add(max)).max(start)
}
#[must_use]
pub(crate) fn py_string_to_null_found(
len: usize,
pos: i128,
max_length: usize,
nul: Option<usize>,
) -> Option<(Range<usize>, i128)> {
let l = i128::try_from(len).ok()?;
let max = i128::try_from(max_length).ok()?;
if let Some(nul) = nul {
let start = py_string_window(len, pos, max_length).start;
return Some((start..nul, i128::try_from(nul).ok()? + 1));
}
if pos.saturating_add(max) >= l {
return None;
}
Some(py_read(len, pos, i64::try_from(max_length).ok()?))
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ShortArray<const N: usize> {
bytes: [u8; N],
len: usize,
}
const _: () = crate::send_sync::<ShortArray<16>>();
impl<const N: usize> ShortArray<N> {
#[must_use]
pub fn from_slice(got: &[u8]) -> Self {
let len = got.len().min(N);
let mut bytes = [0u8; N];
if let (Some(to), Some(from)) = (bytes.get_mut(..len), got.get(..len)) {
to.copy_from_slice(from);
}
Self { bytes, len }
}
#[must_use]
pub fn as_slice(&self) -> &[u8] {
self.bytes.get(..self.len).unwrap_or_default()
}
}
impl<const N: usize> From<[u8; N]> for ShortArray<N> {
fn from(bytes: [u8; N]) -> Self {
Self { bytes, len: N }
}
}
impl<const N: usize> std::ops::Deref for ShortArray<N> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_slice()
}
}
impl<const N: usize> std::fmt::Debug for ShortArray<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_slice().fmt(f)
}
}
#[derive(Clone, Debug)]
pub struct Reader<'a> {
data: &'a [u8],
pos: i128,
endian: Endian,
kind: ReaderKind,
}
impl<'a> Reader<'a> {
#[must_use]
pub fn new(data: &'a [u8], endian: Endian) -> Self {
Self::with_kind(data, endian, ReaderKind::Memory)
}
#[must_use]
pub fn with_kind(data: &'a [u8], endian: Endian, kind: ReaderKind) -> Self {
Self {
data,
pos: 0,
endian,
kind,
}
}
#[must_use]
pub fn data(&self) -> &'a [u8] {
self.data
}
#[must_use]
pub fn kind(&self) -> ReaderKind {
self.kind
}
#[must_use]
pub fn position(&self) -> i128 {
self.pos
}
pub fn set_position(&mut self, pos: i128) -> Result<(), Error> {
self.pos = self.kind.seek(pos)?;
Ok(())
}
pub(crate) fn advance_to(&mut self, pos: i128) {
self.pos = pos;
}
#[must_use]
pub fn endian(&self) -> Endian {
self.endian
}
pub fn set_endian(&mut self, endian: Endian) {
self.endian = endian;
}
#[must_use]
pub fn remaining(&self) -> usize {
py_read(self.data.len(), self.pos, -1).0.len()
}
pub fn align(&mut self, n: u32) -> Result<(), Error> {
self.pos = self.kind.align(self.pos, n)?;
Ok(())
}
fn have(&self) -> u64 {
u64::try_from(self.remaining()).unwrap_or(u64::MAX)
}
fn out_of_bounds(&self, what: &'static str, pos: i128, need: usize) -> Error {
Error::OutOfBounds {
what,
pos,
need: u64::try_from(need).unwrap_or(u64::MAX),
have: self.have(),
reader: self.kind,
length: self.data.len(),
}
}
pub fn read_range(&mut self, n: i64) -> Result<Range<usize>, Error> {
let (range, pos) = self.kind.read(self.data.len(), self.pos, n)?;
self.pos = pos;
Ok(range)
}
pub fn read(&mut self, n: i64) -> Result<&'a [u8], Error> {
let range = self.read_range(n)?;
Ok(self.data.get(range).unwrap_or_default())
}
pub fn read_bytes(&mut self, n: usize) -> Result<&'a [u8], Error> {
let pos = self.pos;
let got = self.read(i64::try_from(n).unwrap_or(i64::MAX))?;
if got.len() == n {
Ok(got)
} else {
Err(self.out_of_bounds("bytes", pos, n))
}
}
pub fn read_bytes_short(&mut self, n: usize) -> &'a [u8] {
self.read(i64::try_from(n).unwrap_or(i64::MAX))
.unwrap_or_default()
}
pub fn read_string_to_null(&mut self) -> Result<&'a [u8], Error> {
let pos = self.pos;
match self
.kind
.string_to_null(self.data, pos, STRING_TO_NULL_WINDOW)
{
Some((range, new)) => {
self.pos = new;
Ok(self.data.get(range).unwrap_or_default())
}
None => Err(self.out_of_bounds("string to null", pos, 1)),
}
}
pub(crate) fn string_to_null_error(&self) -> Error {
self.out_of_bounds("string to null", self.pos, 1)
}
fn one_byte(&mut self, what: &'static str) -> Result<u8, Error> {
let pos = self.pos;
match self.read_bytes_short(1) {
[b] => Ok(*b),
_ => Err(self.out_of_bounds(what, pos, 1)),
}
}
pub fn read_bool(&mut self) -> Result<bool, Error> {
self.one_byte("bool").map(|b| b != 0)
}
pub fn read_u8(&mut self) -> Result<u8, Error> {
self.one_byte("u8")
}
pub fn read_i8(&mut self) -> Result<i8, Error> {
self.one_byte("i8").map(|b| i8::from_le_bytes([b]))
}
fn fixed<const N: usize>(&mut self, what: &'static str) -> Result<[u8; N], Error> {
let pos = self.pos;
let (at, next) = self.kind.unpack(self.data.len(), pos, N);
let Some(at) = at else {
let e = self.out_of_bounds(what, pos, N);
self.pos = next;
return Err(e);
};
let mut b = [0u8; N];
if let Some(src) = self.data.get(at..at.saturating_add(N)) {
b.copy_from_slice(src);
}
self.pos = next;
if self.endian == Endian::Big {
b.reverse();
}
Ok(b)
}
pub fn read_u16(&mut self) -> Result<u16, Error> {
self.fixed("u16").map(u16::from_le_bytes)
}
pub fn read_i16(&mut self) -> Result<i16, Error> {
self.fixed("i16").map(i16::from_le_bytes)
}
pub fn read_u32(&mut self) -> Result<u32, Error> {
self.fixed("u32").map(u32::from_le_bytes)
}
pub fn read_i32(&mut self) -> Result<i32, Error> {
self.fixed("i32").map(i32::from_le_bytes)
}
pub fn read_u64(&mut self) -> Result<u64, Error> {
self.fixed("u64").map(u64::from_le_bytes)
}
pub fn read_i64(&mut self) -> Result<i64, Error> {
self.fixed("i64").map(i64::from_le_bytes)
}
pub fn read_short_array<const N: usize>(&mut self) -> ShortArray<N> {
ShortArray::from_slice(self.read_bytes_short(N))
}
pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error> {
let got = self.read_bytes(N)?;
let mut b = [0u8; N];
b.copy_from_slice(got);
Ok(b)
}
pub fn read_i32_array(&mut self, meter: &Arc<Meter>) -> Result<MeteredVec<i32>, Error> {
const WHAT: &str = "int array";
let len = self.data.len();
let have = self.have();
let (at, next) = self.kind.unpack(len, self.pos, 4);
let Some(at) = at else {
let e = Error::OutOfBounds {
what: WHAT,
pos: self.pos,
need: 4,
have,
reader: self.kind,
length: self.data.len(),
};
self.pos = next;
return Err(e);
};
let count = self.i32_at(at);
self.pos = next;
let Ok(n) = usize::try_from(count) else {
return Err(Error::Malformed {
what: WHAT,
detail: format!("negative count {count}").into(),
});
};
let (at, next) = n
.checked_mul(4)
.map_or((None, self.pos), |b| self.kind.unpack(len, self.pos, b));
let Some(at) = at else {
let e = Error::OutOfBounds {
what: WHAT,
pos: self.pos,
need: u64::try_from(n).unwrap_or(u64::MAX).saturating_mul(4),
have: self.have(),
reader: self.kind,
length: self.data.len(),
};
self.pos = next;
return Err(e);
};
let mut out = MeteredVec::with_capacity(meter, n)?;
for i in 0..n {
out.push(self.i32_at(at + 4 * i))?;
}
self.pos = next;
Ok(out)
}
fn i32_at(&self, at: usize) -> i32 {
let mut b = [0u8; 4];
if let Some(src) = self.data.get(at..at + 4) {
b.copy_from_slice(src);
}
match self.endian {
Endian::Little => i32::from_le_bytes(b),
Endian::Big => i32::from_be_bytes(b),
}
}
}
#[cfg(test)]
mod tests {
use super::{Endian, Reader, py_read};
use crate::budget::{Reservation, Unbounded};
use crate::error::Error;
use crate::error::LimitKind;
use crate::limits::{Meter, heap_cost};
#[test]
fn read_i32_array_reads_as_unitypy_and_charges_the_meter() {
let le = |v: &[i32]| v.iter().flat_map(|x| x.to_le_bytes()).collect::<Vec<u8>>();
let be = |v: &[i32]| v.iter().flat_map(|x| x.to_be_bytes()).collect::<Vec<u8>>();
let m = Meter::unlimited();
let ok = |data: &[u8], endian, pos: i128| {
let mut r = Reader::new(data, endian);
r.set_position(pos).unwrap();
let v = r.read_i32_array(&m).unwrap();
(v.as_slice().to_vec(), r.position(), v.charge().bytes())
};
assert_eq!(
ok(&le(&[3, 1, -2, 3]), Endian::Little, 0),
(vec![1, -2, 3], 16, heap_cost(12))
);
assert_eq!(
ok(&be(&[3, 1, -2, 3]), Endian::Big, 0),
(vec![1, -2, 3], 16, heap_cost(12))
);
assert_eq!(
ok(&le(&[1, 7, 9]), Endian::Little, -12),
(vec![7], -4, heap_cost(4))
);
assert_eq!(ok(&le(&[0]), Endian::Little, 0), (vec![], 4, 0));
let err = |data: &[u8], limit: u64| {
let m = Meter::new(limit, std::sync::Arc::new(Unbounded), Reservation::Input);
let mut r = Reader::new(data, Endian::Little);
let e = r.read_i32_array(&m).unwrap_err();
(e, r.position(), m.used())
};
let (e, pos, used) = err(&le(&[-1, 5]), u64::MAX);
assert!(matches!(e, Error::Malformed { .. }), "{e:?}");
assert_eq!((pos, used), (4, 0));
let (e, pos, used) = err(&le(&[i32::MAX, 5]), u64::MAX);
assert!(
matches!(
e,
Error::OutOfBounds {
need: 8_589_934_588,
..
}
),
"{e:?}"
);
assert_eq!((pos, used), (4, 0));
let (e, pos, _) = err(&[1, 0], u64::MAX);
assert!(matches!(e, Error::OutOfBounds { need: 4, .. }), "{e:?}");
assert_eq!(pos, 0);
let (e, pos, used) = err(&le(&[64; 65]), 100);
assert!(
matches!(&e, Error::Limit(l) if l.kind() == LimitKind::InputMemory),
"{e:?}"
);
assert_eq!((pos, used), (4, 0));
}
#[test]
fn py_read_is_a_python_slice() {
let cases: &[(i128, i64, std::ops::Range<usize>, i128)] = &[
(0, 4, 0..4, 4),
(8, 4, 8..10, 12),
(12, 4, 10..10, 16),
(3, 0, 0..0, 3),
(3, -1, 3..10, 10),
(-3, 2, 7..9, -1),
(-3, 5, 7..7, 2),
(5, -3, 5..5, 2),
(2, -5, 2..7, -3),
(-20, 5, 0..0, -15),
(-1, -1, 9..10, 10),
(12, -1, 10..10, 10),
(15, -1, 10..10, 10),
(i128::from(i64::MIN), i64::MAX, 0..9, -1),
];
for (pos, size, range, new_pos) in cases {
let (r, p) = py_read(10, *pos, *size);
assert_eq!(r.len(), range.len(), "({pos}, {size})");
if !range.is_empty() {
assert_eq!(r, *range, "({pos}, {size})");
}
assert_eq!(p, *new_pos, "({pos}, {size})");
}
}
}