zipatch-rs 1.6.0

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

use super::SqpackFileId;

/// SQPK `E` command body: grow a `.dat` file by writing empty-block markers
/// into a previously unallocated region.
///
/// `ExpandData` and [`SqpkDeleteData`](super::SqpkDeleteData) produce the same
/// on-disk result — both write a `SqPack` empty-block header at `block_offset`
/// followed by zeroed bytes for the full block range. The semantic difference is
/// in the patch's intent:
///
/// - `E` (`ExpandData`) extends the file into space that did not previously exist,
///   growing the archive. It typically precedes a series of `A` (`AddData`) writes
///   into that newly allocated space.
/// - `D` (`DeleteData`) clears existing live blocks, logically freeing them.
///
/// The apply implementation (`src/apply/sqpk.rs`) handles both commands with
/// the same `write_empty_block` helper.
///
/// ## Wire format (all big-endian)
///
/// ```text
/// ┌────────────────────────────────────────────────────────────────────┐
/// │ <padding>        : [u8; 3]   (reserved, always zero)               │  bytes 0–2
/// │ main_id          : u16 BE                                          │  bytes 3–4
/// │ sub_id           : u16 BE                                          │  bytes 5–6
/// │ file_id          : u32 BE                                          │  bytes 7–10
/// │ block_offset_raw : u32 BE    multiply by 128 to get byte offset    │  bytes 11–14
/// │ block_count      : u32 BE    number of 128-byte blocks to allocate │  bytes 15–18
/// │ <reserved>       : u32       (always zero)                         │  bytes 19–22
/// └────────────────────────────────────────────────────────────────────┘
/// ```
///
/// `block_offset_raw` is in **128-byte `SqPack` block units** and is multiplied
/// by 128 (`<< 7`) during parsing. `block_count` is a direct block count (not
/// a byte count) and is stored as-is.
///
/// The total byte range affected by this command is `block_count * 128` bytes
/// starting at `block_offset`.
///
/// ## Reference
///
/// # Errors
///
/// Parsing returns [`crate::ParseError::Decode`] if the body is too
/// short to contain all required fields.
#[derive(BinRead, Debug, Clone, PartialEq, Eq)]
#[br(big)]
pub struct SqpkExpandData {
    /// `SqPack` file to expand.
    ///
    /// Preceded by 3 bytes of alignment padding in the wire format.
    #[br(pad_before = 3)]
    pub target_file: SqpackFileId,
    /// Byte offset within the target `.dat` file at which the new block range
    /// begins.
    ///
    /// Decoded from a raw big-endian `u32` by multiplying by 128 (`raw << 7`).
    /// The raw wire value is in 128-byte `SqPack` block units.
    #[br(map = |raw: u32| (raw as u64) << 7)]
    pub block_offset: u64,
    /// Number of 128-byte `SqPack` blocks to allocate.
    ///
    /// Stored directly as a big-endian `u32` without any unit shift. The
    /// total byte length of the affected region is `block_count * 128`.
    /// Must be non-zero; the apply layer's `write_empty_block` helper returns
    /// an error for `block_count == 0`.
    ///
    /// Followed by 4 bytes of reserved padding (`pad_after = 4`) in the wire format.
    #[br(pad_after = 4)]
    pub block_count: u32,
}

pub(crate) fn parse(body: &[u8]) -> crate::ParseResult<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);
    }
}