diff options
| author | iximeow <me@iximeow.net> | 2020-01-12 17:08:45 -0800 | 
|---|---|---|
| committer | iximeow <me@iximeow.net> | 2020-01-12 17:08:45 -0800 | 
| commit | 9a1d438b6bfc38bfef1841786c03032143cad8b9 (patch) | |
| tree | ad041301a5fe94a1f1812f254aea704bb1c8f6d6 | |
| parent | 8734e0cea4778fbdc9f93a70f86e709b14ce2fb3 (diff) | |
| parent | 274fdaf33a7debb32e95ab57a41f9c03b173e164 (diff) | |
Merge remote-tracking branch 'master'
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/display.rs | 66 | ||||
| -rw-r--r-- | src/lib.rs | 1 | ||||
| -rw-r--r-- | test/test.rs | 8 | 
5 files changed, 51 insertions, 27 deletions
| diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c96eb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock @@ -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 <T: std::fmt::Write> ShowContextual<<MSP430 as Arch>::Address, [Option<String>], T> for Instruction { +    fn contextualize(&self, colors: Option<&ColorSettings>, address: <MSP430 as Arch>::Address, context: Option<&[Option<String>]>, 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 <T: std::fmt::Write> Colorize<T> 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, "<No Operand>") +                write!(out, "<No Operand>")              }          }      }  } - @@ -1,4 +1,5 @@  extern crate yaxpeax_arch; +extern crate termion;  use yaxpeax_arch::{Arch, Decodable, LengthedInstruction}; diff --git a/test/test.rs b/test/test.rs index a917ba9..48cf90b 100644 --- a/test/test.rs +++ b/test/test.rs @@ -8,21 +8,21 @@ use yaxpeax_msp430_mc::{Instruction, Opcode};  fn test_decode() {      let data = [0x02, 0x12];      let mut instr = Instruction::blank(); -    instr.decode_into(&data); +    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); +    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); +    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); +    instr.decode_into(data.iter().map(|x| *x));      assert!(instr.opcode == Opcode::PUSH);  } | 
