Skip to main content

Reader

Struct Reader 

Source
pub struct Reader<'a> { /* private fields */ }
Expand description

A cursor over data, read by the rules of one of UnityPy’s readers (ReaderKind). Positions are absolute within data and signed, as a Python Position (module docs): on the memory reader every read follows the helper of its Python primitive at whatever position it is, negative or past the end; the stream reader’s position is never negative.

Implementations§

Source§

impl<'a> Reader<'a>

Source

pub fn new(data: &'a [u8], endian: Endian) -> Self

A memory reader at position 0 (what UnityPy reads bytes with).

Source

pub fn with_kind(data: &'a [u8], endian: Endian, kind: ReaderKind) -> Self

A reader of kind at position 0.

Source

pub fn data(&self) -> &'a [u8] ⓘ

The whole slice.

Source

pub fn kind(&self) -> ReaderKind

Which reader’s rules it reads by.

Source

pub fn position(&self) -> i128

The position, as Python holds it.

Source

pub fn set_position(&mut self, pos: i128) -> Result<(), Error>

An assignment to reader.Position: the memory reader takes any position, negative or past the end; the stream reader refuses a negative one or one at or above 2^63 (ReaderKind::seek), and stays where it was.

Source

pub fn endian(&self) -> Endian

The byte order of multi-byte reads.

Source

pub fn set_endian(&mut self, endian: Endian)

Changes the byte order (a SerializedFile header switches it).

Source

pub fn remaining(&self) -> usize

The bytes read(-1) would return from here: what a table’s growth may count on (plan section 3.3). The same on both readers.

Source

pub fn align(&mut self, n: u32) -> Result<(), Error>

align_stream(n): py_align, without a bounds check, assigned to the position: the memory reader takes any result; the stream reader’s position is never negative, so the arithmetic is the same, and it refuses a result at or above 2^63 as its seek does (ReaderKind::align), staying where it was.

Source

pub fn read_range(&mut self, n: i64) -> Result<Range<usize>, Error>

read(n), the range of data it returns: the memory reader’s py_read slice, short or empty at the ends (read_bytes on a truncated block, File.py:79), the position moved by n; or the stream reader’s, which refuses an n below -1 and stops at the end (ReaderKind::read).

Source

pub fn read(&mut self, n: i64) -> Result<&'a [u8], Error>

read(n) as it is: Self::read_range’s bytes.

Source

pub fn read_bytes(&mut self, n: usize) -> Result<&'a [u8], Error>

n bytes where UnityPy’s caller unpacks them whole (read(n), then struct.unpack): what read(n) returns must hold n bytes, else OutOfBounds; the position moves as read(n) moves it either way.

Source

pub fn read_bytes_short(&mut self, n: usize) -> &'a [u8] ⓘ

read(n) as it is for a size UnityPy’s own code fixes (3, 16 or 32 bytes), which neither reader refuses: short or empty at the ends.

Source

pub fn read_string_to_null(&mut self) -> Result<&'a [u8], Error>

read_string_to_null with the 32,767-byte window, by the reader’s rule (py_string_to_null or the stream reader’s loop, ReaderKind::string_to_null).

Source

pub fn read_bool(&mut self) -> Result<bool, Error>

One byte as a bool (non-zero is true): unpack("?", read(1)).

Source

pub fn read_u8(&mut self) -> Result<u8, Error>

u8: unpack("B", read(1)).

Source

pub fn read_i8(&mut self) -> Result<i8, Error>

i8: unpack("b", read(1)).

Source

pub fn read_u16(&mut self) -> Result<u16, Error>

u16 in the reader’s byte order: ReaderKind::unpack.

Source

pub fn read_i16(&mut self) -> Result<i16, Error>

i16 in the reader’s byte order: ReaderKind::unpack.

Source

pub fn read_u32(&mut self) -> Result<u32, Error>

u32 in the reader’s byte order: ReaderKind::unpack.

Source

pub fn read_i32(&mut self) -> Result<i32, Error>

i32 in the reader’s byte order: ReaderKind::unpack.

Source

pub fn read_u64(&mut self) -> Result<u64, Error>

u64 in the reader’s byte order: ReaderKind::unpack.

Source

pub fn read_i64(&mut self) -> Result<i64, Error>

i64 in the reader’s byte order: ReaderKind::unpack.

Source

pub fn read_short_array<const N: usize>(&mut self) -> ShortArray<N>

read(N) kept as it is (ShortArray): a GUID or a hash, which UnityPy stores without checking its length, so a short read (on the memory reader at a position in -N..0; at the end on either) is no error; the position moves as read(N) moves it.

Source

pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>

A fixed-size byte array: read(N), which must hold N bytes where UnityPy’s caller needs them all (it unpacks them); where it keeps them as they are, Self::read_short_array.

Source

pub fn read_i32_array( &mut self, meter: &Arc<Meter>, ) -> Result<MeteredVec<i32>, Error>

read_int_array (EndianBinaryReader.py:182, read_array_struct at :164-168 and :258-264): an i32 count read as the reader’s own read_int (ReaderKind::unpack), then the array: on the memory reader struct.unpack_from of that many i32 at the new position (a negative one counts from the end), the position moving by 4 * count; on the stream reader struct.unpack(read(4 * count)), which moves it by what it read, even when that is short. A negative count raises (Struct("<-1i") is a bad format) with the position after the count, as does an array the bytes do not hold (the stream reader’s has moved). The array is a vector on meter, the input’s, charged after the bytes are checked and before it is allocated, so a count allocates nothing the bytes do not back and the array counts against its input while it is kept (round 5: this returned a plain Vec, the one input-sized allocation a parser made uncharged); a refused charge is the meter’s or the budget’s error, with the position after the count. Implemented by the scaffold; the cases are recorded from UnityPy 1.25.2 (read_i32_array_reads_as_unitypy_and_charges_the_meter).

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Reader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Reader<'a>

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> Move for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnsafeUnpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.