-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBNPlatformTypeParserCallbacks.cs
More file actions
149 lines (139 loc) · 4.41 KB
/
Copy pathBNPlatformTypeParserCallbacks.cs
File metadata and controls
149 lines (139 loc) · 4.41 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace BinaryNinja
{
public partial class Platform
{
private void InvokeAdjustTypeParserInput(
IntPtr context,
IntPtr parser,
IntPtr argumentsIn,
ulong argumentsLengthIn,
IntPtr sourceFileNamesIn,
IntPtr sourceFileValuesIn,
ulong sourceFilesLengthIn,
IntPtr argumentsOut,
IntPtr argumentsLengthOut,
IntPtr sourceFileNamesOut,
IntPtr sourceFileValuesOut,
IntPtr sourceFilesLengthOut
)
{
Platform.ResetTypeParserOutput(
argumentsOut,
argumentsLengthOut,
sourceFileNamesOut,
sourceFileValuesOut,
sourceFilesLengthOut
);
IntPtr nativeArguments = IntPtr.Zero;
IntPtr nativeSourceNames = IntPtr.Zero;
IntPtr nativeSourceValues = IntPtr.Zero;
try
{
string[] inputArguments = UnsafeUtils.ReadAnsiStringArray(
argumentsIn, argumentsLengthIn
);
string[] inputSourceNames = UnsafeUtils.ReadAnsiStringArray(
sourceFileNamesIn, sourceFilesLengthIn
);
string[] inputSourceValues = UnsafeUtils.ReadAnsiStringArray(
sourceFileValuesIn, sourceFilesLengthIn
);
List<string> arguments = new List<string>(inputArguments);
List<PlatformTypeParserSourceFile> sourceFiles =
new List<PlatformTypeParserSourceFile>();
for (int i = 0; i < inputSourceNames.Length; i++)
{
sourceFiles.Add(new PlatformTypeParserSourceFile(
inputSourceNames[i], inputSourceValues[i]
));
}
TypeParser managedParser = TypeParser.MustBorrowHandle(parser);
this.AdjustTypeParserInput(managedParser, arguments, sourceFiles);
string[] outputArguments = arguments.ToArray();
string[] outputSourceNames = new string[sourceFiles.Count];
string[] outputSourceValues = new string[sourceFiles.Count];
for (int i = 0; i < sourceFiles.Count; i++)
{
outputSourceNames[i] = sourceFiles[i].Name;
outputSourceValues[i] = sourceFiles[i].Contents;
}
Marshal.WriteInt64(argumentsLengthOut, outputArguments.Length);
Marshal.WriteInt64(sourceFilesLengthOut, sourceFiles.Count);
nativeArguments = Platform.AllocateStringList(outputArguments);
nativeSourceNames = Platform.AllocateStringList(outputSourceNames);
nativeSourceValues = Platform.AllocateStringList(outputSourceValues);
Marshal.WriteIntPtr(argumentsOut, nativeArguments);
Marshal.WriteIntPtr(sourceFileNamesOut, nativeSourceNames);
Marshal.WriteIntPtr(sourceFileValuesOut, nativeSourceValues);
}
catch (Exception exception)
{
Platform.FreeStringList(nativeArguments, argumentsLengthOut);
Platform.FreeStringList(nativeSourceNames, sourceFilesLengthOut);
Platform.FreeStringList(nativeSourceValues, sourceFilesLengthOut);
Platform.ResetTypeParserOutput(
argumentsOut,
argumentsLengthOut,
sourceFileNamesOut,
sourceFileValuesOut,
sourceFilesLengthOut
);
Core.LogError(
"Unhandled exception in Platform.AdjustTypeParserInput: {0}",
exception
);
}
}
private void InvokeFreeTypeParserInput(
IntPtr context,
IntPtr arguments,
ulong argumentsLength,
IntPtr sourceFileNames,
IntPtr sourceFileValues,
ulong sourceFilesLength
)
{
NativeMethods.BNFreeStringList(arguments, argumentsLength);
NativeMethods.BNFreeStringList(sourceFileNames, sourceFilesLength);
NativeMethods.BNFreeStringList(sourceFileValues, sourceFilesLength);
}
private static IntPtr AllocateStringList(string[] values)
{
if (0 == values.Length)
{
return IntPtr.Zero;
}
using (ScopedAllocator allocator = new ScopedAllocator())
{
IntPtr pointers = allocator.AllocUtf8StringArray(values);
return NativeMethods.BNAllocStringList(pointers, (ulong)values.Length);
}
}
private static void FreeStringList(IntPtr values, IntPtr countPointer)
{
if (IntPtr.Zero == values)
{
return;
}
ulong count = unchecked((ulong)Marshal.ReadInt64(countPointer));
NativeMethods.BNFreeStringList(values, count);
}
private static void ResetTypeParserOutput(
IntPtr argumentsOut,
IntPtr argumentsLengthOut,
IntPtr sourceFileNamesOut,
IntPtr sourceFileValuesOut,
IntPtr sourceFilesLengthOut
)
{
Marshal.WriteIntPtr(argumentsOut, IntPtr.Zero);
Marshal.WriteInt64(argumentsLengthOut, 0);
Marshal.WriteIntPtr(sourceFileNamesOut, IntPtr.Zero);
Marshal.WriteIntPtr(sourceFileValuesOut, IntPtr.Zero);
Marshal.WriteInt64(sourceFilesLengthOut, 0);
}
}
}