zipatch-rs 1.0.0

Parser for FFXIV ZiPatch patch files
Documentation
use binrw::{BinRead, BinResult, Endian};
use std::io::Cursor;

use super::SqpackFile;

/// Index sub-command byte of a SQPK `I` chunk.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexCommand {
    /// Add an index entry.
    Add,
    /// Remove an index entry.
    Delete,
}

fn read_index_command<R: std::io::Read + std::io::Seek>(
    reader: &mut R,
    _: Endian,
    (): (),
) -> BinResult<IndexCommand> {
    let byte = <u8 as BinRead>::read_options(reader, Endian::Big, ())?;
    match byte {
        b'A' => Ok(IndexCommand::Add),
        b'D' => Ok(IndexCommand::Delete),
        _ => Err(binrw::Error::Custom {
            pos: 0,
            err: Box::new(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "unknown IndexCommand",
            )),
        }),
    }
}

/// SQPK `I` command body: add or remove a `SqPack` index entry.
#[derive(BinRead, Debug, Clone, PartialEq, Eq)]
#[br(big)]
pub struct SqpkIndex {
    /// Whether to add or remove the entry.
    #[br(parse_with = read_index_command)]
    pub command: IndexCommand,
    /// `true` if the entry is a synonym (hash collision) record.
    #[br(map = |x: u8| x != 0)]
    pub is_synonym: bool,
    /// `SqPack` file the entry lives in.
    #[br(pad_before = 1)]
    pub target_file: SqpackFile,
    /// 64-bit hash of the indexed asset path.
    pub file_hash: u64,
    /// Block offset within the target file.
    pub block_offset: u32,
    /// Block number used by the index lookup.
    pub block_number: u32,
}

/// SQPK `X` command body: patch install info (status, version, declared install size).
#[derive(BinRead, Debug, Clone, PartialEq, Eq)]
#[br(big)]
pub struct SqpkPatchInfo {
    /// Status byte for the patch install.
    pub status: u8,
    /// Patch info structure version.
    pub version: u8,
    /// Declared total installed size after the patch.
    #[br(pad_before = 1)]
    pub install_size: u64,
}

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

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

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

    #[test]
    fn parses_sqpk_index_add() {
        let mut body = Vec::new();
        body.push(b'A'); // command = Add
        body.push(1u8); // is_synonym = true
        body.push(0u8); // alignment
        body.extend_from_slice(&0x0102u16.to_be_bytes()); // main_id
        body.extend_from_slice(&0x0304u16.to_be_bytes()); // sub_id
        body.extend_from_slice(&0u32.to_be_bytes()); // file_id
        body.extend_from_slice(&0x0807060504030201u64.to_be_bytes()); // file_hash (would differ in LE)
        body.extend_from_slice(&5u32.to_be_bytes()); // block_offset
        body.extend_from_slice(&10u32.to_be_bytes()); // block_number

        let idx = parse_index(&body).unwrap();
        assert!(matches!(idx.command, IndexCommand::Add));
        assert!(idx.is_synonym);
        assert_eq!(idx.target_file.main_id, 0x0102);
        assert_eq!(idx.file_hash, 0x0807060504030201);
        assert_eq!(idx.block_offset, 5);
        assert_eq!(idx.block_number, 10);
    }

    #[test]
    fn rejects_unknown_index_command() {
        let mut body = Vec::new();
        body.push(b'Z'); // invalid
        body.extend_from_slice(&[0u8; 20]);
        assert!(parse_index(&body).is_err());
    }

    #[test]
    fn parses_sqpk_patch_info() {
        let mut body = Vec::new();
        body.push(3u8); // status
        body.push(1u8); // version
        body.push(0u8); // alignment
        body.extend_from_slice(&0x0102030405060708u64.to_be_bytes()); // install_size (BE, not LE)

        let info = parse_patch_info(&body).unwrap();
        assert_eq!(info.status, 3);
        assert_eq!(info.version, 1);
        assert_eq!(info.install_size, 0x0102030405060708);
    }
}