Package Peach :: Package Generators :: Module static
[hide private]

Source Code for Module Peach.Generators.static

  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  # Copyright (c) 2005-2007 Michael Eddington 
 12  # Copyright (c) 2004-2005 IOActive Inc. 
 13  # 
 14  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 15  # of this software and associated documentation files (the "Software"), to deal 
 16  # in the Software without restriction, including without limitation the rights  
 17  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 18  # copies of the Software, and to permit persons to whom the Software is  
 19  # furnished to do so, subject to the following conditions: 
 20  # 
 21  # The above copyright notice and this permission notice shall be included in     
 22  # all copies or substantial portions of the Software. 
 23  # 
 24  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 25  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 26  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 27  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 28  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 29  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 30  # SOFTWARE. 
 31  # 
 32   
 33  # Authors: 
 34  #   Michael Eddington (mike@phed.org) 
 35   
 36  # $Id: Peach.Generators.static-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 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
60 - def __init__(self, value):
61 ''' 62 @type value: string 63 @param value: Static data 64 ''' 65 Generator.__init__(self) 66 self.setValue(value)
67
68 - def getRawValue(self):
69 return self._value
70
71 - def setValue(self, value):
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
83 - def next(self):
84 raise generator.GeneratorCompleted("STATIC")
85
86 - def unittest():
87 s = Static('hello world') 88 if s.getValue() != 'hello world': 89 raise Exception('Static::unittest(): getValue failed') 90 s.setValue('meow') 91 if s.getValue() != 'meow': 92 raise Exception('Static::unittest(): setValue failed')
93 unittest = staticmethod(unittest)
94
95 -class _StaticFromTemplate(Static):
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
101 - def __init__(self, action, node):
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
112 - def getRawValue(self):
113 node = self.action.template.findDataElementByName(self.elementName) 114 return node.getInternalValue()
115
116 -class _StaticCurrentValueFromDom(Static):
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
122 - def __init__(self, obj):
123 ''' 124 @type value: string 125 @param value: String of hex values 126 ''' 127 Static.__init__(self, None) 128 self.template = obj
129
130 - def getRawValue(self):
131 return self.template.currentValue
132 133
134 -class StaticBinary(Static):
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 # Ordering of regex's can be important as the last 151 # regex can falsly match some of its priors. 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
160 - def __init__(self, value):
161 ''' 162 @type value: string 163 @param value: String of hex values 164 ''' 165 Static.__init__(self, value) 166 self.setValue(value)
167
168 - def setValue(self, value):
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
188 - def unittest():
189 s = StaticBinary('41 41 41 41') 190 if s.getValue() != 'AAAA': 191 raise Exception('StaticBinary::unittest(): getValue 1 failed') 192 s = StaticBinary('0x41 0x41 0x41 0x41') 193 if s.getValue() != 'AAAA': 194 raise Exception('StaticBinary::unittest(): getValue 2 failed') 195 s = StaticBinary('''41 196 41 197 41 198 41''') 199 if s.getValue() != 'AAAA': 200 raise Exception('StaticBinary::unittest(): getValue 3 failed') 201 s = StaticBinary('\\x41 \\x41 \\x41 \\x41') 202 if s.getValue() != 'AAAA': 203 raise Exception('StaticBinary::unittest(): getValue 2 failed [%s]' 204 % s.getValue())
205 unittest = staticmethod(unittest)
206 207
208 -class _Number(Static):
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 # if value has a null in it '123\0' we error 232 # so lets try and remove nulls from the string 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
241 - def setValue(self, value):
242 ''' 243 Set value. 244 245 @type value: number 246 @param value: Value to set 247 ''' 248 self._value = value
249
250 - def isSigned(self):
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
259 - def setSigned(self, isSigned):
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
267 - def isLittleEndian(self):
268 ''' 269 Get byte ordering. 270 271 @rtype: number 272 @return: 1 is little, 0 is big/network. 273 ''' 274 return self._isLittleEndian
275
276 - def setLittleEndian(self, isLittleEndian):
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
286 - def unittest():
287 pass
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
297 - def getRawValue(self):
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
313 - def unittest():
314 s = Int8(255) 315 print s.getValue()
316 unittest = staticmethod(unittest)
317 318
319 -class Int16(_Number):
320 ''' 321 Static 16 bit integer. Can toggle signed/unsigned and also little/big 322 endian. Network byte order is big endian. 323 ''' 324
325 - def getRawValue(self):
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
340 - def unittest():
341 s = Int16(2555) 342 print s.getValue()
343 unittest = staticmethod(unittest)
344 345
346 -class Int32(_Number):
347 ''' 348 Static 32 bit integer. Can toggle signed/unsigned and also little/big 349 endian. Network byte order is big endian. 350 ''' 351
352 - def getRawValue(self):
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
367 - def unittest():
368 s = Int32(2555555) 369 print s.getValue()
370 unittest = staticmethod(unittest)
371 372
373 -class Int64(_Number):
374 ''' 375 Static 64 bit integer. Can toggle signed/unsigned and also little/big 376 endian. Network byte order is big endian. 377 ''' 378
379 - def getRawValue(self):
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
395 - def unittest():
396 s = Int64(255555555555555) 397 print s.getValue()
398 unittest = staticmethod(unittest)
399 400
401 -class Float(_Number):
402 ''' 403 Static 4 bit floating point number. Can toggle little/big endian. 404 Network byte order is big endian. 405 ''' 406
407 - def getRawValue(self):
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
420 - def unittest():
421 s = Float(1.2251) 422 print s.getValue()
423 unittest = staticmethod(unittest)
424 425
426 -class Double(_Number):
427 ''' 428 Static 8 bit floating point number. Can toggle little/big endian. 429 Network byte order is big endian. 430 ''' 431
432 - def getRawValue(self):
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
445 - def unittest():
446 s = Double(1.23456789) 447 print s.getValue()
448 unittest = staticmethod(unittest)
449 450 451 # end 452