binaryninja/
llvm.rs

1use binaryninjacore_sys::BNLlvmServicesDisasmInstruction;
2use std::ffi::{CStr, CString};
3use std::os::raw::{c_char, c_int};
4
5pub fn disas_instruction(triplet: &str, data: &[u8], address64: u64) -> Option<(usize, String)> {
6    unsafe {
7        let triplet = CString::new(triplet).ok()?;
8        let mut src = data.to_vec();
9        let mut buf = vec![0u8; 256];
10        let instr_len = BNLlvmServicesDisasmInstruction(
11            triplet.as_ptr(),
12            src.as_mut_ptr(),
13            src.len() as c_int,
14            address64,
15            buf.as_mut_ptr() as *mut c_char,
16            buf.len(),
17        );
18
19        if instr_len > 0 {
20            // Convert buf (u8) → &CStr by finding the first NUL
21            if let Some(z) = buf.iter().position(|&b| b == 0) {
22                let s = CStr::from_bytes_with_nul(&buf[..=z])
23                    .unwrap()
24                    .to_string_lossy()
25                    .into_owned();
26                Some((instr_len as usize, s))
27            } else {
28                // Callee didn't NULL terminate, return an empty string
29                Some((instr_len as usize, String::new()))
30            }
31        } else {
32            None
33        }
34    }
35}