You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
import struct
|
|
|
|
class Data:
|
|
def __init__(self, rawdata = b"", off = 0):
|
|
self.data = rawdata
|
|
self.offset = off
|
|
def setData(self, rawdata, off = 0):
|
|
self.data = rawdata
|
|
self.offset = off
|
|
def seek(self, off):
|
|
self.offset = off
|
|
def seekEnd(self, off = 0):
|
|
self.offset = len(self.data) + off
|
|
def seekRel(self, off = 0):
|
|
self.offset += off
|
|
def tell(self):
|
|
return self.offset
|
|
def decode(self, fmt):
|
|
size = struct.calcsize(fmt)
|
|
val = struct.unpack(fmt, self.data[self.offset : self.offset + size])
|
|
self.offset += size
|
|
return val
|
|
def encode(self, fmt, *args):
|
|
data = struct.pack(fmt, *args)
|
|
self.write(data)
|
|
def read(self, length):
|
|
val = self.data[self.offset : self.offset + length]
|
|
self.offset += length
|
|
return val
|
|
def write(self, data):
|
|
#todo: handle
|
|
if self.offset == len(self.data):
|
|
self.data += data
|
|
self.offset = len(self.data)
|
|
elif self.offset > len(self.data):
|
|
self.data += ZERO_BYTE * (self.offset - len(self.data))
|
|
self.data += data
|
|
self.offset = len(self.data)
|
|
elif self.offset + len(data) >= len(self.data):
|
|
self.data = self.data[0 : self.offset] + data
|
|
else:
|
|
self.data = self.data[0 : self.offset] + data + self.data [self.offset + len(data) : ]
|
|
def truncate(self, offset = None):
|
|
if offset is None:
|
|
offset = self.offset
|
|
self.data[0 : offset]
|
|
def __len__(self):
|
|
return len(self.data)
|
|
def __str__(self):
|
|
return str(self.data)
|
|
def __repr__(self):
|
|
return repr(self.data)
|
|
|