use failure::Error;
use format::elf::{
Elf32,
Elf64,
parse_elf,
};
use nom::{
*,
Needed::*,
IResult::*,
};
use error::RustepErrorKind;
use num::FromPrimitive;
pub enum Executable<'a> {
Elf32(Elf32<'a>),
Elf64(Elf64<'a>),
}
#[derive(FromPrimitive, ToPrimitive, Eq, PartialEq)]
enum ExecutableFormat {
Elf = 0x464c457f,
Pe = 0x4550,
Mach32 = 0xfeedface,
Mach64 = 0xfeedfacf,
}
impl<'a> Executable<'a> {
pub fn from_u8_array(input: &'a [u8]) -> Result<Executable<'a>, Error> {
println!("{:?}", nom_try!(
alt!(input, tag!("\x7fELF") | tag!("PE\x00\x00")))
);
let res = nom_try!(
call!(input, le_u32)
);
let format: ExecutableFormat = FromPrimitive::from_u32(res).unwrap();
match format {
ExecutableFormat::Elf => parse_elf(input),
_ => panic!("File format other than ELF is not yet supported"),
}
}
}
#[test]
fn test_executable() {
use std::{
fs::File,
io::prelude::*,
};
let mut file = File::open("test/test").unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
match Executable::from_u8_array(&buf).unwrap() {
Executable::Elf64(_elf) => {},
_ => { panic!("Wrong file format detection") }
}
}