-
Notifications
You must be signed in to change notification settings - Fork 4
/
static.py
executable file
·422 lines (321 loc) · 9.99 KB
/
static.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import re
import struct
from Peach import generator
from Peach.generator import *
class Static(generator.Generator):
"""
Contains a static value that never changes.
Value can be any form of static data.
Example:
>>> gen = Static('Hello world')
>>> print gen.getValue()
Hello world
@see: L{StaticBinary}
"""
_value = ''
def __init__(self, value):
"""
@type value: string
@param value: Static data
"""
Generator.__init__(self)
self.setValue(value)
def getRawValue(self):
return self._value
def setValue(self, value):
"""
Set static value to return.
@type value: string
@param value: Static data
@rtype: Static
@return: self
"""
self._value = str(value)
return self
def next(self):
raise generator.GeneratorCompleted("STATIC")
class _StaticFromTemplate(Static):
"""
This Static is for use with Peach 2.0. The value
will be gotten from the Template object every time
"""
def __init__(self, action, node):
"""
@type action: Action instance
@param action: Action that contains data model
@type node: DataElement
@param node: Data element to get value from
"""
Static.__init__(self, None)
self.action = action
self.elementName = node.getFullnameInDataModel()
def getRawValue(self):
"""
Get the "raw" value which will then get run threw any transformers
associated with this Generator.
However, since we are getting the value of a DataElement we don't
want to get the internal value, we want the actual value.
"""
node = self.action.template.findDataElementByName(self.elementName)
return node.getValue()
class _StaticAlwaysNone(Static):
def __init__(self):
Static.__init__(self, None)
def getRawValue(self):
"""
Get the "raw" value which will then get run threw any transformers
associated with this Generator.
However, since we are getting the value of a DataElement we don't
want to get the internal value, we want the actual value.
"""
return None
class _StaticCurrentValueFromDom(Static):
"""
This Static is for use with Peach 2.0. The value
will be gotten from the Template object every time
"""
def __init__(self, obj):
"""
@type value: string
@param value: String of hex values
"""
Static.__init__(self, None)
self.template = obj
def getRawValue(self):
return self.template.currentValue
class StaticBinary(Static):
"""
Contains some binary data. Can be set by string containing
several formats of binary data such as " FF FF FF FF " or
"\xFF \xFF \xFF", etc.
Example:
>>> gen = StaticBinary(41414141414141)
>>>
>>> print gen.getValue()
AAAAAAAAA
"""
# Ordering of regex's can be important as the last
# regex can falsly match some of its priors.
_regsHex = (
re.compile(r"^(\s*\\x([a-zA-Z0-9]{2})\s*)"),
re.compile(r"^(\s*%([a-zA-Z0-9]{2})\s*)"),
re.compile(r"^(\s*0x([a-zA-Z0-9]{2})\s*)"),
re.compile(r"^(\s*x([a-zA-Z0-9]{2})\s*)"),
re.compile(r"^(\s*([a-zA-Z0-9]{2})\s*)")
)
def __init__(self, value):
"""
@type value: string
@param value: String of hex values
"""
Static.__init__(self, value)
self.setValue(value)
def setValue(self, value):
"""
Set binary data to be used.
@type value: string
@param value: String of hex values
"""
ret = ''
for i in range(len(self._regsHex)):
match = self._regsHex[i].search(value)
if match is not None:
while match is not None:
ret += chr(int(match.group(2), 16))
value = self._regsHex[i].sub('', value)
match = self._regsHex[i].search(value)
break
self._value = ret
@staticmethod
def unittest():
s = StaticBinary('41 41 41 41')
if s.getValue() != 'AAAA':
raise Exception('StaticBinary::unittest(): getValue 1 failed')
s = StaticBinary('0x41 0x41 0x41 0x41')
if s.getValue() != 'AAAA':
raise Exception('StaticBinary::unittest(): getValue 2 failed')
s = StaticBinary('''41 41 41 41''')
if s.getValue() != 'AAAA':
raise Exception('StaticBinary::unittest(): getValue 3 failed')
s = StaticBinary('\\x41 \\x41 \\x41 \\x41')
if s.getValue() != 'AAAA':
raise Exception('StaticBinary::unittest(): getValue 2 failed [%s]'
% s.getValue())
class _Number(Static):
"""
Base class for static numerical generators
"""
_value = None
_isLittleEndian = None
_isSigned = None
def __init__(self, value, isSigned=1, isLittleEndian=1):
"""
@type value: number
@param value: Value to set
@type isSigned: number
@param isSigned: 1 for signed, 0 for unsigned
@type isLittleEndian: number
@param isLittleEndian: 1 for signed, 0 for unsigned
"""
Generator.__init__(self)
if isinstance(value, (int, float, long, complex)):
self._value = value
else:
# if value has a null in it '123\0' we error
# so lets try and remove nulls from the string
if isinstance(value, basestring):
value = value.replace("\0", "")
self._value = int(value)
self._isSigned = isSigned
self._isLittleEndian = isLittleEndian
def setValue(self, value):
"""
Set value.
@type value: number
@param value: Value to set
"""
self._value = value
def isSigned(self):
"""
Check if value should be signed.
@rtype: number
@return: 1 for signed, 0 unsigned
"""
return self._isSigned
def setSigned(self, isSigned):
"""Set sign of number.
@type isSigned: number
@param isSigned: 1 is signed, 0 is unsigned.
"""
self._isSigned = isSigned
def isLittleEndian(self):
"""
Get byte ordering.
@rtype: number
@return: 1 is little, 0 is big/network.
"""
return self._isLittleEndian
def setLittleEndian(self, isLittleEndian):
"""
Set byte ordering. Network byte order is
big endian (false).
@type isLittleEndian: number
@param isLittleEndian: 1 is little, 0 is big
"""
self._isLittleEndian = isLittleEndian
@staticmethod
def unittest():
pass
class Int8(_Number):
"""
Static 8 bit integer. Can toggle signed/unsigned and also little/big
endian. Network byte order is big endian.
"""
def getRawValue(self):
packStr = ''
if self.isLittleEndian() == 1:
packStr = '<'
else:
packStr = '>'
if self.isSigned() == 1:
packStr += 'b'
else:
packStr += 'B'
return struct.pack(packStr, self._value)
@staticmethod
def unittest():
s = Int8(255)
print(s.getValue())
class Int16(_Number):
"""
Static 16 bit integer. Can toggle signed/unsigned and also little/big
endian. Network byte order is big endian.
"""
def getRawValue(self):
packStr = ''
if self.isLittleEndian() == 1:
packStr = '<'
else:
packStr = '>'
if self.isSigned() == 1:
packStr += 'h'
else:
packStr += 'H'
return struct.pack(packStr, self._value)
@staticmethod
def unittest():
s = Int16(2555)
print(s.getValue())
class Int32(_Number):
"""
Static 32 bit integer. Can toggle signed/unsigned and also little/big
endian. Network byte order is big endian.
"""
def getRawValue(self):
packStr = ''
if self.isLittleEndian() == 1:
packStr = '<'
else:
packStr = '>'
if self.isSigned() == 1:
packStr += 'l'
else:
packStr += 'L'
return struct.pack(packStr, self._value)
@staticmethod
def unittest():
s = Int32(2555555)
print(s.getValue())
class Int64(_Number):
"""
Static 64 bit integer. Can toggle signed/unsigned and also little/big
endian. Network byte order is big endian.
"""
def getRawValue(self):
packStr = ''
if self.isLittleEndian() == 1:
packStr = '<'
else:
packStr = '>'
if self.isSigned() == 1:
packStr += 'q'
else:
packStr += 'Q'
return struct.pack(packStr, self._value)
class Float(_Number):
"""
Static 4 bit floating point number. Can toggle little/big endian.
Network byte order is big endian.
"""
def getRawValue(self):
packStr = ''
if self.isLittleEndian() == 1:
packStr = '<'
else:
packStr = '>'
packStr += 'f'
return struct.pack(packStr, self._value)
@staticmethod
def unittest():
s = Float(1.2251)
print(s.getValue())
class Double(_Number):
"""
Static 8 bit floating point number. Can toggle little/big endian.
Network byte order is big endian.
"""
def getRawValue(self):
packStr = ''
if self.isLittleEndian() == 1:
packStr = '<'
else:
packStr = '>'
packStr += 'd'
return struct.pack(packStr, self._value)
@staticmethod
def unittest():
s = Double(1.23456789)
print(s.getValue())