Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 176 additions & 4 deletions Handle/BNBinaryView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -152,6 +154,63 @@ public ulong Length
)
);
}

public Task<BinaryView?> 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<NativeDelegates.BNProgressFunction>(
UnsafeUtils.WrapProgressDelegate(progress)
),
IntPtr.Zero
)
);

if (null == view)
{
return null;
}

if (cancelled && ( null != cancellationToken) )
{
cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested();
}

return view;
}
);
}

/// <summary>
///
Expand Down Expand Up @@ -182,7 +241,64 @@ public ulong Length
);
}

public static BinaryView? OpenExisting(string filename , ProgressDelegate? progress = null)
public static Task<BinaryView?> 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<NativeDelegates.BNProgressFunction>(
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);

Expand Down Expand Up @@ -210,6 +326,62 @@ public ulong Length
}
}

public static Task<BinaryView?> 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<NativeDelegates.BNProgressFunction>(
UnsafeUtils.WrapProgressDelegate(progress)) ,
IntPtr.Zero
)
);

if (null == view)
{
return null;
}

if (cancelled && ( null != cancellationToken) )
{
cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested();
}

return view;
}
);
}

public BinaryView? Parent
{
get
Expand Down