-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbitconv.py
More file actions
48 lines (44 loc) · 1.57 KB
/
bitconv.py
File metadata and controls
48 lines (44 loc) · 1.57 KB
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
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
def str_encode(data, pos, value, strsz, encoding='ascii', terminator=b'\x00'):
try:
bytedata = value.encode(encoding)
if strsz < 0:
bytedata += terminator
strsz = len(bytedata)
if len(bytedata) < strsz:
bytedata += terminator * (strsz - len(bytedata))
elif len(bytedata) > strsz:
raise ValueError('input string too long')
if pos >= len(data):
data += bytedata
else:
data = data[:pos] + bytedata + data[pos+strsz:]
return data, pos + strsz
except IndexError:
return data, pos + strsz
def int_encode(data, pos, value, intsz=4, byteorder='little'):
try:
bytedata = value.to_bytes(intsz, byteorder=byteorder)
if pos >= len(data):
data += bytedata
else:
data = data[:pos] + bytedata + data[pos+intsz:]
return data, pos + intsz
except IndexError:
return data, pos + intsz
def str_decode(data, pos, strsz, encoding='ascii', terminator=b'\x00'):
try:
if strsz < 0:
strsz = data[pos:].find(terminator) + 1
if strsz < 0:
strsz = 1
value = data[pos:pos+strsz].rstrip(terminator).decode(encoding)
return value, pos + strsz
except IndexError:
return None, pos + strsz
def int_decode(data, pos, intsz=4, byteorder='little'):
try:
value = int.from_bytes(data[pos:pos+intsz], byteorder=byteorder)
return value, pos + intsz
except IndexError:
return None, pos + intsz