-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.py
executable file
·79 lines (53 loc) · 1.81 KB
/
packet.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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import codes
from pack import pack_uint8
from unpack import unpack_uint8
from tag import ECTag, unpack_ectag, unpack_ectag_tagcount
from misc import indentext
class ECPacket(ECTag):
def __init__(self, op, subtags=[ ]):
ECTag.__init__(self,)
self.op = op
self.subtags = subtags
def assertself(self):
assert self.op in codes.ops.keys()
for subtag in self.subtags:
subtag.assertself()
def pack(self):
self.assertself()
result = ""
result += self._pack_op()
result += self._pack_tagcount()
for tag in self.subtags:
result += tag.pack()
return result
def _pack_op(self):
op = codes.ops[self.op]
return pack_uint8(op)
def debugrepr(self, indent_level=0 ):
self.assertself()
result = ""
result += indentext( self._op_debugrepr(), indent_level )
result += indentext( "tagcount: %d \n" % len(self.subtags),
indent_level )
for tag in self.subtags:
result += tag.debugrepr(indent_level + 1)
return result
def _op_debugrepr(self):
opcode = codes.ops[self.op]
#return "op: %s | %s | %d \n" % ( self.op, hex(opcode), opcode )
return "op: %s | %s \n" % ( self.op, hex(opcode), )
def unpack_ecpacket(data, utf8_num=True ):
op, data = unpack_ecpacket_op(data)
op = codes.ops_rev[op]
tagcount, data = unpack_ectag_tagcount(data, utf8_num)
subtags = [ ]
for i in range(tagcount):
tag, data, _ = unpack_ectag(data, utf8_num)
subtags.append(tag)
return ECPacket( op, subtags)
def unpack_ecpacket_op(data):
length = 1
value, _ = unpack_uint8(data)
return value, data[length:]