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

implement an ihex/srec BinaryView #5832

Closed
wants to merge 11 commits into from
44 changes: 14 additions & 30 deletions rust/src/custombinaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ where
let data = BinaryView::from_raw(BNNewViewReference(data));

match view_type.load_settings_for_data(&data) {
Ok(settings) => Ref::into_raw(settings).handle,
_ => ptr::null_mut() as *mut _,
Some(settings) => Ref::into_raw(settings).handle,
None => ptr::null_mut() as *mut _,
}
})
}
Expand Down Expand Up @@ -156,28 +156,23 @@ where
pub trait BinaryViewTypeBase: AsRef<BinaryViewType> {
fn is_valid_for(&self, data: &BinaryView) -> bool;

fn is_deprecated(&self) -> bool;
fn is_deprecated(&self) -> bool {
false
}

fn default_load_settings_for_data(&self, data: &BinaryView) -> Result<Ref<Settings>> {
fn default_load_settings_for_data(&self, data: &BinaryView) -> Option<Ref<Settings>> {
let settings_handle =
unsafe { BNGetBinaryViewDefaultLoadSettingsForData(self.as_ref().0, data.handle) };

if settings_handle.is_null() {
Err(())
None
} else {
unsafe { Ok(Settings::from_raw(settings_handle)) }
unsafe { Some(Settings::from_raw(settings_handle)) }
}
}

fn load_settings_for_data(&self, data: &BinaryView) -> Result<Ref<Settings>> {
let settings_handle =
unsafe { BNGetBinaryViewLoadSettingsForData(self.as_ref().0, data.handle) };

if settings_handle.is_null() {
Err(())
} else {
unsafe { Ok(Settings::from_raw(settings_handle)) }
}

fn load_settings_for_data(&self, _data: &BinaryView) -> Option<Ref<Settings>> {
None
}
}

Expand Down Expand Up @@ -265,24 +260,13 @@ impl BinaryViewTypeBase for BinaryViewType {
unsafe { BNIsBinaryViewTypeDeprecated(self.0) }
}

fn default_load_settings_for_data(&self, data: &BinaryView) -> Result<Ref<Settings>> {
let settings_handle =
unsafe { BNGetBinaryViewDefaultLoadSettingsForData(self.0, data.handle) };

if settings_handle.is_null() {
Err(())
} else {
unsafe { Ok(Settings::from_raw(settings_handle)) }
}
}

fn load_settings_for_data(&self, data: &BinaryView) -> Result<Ref<Settings>> {
fn load_settings_for_data(&self, data: &BinaryView) -> Option<Ref<Settings>> {
let settings_handle = unsafe { BNGetBinaryViewLoadSettingsForData(self.0, data.handle) };

if settings_handle.is_null() {
Err(())
None
} else {
unsafe { Ok(Settings::from_raw(settings_handle)) }
unsafe { Some(Settings::from_raw(settings_handle)) }
}
}
}
Expand Down
88 changes: 88 additions & 0 deletions view/bintxt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)

project(view_bintext)

file(GLOB PLUGIN_SOURCES CONFIGURE_DEPENDS
${PROJECT_SOURCE_DIR}/Cargo.toml
${PROJECT_SOURCE_DIR}/src/*.rs)

file(GLOB_RECURSE API_SOURCES CONFIGURE_DEPENDS
${PROJECT_SOURCE_DIR}/../../binaryninjacore.h
${PROJECT_SOURCE_DIR}/../../rust/Cargo.toml
${PROJECT_SOURCE_DIR}/../../rust/src/*.rs)

if(CMAKE_BUILD_TYPE MATCHES Debug)
set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/debug)
set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target)
else()
set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/release)
set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target --release)
set(OUTPUT_PDB_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}view_bintxt.pdb)
endif()

set(OUTPUT_FILE ${CMAKE_STATIC_LIBRARY_PREFIX}view_bintxt${CMAKE_SHARED_LIBRARY_SUFFIX})
set(PLUGIN_PATH ${TARGET_DIR}/${OUTPUT_FILE})

add_custom_target(view_bintext ALL DEPENDS ${PLUGIN_PATH})
add_dependencies(view_bintext binaryninjaapi)

find_program(RUSTUP_PATH rustup REQUIRED HINTS ~/.cargo/bin)
if(CARGO_API_VERSION)
set(RUSTUP_COMMAND ${RUSTUP_PATH} run ${CARGO_API_VERSION} cargo build)
else()
set(RUSTUP_COMMAND ${RUSTUP_PATH} run ${CARGO_STABLE_VERSION} cargo build)
endif()

if(APPLE)
if(UNIVERSAL)
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/debug/${OUTPUT_FILE})
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE})
else()
set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/release/${OUTPUT_FILE})
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE})
endif()

add_custom_command(
OUTPUT ${PLUGIN_PATH}
COMMAND ${CMAKE_COMMAND} -E env
MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
${RUSTUP_COMMAND} --target=aarch64-apple-darwin ${CARGO_OPTS}
COMMAND ${CMAKE_COMMAND} -E env
MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BN_CORE_OUTPUT_DIR}
${RUSTUP_COMMAND} --target=x86_64-apple-darwin ${CARGO_OPTS}
COMMAND mkdir -p ${TARGET_DIR}
COMMAND lipo -create ${AARCH64_LIB_PATH} ${X86_64_LIB_PATH} -output ${PLUGIN_PATH}
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_PATH} ${BN_CORE_PLUGIN_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
else()
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(LIB_PATH ${PROJECT_BINARY_DIR}/target/debug/${OUTPUT_FILE})
else()
set(LIB_PATH ${PROJECT_BINARY_DIR}/target/release/${OUTPUT_FILE})
endif()

add_custom_command(
OUTPUT ${PLUGIN_PATH}
COMMAND ${CMAKE_COMMAND} -E env MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} ${RUSTUP_COMMAND} ${CARGO_OPTS}
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_PATH} ${BN_CORE_PLUGIN_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
endif()
elseif(WIN32)
add_custom_command(
OUTPUT ${PLUGIN_PATH}
COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} ${RUSTUP_COMMAND} ${CARGO_OPTS}
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_PATH} ${BN_CORE_PLUGIN_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_PDB_NAME} ${BN_CORE_PLUGIN_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
else()
add_custom_command(
OUTPUT ${PLUGIN_PATH}
COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} ${RUSTUP_COMMAND} ${CARGO_OPTS}
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_PATH} ${BN_CORE_PLUGIN_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
endif()
Loading
Loading