Skip to main content

ApModel

Struct ApModel 

Source
pub struct ApModel { /* private fields */ }
Expand description

AP deobfuscation model for a single SA17 page-store file.

Build one with ApModel::learn, then call ApModel::deobfuscate to recover plaintext for any page.

The model is cheap to clone; it stores at most one u8 per 16-page block.

Implementations§

Source§

impl ApModel

Source

pub fn learn(store: &PageStore) -> Self

Construct an ApModel by scanning every pure-AP page in store.

Pure-AP page types — '@', 'C', 'H', 'M' — have bodies dominated by the AP fill (sectors 1..6 are almost entirely fill). From those sectors we can back-calculate bv(block) precisely.

Pages of other types, whose plaintext is dense, contribute nothing to the calibration. At least one pure-AP page must exist in the store (SA17 files always have some).

Source

pub fn bv_at(&self, bi: u64) -> u8

Return the bv for block bi, falling back to bv0 for unknown blocks.

For files where every block has at least one pure-AP page, every block will be in bv_map. For the rare blocks that lack one, bv0 is used (an approximation that degrades gracefully).

Source

pub fn deobfuscate(&self, raw: &[u8], pn: u64) -> Vec<u8>

Deobfuscate a full 4 KiB page.

pn is the zero-based page index within the store.

The page trailer (0xFF0..0xFFC) and CRC footer (0xFFC..0x1000) are copied verbatim; they are stored in plaintext and must not be modified.

For blocks whose bv was not learned from a pure-AP page this falls back to bv0. Use ApModel::deobfuscate_with_store to get the correct bv for every block.

Source

pub fn learned_block_count(&self) -> usize

Number of blocks for which bv was directly learned from a pure-AP page.

Source

pub fn recover_bv_for_block(&self, store: &PageStore, bi: u64) -> u8

Brute-force recover bv for a block that has no pure-AP page.

For each candidate bv (0..=255), score it by computing the peak of the histogram of (stored[i] - (pn + si - bias) - i*step) mod 256 for each candidate step, then summing the best-step peak across all sectors of up to 3 sample pages in the block. The candidate with the highest total score is returned.

The algorithm matches APModel._recover_bv_for_block in the Python reference implementation. At ~2.7 M arithmetic ops per block it is fast in release mode (< 2 ms on modern hardware).

Source

pub fn deobfuscate_with_store( &self, raw: &[u8], pn: u64, store: &PageStore, ) -> Vec<u8>

Deobfuscate a full 4 KiB page, recovering the block’s bv from the store on demand if it was not learned from pure-AP pages.

Use this variant when you need accurate deobfuscation for blocks that have no pure-AP pages (which is the common case for dense data blocks that consist entirely of ‘E’-type pages).

Source

pub fn deobfuscate_with_bv(&self, raw: &[u8], pn: u64, bv: u8) -> Vec<u8>

Deobfuscate using an explicitly supplied bv.

The caller is responsible for supplying the correct block bv; the model’s internal bv_map is not consulted.

Trait Implementations§

Source§

impl Clone for ApModel

Source§

fn clone(&self) -> ApModel

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 Debug for ApModel

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ApModel

Source§

fn default() -> ApModel

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.