| 247 | } |
| 248 | |
| 249 | pub fn register<S, C>(name: S, demangler: C) -> Self |
| 250 | where |
| 251 | S: BnStrCompatible, |
| 252 | C: CustomDemangler, |
| 253 | { |
| 254 | extern "C" fn cb_is_mangled_string<C>(ctxt: *mut c_void, name: *const c_char) -> bool |
| 255 | where |
| 256 | C: CustomDemangler, |
| 257 | { |
| 258 | ffi_wrap!("CustomDemangler::cb_is_mangled_string", unsafe { |
| 259 | let cmd = &*(ctxt as *const C); |
| 260 | let Some(name) = raw_to_string(name) else { |
| 261 | return false; |
| 262 | }; |
| 263 | cmd.is_mangled_string(&name) |
| 264 | }) |
| 265 | } |
| 266 | extern "C" fn cb_demangle<C>( |
| 267 | ctxt: *mut c_void, |
| 268 | arch: *mut BNArchitecture, |
| 269 | name: *const c_char, |
| 270 | out_type: *mut *mut BNType, |
| 271 | out_var_name: *mut BNQualifiedName, |
| 272 | view: *mut BNBinaryView, |
| 273 | ) -> bool |
| 274 | where |
| 275 | C: CustomDemangler, |
| 276 | { |
| 277 | ffi_wrap!("CustomDemangler::cb_demangle", unsafe { |
| 278 | let cmd = &*(ctxt as *const C); |
| 279 | let arch = CoreArchitecture::from_raw(arch); |
| 280 | let Some(name) = raw_to_string(name) else { |
| 281 | return false; |
| 282 | }; |
| 283 | let view = match view.is_null() { |
| 284 | false => Some(BinaryView::from_raw(view).to_owned()), |
| 285 | true => None, |
| 286 | }; |
| 287 | |
| 288 | match cmd.demangle(&arch, &name, view) { |
| 289 | Some((name, ty)) => { |
| 290 | // NOTE: Leaked to the caller, who must pick the ref up. |
| 291 | *out_type = match ty { |
| 292 | Some(t) => Ref::into_raw(t).handle, |
| 293 | None => std::ptr::null_mut(), |
| 294 | }; |
| 295 | // NOTE: Leaked to be freed with `cb_free_var_name`. |
| 296 | *out_var_name = QualifiedName::into_raw(name); |
| 297 | true |
| 298 | } |
| 299 | None => false, |
| 300 | } |
| 301 | }) |
| 302 | } |
| 303 | |
| 304 | extern "C" fn cb_free_var_name(_ctxt: *mut c_void, name: *mut BNQualifiedName) { |
| 305 | ffi_wrap!("CustomDemangler::cb_free_var_name", unsafe { |
| 306 | // TODO: What is the point of this free callback? |