-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
38 lines (35 loc) · 895 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def read_varlen(data):
NEXTBYTE = 1
value = 0
while NEXTBYTE:
chr = next(data)
# is the hi-bit set?
if not (chr & 0x80):
# no next BYTE
NEXTBYTE = 0
# mask out the 8th bit
chr = chr & 0x7f
# shift last value up 7 bits
value = value << 7
# add new value
value += chr
return value
def write_varlen(value):
chr1 = chr(value & 0x7F)
value >>= 7
if value:
chr2 = chr((value & 0x7F) | 0x80)
value >>= 7
if value:
chr3 = chr((value & 0x7F) | 0x80)
value >>= 7
if value:
chr4 = chr((value & 0x7F) | 0x80)
res = chr4 + chr3 + chr2 + chr1
else:
res = chr3 + chr2 + chr1
else:
res = chr2 + chr1
else:
res = chr1
return res