-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCustomStringType.cs
More file actions
133 lines (116 loc) · 3.04 KB
/
Copy pathCustomStringType.cs
File metadata and controls
133 lines (116 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Runtime.InteropServices;
namespace BinaryNinja
{
[StructLayout(LayoutKind.Sequential)]
internal struct BNCustomStringTypeInfo
{
internal IntPtr name;
internal IntPtr stringPrefix;
internal IntPtr stringPostfix;
}
/// <summary>
/// Describes the name and rendering delimiters for strings produced by a custom recognizer.
/// </summary>
public sealed class CustomStringType : AbstractSafeHandle<CustomStringType>
{
private CustomStringType(IntPtr handle)
: base(handle, false)
{
}
internal static CustomStringType MustFromHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
throw new ArgumentNullException(nameof(handle));
}
return new CustomStringType(handle);
}
internal static CustomStringType? FromHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
return null;
}
return new CustomStringType(handle);
}
/// <summary>Registers a custom string type with the core.</summary>
public static CustomStringType Register(
string name,
string stringPrefix = "",
string stringPostfix = ""
)
{
if (null == name)
{
throw new ArgumentNullException(nameof(name));
}
using (ScopedAllocator allocator = new ScopedAllocator())
{
BNCustomStringTypeInfo info = new BNCustomStringTypeInfo();
info.name = allocator.AllocUtf8String(name);
info.stringPrefix = allocator.AllocUtf8String(stringPrefix ?? string.Empty);
info.stringPostfix = allocator.AllocUtf8String(stringPostfix ?? string.Empty);
return CustomStringType.MustFromHandle(
NativeMethods.BNRegisterCustomStringType(in info)
);
}
}
/// <summary>Finds a registered custom string type by name.</summary>
public static CustomStringType? FromName(string name)
{
if (null == name)
{
throw new ArgumentNullException(nameof(name));
}
return CustomStringType.FromHandle(
NativeMethods.BNGetCustomStringTypeByName(name)
);
}
/// <summary>Gets every custom string type registered with the core.</summary>
public static CustomStringType[] GetTypes()
{
IntPtr types = NativeMethods.BNGetCustomStringTypeList(out ulong count);
return UnsafeUtils.TakeHandleArray<CustomStringType>(
types,
count,
CustomStringType.MustFromHandle,
NativeMethods.BNFreeCustomStringTypeList
);
}
/// <summary>Gets the unique registered name of this string type.</summary>
public string Name
{
get
{
return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetCustomStringTypeName(this.handle)
);
}
}
/// <summary>Gets the text rendered before the opening quote.</summary>
public string StringPrefix
{
get
{
return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetCustomStringTypePrefix(this.handle)
);
}
}
/// <summary>Gets the text rendered after the closing quote.</summary>
public string StringPostfix
{
get
{
return UnsafeUtils.TakeUtf8String(
NativeMethods.BNGetCustomStringTypePostfix(this.handle)
);
}
}
public override string ToString()
{
return this.Name;
}
}
}