-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
65 lines (49 loc) · 1.49 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
from collections import namedtuple
from struct import calcsize, pack, unpack
class StructBase:
structure = {}
def get_format(self):
return pack_format(self.structure)
def get_items(self):
return (getattr(self, k) for k, v in self.structure.items())
def get_packed(self):
return pack(self.get_format(), *self.get_items())
def get_size(self):
return calcsize(self.get_format())
def set_data(self, data):
Structure = namedtuple('Structure', ' '.join(self.structure.keys()))
unpacked = Structure._make(unpack(self.get_format(), data[:self.get_size()]))
for key in self.structure:
setattr(self, key, getattr(unpacked, key))
def __init_subclass__(cls, data=None):
super().__init_subclass__()
for k in cls.structure:
setattr(cls, k, b'\x00' * cls.structure[k])
if isinstance(data, bytes):
cls.set_data(data)
def byte2int(val):
return int.from_bytes(val, 'big')
def get_hex_str(val, val_len=1):
return '{0:0{1}x}'.format(val, val_len*2)
def get_pretty_hex(val, val_len=1):
return '0x' + get_hex_str(val, val_len)
def int2byte(val, val_len=1):
return bytes(bytearray.fromhex(get_hex_str(val, val_len)))
def pad_by(val, size=64):
by = val % size
if by:
by = size - by
return by
def pad_zeros(val, size=64):
rval = b''
for i in range(pad_by(val, size)):
rval += b'\00'
return rval
def pack_format(format_dict):
rval = '>'
for k, v in format_dict.items():
rval += str(v) + 's'
return rval
def str2byte(val):
return bytes(bytearray.fromhex(val))