1
2 '''
3 Default static generators. Includes generic string, binary, and numeric
4 static generators.
5
6 @author: Michael Eddington
7 @version: $Id: Peach.Generators.static-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $
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 import re, struct, sys
40 from Peach import generator
41 from Peach.generator import *
42
43 -class Static(generator.Generator):
44 '''
45 Contains a static value that never changes.
46 Value can be any form of static data.
47
48 Example:
49
50 >>> gen = Static('Hello world')
51 >>> print gen.getValue()
52 Hello world
53
54 @see: L{StaticBinary}
55
56 '''
57
58 _value = ''
59
67
70
72 '''
73 Set static value to return.
74
75 @type value: string
76 @param value: Static data
77 @rtype: Static
78 @return: self
79 '''
80 self._value = str(value)
81 return self
82
85
93 unittest = staticmethod(unittest)
94
96 '''
97 This Static is for use with Peach 2.0. The value
98 will be gotten from the Template object every time
99 '''
100
102 '''
103 @type action: Action instance
104 @param action: Action that contains data model
105 @type node: DataElement
106 @param node: Data element to get value from
107 '''
108 Static.__init__(self, None)
109 self.action = action
110 self.elementName = node.getFullnameInDataModel()
111
115
117 '''
118 This Static is for use with Peach 2.0. The value
119 will be gotten from the Template object every time
120 '''
121
123 '''
124 @type value: string
125 @param value: String of hex values
126 '''
127 Static.__init__(self, None)
128 self.template = obj
129
131 return self.template.currentValue
132
133
135 '''
136 Contains some binary data. Can be set by string containing
137 several formats of binary data such as " FF FF FF FF " or
138 "\xFF \xFF \xFF", etc.
139
140 Example:
141
142 >>> gen = StaticBinary("""41 41 41
143 ... 41 41 41 41 0x41 0x41""")
144 >>>
145 >>> print gen.getValue()
146 AAAAAAAAA
147
148 '''
149
150
151
152 _regsHex = (
153 re.compile(r"^(\s*\\x([a-zA-Z0-9]{2})\s*)"),
154 re.compile(r"^(\s*%([a-zA-Z0-9]{2})\s*)"),
155 re.compile(r"^(\s*0x([a-zA-Z0-9]{2})\s*)"),
156 re.compile(r"^(\s*x([a-zA-Z0-9]{2})\s*)"),
157 re.compile(r"^(\s*([a-zA-Z0-9]{2})\s*)")
158 )
159
161 '''
162 @type value: string
163 @param value: String of hex values
164 '''
165 Static.__init__(self, value)
166 self.setValue(value)
167
169 '''
170 Set binary data to be used.
171
172 @type value: string
173 @param value: String of hex values
174 '''
175 ret = ''
176
177 for i in range(len(self._regsHex)):
178 match = self._regsHex[i].search(value)
179 if match != None:
180 while match != None:
181 ret += chr(int(match.group(2),16))
182 value = self._regsHex[i].sub('', value)
183 match = self._regsHex[i].search(value)
184 break
185
186 self._value = ret
187
205 unittest = staticmethod(unittest)
206
207
209 '''
210 Base class for static numerical generators
211 '''
212
213 _value = None
214 _isLittleEndian = None
215 _isSigned = None
216
217 - def __init__(self, value, isSigned = 1, isLittleEndian = 1):
218 '''
219 @type value: number
220 @param value: Value to set
221 @type isSigned: number
222 @param isSigned: 1 for signed, 0 for unsigned
223 @type isLittleEndian: number
224 @param isLittleEndian: 1 for signed, 0 for unsigned
225 '''
226
227 Generator.__init__(self)
228 if isinstance(value, (int, float, long, complex)):
229 self._value = value
230 else:
231
232
233 if isinstance(value, basestring):
234 value = value.replace("\0", "")
235
236 self._value = int(value)
237
238 self._isSigned = isSigned
239 self._isLittleEndian = isLittleEndian
240
242 '''
243 Set value.
244
245 @type value: number
246 @param value: Value to set
247 '''
248 self._value = value
249
251 '''
252 Check if value should be signed.
253
254 @rtype: number
255 @return: 1 for signed, 0 unsigned
256 '''
257 return self._isSigned
258
260 '''Set sign of number.
261
262 @type isSigned: number
263 @param isSigned: 1 is signed, 0 is unsigned.
264 '''
265 self._isSigned = isSigned
266
268 '''
269 Get byte ordering.
270
271 @rtype: number
272 @return: 1 is little, 0 is big/network.
273 '''
274 return self._isLittleEndian
275
277 '''
278 Set byte ordering. Network byte order is
279 big endian (false).
280
281 @type isLittleEndian: number
282 @param isLittleEndian: 1 is little, 0 is big
283 '''
284 self._isLittleEndian = isLittleEndian
285
288 unittest = staticmethod(unittest)
289
290
291 -class Int8(_Number):
292 '''
293 Static 8 bit integer. Can toggle signed/unsigned and also little/big
294 endian. Network byte order is big endian.
295 '''
296
298
299 packStr = ''
300
301 if self.isLittleEndian() == 1:
302 packStr = '<'
303 else:
304 packStr = '>'
305
306 if self.isSigned() == 1:
307 packStr += 'b'
308 else:
309 packStr += 'B'
310
311 return struct.pack(packStr, self._value)
312
316 unittest = staticmethod(unittest)
317
318
320 '''
321 Static 16 bit integer. Can toggle signed/unsigned and also little/big
322 endian. Network byte order is big endian.
323 '''
324
326 packStr = ''
327
328 if self.isLittleEndian() == 1:
329 packStr = '<'
330 else:
331 packStr = '>'
332
333 if self.isSigned() == 1:
334 packStr += 'h'
335 else:
336 packStr += 'H'
337
338 return struct.pack(packStr, self._value)
339
343 unittest = staticmethod(unittest)
344
345
347 '''
348 Static 32 bit integer. Can toggle signed/unsigned and also little/big
349 endian. Network byte order is big endian.
350 '''
351
353 packStr = ''
354
355 if self.isLittleEndian() == 1:
356 packStr = '<'
357 else:
358 packStr = '>'
359
360 if self.isSigned() == 1:
361 packStr += 'l'
362 else:
363 packStr += 'L'
364
365 return struct.pack(packStr, self._value)
366
370 unittest = staticmethod(unittest)
371
372
374 '''
375 Static 64 bit integer. Can toggle signed/unsigned and also little/big
376 endian. Network byte order is big endian.
377 '''
378
380
381 packStr = ''
382
383 if self.isLittleEndian() == 1:
384 packStr = '<'
385 else:
386 packStr = '>'
387
388 if self.isSigned() == 1:
389 packStr += 'q'
390 else:
391 packStr += 'Q'
392
393 return struct.pack(packStr, self._value)
394
398 unittest = staticmethod(unittest)
399
400
402 '''
403 Static 4 bit floating point number. Can toggle little/big endian.
404 Network byte order is big endian.
405 '''
406
408
409 packStr = ''
410
411 if self.isLittleEndian() == 1:
412 packStr = '<'
413 else:
414 packStr = '>'
415
416 packStr += 'f'
417
418 return struct.pack(packStr, self._value)
419
423 unittest = staticmethod(unittest)
424
425
427 '''
428 Static 8 bit floating point number. Can toggle little/big endian.
429 Network byte order is big endian.
430 '''
431
433
434 packStr = ''
435
436 if self.isLittleEndian() == 1:
437 packStr = '<'
438 else:
439 packStr = '>'
440
441 packStr += 'd'
442
443 return struct.pack(packStr, self._value)
444
448 unittest = staticmethod(unittest)
449
450
451
452