bhd: sort out basic nom parsing

This commit is contained in:
Dece 2020-04-13 01:17:05 +02:00
parent dec77551e2
commit 817c8491ef
2 changed files with 29 additions and 18 deletions

View file

@ -1,12 +1,14 @@
extern crate clap;
use std::fs::File;
use std::io::{BufReader, Error, Read};
//use std::path::Path;
use std::io::{Error, Read};
extern crate clap;
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
mod parsers {
pub mod bhd;
}
use parsers::*;
fn main() {
let matches = App::new("Rusted Iron Ring")
.setting(AppSettings::ArgRequiredElseHelp)
@ -34,11 +36,10 @@ fn cmd_bhd(args: &ArgMatches) -> Result::<(), Error> {
println!("File: {:?}", filepath);
println!("Output: {:?}", outputpath);
let bhd_data: File = File::open(filepath)?;
let mut bhd_reader = BufReader::new(bhd_data);
let mut magic: [u8; 4] = [0; 4];
bhd_reader.read(&mut magic)?;
let mut bhd_file: File = File::open(filepath)?;
let mut bhd_data = vec![0u8; bhd_file.metadata()?.len() as usize];
bhd_file.read_to_end(&mut bhd_data)?;
println!("First byte: 0x{:X}", magic[0]);
bhd::parse(&bhd_data);
Ok(())
}

View file

@ -1,13 +1,23 @@
extern crate nom;
use nom::{IResult, named, verify};
use nom::number::complete::*;
#[derive(Debug)]
pub struct BhdHeader {
pub magic: u8[4];
pub unk04: i8;
pub unk05: i8;
pub unk06: i8;
pub unk07: i8;
pub unk08: u32;
pub num_buckets: u32;
pub ofs_buckets: u32;
pub magic: u32,
// pub unk04: i8,
// pub unk05: i8,
// pub unk06: i8,
// pub unk07: i8,
// pub unk08: u32,
// pub num_buckets: u32,
// pub ofs_buckets: u32,
}
const MAGIC: u32 = 0x35444842;
named!(check_magic<&[u8], u32>, verify!(le_u32, |m| *m == MAGIC));
pub fn parse(i: &[u8]) -> IResult<&[u8], BhdHeader> {
let (i, magic) = check_magic(i)?;
Ok((i, BhdHeader { magic }))
}