From 041ef2605f5de35e268bd0f82d9a6240a9a58e61 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 3 Jul 2021 19:36:15 -0700 Subject: add a Reader type that can read architecture-defined words this is useful for instruction sets like arm where instructions are guaranteed to be 4 bytes, as well as instruction sets where an instruction word is not a multiple of u8 bytes. --- src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 89b3579..1862993 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,8 @@ pub use color::{Colorize, NoColors, YaxColors}; pub use color::ColorSettings; pub mod display; +mod reader; +pub use reader::{Reader, ReadError, U8Reader}; //, U16le, U16be, U32le, U32be, U64le, U64be}; pub trait DecodeError { fn data_exhausted(&self) -> bool; @@ -61,12 +63,13 @@ impl DecodeError for StandardDecodeError { fn bad_operand(&self) -> bool { *self == StandardDecodeError::InvalidOperand } } - fn decode>(&self, bytes: &mut T) -> Result { +pub trait Decoder { + fn decode>(&self, words: &mut T) -> Result { let mut inst = A::Instruction::default(); - self.decode_into(&mut inst, bytes).map(|_: ()| inst) + self.decode_into(&mut inst, words).map(|_: ()| inst) } - fn decode_into>(&self, inst: &mut A::Instruction, bytes: &mut T) -> Result<(), Self::Error>; + fn decode_into>(&self, inst: &mut A::Instruction, words: &mut T) -> Result<(), A::DecodeError>; } #[cfg(feature="use-serde")] @@ -75,7 +78,7 @@ pub trait Arch { type Address: Address + Debug + Hash + PartialEq + Eq + Serialize + for<'de> Deserialize<'de>; type Instruction: Instruction + LengthedInstruction> + Debug + Default + Sized; type DecodeError: DecodeError + Debug + Display; - type Decoder: Decoder + Default; + type Decoder: Decoder + Default; type Operand; } @@ -85,7 +88,7 @@ pub trait Arch { type Address: Address + Debug + Hash + PartialEq + Eq; type Instruction: Instruction + LengthedInstruction> + Debug + Default + Sized; type DecodeError: DecodeError + Debug + Display; - type Decoder: Decoder + Default; + type Decoder: Decoder + Default; type Operand; } -- cgit v1.1