From 9be148783d4797986a0a666ca84566bca0ed5628 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sun, 3 May 2020 20:09:58 -0700 Subject: =?UTF-8?q?=E2=94=8F=E2=94=93=20=E2=94=83=E2=94=83=E2=95=B1?= =?UTF-8?q?=E2=95=B2=20in=20this=20=E2=94=83=E2=95=B1=E2=95=B1=E2=95=B2?= =?UTF-8?q?=E2=95=B2=20=20house=20=E2=95=B1=E2=95=B1=E2=95=AD=E2=95=AE?= =?UTF-8?q?=E2=95=B2=E2=95=B2=20=20we=20=E2=96=94=E2=96=8F=E2=94=97?= =?UTF-8?q?=E2=94=9B=E2=96=95=E2=96=94=20=20=20=20run=20=E2=95=B1=E2=96=94?= =?UTF-8?q?=E2=96=94=E2=96=94=E2=96=94=E2=96=94=E2=96=94=E2=96=94=E2=96=94?= =?UTF-8?q?=E2=96=94=E2=96=94=E2=95=B2=20=20=20cargo=20fmt=20=E2=95=B1?= =?UTF-8?q?=E2=95=B1=E2=94=8F=E2=94=B3=E2=94=93=E2=95=AD=E2=95=AE=E2=94=8F?= =?UTF-8?q?=E2=94=B3=E2=94=93=20=E2=95=B2=E2=95=B2=20=E2=96=94=E2=96=8F?= =?UTF-8?q?=E2=94=97=E2=94=BB=E2=94=9B=E2=94=83=E2=94=83=E2=94=97=E2=94=BB?= =?UTF-8?q?=E2=94=9B=E2=96=95=E2=96=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 102 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index 97ec7e8..d194a50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,52 +10,58 @@ use std::io::Read; fn main() { let _ = include_str!("../Cargo.toml"); let app = app_from_crate!() - .arg(Arg::with_name("arch") - .short("a") - .long("--architecture") - .takes_value(true) - .possible_values(&["x86_64", "x86:32", "armv7", "armv8", "avr", "mips", "msp430", "pic17", "pic18", "m16c"]) - .help("architecture to disassemble input as.")) - .arg(Arg::with_name("file") - .short("f") - .long("file") - .takes_value(true) - .help("file of bytes to decode")) - .arg(Arg::with_name("verbose") - .short("v") - .long("--verbose") - .help("increased detail when decoding instructions")) - .arg(Arg::with_name("data") - .help("hex bytes to decode by the selected architecture. for example, try -a x86_64 33c0c3")); + .arg( + Arg::with_name("arch") + .short("a") + .long("--architecture") + .takes_value(true) + .possible_values(&[ + "x86_64", "x86:32", "armv7", "armv8", "avr", "mips", "msp430", "pic17", + "pic18", "m16c", + ]) + .help("architecture to disassemble input as."), + ) + .arg( + Arg::with_name("file") + .short("f") + .long("file") + .takes_value(true) + .help("file of bytes to decode"), + ) + .arg( + Arg::with_name("verbose") + .short("v") + .long("--verbose") + .help("increased detail when decoding instructions"), + ) + .arg(Arg::with_name("data").help( + "hex bytes to decode by the selected architecture. for example, try -a x86_64 33c0c3", + )); let matches = app.get_matches(); let arch_str = matches.value_of("arch").unwrap_or("x86_64"); let buf: Vec = match matches.value_of("data") { - Some(data) => { - match hex::decode(data) { - Ok(buf) => buf, - Err(e) => { - eprintln!("Invalid input, {}. Expected a sequence of bytes as hex", e); - return; - } + Some(data) => match hex::decode(data) { + Ok(buf) => buf, + Err(e) => { + eprintln!("Invalid input, {}. Expected a sequence of bytes as hex", e); + return; } - } + }, None => { let mut v = Vec::new(); match matches.value_of("file") { - Some(name) => { - match File::open(name) { - Ok(mut f) => { - f.read_to_end(&mut v).expect("can read the file"); - v - } - Err(e) => { - eprintln!("error opening {}: {}", name, e); - return; - } + Some(name) => match File::open(name) { + Ok(mut f) => { + f.read_to_end(&mut v).expect("can read the file"); + v } - } + Err(e) => { + eprintln!("error opening {}: {}", name, e); + return; + } + }, None => { eprintln!("data must be provided by either an argument consisting of hex bytes, or by the --file argument."); return; @@ -76,21 +82,32 @@ fn main() { "pic17" => decode_input::(&buf, verbose), "pic18" => decode_input::(&buf, verbose), "m16c" => decode_input::(&buf, verbose), -// "pic24" => decode_input::(buf), + // "pic24" => decode_input::(buf), other => { println!("unsupported architecture: {}", other); } } } -fn decode_input(buf: &[u8], verbose: bool) where A::Instruction: fmt::Display { +fn decode_input(buf: &[u8], verbose: bool) +where + A::Instruction: fmt::Display, +{ let decoder = A::Decoder::default(); let start = A::Address::zero(); let mut addr = start; loop { match decoder.decode(buf[addr.to_linear()..].iter().cloned()) { Ok(inst) => { - println!("{:#010x}: {:14}: {}", addr.to_linear(), hex::encode(&buf[addr.to_linear()..][..A::Address::zero().wrapping_offset(inst.len()).to_linear()]), inst); + println!( + "{:#010x}: {:14}: {}", + addr.to_linear(), + hex::encode( + &buf[addr.to_linear()..] + [..A::Address::zero().wrapping_offset(inst.len()).to_linear()] + ), + inst + ); if verbose { println!(" {:?}", inst); if !inst.well_defined() { @@ -98,8 +115,11 @@ fn decode_input(buf: &[u8], verbose: bool) where A::Instruction: fmt::D } } addr += inst.len(); - }, - Err(e) => { println!("{:#010x}: {}", addr.to_linear(), e); addr += A::Instruction::min_size(); }, + } + Err(e) => { + println!("{:#010x}: {}", addr.to_linear(), e); + addr += A::Instruction::min_size(); + } } if addr.to_linear() >= buf.len() { break; -- cgit v1.1