Skip to content

Commit 2177f3a

Browse files
rbranemesare
authored andcommitted
Implement LanguageRepresentation and LineFormatter in Rust API
1 parent eb8d1ce commit 2177f3a

File tree

13 files changed

+1468
-6
lines changed

13 files changed

+1468
-6
lines changed

rust/src/function.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub use binaryninjacore_sys::BNHighlightStandardColor as HighlightStandardColor;
4040
use crate::architecture::RegisterId;
4141
use crate::confidence::Conf;
4242
use crate::high_level_il::HighLevelILFunction;
43+
use crate::language_representation::CoreLanguageRepresentationFunction;
4344
use crate::low_level_il::{LiftedILFunction, RegularLowLevelILFunction};
4445
use crate::medium_level_il::MediumLevelILFunction;
4546
use crate::variable::{
@@ -476,6 +477,42 @@ impl Function {
476477
}
477478
}
478479

480+
/// Get the language representation of the function.
481+
///
482+
/// * `language` - The language representation, ex. "Pseudo C".
483+
pub fn language_representation<S: BnStrCompatible>(
484+
&self,
485+
language: S,
486+
) -> Option<Ref<CoreLanguageRepresentationFunction>> {
487+
let lang_name = language.into_bytes_with_nul();
488+
let repr = unsafe {
489+
BNGetFunctionLanguageRepresentation(
490+
self.handle,
491+
lang_name.as_ref().as_ptr() as *const c_char,
492+
)
493+
};
494+
NonNull::new(repr)
495+
.map(|handle| unsafe { CoreLanguageRepresentationFunction::ref_from_raw(handle) })
496+
}
497+
498+
/// Get the language representation of the function, if available.
499+
///
500+
/// * `language` - The language representation, ex. "Pseudo C".
501+
pub fn language_representation_if_available<S: BnStrCompatible>(
502+
&self,
503+
language: S,
504+
) -> Option<Ref<CoreLanguageRepresentationFunction>> {
505+
let lang_name = language.into_bytes_with_nul();
506+
let repr = unsafe {
507+
BNGetFunctionLanguageRepresentationIfAvailable(
508+
self.handle,
509+
lang_name.as_ref().as_ptr() as *const c_char,
510+
)
511+
};
512+
NonNull::new(repr)
513+
.map(|handle| unsafe { CoreLanguageRepresentationFunction::ref_from_raw(handle) })
514+
}
515+
479516
pub fn high_level_il(&self, full_ast: bool) -> Result<Ref<HighLevelILFunction>, ()> {
480517
unsafe {
481518
let hlil_ptr = BNGetFunctionHighLevelIL(self.handle);

rust/src/high_level_il.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod function;
44
mod instruction;
55
mod lift;
66
pub mod operation;
7+
pub mod token_emitter;
78

89
pub use self::block::*;
910
pub use self::function::*;

rust/src/high_level_il/function.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ pub struct HighLevelILFunction {
1515
}
1616

1717
impl HighLevelILFunction {
18+
pub(crate) unsafe fn from_raw(handle: *mut BNHighLevelILFunction, full_ast: bool) -> Self {
19+
debug_assert!(!handle.is_null());
20+
Self { handle, full_ast }
21+
}
22+
1823
pub(crate) unsafe fn ref_from_raw(
1924
handle: *mut BNHighLevelILFunction,
2025
full_ast: bool,
2126
) -> Ref<Self> {
2227
debug_assert!(!handle.is_null());
23-
Self { handle, full_ast }.to_owned()
28+
Ref::new(Self { handle, full_ast })
2429
}
2530

2631
pub fn instruction_from_index(

0 commit comments

Comments
 (0)