-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `StringList` and `StringIndex` and associated methods * Add `address_to_string` wrapper for `ea2str`
- Loading branch information
Showing
7 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "strlist.hpp" | ||
|
||
#include "cxx.h" | ||
|
||
ea_t idalib_get_strlist_item_addr(size_t n) { | ||
string_info_t si; | ||
get_strlist_item(&si, n); | ||
return si.ea; | ||
} | ||
|
||
size_t idalib_get_strlist_item_length(size_t n) { | ||
string_info_t si; | ||
get_strlist_item(&si, n); | ||
return (size_t)si.length; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use idalib::idb::IDB; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
println!("Trying to open IDA database..."); | ||
|
||
// Open IDA database | ||
let idb = IDB::open("./tests/ls")?; | ||
|
||
println!("Testing len(), get_by_index(), and get_address_by_index() (valid indexes)"); | ||
// len() | ||
for i in 0..idb.strings().len() { | ||
/* | ||
println!( | ||
"\t{:#x}\t{:?}", | ||
idb.strings().get_address_by_index(i).unwrap(), | ||
idb.strings().get_by_index(i).unwrap() | ||
); | ||
*/ | ||
// get_by_index() | ||
assert!(idb.strings().get_by_index(i).is_some()); | ||
// get_address_by_index() | ||
assert!(idb.strings().get_address_by_index(i).is_some()); | ||
} | ||
|
||
println!("Testing len(), get_by_index(), and get_address_by_index() (invalid index)"); | ||
// len() | ||
let len = idb.strings().len(); | ||
// get_by_index() | ||
assert!(idb.strings().get_by_index(len).is_none()); | ||
// get_address_by_index() | ||
assert!(idb.strings().get_address_by_index(len).is_none()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::marker::PhantomData; | ||
|
||
use crate::ffi::bytes::idalib_get_bytes; | ||
use crate::ffi::strings::{ | ||
build_strlist, clear_strlist, get_strlist_qty, idalib_get_strlist_item_addr, | ||
idalib_get_strlist_item_length, | ||
}; | ||
use crate::ffi::BADADDR; | ||
|
||
use crate::idb::IDB; | ||
use crate::Address; | ||
|
||
pub type StringIndex = usize; | ||
|
||
pub struct StringList<'a> { | ||
_marker: PhantomData<&'a IDB>, | ||
} | ||
|
||
impl<'a> StringList<'a> { | ||
pub(crate) fn new(_: &'a IDB) -> Self { | ||
Self { | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
pub fn rebuild(&self) { | ||
unsafe { build_strlist() } | ||
} | ||
|
||
pub fn clear(&self) { | ||
unsafe { clear_strlist() } | ||
} | ||
|
||
pub fn get_by_index(&self, index: StringIndex) -> Option<String> { | ||
let addr = self.get_address_by_index(index)?; | ||
let size = self.get_length_by_index(index); | ||
|
||
// See also `IDB::get_bytes` | ||
let mut buf = Vec::with_capacity(size); | ||
let Ok(new_len) = (unsafe { idalib_get_bytes(addr.into(), &mut buf) }) else { | ||
return None; | ||
}; | ||
unsafe { | ||
buf.set_len(new_len); | ||
} | ||
|
||
// TODO: switch to `String::from_utf8_lossy_owned` once it's stable | ||
Some(String::from_utf8_lossy(&buf).into_owned()) | ||
} | ||
|
||
pub fn get_address_by_index(&self, index: StringIndex) -> Option<Address> { | ||
let addr = unsafe { idalib_get_strlist_item_addr(index) }; | ||
if addr == BADADDR { | ||
None | ||
} else { | ||
Some(addr.into()) | ||
} | ||
} | ||
|
||
fn get_length_by_index(&self, index: StringIndex) -> usize { | ||
unsafe { idalib_get_strlist_item_length(index) } | ||
} | ||
|
||
pub fn len(&self) -> StringIndex { | ||
unsafe { get_strlist_qty() } | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
} |