Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Function/BNCreateCustomLanguageRepresentationFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static extern IntPtr BNCreateCustomLanguageRepresentationFunction(
IntPtr highLevelIL ,

// BNCustomLanguageRepresentationFunction* callbacks
IntPtr callbacks
in BNCustomLanguageRepresentationFunction callbacks
);
}
}
}
6 changes: 3 additions & 3 deletions Function/BNGetLanguageRepresentationFunctionExprText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ internal static extern IntPtr BNGetLanguageRepresentationFunctionExprText(
IntPtr settings ,

// bool asFullAst
bool asFullAst ,
[MarshalAs(UnmanagedType.I1)] bool asFullAst ,

// BNOperatorPrecedence precedence
OperatorPrecedence precedence ,

// bool statement
bool statement ,
[MarshalAs(UnmanagedType.I1)] bool statement ,

// uint64_t* count
out ulong count
);
}
}
}
4 changes: 2 additions & 2 deletions Function/BNGetLanguageRepresentationFunctionLinearLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ internal static extern IntPtr BNGetLanguageRepresentationFunctionLinearLines(
IntPtr settings ,

// bool asFullAst
bool asFullAst ,
[MarshalAs(UnmanagedType.I1)] bool asFullAst ,

// uint64_t* count
out ulong count
);
}
}
}
4 changes: 2 additions & 2 deletions Function/BNRegisterLanguageRepresentationFunctionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal static extern IntPtr BNRegisterLanguageRepresentationFunctionType(
[MarshalAs(UnmanagedType.LPUTF8Str)] string name ,

// BNCustomLanguageRepresentationFunctionType* type
IntPtr type
in BNCustomLanguageRepresentationFunctionType type

);
}
}
}
2 changes: 1 addition & 1 deletion Handle/BNFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ public HighLevelILFunction HighLevelIL
return null;
}

return new LanguageRepresentationFunction(raw , true);
return LanguageRepresentationFunction.MustTakeHandle(raw);
}

public FunctionType Type
Expand Down
161 changes: 49 additions & 112 deletions Handle/BNLanguageRepresentationFunction.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;

namespace BinaryNinja
{
public sealed class LanguageRepresentationFunction : AbstractSafeHandle<LanguageRepresentationFunction>
public abstract partial class LanguageRepresentationFunction : AbstractSafeHandle<LanguageRepresentationFunction>
{
private bool custom;

internal LanguageRepresentationFunction(IntPtr handle , bool owner)
: base(handle , owner)
{
Expand All @@ -21,7 +21,7 @@ internal LanguageRepresentationFunction(IntPtr handle , bool owner)
return null;
}

return new LanguageRepresentationFunction(
return new CoreLanguageRepresentationFunction(
NativeMethods.BNNewLanguageRepresentationFunctionReference(handle) ,
true
);
Expand All @@ -34,7 +34,7 @@ internal static LanguageRepresentationFunction MustNewFromHandle(IntPtr handle)
throw new ArgumentNullException(nameof(handle));
}

return new LanguageRepresentationFunction(
return new CoreLanguageRepresentationFunction(
NativeMethods.BNNewLanguageRepresentationFunctionReference(handle) ,
true
);
Expand All @@ -47,7 +47,7 @@ internal static LanguageRepresentationFunction MustNewFromHandle(IntPtr handle)
return null;
}

return new LanguageRepresentationFunction(handle, true);
return new CoreLanguageRepresentationFunction(handle, true);
}

internal static LanguageRepresentationFunction MustTakeHandle(IntPtr handle)
Expand All @@ -57,7 +57,7 @@ internal static LanguageRepresentationFunction MustTakeHandle(IntPtr handle)
throw new ArgumentNullException(nameof(handle));
}

return new LanguageRepresentationFunction(handle, true);
return new CoreLanguageRepresentationFunction(handle, true);
}

internal static LanguageRepresentationFunction? BorrowHandle(IntPtr handle)
Expand All @@ -67,7 +67,7 @@ internal static LanguageRepresentationFunction MustTakeHandle(IntPtr handle)
return null;
}

return new LanguageRepresentationFunction(handle, false);
return new CoreLanguageRepresentationFunction(handle, false);
}

internal static LanguageRepresentationFunction MustBorrowHandle(IntPtr handle)
Expand All @@ -77,11 +77,22 @@ internal static LanguageRepresentationFunction MustBorrowHandle(IntPtr handle)
throw new ArgumentNullException(nameof(handle));
}

return new LanguageRepresentationFunction(handle, false);
return new CoreLanguageRepresentationFunction(handle, false);
}

