-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
96 lines (75 loc) · 2.02 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
92
93
94
95
96
# -*- coding: utf-8 -*-
MIN_UNICODE_KANJI = u"\u4e00"
MAX_UNICODE_KANJI = u"\u9faf"
table = open("table.txt")
str2byte = {}
byte2str = {}
value = None
for line in table:
oldvalue = value
line = line.strip().split()
if len(line) >= 2:
value = int(line[-1], 16)
char = line[0]
elif len(line) == 1:
value = value + 1
char = line[0]
assert value
str2byte[char] = value
byte2str[value] = char
str2byte["\n"] = 0xff03
str2byte[" "] = 0xfffe
byte2str[0xfffe] = " "
byte2str[0xff03] = "\n"
def ints2int(data, bigend=True):
data = list(data)
if bigend:
data.reverse()
value = 0
for d in data:
value = (value << 8) | d
return value
def int2ints(value, size, bigend=True):
data = []
for i in range(size):
data.append(value & 0xff)
value = value >> 8
if not bigend:
data.reverse()
return data
def hexify(data, pad=None):
if pad:
h = lambda n: ("{0:0>%s}" % pad).format("%x" % n)
else:
h = lambda n: "%x" % n
try:
if type(data) in (list, tuple):
return map(h, data)
else:
return h(data)
except TypeError:
return None
def is_unicode_kanji(char):
if MIN_UNICODE_KANJI <= char <= MAX_UNICODE_KANJI:
return True
else:
return False
def gen_formatted(message, lookup=None, spaces=False):
if lookup is None:
lookup = byte2str
output = ""
for i in range(0, len(message), 2):
value = ints2int(message[i:i+2])
if value in lookup:
output += lookup[value]
else:
value = ints2int(message[i:i+2], bigend=True)
if spaces:
output = output.strip(' ')
output = "{0} $<{1:0>4}> ".format(output, hexify(value))
else:
output = "{0}$<{1:0>4}>".format(output, hexify(value))
output = "\n".join(line.strip() for line in output.split("\n"))
return output.strip()
if __name__ == "__main__":
pass