FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sharp-binaryninja/ScopedAllocator.cs at master · tinysec/sharp-binaryninja · GitHub
tinysec
/
sharp-binaryninja
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
22
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sharp-binaryninja
/
ScopedAllocator.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (184 loc) · 5.6 KB
Breadcrumbs
sharp-binaryninja
/
ScopedAllocator.cs
Copy path
File metadata and controls
235 lines (184 loc) · 5.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Text
;
namespace
BinaryNinja
{
public
class
ScopedAllocator
:
IDisposable
{
private
readonly
List
<
IntPtr
>
m_pointers
=
new
List
<
IntPtr
>
(
)
;
private
bool
m_disposed
=
false
;
public
void
Dispose
(
)
{
if
(
!
m_disposed
)
{
foreach
(
IntPtr
pointer
in
m_pointers
)
{
if
(
pointer
!=
IntPtr
.
Zero
)
{
Marshal
.
FreeHGlobal
(
pointer
)
;
}
}
m_pointers
.
Clear
(
)
;
m_disposed
=
true
;
}
}
internal
IntPtr
AllocHGlobal
(
int
size
)
{
IntPtr
pointer
=
Marshal
.
AllocHGlobal
(
size
)
;
m_pointers
.
Add
(
pointer
)
;
return
pointer
;
}
// BN core expects UTF-8 for every string input. Encode into an HGlobal buffer
// tracked by this allocator, so disposal keeps using Marshal.FreeHGlobal.
internal
IntPtr
AllocUtf8String
(
string
text
)
{
byte
[
]
utf8
=
Encoding
.
UTF8
.
GetBytes
(
text
??
string
.
Empty
)
;
IntPtr
pointer
=
this
.
AllocHGlobal
(
utf8
.
Length
+
1
)
;
Marshal
.
Copy
(
utf8
,
0
,
pointer
,
utf8
.
Length
)
;
Marshal
.
WriteByte
(
pointer
,
utf8
.
Length
,
0
)
;
return
pointer
;
}
internal
IntPtr
AllocUtf8StringArray
(
string
[
]
texts
)
{
if
(
texts
==
null
||
texts
.
Length
==
0
)
{
return
IntPtr
.
Zero
;
}
IntPtr
arrayPointer
=
this
.
AllocHGlobal
(
IntPtr
.
Size
*
texts
.
Length
)
;
for
(
int
i
=
0
;
i
<
texts
.
Length
;
i
++
)
{
IntPtr
textPointer
=
this
.
AllocUtf8String
(
texts
[
i
]
)
;
Marshal
.
WriteIntPtr
(
arrayPointer
,
i
*
IntPtr
.
Size
,
textPointer
)
;
}
return
arrayPointer
;
}
// The "Ansi"-named builders are misnomers retained for the generated call
// sites; they delegate to the UTF-8 builders so every write path emits UTF-8.
internal
IntPtr
AllocAnsiString
(
string
text
)
{
return
this
.
AllocUtf8String
(
text
)
;
}
internal
IntPtr
AllocAnsiStringArray
(
string
[
]
texts
)
{
return
this
.
AllocUtf8StringArray
(
texts
)
;
}
internal
IntPtr
AllocInteger
<
T
>
(
T
value
)
where
T
:
IConvertible
{
int
elementSize
=
Marshal
.
SizeOf
<
T
>
(
)
;
IntPtr
pointer
=
this
.
AllocHGlobal
(
elementSize
)
;
if
(
1
==
elementSize
)
{
Marshal
.
WriteByte
(
pointer
,
Convert
.
ToByte
(
(
IConvertible
)
value
)
)
;
}
else
if
(
2
==
elementSize
)
{
Marshal
.
WriteInt16
(
pointer
,
Convert
.
ToInt16
(
(
IConvertible
)
value
)
)
;
}
else
if
(
4
==
elementSize
)
{
Marshal
.
WriteInt32
(
pointer
,
Convert
.
ToInt32
(
(
IConvertible
)
value
)
)
;
}
else
if
(
8
==
elementSize
)
{
Marshal
.
WriteInt64
(
pointer
,
Convert
.
ToInt64
(
(
IConvertible
)
value
)
)
;
}
else
{
throw
new
NotSupportedException
(
"Unsupported element type"
)
;
}
return
pointer
;
}
internal
IntPtr
AllocIntegerArray
<
T
>
(
T
[
]
items
)
where
T
:
IConvertible
{
if
(
items
==
null
||
items
.
Length
==
0
)
{
return
IntPtr
.
Zero
;
}
int
elementSize
=
Marshal
.
SizeOf
<
T
>
(
)
;
// Stride by the actual element size, not IntPtr.Size: AllocIntegerArray<uint>
// (BNRegisterSetWithConfidence) writes 4 bytes per element, so the previous
// IntPtr.Size stride over-allocated 2x (8x for byte arrays). Matches the
// element-sized allocation already used by AllocStructArray.
int
totalSize
=
elementSize
*
items
.
Length
;
IntPtr
arrayPointer
=
this
.
AllocHGlobal
(
totalSize
)
;
for
(
int
i
=
0
;
i
<
items
.
Length
;
i
++
)
{
IntPtr
addressOfElement
=
IntPtr
.
Add
(
arrayPointer
,
i
*
elementSize
)
;
if
(
1
==
elementSize
)
{
Marshal
.
WriteByte
(
addressOfElement
,
Convert
.
ToByte
(
(
IConvertible
)
items
[
i
]
)
)
;
}
else
if
(
2
==
elementSize
)
{
Marshal
.
WriteInt16
(
addressOfElement
,
Convert
.
ToInt16
(
(
IConvertible
)
items
[
i
]
)
)
;
}
else
if
(
4
==
elementSize
)
{
Marshal
.
WriteInt32
(
addressOfElement
,
Convert
.
ToInt32
(
(
IConvertible
)
items
[
i
]
)
)
;
}
else
if
(
8
==
elementSize
)
{
Marshal
.
WriteInt64
(
addressOfElement
,
Convert
.
ToInt64
(
(
IConvertible
)
items
[
i
]
)
)
;
}
else
{
throw
new
NotSupportedException
(
"Unsupported element type"
)
;
}
}
return
arrayPointer
;
}
internal
IntPtr
AllocStruct
<
T
>
(
T
structure
)
where
T
:
struct
{
int
size
=
Marshal
.
SizeOf
<
T
>
(
)
;
IntPtr
pointer
=
this
.
AllocHGlobal
(
size
)
;
Marshal
.
StructureToPtr
(
structure
,
pointer
,
false
)
;
return
pointer
;
}
internal
IntPtr
AllocStructArray
<
T
>
(
T
[
]
structures
)
where
T
:
struct
{
if
(
structures
==
null
||
structures
.
Length
==
0
)
{
return
IntPtr
.
Zero
;
}
int
structSize
=
Marshal
.
SizeOf
<
T
>
(
)
;
int
totalSize
=
structSize
*
structures
.
Length
;
IntPtr
arrayPointer
=
this
.
AllocHGlobal
(
totalSize
)
;
for
(
int
i
=
0
;
i
<
structures
.
Length
;
i
++
)
{
IntPtr
addressOfElement
=
IntPtr
.
Add
(
arrayPointer
,
i
*
structSize
)
;
Marshal
.
StructureToPtr
(
structures
[
i
]
,
addressOfElement
,
false
)
;
}
return
arrayPointer
;
}
internal
IntPtr
AllocHandleArray
<
T
>
(
T
[
]
handles
)
where
T
:
SafeHandle
{
if
(
handles
==
null
||
handles
.
Length
==
0
)
{
return
IntPtr
.
Zero
;
}
int
totalSize
=
IntPtr
.
Size
*
handles
.
Length
;
IntPtr
arrayPointer
=
this
.
AllocHGlobal
(
totalSize
)
;
for
(
int
i
=
0
;
i
<
handles
.
Length
;
i
++
)
{
IntPtr
addressOfElement
=
IntPtr
.
Add
(
arrayPointer
,
i
*
IntPtr
.
Size
)
;
Marshal
.
WriteIntPtr
(
addressOfElement
,
handles
[
i
]
.
DangerousGetHandle
(
)
)
;
}
return
arrayPointer
;
}
internal
TNative
[
]
ConvertToNativeArrayEx
<
TNative
,
TManaged
>
(
TManaged
[
]
sources
)
where
TManaged
:
INativeWrapperEx
<
TNative
>
{
if
(
sources
==
null
||
sources
.
Length
==
0
)
{
return
Array
.
Empty
<
TNative
>
(
)
;
}
List
<
TNative
>
targets
=
new
List
<
TNative
>
(
)
;
for
(
int
i
=
0
;
i
<
sources
.
Length
;
i
++
)
{
targets
.
Add
(
sources
[
i
]
.
ToNativeEx
(
this
)
)
;
}
return
targets
.
ToArray
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL