Package Peach :: Package Fixups :: Module checksums
[hide private]

Source Code for Module Peach.Fixups.checksums

  1   
  2  ''' 
  3  A few standard fixups. 
  4  ''' 
  5   
  6  # 
  7  # Copyright (c) 2008 Michael Eddington 
  8  # 
  9  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 10  # of this software and associated documentation files (the "Software"), to deal 
 11  # in the Software without restriction, including without limitation the rights  
 12  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 13  # copies of the Software, and to permit persons to whom the Software is  
 14  # furnished to do so, subject to the following conditions: 
 15  # 
 16  # The above copyright notice and this permission notice shall be included in     
 17  # all copies or substantial portions of the Software. 
 18  # 
 19  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 20  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 21  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 22  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 23  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 24  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 25  # SOFTWARE. 
 26  # 
 27   
 28  # Authors: 
 29  #   Michael Eddington (mike@phed.org) 
 30   
 31  # $Id$ 
 32   
 33  import zlib, struct, binascii, array 
 34  from Peach.fixup import Fixup 
 35  from Peach.Engine.common import * 
 36   
37 -class ExpressionFixup(Fixup):
38 ''' 39 Sometimes you need to perform some math as the fixup. This 40 relation will take a ref, then an expression (python). 41 ''' 42
43 - def __init__(self, ref, expression):
44 Fixup.__init__(self) 45 self.ref = ref 46 self.expression = expression
47
48 - def fixup(self):
49 ref = self._findDataElementByName(self.ref) 50 stuff = ref.getValue() 51 if stuff == None: 52 raise Exception("Error: ExpressionFixup was unable to locate [%s]" % self.ref) 53 54 return evalEvent(self.expression, { "self" : self, "ref" : ref, "data" : stuff })
55 56
57 -class Crc32Fixup(Fixup):
58 ''' 59 Standard CRC32 as defined by ISO 3309. Used by PNG, zip, etc. 60 ''' 61
62 - def __init__(self, ref):
63 Fixup.__init__(self) 64 self.ref = ref
65
66 - def fixup(self):
67 stuff = self._findDataElementByName(self.ref).getValue() 68 if stuff == None: 69 raise Exception("Error: Crc32Fixup was unable to locate [%s]" % self.ref) 70 71 return zlib.crc32(stuff)
72 73
74 -class Crc32DualFixup(Fixup):
75 ''' 76 Standard CRC32 as defined by ISO 3309. Used by PNG, zip, etc. 77 ''' 78
79 - def __init__(self, ref1, ref2):
80 Fixup.__init__(self) 81 self.ref1 = ref1 82 self.ref2 = ref2
83
84 - def fixup(self):
85 stuff1 = self._findDataElementByName(self.ref1).getValue() 86 stuff2 = self._findDataElementByName(self.ref2).getValue() 87 if stuff1 == None or stuff2 == None: 88 raise Exception("Error: Crc32DualFixup was unable to locate [%s] or [%s]" % (self.ref1, self.ref2)) 89 90 crc1 = zlib.crc32(stuff1) 91 return zlib.crc32(stuff2, crc1)
92 93
94 -class EthernetChecksumFixup(Fixup):
95 ''' 96 Ethernet Chucksum Fixup. 97 ''' 98
99 - def __init__(self, ref):
100 Fixup.__init__(self) 101 self.ref = ref
102
103 - def _checksum(self, checksum_packet):
104 """Calculate checksum""" 105 ethernetKey = 0x04C11DB7 106 return binascii.crc32(checksum_packet, ethernetKey)
107
108 - def fixup(self):
109 stuff = self._findDataElementByName(self.ref).getValue() 110 if stuff == None: 111 raise Exception("Error: EthernetChecksumFixup was unable to locate [%s]" % self.ref) 112 113 return self._checksum(stuff)
114 115
116 -class IcmpChecksumFixup(Fixup):
117 ''' 118 Ethernet Chucksum Fixup. 119 ''' 120
121 - def __init__(self, ref):
122 Fixup.__init__(self) 123 self.ref = ref
124
125 - def _checksum(self, checksum_packet):
126 """Calculate checksum""" 127 # add byte if not dividable by 2 128 if len(checksum_packet) & 1: 129 checksum_packet = checksum_packet + '\0' 130 # split into 16-bit word and insert into a binary array 131 words = array.array('h', checksum_packet) 132 sum = 0 133 134 # perform ones complement arithmetic on 16-bit words 135 for word in words: 136 sum += (word & 0xffff) 137 138 hi = sum >> 16 139 lo = sum & 0xffff 140 sum = hi + lo 141 sum = sum + (sum >> 16) 142 143 return (~sum) & 0xffff # return ones complement
144
145 - def fixup(self):
146 stuff = self._findDataElementByName(self.ref).getValue() 147 if stuff == None: 148 raise Exception("Error: IcmpChecksumFixup was unable to locate [%s]" % self.ref) 149 150 return self._checksum(stuff)
151 152 # end 153