From e380a481b8f129b50ee49be818ad889bedcd3b2f Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 20 Mar 2019 03:14:39 -0700 Subject: update and impl new display-related triats --- Cargo.toml | 1 + src/display.rs | 66 ++++++++++++++++++++++++++++++++++++++-------------------- src/lib.rs | 1 + 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 58e3a3c..384bae6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ A rust msp430 decoder (specifically the microcorruption flavor) [dependencies] yaxpeax-arch = { path = "../../yaxpeax-arch" } +"termion" = "1.4.0" [[test]] name = "test" diff --git a/src/display.rs b/src/display.rs index 7864248..b51ae9b 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,25 +1,38 @@ -use std; -use std::fmt::{Display, Formatter}; +use ::{MSP430, Operand, Opcode, Instruction, Width}; -use ::{Operand, Opcode, Instruction, Width}; +use std::collections::HashMap; +use std::fmt::{Display, Formatter}; +use std::hash::Hash; +use std::rc::Rc; +use std; +use termion::color; +use yaxpeax_arch::{Arch, Colorize, ColorSettings, LengthedInstruction, ShowContextual}; impl Display for Instruction { fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { - write!(f, "{}", self.opcode)?; + let mut s = String::new(); + self.contextualize(None, 0, None, &mut s).unwrap(); + write!(f, "{}", s) + } +} + +impl ShowContextual<::Address, [Option], T> for Instruction { + fn contextualize(&self, colors: Option<&ColorSettings>, address: ::Address, context: Option<&[Option]>, out: &mut T) -> std::fmt::Result { + write!(out, "{}", self.opcode)?; match self.op_width { - Width::B => { write!(f, ".b")? }, + Width::B => { write!(out, ".b")? }, Width::W => { } }; match self.operands[0] { Operand::Nothing => return Ok(()), x @ _ => { - write!(f, " {}", x)?; + write!(out, " {}", x)?; } }; match self.operands[1] { Operand::Nothing => return Ok(()), x @ _ => { - write!(f, ", {}", x)?; + write!(out, ", {}", x)?; } }; Ok(()) @@ -64,6 +77,14 @@ impl Display for Opcode { impl Display for Operand { fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { + let mut s = String::new(); + self.colorize(None, &mut s).unwrap(); + write!(f, "{}", s) + } +} + +impl Colorize for Operand { + fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result { fn signed_hex(num: i16) -> String { if num >= 0 { format!("+{:#x}", num) @@ -73,51 +94,50 @@ impl Display for Operand { } match self { Operand::Register(reg) => { - write!(f, "R{}", reg) + write!(out, "R{}", reg) }, Operand::Indexed(reg, offset) => { - write!(f, "{}(R{})", signed_hex(*offset as i16), reg) + write!(out, "{}(R{})", signed_hex(*offset as i16), reg) }, Operand::RegisterIndirect(reg) => { - write!(f, "@R{}", reg) + write!(out, "@R{}", reg) }, Operand::IndirectAutoinc(reg) => { - write!(f, "@R{}+", reg) + write!(out, "@R{}+", reg) }, Operand::Offset(offset) => { - write!(f, "${}", signed_hex(*offset as i16)) + write!(out, "${}", signed_hex(*offset as i16)) }, Operand::Symbolic(offset) => { - write!(f, "{}(PC)", signed_hex(*offset as i16)) + write!(out, "{}(PC)", signed_hex(*offset as i16)) }, Operand::Immediate(imm) => { - write!(f, "#0x{:x}", imm) + write!(out, "#0x{:x}", imm) }, Operand::Absolute(offset) => { - write!(f, "&0x{:x}", offset) + write!(out, "&0x{:x}", offset) }, Operand::Const4 => { - write!(f, "4") + write!(out, "4") }, Operand::Const8 => { - write!(f, "8") + write!(out, "8") }, Operand::Const0 => { - write!(f, "0") + write!(out, "0") }, Operand::Const1 => { - write!(f, "1") + write!(out, "1") }, Operand::Const2 => { - write!(f, "2") + write!(out, "2") }, Operand::ConstNeg1 => { - write!(f, "-1") + write!(out, "-1") }, Operand::Nothing => { - write!(f, "") + write!(out, "") } } } } - diff --git a/src/lib.rs b/src/lib.rs index c215b68..81ceceb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ extern crate yaxpeax_arch; +extern crate termion; use yaxpeax_arch::{Arch, Decodable, LengthedInstruction}; -- cgit v1.1 From 091373f8077053f0ccdf7fa6e70b9e33db802cc8 Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 20 Mar 2019 14:43:05 -0700 Subject: these tests existed but i forgot to commit them before, oops --- test/test.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/test.rs diff --git a/test/test.rs b/test/test.rs new file mode 100644 index 0000000..48cf90b --- /dev/null +++ b/test/test.rs @@ -0,0 +1,28 @@ +extern crate yaxpeax_arch; +extern crate yaxpeax_msp430_mc; + +use yaxpeax_arch::Decodable; +use yaxpeax_msp430_mc::{Instruction, Opcode}; + +#[test] +fn test_decode() { + let data = [0x02, 0x12]; + let mut instr = Instruction::blank(); + instr.decode_into(data.iter().map(|x| *x)); + assert!(instr.opcode == Opcode::PUSH); + + let data = [0xb1, 0x92, 0x8d, 0x49]; + let mut instr = Instruction::blank(); + instr.decode_into(data.iter().map(|x| *x)); + assert!(instr.opcode == Opcode::CMP); + + let data = [0x12, 0x00, 0x3f, 0x40]; + let mut instr = Instruction::blank(); + instr.decode_into(data.iter().map(|x| *x)); + assert!(instr.opcode == Opcode::RRC); + + let data = [0x20, 0x0e]; + let mut instr = Instruction::blank(); + instr.decode_into(data.iter().map(|x| *x)); + assert!(instr.opcode == Opcode::PUSH); +} -- cgit v1.1 From 274fdaf33a7debb32e95ab57a41f9c03b173e164 Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 20 Mar 2019 14:54:22 -0700 Subject: add gitignore, oops --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c96eb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock -- cgit v1.1