summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--readers.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/readers.py b/readers.py
new file mode 100644
index 0000000..028f5c4
--- /dev/null
+++ b/readers.py
@@ -0,0 +1,33 @@
1import struct
2
3class ByteReader:
4 def __init__(self, data):
5 self.data = data
6 self.o = 0
7
8 def readbyte(self):
9 if self.o+1 > len(self.data): raise Exception("EOF")
10 self.o += 1
11 return struct.unpack_from("<B", self.data, self.o-1)[0]
12 def readword(self):
13 if self.o+2 > len(self.data): raise Exception("EOF")
14 self.o += 2
15 return struct.unpack_from("<H", self.data, self.o-2)[0]
16 def readdword(self):
17 if self.o+4 > len(self.data): raise Exception("EOF")
18 self.o += 4
19 return struct.unpack_from("<L", self.data, self.o-4)[0]
20 def readbytes(self, n=None):
21 if n is None:
22 n = len(self.data) - self.o
23 if self.o+n > len(self.data): raise Exception("EOF")
24 self.o += n
25 return self.data[self.o-n:self.o]
26
27 def readname(self):
28 namelen = self.readbyte()
29 return self.readbytes(namelen).decode('cp1251')
30
31 def eof(self):
32 return self.o >= len(self.data)
33