Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes higher level API functions from r2pipe.rs #23

Merged
merged 3 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "r2pipe"
version = "0.4.0"
version = "0.5.0"
authors = [
"pancake <pancake@nopcode.org>",
"sushant94 <sushant.dinesh94@gmail.com>"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand Down
16 changes: 0 additions & 16 deletions examples/r2_example.rs

This file was deleted.

5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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;
Expand Down
148 changes: 4 additions & 144 deletions src/r2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<LCallInfo>>,
pub calltype: Option<String>,
pub codexrefs: Option<Vec<LCallInfo>>,
pub datarefs: Option<Vec<u64>>,
pub dataxrefs: Option<Vec<u64>>,
pub name: Option<String>,
pub offset: Option<u64>,
pub realsz: Option<u64>,
pub size: Option<u64>,
pub ftype: Option<String>,
}

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,
Expand Down Expand Up @@ -107,13 +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");
self.analyze();
}

pub fn close(&mut self) {
self.send("q!");
}
Expand Down Expand Up @@ -141,90 +87,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<LFunctionInfo, Error> {
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<u64>, offset: Option<&str>) -> Result<Vec<LOpInfo>, 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<LRegInfo, Error> {
self.send("drpj");
let raw_json = self.recv();
serde_json::from_str(&raw_json)
}

pub fn flag_info(&mut self) -> Result<Vec<LFlagInfo>, Error> {
self.send("fj");
let raw_json = self.recv();
serde_json::from_str(&raw_json)
}

pub fn bin_info(&mut self) -> Result<LBinInfo, Error> {
self.send("ij");
let raw_json = self.recv();
serde_json::from_str(&raw_json)
}

pub fn fn_list(&mut self) -> Result<Vec<FunctionInfo>, Error> {
self.send("aflj");
let raw_json = self.recv();
let mut finfo: Result<Vec<FunctionInfo>, Error> =
serde_json::from_str::<Vec<t_structs::FunctionInfo_>>(&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<Vec<LSectionInfo>, Error> {
self.send("Sj");
serde_json::from_str(&self.recv())
}

pub fn strings(&mut self, data_only: bool) -> Result<Vec<LStringInfo>, Error> {
if data_only {
self.send("izj");
serde_json::from_str(&self.recv())
} else {
self.send("izzj");
let x: Result<Vec<LStringInfo>, Error> = serde_json::from_str(&self.recv());
x
}
}

pub fn locals_of(&mut self, location: u64) -> Result<Vec<LVarInfo>, Error> {
self.send(&format!("afvbj @ {}", location));
let x: Result<Vec<LVarInfo>, Error> = serde_json::from_str(&self.recv());
x
}
}
127 changes: 0 additions & 127 deletions src/structs.rs

This file was deleted.