> {
let filename = filename.into_bytes_with_nul();
let options = "\x00";
let handle = unsafe {
binaryninjacore_sys::BNLoadFilename(
filename.as_ref().as_ptr() as *mut _,
true,
options.as_ptr() as *mut core::ffi::c_char,
None,
)
};
if handle.is_null() {
None
} else {
Some(unsafe { BinaryView::from_raw(handle) })
}
}
/// The main way to open and load files (with options) into Binary Ninja. Make sure you've properly initialized the core before calling this function. See [`crate::headless::init()`]
///
/// Strict JSON doesn't support single quotes for strings, so you'll need to either use a raw strings (f#"{"setting": "value"}"#) or escape double quotes ("{\"setting\": \"value\"}"). Or use serde_json::json.
///
/// ```no_run
/// # // Mock implementation of json! macro for documentation purposes
/// # macro_rules! json {
/// # ($($arg:tt)*) => {
/// # stringify!($($arg)*)
/// # };
/// # }
/// use binaryninja::{metadata::Metadata, rc::Ref};
/// use std::collections::HashMap;
///
/// let bv = binaryninja::load_with_options("/bin/cat", true, Some(json!("analysis.linearSweep.autorun": false).to_string()))
/// .expect("Couldn't open `/bin/cat`");
/// ```
pub fn load_with_options(
filename: S,
update_analysis_and_wait: bool,
options: Option,
) -> Option> {
let filename = filename.into_bytes_with_nul();
let options_or_default = if let Some(opt) = options {
opt.get_json_string()
.ok()?
.into_bytes_with_nul()
.as_ref()
.to_vec()
} else {
Metadata::new_of_type(MetadataType::KeyValueDataType)
.get_json_string()
.ok()?
.as_ref()
.to_vec()
};
let handle = unsafe {
binaryninjacore_sys::BNLoadFilename(
filename.as_ref().as_ptr() as *mut _,
update_analysis_and_wait,
options_or_default.as_ptr() as *mut core::ffi::c_char,
None,
)
};
if handle.is_null() {
None
} else {
Some(unsafe { BinaryView::from_raw(handle) })
}
}
pub fn load_view(
bv: &BinaryView,
update_analysis_and_wait: bool,
options: Option,
) -> Option> {
let options_or_default = if let Some(opt) = options {
opt.get_json_string()
.ok()?
.into_bytes_with_nul()
.as_ref()
.to_vec()
} else {
Metadata::new_of_type(MetadataType::KeyValueDataType)
.get_json_string()
.ok()?
.as_ref()
.to_vec()
};
let handle = unsafe {
binaryninjacore_sys::BNLoadBinaryView(
bv.handle as *mut _,
update_analysis_and_wait,
options_or_default.as_ptr() as *mut core::ffi::c_char,
None,
)
};
if handle.is_null() {
None
} else {
Some(unsafe { BinaryView::from_raw(handle) })
}
}
pub fn install_directory() -> Result {
let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetInstallDirectory() };
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn bundled_plugin_directory() -> Result {
let s: *mut std::os::raw::c_char =
unsafe { binaryninjacore_sys::BNGetBundledPluginDirectory() };
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn set_bundled_plugin_directory(new_dir: S) {
unsafe {
binaryninjacore_sys::BNSetBundledPluginDirectory(
new_dir.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char,
)
};
}
pub fn user_directory() -> Result {
let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetUserDirectory() };
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn user_plugin_directory() -> Result {
let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetUserPluginDirectory() };
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn repositories_directory() -> Result {
let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetRepositoriesDirectory() };
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn settings_file_name() -> Result {
let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetSettingsFileName() };
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn save_last_run() {
unsafe { binaryninjacore_sys::BNSaveLastRun() };
}
pub fn path_relative_to_bundled_plugin_directory(
path: S,
) -> Result {
let s: *mut std::os::raw::c_char = unsafe {
binaryninjacore_sys::BNGetPathRelativeToBundledPluginDirectory(
path.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char,
)
};
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn path_relative_to_user_plugin_directory(
path: S,
) -> Result {
let s: *mut std::os::raw::c_char = unsafe {
binaryninjacore_sys::BNGetPathRelativeToUserPluginDirectory(
path.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char,
)
};
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn path_relative_to_user_directory(path: S) -> Result {
let s: *mut std::os::raw::c_char = unsafe {
binaryninjacore_sys::BNGetPathRelativeToUserDirectory(
path.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char,
)
};
if s.is_null() {
return Err(());
}
Ok(PathBuf::from(
unsafe { string::BnString::from_raw(s) }.to_string(),
))
}
pub fn version() -> string::BnString {
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetVersionString()) }
}
pub fn build_id() -> u32 {
unsafe { binaryninjacore_sys::BNGetBuildId() }
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct VersionInfo {
pub major: u32,
pub minor: u32,
pub build: u32,
pub channel: string::BnString,
}
pub fn version_info() -> VersionInfo {
let info_raw = unsafe { binaryninjacore_sys::BNGetVersionInfo() };
VersionInfo {
major: info_raw.major,
minor: info_raw.minor,
build: info_raw.build,
channel: unsafe { string::BnString::from_raw(info_raw.channel) },
}
}
pub fn serial_number() -> string::BnString {
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetSerialNumber()) }
}
pub fn is_license_validated() -> bool {
unsafe { binaryninjacore_sys::BNIsLicenseValidated() }
}
pub fn licensed_user_email() -> string::BnString {
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetLicensedUserEmail()) }
}
pub fn license_count() -> i32 {
unsafe { binaryninjacore_sys::BNGetLicenseCount() }
}
pub fn set_license(license: S) {
let license = license.into_bytes_with_nul();
let license_slice = license.as_ref();
unsafe { binaryninjacore_sys::BNSetLicense(license_slice.as_ptr() as *const std::os::raw::c_char) }
}
pub fn product() -> string::BnString {
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetProduct()) }
}
pub fn product_type() -> string::BnString {
unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetProductType()) }
}
pub fn license_expiration_time() -> std::time::SystemTime {
let m = std::time::Duration::from_secs(unsafe {
binaryninjacore_sys::BNGetLicenseExpirationTime()
});
std::time::UNIX_EPOCH + m
}
pub fn is_ui_enabled() -> bool {
unsafe { binaryninjacore_sys::BNIsUIEnabled() }
}
pub fn is_database(filename: S) -> bool {
let filename = filename.into_bytes_with_nul();
let filename_slice = filename.as_ref();
unsafe { binaryninjacore_sys::BNIsDatabase(filename_slice.as_ptr() as *const std::os::raw::c_char) }
}
pub fn plugin_abi_version() -> u32 {
binaryninjacore_sys::BN_CURRENT_CORE_ABI_VERSION
}
pub fn plugin_abi_minimum_version() -> u32 {
binaryninjacore_sys::BN_MINIMUM_CORE_ABI_VERSION
}
pub fn core_abi_version() -> u32 {
unsafe { binaryninjacore_sys::BNGetCurrentCoreABIVersion() }
}
pub fn core_abi_minimum_version() -> u32 {
unsafe { binaryninjacore_sys::BNGetMinimumCoreABIVersion() }
}
pub fn plugin_ui_abi_version() -> u32 {
binaryninjacore_sys::BN_CURRENT_UI_ABI_VERSION
}
pub fn plugin_ui_abi_minimum_version() -> u32 {
binaryninjacore_sys::BN_MINIMUM_UI_ABI_VERSION
}
pub fn add_required_plugin_dependency(name: S) {
unsafe {
binaryninjacore_sys::BNAddRequiredPluginDependency(
name.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char,
)
};
}
pub fn add_optional_plugin_dependency(name: S) {
unsafe {
binaryninjacore_sys::BNAddOptionalPluginDependency(
name.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char,
)
};
}
// Provide ABI version automatically so that the core can verify binary compatibility
#[cfg(not(feature = "noexports"))]
#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn CorePluginABIVersion() -> u32 {
plugin_abi_version()
}
#[cfg(not(feature = "noexports"))]
#[no_mangle]
pub extern "C" fn UIPluginABIVersion() -> u32 {
plugin_ui_abi_version()
}