diff options
| author | Peter LaFosse <peter@vector35.com> | 2021-08-27 13:18:01 -0400 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2021-09-06 11:46:43 -0400 |
| commit | 63b3a0838334e6eb728d0e3c8250265e58c4cf4c (patch) | |
| tree | 7c1ef1850391906c96674b83a708448c5d4cee3b /python/databuffer.py | |
| parent | bff5a6e38c945833ddb22c379d57f556244a2e25 (diff) | |
Add some additional typehints to architecture.py, function.py and metadata.py, callingconvention.py, binaryview.py, highlevelil.py, scriptingprovider.py, types.py
Fix typehint for Union[QualifiedName, str]
Diffstat (limited to 'python/databuffer.py')
| -rw-r--r-- | python/databuffer.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/python/databuffer.py b/python/databuffer.py index d99933a4..baf35b17 100644 --- a/python/databuffer.py +++ b/python/databuffer.py @@ -19,18 +19,22 @@ # 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:bytes=b"", handle=None): if handle is not None: self.handle = core.handle_of_type(handle, core.BNDataBuffer) - elif isinstance(contents, int) or isinstance(contents, int): + 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"))) else: self.handle = core.BNCreateDataBuffer(contents, len(contents)) @@ -125,34 +129,34 @@ class DataBuffer: ctypes.memmove(buf, data, len(self)) return buf.raw - def escape(self): + def escape(self) -> str: return core.BNDataBufferToEscapedString(self.handle) - def unescape(self): + def unescape(self) -> 'DataBuffer': return DataBuffer(handle=core.BNDecodeEscapedString(bytes(self))) - def base64_encode(self): + def base64_encode(self) -> str: return core.BNDataBufferToBase64(self.handle) - def base64_decode(self): + def base64_decode(self) -> 'DataBuffer': return DataBuffer(handle = core.BNDecodeBase64(bytes(self))) - def zlib_compress(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): + 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): +def escape_string(text:bytes) -> str: return DataBuffer(text).escape() -def unescape_string(text): +def unescape_string(text:bytes) -> 'DataBuffer': return DataBuffer(text).unescape() |