protected override bool ReleaseHandle()
{
if (this.custom)
{
if (this.initialReferencePending)
{
this.initialReferencePending = false;
NativeMethods.BNFreeLanguageRepresentationFunction(this.handle);
}

return true;
}

if ( !this.IsInvalid )
{
NativeMethods.BNFreeLanguageRepresentationFunction(this.handle);
Expand Down Expand Up @@ -335,11 +346,16 @@ public string LinearDisassemblyText
/// <summary>
/// Gets the string that marks the start of an annotation in this language.
/// </summary>
public string AnnotationStartString
public virtual string AnnotationStartString
{
get
{
return UnsafeUtils.TakeAnsiString(
if (this.custom)
{
return "{";
}

return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetLanguageRepresentationFunctionAnnotationStartString(this.handle)
);
}
Expand All @@ -348,11 +364,16 @@ public string AnnotationStartString
/// <summary>
/// Gets the string that marks the end of an annotation in this language.
/// </summary>
public string AnnotationEndString
public virtual string AnnotationEndString
{
get
{
return UnsafeUtils.TakeAnsiString(
if (this.custom)
{
return "}";
}

return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetLanguageRepresentationFunctionAnnotationEndString(this.handle)
);
}
Expand All @@ -361,11 +382,16 @@ public string AnnotationEndString
/// <summary>
/// Gets the string that marks the start of a comment in this language.
/// </summary>
public string CommentStartString
public virtual string CommentStartString
{
get
{
return UnsafeUtils.TakeAnsiString(
if (this.custom)
{
return "// ";
}

return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetLanguageRepresentationFunctionCommentStartString(this.handle)
);
}
Expand All @@ -374,109 +400,20 @@ public string CommentStartString
/// <summary>
/// Gets the string that marks the end of a comment in this language.
/// </summary>
public string CommentEndString
public virtual string CommentEndString
{
get
{
return UnsafeUtils.TakeAnsiString(
if (this.custom)
{
return string.Empty;
}

return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetLanguageRepresentationFunctionCommentEndString(this.handle)
);
}
}

// ===================================================================
// Highlight
// ===================================================================

/// <summary>
/// Gets the highlight color for this language representation function at the specified basic block.
/// </summary>
/// <param name="block">The basic block to get the highlight for.</param>
/// <returns>The highlight color for the block.</returns>
public HighlightColor GetHighlight(BasicBlock block)
{
return HighlightColor.FromNative(
NativeMethods.BNGetLanguageRepresentationFunctionHighlight(
this.handle ,
block.DangerousGetHandle()
)
);
}

// ===================================================================
// Language type lookup
// ===================================================================

/// <summary>
/// Gets the language representation function type associated with this instance.
/// </summary>
/// <returns>The LanguageRepresentationFunctionType, or null if not available.</returns>
public LanguageRepresentationFunctionType? GetLanguageType()
{
return LanguageRepresentationFunctionType.BorrowHandle(
NativeMethods.BNGetLanguageRepresentationType(this.handle)
);
}

/// <summary>
/// Gets a registered language representation function type by its name.
/// </summary>
/// <param name="name">The name of the language type (e.g., "Pseudo C").</param>
/// <returns>The matching LanguageRepresentationFunctionType, or null if not found.</returns>
public static LanguageRepresentationFunctionType? GetLanguageTypeByName(string name)
{
return LanguageRepresentationFunctionType.BorrowHandle(
NativeMethods.BNGetLanguageRepresentationFunctionTypeByName(name)
);
}

/// <summary>
/// Gets the line formatter settings for this language representation function
/// using the given disassembly settings. Returns a LineFormatterSettings populated
/// with language-specific formatting defaults.
/// </summary>
/// <param name="settings">The disassembly settings providing base formatting context.</param>
/// <returns>A LineFormatterSettings populated with language-appropriate values.</returns>
public LineFormatterSettings GetLineFormatterSettings(DisassemblySettings settings)
{
// 1. Call the native API.
IntPtr ptr = NativeMethods.BNGetLanguageRepresentationLineFormatterSettings(
settings.DangerousGetHandle() ,
this.handle
);

// 2. Handle null return.
if (IntPtr.Zero == ptr)
{
return new LineFormatterSettings();
}

// 3. Read the native struct from the pointer.
BNLineFormatterSettings native = Marshal.PtrToStructure<BNLineFormatterSettings>(ptr);

// 4. Convert to managed type.
LineFormatterSettings result = new LineFormatterSettings();
result.DesiredLineLength = native.desiredLineLength;
result.MinimumContentLength = native.minimumContentLength;
result.TabWidth = native.tabWidth;
result.MaximumAnnotationLength = native.maximumAnnotationLength;
result.StringWrappingWidth = native.stringWrappingWidth;
result.LanguageName = UnsafeUtils.ReadAnsiString(native.languageName);
result.CommentStartString = UnsafeUtils.ReadAnsiString(native.commentStartString);
result.CommentEndString = UnsafeUtils.ReadAnsiString(native.commentEndString);
result.AnnotationStartString = UnsafeUtils.ReadAnsiString(native.annotationStartString);
result.AnnotationEndString = UnsafeUtils.ReadAnsiString(native.annotationEndString);

// 5. The HLIL function is a borrowed pointer inside the struct; wrap if non-null.
result.HighLevelIL = (native.highLevelIL != IntPtr.Zero)
? HighLevelILFunction.NewFromHandle(native.highLevelIL)
: null;

// 6. Free the native struct allocation.
NativeMethods.BNFreeLineFormatterSettings(ptr);

return result;
}

}
}
}
Loading
Loading