From 675931d48484dcb815dd20ce12a9cc22afdb169f Mon Sep 17 00:00:00 2001 From: chinmaydd Date: Mon, 10 Jul 2017 22:30:55 +0530 Subject: [PATCH 1/3] This aims to make the following changes: * Remove all higher-level structs * Remove certain functions used to interact with the r2 instance and retrieve specific information This functionality aims to be moved to r2pipe.rs-frontend repository. --- src/lib.rs | 5 +- src/r2.rs | 142 ++----------------------------------------------- src/structs.rs | 127 ------------------------------------------- 3 files changed, 7 insertions(+), 267 deletions(-) delete mode 100644 src/structs.rs diff --git a/src/lib.rs b/src/lib.rs index f20a333..99a8d87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ -//! `R2Pipe` provides functions to interact with [radare2](http://rada.re/r/) +//! `R2Pipe` provides functions to interact with [radare2](http://rada.re/r/). +//! This aims to be a raw API. For more higher-level functions and structs to abstract +//! over the generated output, see [r2pipe.rs-frontend](). //! //! Hence this requires you to have radare2 installed on you system. For more //! information refer to the r2 [repository](https://github.com/radare/radare2). @@ -60,7 +62,6 @@ extern crate serde_derive; #[macro_use] pub mod r2pipe; pub mod r2; -pub mod structs; // Rexport to bring it out one module. pub use self::r2pipe::R2PipeSpawnOptions; diff --git a/src/r2.rs b/src/r2.rs index 255c622..1493eaf 100644 --- a/src/r2.rs +++ b/src/r2.rs @@ -5,64 +5,17 @@ // This file may not be copied, modified, or distributed // except according to those terms. -//! `R2` struct to make interaction with r2 easier +//! Few functions for initialization, communication and termination of r2. //! -//! This module is for convenience purposes. It provides a nicer way to -//! interact with r2 by -//! parsing the JSON into structs. Note that all commands are not supported and -//! this -//! module is updated only on a need basis. r2 commands that are not supported -//! by this module can -//! still be called by using the send() and recv() that `R2` provides. If this -//! is a command that -//! you see yourself using frequently and feel it is important to have nice -//! rust wrappers -//! feel free to raise an issue, or better yet a pull request implementing the -//! same. +//! If you wish to write wrappers for certain r2 functionalities, +//! contribute to the r2pipe.rs-frontend project. This aims to be a +//! barebones implementation of the pipe concept. use r2pipe::R2Pipe; use serde_json; use serde_json::Error; use serde_json::Value; -use super::structs::*; - -mod t_structs { - use structs::{FunctionInfo, LCallInfo}; - - #[derive(Debug, Clone, Default, Serialize, Deserialize)] - pub struct FunctionInfo_ { - pub callrefs: Option>, - pub calltype: Option, - pub codexrefs: Option>, - pub datarefs: Option>, - pub dataxrefs: Option>, - pub name: Option, - pub offset: Option, - pub realsz: Option, - pub size: Option, - pub ftype: Option, - } - - impl<'a> From<&'a FunctionInfo_> for FunctionInfo { - fn from(finfo: &'a FunctionInfo_) -> FunctionInfo { - FunctionInfo { - callrefs: finfo.callrefs.clone(), - calltype: finfo.calltype.clone(), - codexrefs: finfo.codexrefs.clone(), - datarefs: finfo.datarefs.clone(), - dataxrefs: finfo.dataxrefs.clone(), - name: finfo.name.clone(), - offset: finfo.offset.clone(), - realsz: finfo.realsz.clone(), - size: finfo.size.clone(), - ftype: finfo.ftype.clone(), - locals: None, - } - } - } -} - pub struct R2 { pipe: R2Pipe, readin: String, @@ -111,7 +64,6 @@ impl R2 { pub fn init(&mut self) { self.send("e asm.esil = true"); self.send("e scr.color = false"); - self.analyze(); } pub fn close(&mut self) { @@ -141,90 +93,4 @@ impl R2 { pub fn flush(&mut self) { self.readin = String::from(""); } - - pub fn analyze(&mut self) { - self.send("aa"); - self.flush(); - } - - pub fn function(&mut self, func: &str) -> Result { - let cmd = format!("pdfj @ {}", func); - self.send(&cmd); - let raw_json = self.recv(); - // Handle Error here. - serde_json::from_str(&raw_json) - } - - // get 'n' (or 16) instructions at 'offset' (or current position if offset in - // `None`) - pub fn insts(&mut self, n: Option, offset: Option<&str>) -> Result, Error> { - let n = n.unwrap_or(16); - let offset: &str = offset.unwrap_or_default(); - let mut cmd = format!("pdj{}", n); - if !offset.is_empty() { - cmd = format!("{} @ {}", cmd, offset); - } - self.send(&cmd); - let raw_json = self.recv(); - serde_json::from_str(&raw_json) - } - - pub fn reg_info(&mut self) -> Result { - self.send("drpj"); - let raw_json = self.recv(); - serde_json::from_str(&raw_json) - } - - pub fn flag_info(&mut self) -> Result, Error> { - self.send("fj"); - let raw_json = self.recv(); - serde_json::from_str(&raw_json) - } - - pub fn bin_info(&mut self) -> Result { - self.send("ij"); - let raw_json = self.recv(); - serde_json::from_str(&raw_json) - } - - pub fn fn_list(&mut self) -> Result, Error> { - self.send("aflj"); - let raw_json = self.recv(); - let mut finfo: Result, Error> = - serde_json::from_str::>(&raw_json) - .map(|x| x.iter().map(From::from).collect()); - if let Ok(ref mut fns) = finfo { - for f in fns.iter_mut() { - let res = self.locals_of(f.offset.unwrap()); - if res.is_ok() { - f.locals = res.ok(); - } else { - f.locals = Some(Vec::new()); - } - } - } - finfo - } - - pub fn sections(&mut self) -> Result, Error> { - self.send("Sj"); - serde_json::from_str(&self.recv()) - } - - pub fn strings(&mut self, data_only: bool) -> Result, Error> { - if data_only { - self.send("izj"); - serde_json::from_str(&self.recv()) - } else { - self.send("izzj"); - let x: Result, Error> = serde_json::from_str(&self.recv()); - x - } - } - - pub fn locals_of(&mut self, location: u64) -> Result, Error> { - self.send(&format!("afvbj @ {}", location)); - let x: Result, Error> = serde_json::from_str(&self.recv()); - x - } } diff --git a/src/structs.rs b/src/structs.rs deleted file mode 100644 index e75c7eb..0000000 --- a/src/structs.rs +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright (c) 2015, The Radare Project. All rights reserved. -// See the COPYING file at the top-level directory of this distribution. -// Licensed under the BSD 3-Clause License: -// -// This file may not be copied, modified, or distributed -// except according to those terms. - -//! Basic structs for JSON encoding and decoding. - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LOpInfo { - pub esil: Option, - pub offset: Option, - pub opcode: Option, - pub optype: Option, - pub size: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LFunctionInfo { - pub addr: Option, - pub name: Option, - pub ops: Option>, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LRegInfo { - pub alias_info: Vec, - pub reg_info: Vec, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LAliasInfo { - pub reg: String, - pub role: u64, - pub role_str: String, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LRegProfile { - pub name: String, - pub offset: usize, - pub size: usize, - pub type_str: String, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LFlagInfo { - pub offset: u64, - pub name: String, - pub size: u64, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LBinInfo { - pub core: Option, - pub bin: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LCoreInfo { - pub file: Option, - pub size: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LBin { - pub arch: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct FunctionInfo { - pub callrefs: Option>, - pub calltype: Option, - pub codexrefs: Option>, - pub datarefs: Option>, - pub dataxrefs: Option>, - pub name: Option, - pub offset: Option, - pub realsz: Option, - pub size: Option, - pub ftype: Option, - pub locals: Option>, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LCallInfo { - pub target: Option, - pub call_type: Option, - pub source: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LSectionInfo { - pub flags: Option, - pub name: Option, - pub paddr: Option, - pub size: Option, - pub vaddr: Option, - pub vsize: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LStringInfo { - pub length: Option, - pub ordinal: Option, - pub paddr: Option, - pub section: Option, - pub size: Option, - pub string: Option, - pub vaddr: Option, - pub stype: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LVarInfo { - pub name: Option, - pub kind: Option, - pub vtype: Option, - pub reference: Option, -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct LVarRef { - pub base: Option, - pub offset: Option, -} From 7815ac64ed6229dd7609eb25acf7e61b57c844f5 Mon Sep 17 00:00:00 2001 From: chinmaydd Date: Wed, 12 Jul 2017 00:44:05 +0530 Subject: [PATCH 2/3] Removes outdated test, bumps version --- Cargo.toml | 2 +- README.md | 4 ++++ examples/r2_example.rs | 16 ---------------- 3 files changed, 5 insertions(+), 17 deletions(-) delete mode 100644 examples/r2_example.rs diff --git a/Cargo.toml b/Cargo.toml index c69d202..ac4ad26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "r2pipe" -version = "0.4.0" +version = "0.5.0" authors = [ "pancake ", "sushant94 " diff --git a/README.md b/README.md index 1ca8495..d7cde48 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ The Rust Crate to interact with radare2. Please check [Documentation](https://radare.github.io/r2pipe.rs) to get started. +**NOTICE** + +Structures and API functions have been moved to [radare2-r2pipe-api](https://github.com/radare/radare2-r2pipe-api) +repository. Please make sure you update your dependencies and imports. TODO ---- diff --git a/examples/r2_example.rs b/examples/r2_example.rs deleted file mode 100644 index 635c7cd..0000000 --- a/examples/r2_example.rs +++ /dev/null @@ -1,16 +0,0 @@ -extern crate r2pipe; -extern crate serde_json; - -use r2pipe::r2::R2; - -fn main() { - let path = "/Users/sushant/projects/radare/rust/radeco-lib/ct1_sccp_ex.o"; - let mut r2 = R2::new(Some(path)).expect("Failed to spawn r2"); - r2.init(); - println!("{}", serde_json::to_string(&r2.fn_list().expect("Failed to get Function data")).expect("Serialize failed")); - //println!("{:#?}", r2.sections().expect("Failed to get section data")); - println!("{:#?}", r2.fn_list().expect("Failed to get function data")); - //println!("{:#?}", r2.flag_info().expect("Failed to get flag data")); - //println!("{:#?}", r2.strings(true).expect("Failed to get strings data")); - //println!("{:#?}", r2.strings(false).expect("Failed to get strings data")); -} From 95a791ea2b56107d11e89ccac545fdef1c210b8c Mon Sep 17 00:00:00 2001 From: chinmaydd Date: Thu, 13 Jul 2017 20:30:20 +0530 Subject: [PATCH 3/3] Removes init() --- src/r2.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/r2.rs b/src/r2.rs index 1493eaf..9dccc86 100644 --- a/src/r2.rs +++ b/src/r2.rs @@ -60,12 +60,6 @@ impl R2 { } } - // Does some basic configurations (sane defaults). - pub fn init(&mut self) { - self.send("e asm.esil = true"); - self.send("e scr.color = false"); - } - pub fn close(&mut self) { self.send("q!"); }