You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.5 KiB
124 lines
3.5 KiB
7 years ago
|
import struct
|
||
|
|
||
7 years ago
|
from gex.TinyFrame import TF_Msg
|
||
|
|
||
|
|
||
7 years ago
|
class PayloadParser:
|
||
7 years ago
|
"""
|
||
|
Utility for parsing a binary payload
|
||
|
"""
|
||
|
|
||
7 years ago
|
def __init__(self, buf, endian:str='little'):
|
||
7 years ago
|
"""
|
||
|
buf - buffer to parse (bytearray or binary string)
|
||
|
"""
|
||
7 years ago
|
|
||
|
if type(buf) == TF_Msg:
|
||
|
buf = buf.data
|
||
|
|
||
7 years ago
|
self.buf = buf
|
||
|
self.ptr = 0
|
||
|
self.endian = endian
|
||
|
|
||
7 years ago
|
def _slice(self, n:int) -> bytearray:
|
||
7 years ago
|
""" Extract a slice and advance the read pointer for the next slice """
|
||
7 years ago
|
if self.ptr + n > len(self.buf):
|
||
7 years ago
|
raise Exception("Payload parser underrun - frame: %s" % str(self.buf))
|
||
7 years ago
|
|
||
|
slice = self.buf[self.ptr:self.ptr + n]
|
||
|
self.ptr += n
|
||
|
return slice
|
||
|
|
||
7 years ago
|
def length(self) -> int:
|
||
|
""" Measure the tail """
|
||
|
return len(self.buf) - self.ptr
|
||
|
|
||
7 years ago
|
def rewind(self):
|
||
|
""" Reset the slice pointer to the beginning """
|
||
|
self.ptr = 0
|
||
|
|
||
7 years ago
|
def tail(self) -> bytearray:
|
||
7 years ago
|
""" Get all remaining bytes """
|
||
|
return self._slice(len(self.buf) - self.ptr)
|
||
|
|
||
7 years ago
|
def u8(self) -> int:
|
||
7 years ago
|
""" Read a uint8_t """
|
||
|
slice = self._slice(1)
|
||
7 years ago
|
return int.from_bytes(slice, byteorder=self.endian, signed=False)
|
||
|
|
||
7 years ago
|
def u16(self) -> int:
|
||
7 years ago
|
""" Read a uint16_t """
|
||
|
slice = self._slice(2)
|
||
7 years ago
|
return int.from_bytes(slice, byteorder=self.endian, signed=False)
|
||
|
|
||
7 years ago
|
def u24(self) -> int:
|
||
|
""" Read a uint24_t """
|
||
|
slice = self._slice(3)
|
||
|
return int.from_bytes(slice, byteorder=self.endian, signed=False)
|
||
|
|
||
7 years ago
|
def u32(self) -> int:
|
||
7 years ago
|
""" Read a uint32_t """
|
||
|
slice = self._slice(4)
|
||
7 years ago
|
return int.from_bytes(slice, byteorder=self.endian, signed=False)
|
||
|
|
||
7 years ago
|
def u64(self) -> int:
|
||
|
""" Read a uint64_t """
|
||
|
slice = self._slice(8)
|
||
|
return int.from_bytes(slice, byteorder=self.endian, signed=False)
|
||
|
|
||
7 years ago
|
def i8(self) -> int:
|
||
7 years ago
|
""" Read a int8_t """
|
||
|
slice = self._slice(1)
|
||
7 years ago
|
return int.from_bytes(slice, byteorder=self.endian, signed=True)
|
||
|
|
||
7 years ago
|
def i16(self) -> int:
|
||
7 years ago
|
""" Read a int16_t """
|
||
|
slice = self._slice(2)
|
||
7 years ago
|
return int.from_bytes(slice, byteorder=self.endian, signed=True)
|
||
|
|
||
7 years ago
|
def i32(self) -> int:
|
||
7 years ago
|
""" Read a int32_t """
|
||
|
slice = self._slice(4)
|
||
7 years ago
|
return int.from_bytes(slice, byteorder=self.endian, signed=True)
|
||
|
|
||
7 years ago
|
def i64(self) -> int:
|
||
|
""" Read a int64_t """
|
||
|
slice = self._slice(8)
|
||
|
return int.from_bytes(slice, byteorder=self.endian, signed=True)
|
||
|
|
||
7 years ago
|
def float(self) -> float:
|
||
7 years ago
|
""" Read a float (4 bytes) """
|
||
|
slice = self._slice(4)
|
||
7 years ago
|
fmt = '<f' if self.endian == 'little' else '>f'
|
||
|
return struct.unpack(fmt, slice)[0]
|
||
|
|
||
7 years ago
|
def double(self) -> float:
|
||
7 years ago
|
""" Read a double (8 bytes) """
|
||
|
slice = self._slice(8)
|
||
|
fmt = '<d' if self.endian == 'little' else '>d'
|
||
|
return struct.unpack(fmt, slice)[0]
|
||
|
|
||
7 years ago
|
def bool(self) -> bool:
|
||
7 years ago
|
""" Read a bool (1 byte, True if != 0) """
|
||
|
return 0 != self._slice(1)[0]
|
||
7 years ago
|
|
||
7 years ago
|
def str(self) -> str:
|
||
7 years ago
|
""" Read a zero-terminated string """
|
||
7 years ago
|
p = self.ptr
|
||
|
while p < len(self.buf) and self.buf[p] != 0:
|
||
|
p += 1
|
||
|
|
||
7 years ago
|
bs = self._slice(p - self.ptr)
|
||
7 years ago
|
self.ptr += 1
|
||
|
return bs.decode('utf-8')
|
||
|
|
||
7 years ago
|
def blob(self, length) -> bytearray:
|
||
7 years ago
|
""" Read a blob of given length """
|
||
|
return self._slice(length)
|
||
7 years ago
|
|
||
|
def skip(self, nbytes:int):
|
||
7 years ago
|
""" Skip some bytes. returns self for chaining. """
|
||
7 years ago
|
self.blob(nbytes)
|
||
7 years ago
|
return self
|
||
|
|