-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.py
executable file
·292 lines (203 loc) · 7.7 KB
/
tag.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import struct
import codes
from pack import pack_uint8, pack_uint16, pack_uint32, pack_uint, \
pack_string, pack_double, pack_ipv4, pack_hash16
from unpack import unpack_uint8, unpack_uint16, unpack_uint32, unpack_uint, \
unpack_string, unpack_double, unpack_ipv4, unpack_hash16,\
unpack_custom, unpack_unknown, unpack_utf8_num
from misc import indentext
class ECTag:
def __init__ (self, tagname=None, tagtype=None, tagdata=None, subtags=[ ] ):
self.tagname = tagname
self.tagtype = tagtype
self.tagdata = tagdata
self.subtags = subtags
def assertself(self):
""" called befor packing, or after unpacking"""
assert self.tagname in codes.tags.keys()
assert self.tagtype in codes.tagtypes.keys()
for subtag in self.subtags:
subtag.assertself()
def addtag(self, subtag):
self.subtags.append(subtag)
def gettagsbyname(self, tagname):
"""get all tags matching specified tagname"""
result = [ ]
if self.subtags:
for subtag in self.subtags:
result.extend( subtag.gettagsbyname(tagname) )
elif self.tagname == tagname :
result.append(self)
else:
pass
return result
def pack(self):
"""create binary representatio of this ECTag, which is used in
communication"""
self.assertself()
result = ""
result += self._pack_name()
result += self._pack_type()
subtags_bin = self._pack_subtags()
tagdata_bin = self._pack_data()
taglen = len(subtags_bin) + len(tagdata_bin)
result += pack_uint32(taglen)
if self.subtags:
result += self._pack_tagcount()
result += subtags_bin
result += tagdata_bin
return result
def _pack_name(self):
tagname = codes.tags[self.tagname]
if self.subtags :
tagname = tagname * 2 + 1
else:
tagname = tagname * 2
# FIXME : should we alwasy send utf-8-lized number, instead?
return pack_uint16(tagname)
# tagtype should use 1 byte
def _pack_type(self):
return pack_uint8( codes.tagtypes[self.tagtype] )
def _pack_tagcount(self):
return pack_uint16( len(self.subtags) )
def _pack_subtags(self):
result = ""
for subtag in self.subtags:
result += subtag.pack()
return result
def _pack_data(self):
result = ""
tagtype = self.tagtype
tagdata = self.tagdata
if tagtype in ('uint8', 'uint16', 'uint32', 'uint64'):
result += pack_uint(tagdata, tagtype)
elif tagtype == 'string':
result += pack_string(tagdata)
elif tagtype == 'hash16':
result += pack_hash16(tagdata)
elif tagtype == 'ipv4':
result += pack_ipv4(tagdata)
elif tagtype == 'double':
result += pack_double(tagdata)
else:
raise ValueError("type %s is not supported yet" % tagtype)
return result
def debugrepr (self, indent_level=0 ):
result = ""
result += indentext( self._tagname_debugrepr(), indent_level)
result += indentext( self._tagtype_debugrepr(), indent_level)
if self.subtags:
result += indentext( "subtags: %d\n" % len(self.subtags),
indent_level )
for tag in self.subtags:
result += tag.debugrepr(indent_level + 1)
result += indentext (self._tagdata_debugrepr(), indent_level )
result += "\n"
return result
def _tagname_debugrepr(self):
tagnamecode = codes.tags[self.tagname]
return "tagname: %s | %s \n" % ( self.tagname, hex(tagnamecode) )
def _tagtype_debugrepr(self):
tagtypecode = codes.tagtypes[self.tagtype]
return "tagtype: %s | %s \n" % ( self.tagtype, hex(tagtypecode) )
def _tagdata_debugrepr(self):
# strings are always stored as unicode string,
# special treatment is need for them.
if self.tagtype == 'string':
return "tagdata: %s " % unicode.encode(self.tagdata, "utf-8")
else:
return "tagdata: %s " % str(self.tagdata)
def unpack_ectag(data, utf8_num=True):
tagname, data = unpack_ectag_tagname(data, utf8_num)
tagname, has_subtags = analyze_ectag_tagname(tagname)
tagtype, data = unpack_tagtype(data)
taglen, data = unpack_ectag_taglen(data, utf8_num)
consumed_len = 0
subtags = [ ]
if has_subtags :
tagcount, data = unpack_ectag_tagcount(data, utf8_num)
# field taglen did not take the optional field tagcount
# into consideration
# DO NOT add 2 onto consumed_len here!
for i in range(tagcount):
subtag, data, advance = unpack_ectag(data, utf8_num)
subtags.append(subtag)
consumed_len += advance
tagdata_len = taglen - consumed_len
tagdata, data = unpack_ectag_tagdata(data, tagtype, tagdata_len)
tagname = codes.tags_rev[tagname]
tagtype = codes.tagtypes_rev[tagtype]
tag = ECTag(tagname, tagtype, tagdata, subtags)
#print tag.debugrepr()
tag.assertself()
# tagname: 2 bytes
# tagtype: 1 bytes
# taglen: 4 bytes
# tagcount : optional 2 bytes
return tag, data, taglen + 2 + 1 + 4 + ( 2 if has_subtags else 0 )
def analyze_ectag_tagname(tagname):
# if the lowest bit set, then this tag contins subtags
if (tagname % 2) == 1:
has_subtags = True
else:
has_subtags = False
return tagname/2, has_subtags
# uint16 need to take care of utf-8-lized number
def unpack_ectag_tagname(data, utf8_num=True):
value = -1
length = -1
if utf8_num:
return unpack_utf8_num(data)
else:
length = 2
#value, = unpack( '!H', data[:length] )
value, _ = unpack_uint16( data )
return value, data[length:]
# FIXME; this could be optimized as simply ' return data[0], 1'
def unpack_tagtype(data):
value = -1
length = 1
value, _ = unpack_uint8(data)
return value, data[length:]
# uint32 need to take care of utf-8-lized number
def unpack_ectag_taglen(data, utf8_num=True ):
value = -1
length = -1
if utf8_num:
return unpack_utf8_num(data)
else:
length = 4
value, _ = unpack_uint32(data)
return value, data[length:]
def unpack_ectag_tagcount(data, utf8_num=True ):
if utf8_num:
return unpack_utf8_num(data)
else:
length = 2
value, _ = unpack_uint16( data )
return value, data[length:]
def unpack_ectag_tagdata(data, tagtype, length):
value = -1
if tagtype in [ codes.tagtypes['uint8'] ,
codes.tagtypes['uint16'],
codes.tagtypes['uint32'],
codes.tagtypes['uint64'] ]:
value, data = unpack_uint(data, length)
elif tagtype == codes.tagtypes['string'] :
value, data = unpack_string(data, length)
elif tagtype == codes.tagtypes['hash16']:
value, data = unpack_hash16(data, length)
elif tagtype == codes.tagtypes['ipv4']:
value, data = unpack_ipv4(data, length)
elif tagtype == codes.tagtypes['double']:
value, data = unpack_double(data, length)
elif tagtype == codes.tagtypes['custom']:
#raise ValueError("[unpack_ectag_tagdata] type 'custom' is unsupported ")
value, data = unpack_custom(data, length)
elif tagtype == codes.tagtypes['unknown']:
value, data = unpack_unknown(data, length)
else:
raise ValueError("invalid data type %d " % tagtype)
return value, data