From 6c376e3744a2615630cf74b1eefde07e8a405d31 Mon Sep 17 00:00:00 2001 From: tinysec Date: Thu, 23 Jul 2026 03:02:42 +0800 Subject: [PATCH] Add bulk section descriptors --- Handle/BNBinaryView.cs | 56 +++++++++++++++++++++++++++++ Struct/BNSectionInfo.cs | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 Struct/BNSectionInfo.cs diff --git a/Handle/BNBinaryView.cs b/Handle/BNBinaryView.cs index 640b497..3220cce 100644 --- a/Handle/BNBinaryView.cs +++ b/Handle/BNBinaryView.cs @@ -5268,6 +5268,34 @@ public void AddAutoSection( infoData ); } + + public void AddAutoSections(SectionInfo[] sections) + { + if (null == sections) + { + throw new ArgumentNullException(nameof(sections)); + } + + using (ScopedAllocator allocator = new ScopedAllocator()) + { + BNSectionInfo[] nativeSections = new BNSectionInfo[sections.Length]; + for (int i = 0; i < sections.Length; i++) + { + if (null == sections[i]) + { + throw new ArgumentException("Section entries cannot be null.", nameof(sections)); + } + + nativeSections[i] = sections[i].ToNative(allocator); + } + + NativeMethods.BNAddAutoSections( + this.handle, + allocator.AllocStructArray(nativeSections), + new UIntPtr((uint)nativeSections.Length) + ); + } + } public void RemoveAutoSection(string name) { @@ -5301,6 +5329,34 @@ public void AddUserSection( infoData ); } + + public void AddUserSections(SectionInfo[] sections) + { + if (null == sections) + { + throw new ArgumentNullException(nameof(sections)); + } + + using (ScopedAllocator allocator = new ScopedAllocator()) + { + BNSectionInfo[] nativeSections = new BNSectionInfo[sections.Length]; + for (int i = 0; i < sections.Length; i++) + { + if (null == sections[i]) + { + throw new ArgumentException("Section entries cannot be null.", nameof(sections)); + } + + nativeSections[i] = sections[i].ToNative(allocator); + } + + NativeMethods.BNAddUserSections( + this.handle, + allocator.AllocStructArray(nativeSections), + new UIntPtr((uint)nativeSections.Length) + ); + } + } public void RemoveUserSection(string name) { diff --git a/Struct/BNSectionInfo.cs b/Struct/BNSectionInfo.cs new file mode 100644 index 0000000..82346d3 --- /dev/null +++ b/Struct/BNSectionInfo.cs @@ -0,0 +1,79 @@ +using System; +using System.Runtime.InteropServices; + +namespace BinaryNinja +{ + [StructLayout(LayoutKind.Sequential)] + internal struct BNSectionInfo + { + internal IntPtr name; + internal ulong start; + internal ulong length; + internal SectionSemantics semantics; + internal IntPtr type; + internal ulong align; + internal ulong entrySize; + internal IntPtr linkedSection; + internal IntPtr infoSection; + internal ulong infoData; + } + + /// Describes a section for a bulk add operation. + public sealed class SectionInfo + { + public string Name { get; set; } + public ulong Start { get; set; } + public ulong Length { get; set; } + public SectionSemantics Semantics { get; set; } + public string Type { get; set; } + public ulong Align { get; set; } + public ulong EntrySize { get; set; } + public string LinkedSection { get; set; } + public string InfoSection { get; set; } + public ulong InfoData { get; set; } + + public SectionInfo( + string name, + ulong start, + ulong length, + SectionSemantics semantics = SectionSemantics.DefaultSectionSemantics, + string type = "", + ulong align = 1, + ulong entrySize = 1, + string linkedSection = "", + string infoSection = "", + ulong infoData = 0 + ) + { + this.Name = name ?? throw new ArgumentNullException(nameof(name)); + this.Start = start; + this.Length = length; + this.Semantics = semantics; + this.Type = type ?? throw new ArgumentNullException(nameof(type)); + this.Align = align; + this.EntrySize = entrySize; + this.LinkedSection = linkedSection + ?? throw new ArgumentNullException(nameof(linkedSection)); + this.InfoSection = infoSection + ?? throw new ArgumentNullException(nameof(infoSection)); + this.InfoData = infoData; + } + + internal BNSectionInfo ToNative(ScopedAllocator allocator) + { + return new BNSectionInfo() + { + name = allocator.AllocUtf8String(this.Name), + start = this.Start, + length = this.Length, + semantics = this.Semantics, + type = allocator.AllocUtf8String(this.Type), + align = this.Align, + entrySize = this.EntrySize, + linkedSection = allocator.AllocUtf8String(this.LinkedSection), + infoSection = allocator.AllocUtf8String(this.InfoSection), + infoData = this.InfoData, + }; + } + } +}