1 ###############################################################################
\r
3 ## Copyright 2012 Tavendo GmbH
\r
5 ## Licensed under the Apache License, Version 2.0 (the "License");
\r
6 ## you may not use this file except in compliance with the License.
\r
7 ## You may obtain a copy of the License at
\r
9 ## http://www.apache.org/licenses/LICENSE-2.0
\r
11 ## Unless required by applicable law or agreed to in writing, software
\r
12 ## distributed under the License is distributed on an "AS IS" BASIS,
\r
13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 ## See the License for the specific language governing permissions and
\r
15 ## limitations under the License.
\r
17 ###############################################################################
\r
19 from array import array
\r
22 class XorMaskerNull:
\r
24 def __init__(self, mask = None):
\r
33 def process(self, data):
\r
34 self.ptr += len(data)
\r
38 class XorMaskerSimple:
\r
40 def __init__(self, mask):
\r
41 assert len(mask) == 4
\r
43 self.msk = array('B', mask)
\r
51 def process(self, data):
\r
53 payload = array('B', data)
\r
54 for k in xrange(dlen):
\r
55 payload[k] ^= self.msk[self.ptr & 3]
\r
57 return payload.tostring()
\r
60 class XorMaskerShifted1:
\r
62 def __init__(self, mask):
\r
63 assert len(mask) == 4
\r
65 self.mskarray = [array('B'), array('B'), array('B'), array('B')]
\r
67 self.mskarray[0].append(ord(mask[ j & 3]))
\r
68 self.mskarray[1].append(ord(mask[(j + 1) & 3]))
\r
69 self.mskarray[2].append(ord(mask[(j + 2) & 3]))
\r
70 self.mskarray[3].append(ord(mask[(j + 3) & 3]))
\r
78 def process(self, data):
\r
80 payload = array('B', data)
\r
81 msk = self.mskarray[self.ptr & 3]
\r
82 for k in xrange(dlen):
\r
83 payload[k] ^= msk[k & 3]
\r
85 return payload.tostring()
\r