nom_mpq/parser/
mpq_file_header_ext.rs

1//! Nom Parsing The MPQ File Header Extended
2//! NOTES:
3//! - MPyQ uses struct_format: 'q2h'
4
5use super::LITTLE_ENDIAN;
6use crate::dbg_dmp;
7use nom::number::complete::{i16, i64};
8use nom::*;
9
10/// Extended fields only present in the Burning Crusade format and later
11#[derive(Debug, PartialEq, Default, Clone, Copy)]
12pub struct MPQFileHeaderExt {
13    /// Offset to the beginning of the extended block table, relative to the beginning of the archive.
14    extended_block_table_offset: i64,
15    /// High 16 bits of the hash table offset for large archives.
16    hash_table_offset_high: i16,
17    /// High 16 bits of the block table offset for large archives.
18    block_table_offset_high: i16,
19}
20
21impl MPQFileHeaderExt {
22    /// Parses all the fields in the expected order
23    pub fn parse(input: &[u8]) -> IResult<&[u8], MPQFileHeaderExt> {
24        let (input, extended_block_table_offset) = Self::parse_extended_block_table_offset(input)?;
25        let (input, hash_table_offset_high) = Self::parse_hash_table_offset_high(input)?;
26        let (input, block_table_offset_high) = Self::parse_block_table_offset_high(input)?;
27        Ok((
28            input,
29            MPQFileHeaderExt {
30                extended_block_table_offset,
31                hash_table_offset_high,
32                block_table_offset_high,
33            },
34        ))
35    }
36
37    /// `Offset 0x20`: int64 ExtendedBlockTableOffset
38    ///
39    /// Offset to the beginning of the extended block table, relative to the beginning of the archive.
40    pub fn parse_extended_block_table_offset(input: &[u8]) -> IResult<&[u8], i64> {
41        dbg_dmp(i64(LITTLE_ENDIAN), "extended_block_table_offset")(input)
42    }
43
44    /// `Offset 0x28`: int16 HashTableOffsetHigh
45    ///
46    /// High 16 bits of the hash table offset for large archives.
47    pub fn parse_hash_table_offset_high(input: &[u8]) -> IResult<&[u8], i16> {
48        dbg_dmp(i16(LITTLE_ENDIAN), "hash_table_offset_high")(input)
49    }
50
51    /// `Offset 0x2A`: int16 BlockTableOffsetHigh
52    ///
53    /// High 16 bits of the block table offset for large archives.
54    pub fn parse_block_table_offset_high(input: &[u8]) -> IResult<&[u8], i16> {
55        dbg_dmp(i16(LITTLE_ENDIAN), "block_table_offset_high")(input)
56    }
57}