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

Source Code for Module Peach.Generators.repeater

  1  ''' 
  2  Generators that repeate stuff. 
  3   
  4  @author: Michael Eddington 
  5  @version: $Id: Peach.Generators.repeater-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  6  ''' 
  7   
  8  # 
  9  # Copyright (c) 2005-2007 Michael Eddington 
 10  # Copyright (c) 2004-2005 IOActive Inc. 
 11  # 
 12  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 13  # of this software and associated documentation files (the "Software"), to deal 
 14  # in the Software without restriction, including without limitation the rights  
 15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 16  # copies of the Software, and to permit persons to whom the Software is  
 17  # furnished to do so, subject to the following conditions: 
 18  # 
 19  # The above copyright notice and this permission notice shall be included in     
 20  # all copies or substantial portions of the Software. 
 21  # 
 22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 28  # SOFTWARE. 
 29  # 
 30   
 31  # Authors: 
 32  #   Michael Eddington (mike@phed.org) 
 33   
 34  # $Id: Peach.Generators.repeater-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 35   
 36  import static 
 37  from Peach import generator, group 
 38  from Peach.generator import Generator 
 39   
 40  #__all__ = ['Repeater'] 
 41   
42 -class Repeater(generator.Generator):
43 ''' 44 Will repeat a value (generated by a Generator) by round count. Can be 45 used for basic buffer overflow testing. 46 47 Example: 48 49 >>> gen = Repeater(None, String("A"), 3) 50 >>> gen.getValue() 51 A 52 >>> gen.next() 53 >>> gen.getValue() 54 AA 55 >>> gen.next() 56 >>> gen.getValue() 57 AAA 58 59 Example: 60 61 >>> gen = Repeater(None, Static("Peach "), 5, 3) 62 >>> gen.getValue() 63 Peach 64 >>> gen.next() 65 >>> gen.getValue() 66 Peach Peach Peach Peach Peach 67 >>> gen.next() 68 >>> gen.getValue() 69 Peach Peach Peach Peach Peach Peach Peach Peach Peach Peach 70 71 72 ''' 73
74 - def __init__(self, group, generator, incrementor = 1, maxSteps = -1, startStep = None):
75 ''' 76 @type group: Group 77 @param group: Group this generator belongs to 78 @type generator: Generator 79 @param generator: Generator to repeate 80 @type incrementor: number 81 @param incrementor: Multiplier against round count 82 @type maxSteps: number 83 @param maxSteps: Maximum repeates 84 @type startSteps: number 85 @param startSteps: Start at this step 86 ''' 87 Generator.__init__(self) 88 self._incrementor = None 89 self._roundCount = 1 90 self._generator = None 91 self._maxSteps = -1 92 self._generator = generator 93 self._incrementor = incrementor 94 self.setGroup(group) 95 self._maxSteps = maxSteps 96 self._startStep = startStep 97 98 if self._startStep != None: 99 self._roundCount = self._startStep
100
101 - def next(self):
102 self._roundCount+=1 103 if self._maxSteps != -1 and self._roundCount > self._maxSteps: 104 self._roundCount -= 1 105 raise generator.GeneratorCompleted("Peach.repeater.Repeater")
106
107 - def getRawValue(self):
108 # Hah, this is much faster then the old way! 109 ret = str(self._generator.getValue()) * (self._roundCount*self._incrementor) 110 #for i in range(self._roundCount*self._incrementor): 111 # ret += self._generator.getValue() 112 return ret
113
114 - def getGenerator(self):
115 ''' 116 Get Generator who's value we will repeat. 117 118 @rtype: Generator 119 @return: Generator we are repeating 120 ''' 121 return self._generator
122 - def setGenerator(self, generator):
123 ''' 124 Set Generator who's value we will repeat. 125 126 @type generator: Generator 127 @param generator: Generator to repeate 128 ''' 129 self._generator = generator
130
131 - def reset(self):
132 self._roundCount = 1 133 134 if self._startStep != None: 135 self._roundCount = self._startStep 136 137 self._generator.reset()
138
139 - def unittest():
140 g = group.Group() 141 r = Repeater(g, static.Static('A'), 1, 10) 142 143 try: 144 while g.next(): 145 print r.getValue() 146 except group.GroupCompleted: 147 pass
148 unittest = staticmethod(unittest)
149 150
151 -class RepeaterGI(generator.Generator):
152 ''' 153 Will repeat a value (generated by a Generator) by multiplier (generator). 154 155 Example: 156 157 Repeater(None, String("A"), BadUnsignedNumbers(None)) 158 159 Would produce a string of A's the length of each number returned by 160 BadUnsignedNumbers. 161 162 ''' 163
164 - def __init__(self, group, generator, incrementor):
165 ''' 166 @type group: Group 167 @param group: Group this generator belongs to 168 @type generator: Generator 169 @param generator: Generator to repeate 170 @type incrementor: Generator 171 @param incrementor: Multiplier against round count 172 ''' 173 Generator.__init__(self) 174 self._incrementor = None 175 self._roundCount = 1 176 self._generator = None 177 178 self._generator = generator 179 self._incrementor = incrementor 180 self.setGroup(group)
181
182 - def next(self):
183 self._roundCount+=1 184 self._incrementor.next()
185
186 - def getRawValue(self):
187 try: 188 ret = str(self._generator.getValue()) * int(self._incrementor.getValue()) 189 190 except OverflowError: 191 # Integer overflow exception. Oh well, we tried! 192 ret = self._generator.getValue() 193 194 except MemoryError: 195 ret = self._generator.getValue() 196 #print "RepeaterGI: MemoryError! Value is %d long multiplier is %d." % ( 197 # len(str(ret)), int(self._incrementor.getValue())) 198 199 return ret
200
201 - def reset(self):
202 self._roundCount = 1 203 self._incrementor.reset() 204 self._generator.reset()
205 206 207 # end 208