Skip to content

Commit

Permalink
Feature/bookmarks get address (#17)
Browse files Browse the repository at this point in the history
* Add `bookmarks().get_address()` and update tests
  • Loading branch information
0xdea authored Nov 16, 2024
1 parent 8dc31a7 commit edf4d01
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
20 changes: 20 additions & 0 deletions idalib-sys/src/bookmarks_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ rust::String idalib_bookmarks_t_get_desc(uint32 index) {
}
}

ea_t idalib_bookmarks_t_get(uint32 index) {
auto desc = qstring();
ea_t ea = 0;

idaplace_t ipl(ea, 0);
renderer_info_t rinfo;
rinfo.rtype = TCCRT_FLAT;
rinfo.pos.cx = 0;
rinfo.pos.cy = 5;
lochist_entry_t e(&ipl, rinfo);

lochist_entry_t loc(e);

if (bookmarks_t_get(&loc, &desc, &index, nullptr) != 0) {
return loc.place()->toea();
} else {
return BADADDR;
}
}

bool idalib_bookmarks_t_erase(uint32 index) {
ea_t ea = 0;

Expand Down
5 changes: 3 additions & 2 deletions idalib-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ mod ffix {
desc: *const c_char,
) -> u32;
unsafe fn idalib_bookmarks_t_get_desc(index: c_uint) -> String;
unsafe fn idalib_bookmarks_t_get(index: c_uint) -> c_ulonglong;
unsafe fn idalib_bookmarks_t_erase(index: c_uint) -> bool;
unsafe fn idalib_bookmarks_t_size() -> u32;
unsafe fn idalib_bookmarks_t_find_index(ea: c_ulonglong) -> u32;
Expand Down Expand Up @@ -929,8 +930,8 @@ pub mod comments {

pub mod bookmarks {
pub use super::ffix::{
idalib_bookmarks_t_erase, idalib_bookmarks_t_find_index, idalib_bookmarks_t_get_desc,
idalib_bookmarks_t_mark, idalib_bookmarks_t_size,
idalib_bookmarks_t_erase, idalib_bookmarks_t_find_index, idalib_bookmarks_t_get,
idalib_bookmarks_t_get_desc, idalib_bookmarks_t_mark, idalib_bookmarks_t_size,
};
}

Expand Down
16 changes: 14 additions & 2 deletions idalib/examples/bookmarks_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ fn main() -> anyhow::Result<()> {
for (id, f) in idb.functions() {
let addr = f.start_address();
let desc = format!(
"Bookmark added by idalib: {id} {} {:#x}",
"Bookmark added by idalib: {id} {} {addr:#x}",
f.name().unwrap(),
addr
);

// mark()
Expand All @@ -38,6 +37,19 @@ fn main() -> anyhow::Result<()> {
assert_eq!(read_desc.unwrap(), desc);
}

println!("Testing len(), get_address(), and get_description()");
// len()
for i in 0..idb.bookmarks().len() {
// get_address()
let read_addr = idb.bookmarks().get_address(i).unwrap();
let addr_str = format!("{read_addr:#x}");

// get_description()
let read_desc = idb.bookmarks().get_description(read_addr).unwrap();

assert!(read_desc.ends_with(addr_str.as_str()));
}

println!("Testing erase(), get_description(), and len() (pass 2)");
for (_id, f) in idb.functions() {
let addr = f.start_address();
Expand Down
5 changes: 2 additions & 3 deletions idalib/examples/comments_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ fn main() -> anyhow::Result<()> {
for (id, f) in idb.functions() {
let addr = f.start_address();
let comment = format!(
"Comment added by idalib: {id} {} {:#x}",
f.name().unwrap(),
addr
"Comment added by idalib: {id} {} {addr:#x}",
f.name().unwrap()
);

// set_cmt()
Expand Down
16 changes: 14 additions & 2 deletions idalib/src/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::ffi::CString;
use std::marker::PhantomData;

use crate::ffi::bookmarks::{
idalib_bookmarks_t_erase, idalib_bookmarks_t_find_index, idalib_bookmarks_t_get_desc,
idalib_bookmarks_t_mark, idalib_bookmarks_t_size,
idalib_bookmarks_t_erase, idalib_bookmarks_t_find_index, idalib_bookmarks_t_get,
idalib_bookmarks_t_get_desc, idalib_bookmarks_t_mark, idalib_bookmarks_t_size,
};
use crate::ffi::BADADDR;

use crate::idb::IDB;
use crate::{Address, IDAError};
Expand Down Expand Up @@ -57,6 +58,17 @@ impl<'a> Bookmarks<'a> {
self.get_description_by_index(self.find_index(ea)?)
}

// Note: The `bookmarks_t::get` function is used here only to get the address
pub fn get_address(&self, idx: BookmarkIndex) -> Option<Address> {
let addr = unsafe { idalib_bookmarks_t_get(idx.into()) };

if addr == BADADDR {
None
} else {
Some(addr.into())
}
}

// Note: The address parameter has been removed because it is unused by IDA's API
pub fn get_description_by_index(&self, idx: BookmarkIndex) -> Option<String> {
let s = unsafe { idalib_bookmarks_t_get_desc(idx.into()) };
Expand Down

0 comments on commit edf4d01

Please sign in to comment.