FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
binaryninja-api/python/metadata.py at dev · Vector35/binaryninja-api · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Vector35
/
binaryninja-api
Public
Notifications
You must be signed in to change notification settings
Fork
294
Star
1.3k
Code
Issues
1.9k
Pull requests
36
Discussions
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
binaryninja-api
/
python
/
metadata.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
283 lines (247 loc) · 9.3 KB
Breadcrumbs
binaryninja-api
/
python
/
metadata.py
Copy path
File metadata and controls
283 lines (247 loc) · 9.3 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright (c) 2015-2026 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import
ctypes
from
typing
import
Union
,
Optional
,
List
,
Tuple
,
Any
# Binary Ninja components
from
.
import
_binaryninjacore
as
core
from
.
enums
import
MetadataType
MetadataValueType
=
Union
[
'Metadata'
,
int
,
bool
,
str
,
bytes
,
float
,
List
[
'MetadataValueType'
],
Tuple
[
'MetadataValueType'
],
dict
]
class
Metadata
:
def
__init__
(
self
,
value
:
Optional
[
MetadataValueType
]
=
None
,
signed
:
Optional
[
bool
]
=
None
,
raw
:
Optional
[
bool
]
=
None
,
handle
:
Optional
[
core
.
BNMetadata
]
=
None
):
"""
The 'raw' parameter is no longer needed it was a workaround for a Python 2 limitation.
To pass raw data into this API, simply use a `bytes` object.
"""
if
handle
is
not
None
:
self
.
handle
=
handle
elif
isinstance
(
value
,
Metadata
):
# If the value is already a Metadata object, reuse its handle
self
.
handle
=
value
.
handle
elif
isinstance
(
value
,
bool
):
self
.
handle
=
core
.
BNCreateMetadataBooleanData
(
value
)
elif
isinstance
(
value
,
int
):
if
signed
:
self
.
handle
=
core
.
BNCreateMetadataSignedIntegerData
(
value
)
else
:
self
.
handle
=
core
.
BNCreateMetadataUnsignedIntegerData
(
value
)
elif
isinstance
(
value
,
str
):
self
.
handle
=
core
.
BNCreateMetadataStringData
(
value
)
elif
isinstance
(
value
,
bytes
):
buffer
=
(
ctypes
.
c_ubyte
*
len
(
value
)).
from_buffer_copy
(
value
)
self
.
handle
=
core
.
BNCreateMetadataRawData
(
buffer
,
len
(
value
))
elif
isinstance
(
value
,
float
):
self
.
handle
=
core
.
BNCreateMetadataDoubleData
(
value
)
elif
isinstance
(
value
, (
list
,
tuple
)):
self
.
handle
=
core
.
BNCreateMetadataOfType
(
MetadataType
.
ArrayDataType
)
for
elm
in
value
:
md
=
Metadata
(
elm
,
signed
,
raw
)
core
.
BNMetadataArrayAppend
(
self
.
handle
,
md
.
handle
)
elif
isinstance
(
value
,
dict
):
self
.
handle
=
core
.
BNCreateMetadataOfType
(
MetadataType
.
KeyValueDataType
)
for
elm
in
value
:
md
=
Metadata
(
value
[
elm
],
signed
,
raw
)
core
.
BNMetadataSetValueForKey
(
self
.
handle
,
str
(
elm
),
md
.
handle
)
else
:
raise
ValueError
(
f"
{
type
(
value
)
}
is not a supported type: int, bool, str, bytes, float, list, tuple, dict, or Metadata"
)
def
__len__
(
self
):
if
self
.
is_array
or
self
.
is_dict
or
self
.
is_string
or
self
.
is_bytes
:
return
core
.
BNMetadataSize
(
self
.
handle
)
raise
TypeError
(
"Metadata object doesn't support len()"
)
def
__eq__
(
self
,
other
):
if
isinstance
(
other
,
int
)
and
self
.
is_integer
:
return
int
(
self
)
==
other
elif
isinstance
(
other
,
str
)
and
self
.
is_string
:
return
str
(
self
)
==
other
elif
isinstance
(
other
,
bytes
)
and
self
.
is_bytes
:
return
bytes
(
self
)
==
other
elif
isinstance
(
other
,
float
)
and
self
.
is_float
:
return
float
(
self
)
==
other
elif
isinstance
(
other
,
bool
)
and
self
.
is_boolean
:
return
bool
(
self
)
==
other
elif
self
.
is_array
and
isinstance
(
other
,
list
):
if
len
(
self
)
!=
len
(
other
):
return
False
for
a
,
b
in
zip
(
self
,
other
):
if
a
!=
b
:
return
False
return
True
elif
self
.
is_dict
and
isinstance
(
other
,
dict
):
if
len
(
self
)
!=
len
(
other
):
return
False
for
a
,
b
in
zip
(
self
,
other
):
if
a
!=
b
or
self
[
a
]
!=
other
[
b
]:
return
False
return
True
elif
isinstance
(
other
,
Metadata
):
return
core
.
BNMetadataIsEqual
(
self
.
handle
,
other
.
handle
)
return
NotImplemented
def
__ne__
(
self
,
other
):
return
not
self
.
__eq__
(
other
)
def
__iter__
(
self
):
if
self
.
is_array
:
for
i
in
range
(
core
.
BNMetadataSize
(
self
.
handle
)):
yield
Metadata
(
handle
=
core
.
BNMetadataGetForIndex
(
self
.
handle
,
i
)).
value
elif
self
.
is_dict
:
result
=
core
.
BNMetadataGetValueStore
(
self
.
handle
)
assert
result
is
not
None
,
"core.BNMetadataGetValueStore returned None"
try
:
for
i
in
range
(
result
.
contents
.
size
):
if
isinstance
(
result
.
contents
.
keys
[
i
],
bytes
):
yield
result
.
contents
.
keys
[
i
].
decode
(
"utf-8"
)
else
:
yield
result
.
contents
.
keys
[
i
]
finally
:
core
.
BNFreeMetadataValueStore
(
result
)
else
:
raise
TypeError
(
"Metadata object doesn't support iteration"
)
def
__getitem__
(
self
,
value
):
if
self
.
is_array
:
if
not
isinstance
(
value
,
int
):
raise
ValueError
(
"Metadata object only supports integers for indexing"
)
if
value
>=
len
(
self
):
raise
IndexError
(
"Index value out of range"
)
return
Metadata
(
handle
=
core
.
BNMetadataGetForIndex
(
self
.
handle
,
value
)).
value
if
self
.
is_dict
:
if
not
isinstance
(
value
,
str
):
raise
ValueError
(
"Metadata object only supports strings for indexing"
)
handle
=
core
.
BNMetadataGetForKey
(
self
.
handle
,
value
)
if
handle
is
None
:
raise
KeyError
(
value
)
return
Metadata
(
handle
=
handle
).
value
raise
NotImplementedError
(
"Metadata object doesn't support indexing"
)
def
__setitem__
(
self
,
key
,
value
):
if
not
self
.
is_dict
:
raise
TypeError
(
"Metadata object is not a dictionary and does not support item assignment"
)
if
not
isinstance
(
key
,
str
):
raise
ValueError
(
"Metadata keys must be strings"
)
md_value
=
Metadata
(
value
)
core
.
BNMetadataSetValueForKey
(
self
.
handle
,
key
,
md_value
.
handle
)
def
__str__
(
self
):
if
self
.
is_string
:
return
str
(
core
.
BNMetadataGetString
(
self
.
handle
))
raise
ValueError
(
"Metadata object not a string"
)
def
__bool__
(
self
):
if
self
.
is_boolean
:
return
bool
(
core
.
BNMetadataGetBoolean
(
self
.
handle
))
raise
ValueError
(
"Metadata object not a boolean"
)
def
__bytes__
(
self
):
try
:
length
=
ctypes
.
c_ulonglong
()
length
.
value
=
0
native_list
=
core
.
BNMetadataGetRaw
(
self
.
handle
,
ctypes
.
byref
(
length
))
assert
native_list
is
not
None
,
"core.BNMetadataGetRaw returned None"
return
ctypes
.
string_at
(
native_list
,
length
.
value
)
finally
:
core
.
BNFreeMetadataRaw
(
native_list
)
def
__int__
(
self
):
if
self
.
is_signed_integer
:
return
core
.
BNMetadataGetSignedInteger
(
self
.
handle
)
if
self
.
is_unsigned_integer
:
return
core
.
BNMetadataGetUnsignedInteger
(
self
.
handle
)
raise
ValueError
(
"Metadata object not of integer type"
)
def
__float__
(
self
):
if
not
self
.
is_float
:
raise
ValueError
(
"Metadata object is not float type"
)
return
core
.
BNMetadataGetDouble
(
self
.
handle
)
def
__nonzero__
(
self
):
if
not
self
.
is_boolean
:
raise
ValueError
(
"Metadata object is not boolean type"
)
return
core
.
BNMetadataGetBoolean
(
self
.
handle
)
def
get
(
self
,
key
:
str
,
default
:
Any
=
None
)
->
Any
:
try
:
return
self
[
key
]
except
(
KeyError
,
IndexError
,
ValueError
,
NotImplementedError
):
return
default
@
property
def
value
(
self
):
if
self
.
is_integer
:
return
int
(
self
)
elif
self
.
is_string
:
return
str
(
self
)
elif
self
.
is_bytes
:
return
bytes
(
self
)
elif
self
.
is_float
:
return
float
(
self
)
elif
self
.
is_boolean
:
return
bool
(
self
)
elif
self
.
is_array
:
return
list
(
self
)
elif
self
.
is_dict
:
return
self
.
get_dict
()
raise
TypeError
()
def
get_dict
(
self
):
if
not
self
.
is_dict
:
raise
TypeError
()
result
=
{}
for
key
in
self
:
result
[
key
]
=
self
[
key
]
return
result
def
get_json_string
(
self
):
return
str
(
core
.
BNMetadataGetJsonString
(
self
.
handle
))
@
property
def
type
(
self
):
return
MetadataType
(
core
.
BNMetadataGetType
(
self
.
handle
))
@
property
def
is_integer
(
self
):
return
self
.
is_signed_integer
or
self
.
is_unsigned_integer
@
property
def
is_signed_integer
(
self
):
return
core
.
BNMetadataIsSignedInteger
(
self
.
handle
)
@
property
def
is_unsigned_integer
(
self
):
return
core
.
BNMetadataIsUnsignedInteger
(
self
.
handle
)
@
property
def
is_float
(
self
):
return
core
.
BNMetadataIsDouble
(
self
.
handle
)
@
property
def
is_boolean
(
self
):
return
core
.
BNMetadataIsBoolean
(
self
.
handle
)
@
property
def
is_string
(
self
):
return
core
.
BNMetadataIsString
(
self
.
handle
)
@
property
def
is_raw
(
self
):
"""deprecated in favor of `is_bytes`"""
return
core
.
BNMetadataIsRaw
(
self
.
handle
)
@
property
def
is_bytes
(
self
):
return
core
.
BNMetadataIsRaw
(
self
.
handle
)
@
property
def
is_array
(
self
):
return
core
.
BNMetadataIsArray
(
self
.
handle
)
@
property
def
is_dict
(
self
):
return
core
.
BNMetadataIsKeyValueStore
(
self
.
handle
)
def
append
(
self
,
value
):
"""Appends a value to the Metadata array."""
if
not
self
.
is_array
:
raise
TypeError
(
"Metadata object is not an array and does not support append"
)
md_value
=
Metadata
(
value
)
# Convert value to Metadata object
core
.
BNMetadataArrayAppend
(
self
.
handle
,
md_value
.
handle
)
def
remove
(
self
,
key_or_index
):
if
isinstance
(
key_or_index
,
str
)
and
self
.
is_dict
:
core
.
BNMetadataRemoveKey
(
self
.
handle
,
key_or_index
)
elif
isinstance
(
key_or_index
,
int
)
and
self
.
is_array
:
core
.
BNMetadataRemoveIndex
(
self
.
handle
,
key_or_index
)
else
:
raise
TypeError
(
"remove only valid for dict and array objects"
)
Back
|
FazBrowse Home
|
New Git URL