diff options
author | itsme <itsme@xs4all.nl> | 2021-07-07 12:24:15 +0200 |
---|---|---|
committer | itsme <itsme@xs4all.nl> | 2021-07-07 12:24:15 +0200 |
commit | bb1e9b68213b74d5c777425bbd8a3a1216b4c4b8 (patch) | |
tree | 38fd58480df902d10636429ffdb589c4496e8955 | |
parent | 0ce5384a5660e5cb506d0336840e4c99bce0191a (diff) |
forgot to add readers.py
-rw-r--r-- | readers.py | 33 |
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 @@ | |||
1 | import struct | ||
2 | |||
3 | class 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 | |||