zipatch-rs 1.0.0

Parser for FFXIV ZiPatch patch files
Documentation
use binrw::BinRead;
use std::io::Cursor;

use super::SqpackFile;

/// SQPK `E` command body: extend a `.dat` file by writing empty-block markers.
#[derive(BinRead, Debug, Clone, PartialEq, Eq)]
#[br(big)]
pub struct SqpkExpandData {
    /// `SqPack` file the expansion targets.
    #[br(pad_before = 3)]
    pub target_file: SqpackFile,
    /// Start offset of the new range (raw `u32` shifted left 7 bits).
    #[br(map = |raw: u32| (raw as u64) << 7)]
    pub block_offset: u64,
    /// Number of 128-byte blocks to add (not shifted).
    #[br(pad_after = 4)]
    pub block_count: u32,
}

pub(crate) fn parse(body: &[u8]) -> crate::Result<SqpkExpandData> {
    Ok(SqpkExpandData::read_be(&mut Cursor::new(body))?)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_expand_data() {
        let mut body = Vec::new();
        body.extend_from_slice(&[0u8; 3]); // alignment
        body.extend_from_slice(&0u16.to_be_bytes()); // main_id
        body.extend_from_slice(&0u16.to_be_bytes()); // sub_id
        body.extend_from_slice(&1u32.to_be_bytes()); // file_id
        body.extend_from_slice(&4u32.to_be_bytes()); // block_offset raw → 4 << 7 = 512
        body.extend_from_slice(&10u32.to_be_bytes()); // block_count (no shift)
        body.extend_from_slice(&[0u8; 4]); // reserved

        let cmd = parse(&body).unwrap();
        assert_eq!(cmd.block_offset, 512);
        assert_eq!(cmd.block_count, 10);
    }
}