diff --git a/Handle/BNTypeLibrary.cs b/Handle/BNTypeLibrary.cs index 8fc23b0..a3e9a0a 100644 --- a/Handle/BNTypeLibrary.cs +++ b/Handle/BNTypeLibrary.cs @@ -120,9 +120,52 @@ public static bool DecompressToFile(string filename , string outdir) { return null; } - + + return new TypeLibrary(raw, true); + } + + /// + /// Looks up a registered type library by its GUID on the given architecture, mirroring + /// Python TypeLibrary.lookup_by_guid. Returns null when no library with that + /// GUID is registered. + /// + /// The architecture whose type libraries to search. + /// The GUID string to match. + /// The matching TypeLibrary, or null if none is registered. + public static TypeLibrary? LookupByGuid(Architecture arch, string guid) + { + IntPtr raw = NativeMethods.BNLookupTypeLibraryByGuid(arch.DangerousGetHandle(), guid); + + if (IntPtr.Zero == raw) + { + return null; + } + return new TypeLibrary(raw, true); } + + /// + /// Creates a new type library instance with a fresh GUID and the same data as this one, + /// mirroring Python TypeLibrary.duplicate (typelibrary.py:142). + /// + /// An independent copy of this type library. + public TypeLibrary Duplicate() + { + return TypeLibrary.MustTakeHandle( + NativeMethods.BNDuplicateTypeLibrary(this.handle) + ); + } + + /// + /// Makes this created or loaded type library available for platforms to use when loading + /// binaries, mirroring Python TypeLibrary.register (typelibrary.py:244). Python exposes + /// this as a property; the C# binding exposes it as a method since it performs an action with + /// no meaningful return value. + /// + public void Register() + { + NativeMethods.BNRegisterTypeLibrary(this.handle); + } public bool WriteToFile(string filename) { @@ -301,13 +344,55 @@ public void AddNamedType(QualifiedName name , BinaryNinja.Type kind) using (ScopedAllocator allocator = new ScopedAllocator()) { NativeMethods.BNAddTypeLibraryNamedType( - this.handle , - name.ToNativeEx(allocator) , + this.handle , + name.ToNativeEx(allocator) , kind.DangerousGetHandle() ); } } - + + /// + /// Removes a named object from this type library's object store, mirroring Python + /// TypeLibrary.remove_named_object (typelibrary.py:383). Only the object is removed; + /// any types it references are left in place. + /// + /// The qualified name of the object to remove. + public void RemoveNamedObject(QualifiedName name) + { + using (ScopedAllocator allocator = new ScopedAllocator()) + { + // The Remove P/Invoke takes the name as a pointer (BNQualifiedName*), unlike Add which + // takes it by reference, so pin the converted struct and pass its address. + IntPtr namePointer = allocator.AllocStruct(name.ToNativeEx(allocator)); + + NativeMethods.BNRemoveTypeLibraryNamedObject( + this.handle , + namePointer + ); + } + } + + /// + /// Removes a named type from this type library's type store, mirroring Python + /// TypeLibrary.remove_named_type (typelibrary.py:416). Only the type is removed; any + /// objects that reference it are left in place. + /// + /// The qualified name of the type to remove. + public void RemoveNamedType(QualifiedName name) + { + using (ScopedAllocator allocator = new ScopedAllocator()) + { + // The Remove P/Invoke takes the name as a pointer (BNQualifiedName*), unlike Add which + // takes it by reference, so pin the converted struct and pass its address. + IntPtr namePointer = allocator.AllocStruct(name.ToNativeEx(allocator)); + + NativeMethods.BNRemoveTypeLibraryNamedType( + this.handle , + namePointer + ); + } + } + public void AddTypeSource(QualifiedName name , string source) { using (ScopedAllocator allocator = new ScopedAllocator())