-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
91 lines (72 loc) · 2.11 KB
/
utils.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import ctypes
import hashlib
import sys
import time
# Inner Function
def parse_int(_bytes, little_end=True):
if not little_end:
return int(_bytes.hex(), 16)
temp_bytes = b''
for i in range(len(_bytes)):
temp_bytes = _bytes[i: i + 1] + temp_bytes
return int(temp_bytes.hex(), 16)
def parse_str(_bytes):
try:
string = _bytes.decode('utf-8')
string = string.replace('\x00', '')
return string
except UnicodeDecodeError as e:
# print(e)
string = _bytes.hex()
string_list = []
for i in range(int(len(string) / 2)):
string_list.append(string[i * 2: i * 2 + 2])
string = ""
for element in string_list:
string += ("\\x" + element)
return string
def log_error(error):
print("[Error] %s" % (error))
def uleb128(_bytes, offset=0x0):
p = _bytes[offset]
start = offset
result = 0
bit = 0
while True:
slice = p & 0x7f
if bit >= 64 or slice << bit >> bit != slice:
log_error("ULEB128 Error!")
break
else:
result |= (slice << bit)
bit += 7
if p & 0x80 == 0: # 最高位是 0,则结束
break
offset += 1
p = _bytes[offset]
result = ctypes.c_int64(result).value
return result, offset - start
def sorted_list_for_hex_string(l):
def convert_to_int(s):
return int(s, 16)
def convert_to_str(i):
return hex(i)
# before = datetime.now()
l = list(map(convert_to_int, l))
# after = datetime.now()
# print((after - before).microseconds)
# before = datetime.now()
l.sort()
# after = datetime.now()
# print((after - before).microseconds)
# before = datetime.now()
l = list(map(convert_to_str, l))
# after = datetime.now()
# print((after - before).microseconds)
return l
def md5_for_file(file_path):
hash_code = hashlib.md5(open(file_path, 'rb').read()).hexdigest()
return hash_code
def time_to_str(t):
time_struct = time.localtime(t)
return time.strftime('%Y/%m/%d %H:%M:%S', time_struct)