MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / DataBuffer

Class DataBuffer

python/databuffer.py:30–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29
30class DataBuffer:
31 def __init__(self, contents: Union[str, bytes, 'DataBuffer', int] = b"", handle=None):
32 if handle is not None:
33 self.handle = core.handle_of_type(handle, core.BNDataBuffer)
34 elif isinstance(contents, int):
35 self.handle = core.BNCreateDataBuffer(None, contents)
36 elif isinstance(contents, DataBuffer):
37 self.handle = core.BNDuplicateDataBuffer(contents.handle)
38 elif isinstance(contents, str):
39 self.handle = core.BNCreateDataBuffer(contents.encode("utf-8"), len(contents.encode("utf-8")))
40 elif isinstance(contents, bytes):
41 self.handle = core.BNCreateDataBuffer(contents, len(contents))
42 elif isinstance(contents, bytearray):
43 b = bytes(contents)
44 self.handle = core.BNCreateDataBuffer(b, len(b))
45 else:
46 raise TypeError(f"type {type(contents)} not convertable to DataBuffer")
47
48
49 def __del__(self):
50 if core is not None:
51 core.BNFreeDataBuffer(self.handle)
52
53 def __len__(self):
54 return int(core.BNGetDataBufferLength(self.handle))
55
56 def __getitem__(self, i) -> bytes:
57 if isinstance(i, tuple):
58 result = bytes()
59 for s in i:
60 result += self.__getitem__(s)
61 return result
62 elif isinstance(i, slice):
63 if i.step is not None:
64 i = i.indices(len(self))
65 start = i[0]
66 stop = i[1]
67 if stop <= start:
68 return b""
69 buf = ctypes.create_string_buffer(stop - start)
70 data = core.BNGetDataBufferContentsAt(self.handle, start)
71 assert data is not None, "core.BNGetDataBufferContentsAt returned None"
72 ctypes.memmove(buf, data, stop - start)
73 return buf.raw
74 else:
75 return bytes(self)[i]
76 elif i < 0:
77 if i >= -len(self):
78 return core.BNGetDataBufferByte(self.handle, int(len(self) + i)).to_bytes(1, "little")
79 raise IndexError("index out of range")
80 elif i < len(self):
81 return core.BNGetDataBufferByte(self.handle, int(i)).to_bytes(1, "little")
82 else:
83 raise IndexError("index out of range")
84
85 def __setitem__(self, i, value):
86 if isinstance(i, slice):
87 if i.step is not None:

Callers 6

unescapeMethod · 0.70
base64_decodeMethod · 0.70
zlib_compressMethod · 0.70
zlib_decompressMethod · 0.70
escape_stringFunction · 0.70
unescape_stringFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected