Skip to content
Snippets Groups Projects
Commit cbca0b1f authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

* added `BinaryParser.get_remaining_bytes`

* added `BinaryParser.get_remaining_bytes_count`
parent 95001983
No related branches found
No related tags found
No related merge requests found
# v2.18.1 # v2.18.1
* added `BinaryParser.get_remaining_bytes`
* added `BinaryParser.get_remaining_bytes_count`
__version__ = '2.18.1a1' __version__ = '2.18.1a2'
...@@ -6,7 +6,9 @@ from satella.exceptions import NotEnoughBytes ...@@ -6,7 +6,9 @@ from satella.exceptions import NotEnoughBytes
class BinaryParser: class BinaryParser:
""" """
A class that allows parsing binary streams easily A class that allows parsing binary streams easily.
This supports __len__ to return the amount of bytes remaining.
:param b_stream: an object that allows indiced access, and allows subscripts to :param b_stream: an object that allows indiced access, and allows subscripts to
span ranges, which will return items parseable by struct span ranges, which will return items parseable by struct
...@@ -15,6 +17,15 @@ class BinaryParser: ...@@ -15,6 +17,15 @@ class BinaryParser:
:ivar offset: offset from which bytes will be readed :ivar offset: offset from which bytes will be readed
""" """
def __len__(self) -> int:
return self.get_remaining_bytes_count()
def get_remaining_bytes_count(self) -> int:
"""
Return the amount of bytes remaining. This will not advance the pointer
"""
return self.stream_length - self.pointer
def __init__(self, b_stream: tp.Union[bytes, bytearray], offset: int = 0): def __init__(self, b_stream: tp.Union[bytes, bytearray], offset: int = 0):
self.b_stream = b_stream self.b_stream = b_stream
self.struct_cache = {} self.struct_cache = {}
...@@ -57,6 +68,8 @@ class BinaryParser: ...@@ -57,6 +68,8 @@ class BinaryParser:
This must be a single-character struct! This must be a single-character struct!
This will advance the pointer by size of st.
:param st: a single-character struct.Struct or a single character struct specification :param st: a single-character struct.Struct or a single character struct specification
:return: a value returned from it :return: a value returned from it
:raises NotEnoughBytes: not enough bytes remain in the stream! :raises NotEnoughBytes: not enough bytes remain in the stream!
...@@ -85,6 +98,8 @@ class BinaryParser: ...@@ -85,6 +98,8 @@ class BinaryParser:
""" """
Try to obtain as many bytes as this struct requires and return them parsed. Try to obtain as many bytes as this struct requires and return them parsed.
This will advance the pointer by size of st.
:param st: a struct.Struct or a multi character struct specification :param st: a struct.Struct or a multi character struct specification
:return: a tuple of un-parsed values :return: a tuple of un-parsed values
:raises NotEnoughBytes: not enough bytes remain in the stream! :raises NotEnoughBytes: not enough bytes remain in the stream!
...@@ -97,3 +112,11 @@ class BinaryParser: ...@@ -97,3 +112,11 @@ class BinaryParser:
return st.unpack(self.b_stream[self.pointer:self.pointer+st_len]) return st.unpack(self.b_stream[self.pointer:self.pointer+st_len])
finally: finally:
self.pointer += st_len self.pointer += st_len
def get_remaining_bytes(self) -> tp.Union[bytes, bytearray]:
"""
Return the remaining bytes.
This will not advance the pointer
"""
return self.b_stream[self.pointer:]
...@@ -14,3 +14,5 @@ class TestParsing(unittest.TestCase): ...@@ -14,3 +14,5 @@ class TestParsing(unittest.TestCase):
self.assertRaises(NotEnoughBytes, lambda: bp.get_struct('>L')) self.assertRaises(NotEnoughBytes, lambda: bp.get_struct('>L'))
self.assertRaises(NotEnoughBytes, lambda: bp.get_bytes(5)) self.assertRaises(NotEnoughBytes, lambda: bp.get_bytes(5))
self.assertRaises(NotEnoughBytes, lambda: BinaryParser(b'', 1)) self.assertRaises(NotEnoughBytes, lambda: BinaryParser(b'', 1))
self.assertEqual(bp.get_remaining_bytes(), b'\x00')
self.assertEqual(len(bp), 1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment