-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes_lib.py
119 lines (96 loc) · 2.28 KB
/
bytes_lib.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import struct
class BytesReader:
def __init__(self, data):
self.data = data
self.len = len(data)
self.pos = 0
#end define
def get(self, get_len):
new_pos = self.pos + get_len
if new_pos > self.len:
raise Exception(f"BytesReader error: get_len > len: ({self.pos} + {get_len}) > {self.len}")
result = self.data[self.pos:new_pos]
return result, new_pos
#end define
def read(self, read_len):
result, new_pos = self.get(read_len)
self.pos = new_pos
return result
#end define
def show(self, show_len):
result, new_pos = self.get(show_len)
return result
#end define
def read_int8(self):
data = self.read(1)
return struct.unpack("b", data)[0]
#end define
def read_uint8(self):
data = self.read(1)
return struct.unpack("B", data)[0]
#end define
def read_int16(self):
data = self.read(2)
return struct.unpack("h", data)[0]
#end define
def read_uint16(self):
data = self.read(2)
return struct.unpack("H", data)[0]
#end define
def read_int32(self):
data = self.read(4)
return struct.unpack("i", data)[0]
#end define
def read_uint32(self):
data = self.read(4)
return struct.unpack("I", data)[0]
#end define
def show_int8(self):
data = self.show(1)
return struct.unpack("b", data)[0]
#end define
def show_uint8(self):
data = self.show(1)
return struct.unpack("B", data)[0]
#end define
def show_int16(self):
data = self.show(2)
return struct.unpack("h", data)[0]
#end define
def show_uint16(self):
data = self.show(2)
return struct.unpack("H", data)[0]
#end define
def show_uint32(self):
data = self.show(4)
return struct.unpack("I", data)[0]
#end define
#end class
class BytesWriter:
def __init__(self):
self.data = bytes()
self.len = 0
#end define
def write(self, data):
self.data += data
self.len += len(data)
#end define
def write_int8(self, data):
self.write(struct.pack("b", data))
#end define
def write_uint8(self, data):
self.write(struct.pack("B", data))
#end define
def write_uint16(self, data):
self.write(struct.pack("H", data))
#end define
def write_uint32(self, data):
self.write(struct.pack("I", data))
#end define
def write_int16(self, data):
self.write(struct.pack("h", data))
#end define
def write_int32(self, data):
self.write(struct.pack("i", data))
#end define
#end class