forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabuffer.py
176 lines (153 loc) · 6.13 KB
/
databuffer.py
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
# Copyright (c) 2015-2025 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 Optional, Union
# Binary Ninja components
from . import _binaryninjacore as core
DataBufferInputType = Union[str, bytes, 'DataBuffer', int]
class DataBuffer:
def __init__(self, contents: Union[str, bytes, 'DataBuffer', int] = b"", handle=None):
if handle is not None:
self.handle = core.handle_of_type(handle, core.BNDataBuffer)
elif isinstance(contents, int):
self.handle = core.BNCreateDataBuffer(None, contents)
elif isinstance(contents, DataBuffer):
self.handle = core.BNDuplicateDataBuffer(contents.handle)
elif isinstance(contents, str):
self.handle = core.BNCreateDataBuffer(contents.encode("utf-8"), len(contents.encode("utf-8")))
elif isinstance(contents, bytes):
self.handle = core.BNCreateDataBuffer(contents, len(contents))
elif isinstance(contents, bytearray):
b = bytes(contents)
self.handle = core.BNCreateDataBuffer(b, len(b))
else:
raise TypeError(f"type {type(contents)} not convertable to DataBuffer")
def __del__(self):
if core is not None:
core.BNFreeDataBuffer(self.handle)
def __len__(self):
return int(core.BNGetDataBufferLength(self.handle))
def __getitem__(self, i) -> bytes:
if isinstance(i, tuple):
result = bytes()
for s in i:
result += self.__getitem__(s)
return result
elif isinstance(i, slice):
if i.step is not None:
i = i.indices(len(self))
start = i[0]
stop = i[1]
if stop <= start:
return b""
buf = ctypes.create_string_buffer(stop - start)
data = core.BNGetDataBufferContentsAt(self.handle, start)
assert data is not None, "core.BNGetDataBufferContentsAt returned None"
ctypes.memmove(buf, data, stop - start)
return buf.raw
else:
return bytes(self)[i]
elif i < 0:
if i >= -len(self):
return core.BNGetDataBufferByte(self.handle, int(len(self) + i)).to_bytes(1, "little")
raise IndexError("index out of range")
elif i < len(self):
return core.BNGetDataBufferByte(self.handle, int(i)).to_bytes(1, "little")
else:
raise IndexError("index out of range")
def __setitem__(self, i, value):
if isinstance(i, slice):
if i.step is not None:
raise IndexError("step not supported on assignment")
i = i.indices(len(self))
start = i[0]
stop = i[1]
if stop < start:
stop = start
if len(value) != (stop - start):
data = bytes(self)
data = data[0:start] + value + data[stop:]
core.BNSetDataBufferContents(self.handle, data, len(data))
else:
value = str(value)
buf = ctypes.create_string_buffer(len(value))
data = core.BNGetDataBufferContentsAt(self.handle, start)
assert data is not None, "core.BNGetDataBufferContentsAt returned None"
ctypes.memmove(data, buf, len(value))
elif i < 0:
if i >= -len(self):
if len(value) != 1:
raise ValueError("expected single byte for assignment")
value = str(value)
buf = ctypes.create_string_buffer(len(value))
data = core.BNGetDataBufferContentsAt(self.handle, int(len(self) + i))
assert data is not None, "core.BNGetDataBufferContentsAt returned None"
ctypes.memmove(data, buf, 1)
else:
raise IndexError("index out of range")
elif i < len(self):
if len(value) != 1:
raise ValueError("expected single byte for assignment")
value = str(value)
buf = ctypes.create_string_buffer(len(value))
data = core.BNGetDataBufferContentsAt(self.handle, int(i))
assert data is not None, "core.BNGetDataBufferContentsAt returned None"
ctypes.memmove(data, buf, 1)
else:
raise IndexError("index out of range")
def __str__(self):
buf = ctypes.create_string_buffer(len(self))
data = core.BNGetDataBufferContents(self.handle)
assert data is not None, "core.BNGetDataBufferContents returned None"
ctypes.memmove(buf, data, len(self))
return buf.raw.decode('utf8')
def __bytes__(self):
buf = ctypes.create_string_buffer(len(self))
data = core.BNGetDataBufferContents(self.handle)
assert data is not None, "core.BNGetDataBufferContents returned None"
ctypes.memmove(buf, data, len(self))
return buf.raw
def __eq__(self, other: 'DataBuffer') -> bool:
# Not cryptographically secure
if len(self) != len(other):
return False
return bytes(self) == bytes(other)
def escape(self, null_terminates=False, escape_printable=False) -> str:
return core.BNDataBufferToEscapedString(self.handle, null_terminates, escape_printable)
def unescape(self) -> 'DataBuffer':
return DataBuffer(handle=core.BNDecodeEscapedString(str(self)))
def base64_encode(self) -> str:
return core.BNDataBufferToBase64(self.handle)
def base64_decode(self) -> 'DataBuffer':
return DataBuffer(handle=core.BNDecodeBase64(str(self)))
def zlib_compress(self) -> Optional['DataBuffer']:
buf = core.BNZlibCompress(self.handle)
if buf is None:
return None
return DataBuffer(handle=buf)
def zlib_decompress(self) -> Optional['DataBuffer']:
buf = core.BNZlibDecompress(self.handle)
if buf is None:
return None
return DataBuffer(handle=buf)
def escape_string(text: bytes) -> str:
return DataBuffer(text).escape()
def unescape_string(text: bytes) -> 'DataBuffer':
return DataBuffer(text).unescape()