forked from FAForever/legacy-replay-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faPackets.py
executable file
·140 lines (122 loc) · 5.48 KB
/
faPackets.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
#-------------------------------------------------------------------------------
# Copyright (c) 2014 Gael Honorez.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0
# which accompanies this distribution, and is available at
# http://www.gnu.org/licenses/gpl.html
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------
import struct
class Packet():
def __init__(self, headersize=None , data=None, *values, **kwvalues):
self._data = data
self._values = kwvalues
self._headersize = headersize
def Unpack(self, data=None):
print "UNPACK"
if not data:
data = self._data
unpacked = {}
maxData = 0
while data:
maxData = maxData + 1
if maxData > 50 : break
headerSize = struct.unpack("i", data[:4])[0]
print "headerSize", headerSize
headerPackStr = "<i" + str(headerSize) + "si"
header = struct.unpack(headerPackStr, data[:headerSize+8])
print "header", header
headerStr = header[1].replace("/t","\t").replace("/n","\n")
if not unpacked.has_key(header[1]):
unpacked[headerStr] = []
chunkSize = header[2]
data = data[headerSize+8:]
print "chunkSize", chunkSize
print "data", data
chunk = []
for i in range(chunkSize):
fieldType = struct.unpack("b", data[:1])[0]
if fieldType is 0:
number = struct.unpack("i", data[1:5])[0]
chunk.append(number)
data = data[5:]
else:
fieldSize = struct.unpack("i", data[1:5])[0]
packStr = str(fieldSize) + "s"
string = struct.unpack(packStr, data[5:fieldSize+5])[0]
fixedStr = string.replace("/t","\t").replace("/n","\n")
chunk.append(fixedStr)
data = data[fieldSize+5:]
unpacked[headerStr].extend([chunk])
self._values = unpacked
return unpacked
def Pack(self):
if self._data:
return self._data
data = ""
for i, chunk in self._values.iteritems():
headerSize = len(str(i))
headerField = str(i).replace("\t","/t").replace("\n","/n")
chunkSize = len(chunk)
headerPackStr = "<i" + str(headerSize) + "si"
data += struct.pack(headerPackStr, headerSize, headerField, chunkSize)
chunkType = type(chunk)
if chunkType is list:
for field in chunk:
fieldType = 0 if type(field) is int else 1
chunkPackStr = ""
fields = []
if fieldType is 1:
fieldSize = len(field)
chunkPackStr += "<bi" + str(fieldSize) + "s"
fieldStr = field.replace("\t","/t").replace("\n","/n")
fields.extend([fieldType, fieldSize, fieldStr])
elif fieldType is 0:
chunkPackStr += "<bi"
fields.extend([fieldType, field])
data += struct.pack(chunkPackStr, *fields)
return data
def PackUdp(self):
if self._data:
return self._data
data = ""
for i, chunk in self._values.iteritems():
headerSize = len(str(i))
headerField = str(i).replace("\t","/t").replace("\n","/n")
chunkSize = len(chunk)
headerPackStr = "<i" + str(headerSize) + "si"
data += struct.pack(headerPackStr, headerSize, headerField, chunkSize)
chunkType = type(chunk)
i = 0
if chunkType is list:
for field in chunk:
fieldType = 0 if type(field) is int else 1
chunkPackStr = ""
fields = []
if fieldType is 1:
datas = "\x08"
if i == 1 :
fieldSize = len(field) + len(datas)
else :
fieldSize = len(field)
chunkPackStr += "<bi" + str(fieldSize) + "s"
fieldStr = field.replace("\t","/t").replace("\n","/n")
if i == 1 :
fields.extend([2, fieldSize, datas+fieldStr])
else :
fields.extend([fieldType, fieldSize, fieldStr])
elif fieldType is 0:
chunkPackStr += "<bi"
fields.extend([fieldType, field])
data += struct.pack(chunkPackStr, *fields)
i = 1
return data