diff --git a/Handle/BNBinaryView.cs b/Handle/BNBinaryView.cs index 2a34c7d..e648fbb 100644 --- a/Handle/BNBinaryView.cs +++ b/Handle/BNBinaryView.cs @@ -3,6 +3,8 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Win32.SafeHandles; namespace BinaryNinja @@ -133,9 +135,9 @@ public ulong Length } public BinaryView? Load( - bool updateAnalysis , - string options , - ProgressDelegate? progress + bool updateAnalysis = false , + string options = "", + ProgressDelegate? progress = null ) { return BinaryView.TakeHandle( @@ -152,6 +154,63 @@ public ulong Length ) ); } + + public Task LoadAsync( + bool updateAnalysis = false, + string options = "" , + CancellationToken? cancellationToken = null + ) + { + return Task.Run(() => + { + bool cancelled = false; + + ProgressDelegate progress = (param2 , param3) => + { + if (null != cancellationToken) + { + if (cancellationToken.GetValueOrDefault().IsCancellationRequested) + { + cancelled = true; + + return false; + } + } + + return true; + }; + + if (cancelled) + { + return null; + } + + BinaryView? view = BinaryView.TakeHandle( + NativeMethods.BNLoadBinaryView( + this.handle , + updateAnalysis , + options , + Marshal.GetFunctionPointerForDelegate( + UnsafeUtils.WrapProgressDelegate(progress) + ), + IntPtr.Zero + ) + ); + + if (null == view) + { + return null; + } + + if (cancelled && ( null != cancellationToken) ) + { + cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested(); + } + + return view; + } + ); + } /// /// @@ -182,7 +241,64 @@ public ulong Length ); } - public static BinaryView? OpenExisting(string filename , ProgressDelegate? progress = null) + public static Task LoadFileAsync( + string filename , + bool updateAnalysis = false , + string options = "", + CancellationToken? cancellationToken = null + ) + { + return Task.Run(() => + { + bool cancelled = false; + + ProgressDelegate progress = (param2 , param3) => + { + if (null != cancellationToken) + { + if (cancellationToken.GetValueOrDefault().IsCancellationRequested) + { + cancelled = true; + + return false; + } + } + + return true; + }; + + if (cancelled) + { + return null; + } + + BinaryView? view = BinaryView.TakeHandle( + NativeMethods.BNLoadFilename( + filename , + updateAnalysis , + options , + Marshal.GetFunctionPointerForDelegate( + UnsafeUtils.WrapProgressDelegate(progress)) , + IntPtr.Zero + ) + ); + + if (null == view) + { + return null; + } + + if (cancelled && ( null != cancellationToken) ) + { + cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested(); + } + + return view; + } + ); + } + + public static BinaryView? OpenExistingDatabase(string filename , ProgressDelegate? progress = null) { FileMetadata file = new FileMetadata(filename); @@ -210,6 +326,62 @@ public ulong Length } } + public static Task OpenExistingDatabaseAsync( + string filename , + CancellationToken? cancellationToken = null + ) + { + return Task.Run(() => + { + bool cancelled = false; + + ProgressDelegate progress = (param2 , param3) => + { + if (null != cancellationToken) + { + if (cancellationToken.GetValueOrDefault().IsCancellationRequested) + { + cancelled = true; + + return false; + } + } + + return true; + }; + + if (cancelled) + { + return null; + } + + FileMetadata file = new FileMetadata(filename); + + BinaryView? view = BinaryView.TakeHandle( + NativeMethods.BNOpenExistingDatabaseWithProgress( + file.DangerousGetHandle() , + filename , + Marshal.GetFunctionPointerForDelegate( + UnsafeUtils.WrapProgressDelegate(progress)) , + IntPtr.Zero + ) + ); + + if (null == view) + { + return null; + } + + if (cancelled && ( null != cancellationToken) ) + { + cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested(); + } + + return view; + } + ); + } + public BinaryView? Parent { get