15
15
__version__ = '0.8'
16
16
17
17
18
+ if PY2 :
19
+ def integers2bytes (ints ):
20
+ return bytes (bytearray (ints ))
21
+ def bytes2integers (data ):
22
+ return bytearray (data )
23
+ else :
24
+ def integers2bytes (ints ):
25
+ return bytes (ints )
26
+ def bytes2integers (data ):
27
+ return data
28
+
29
+
18
30
class KaitaiStruct (object ):
19
31
def __init__ (self , stream ):
20
32
self ._io = stream
@@ -338,10 +350,7 @@ def process_xor_one(data, key):
338
350
if key == 0 :
339
351
return data
340
352
341
- if PY2 :
342
- return bytes (bytearray (v ^ key for v in bytearray (data )))
343
- else :
344
- return bytes (v ^ key for v in data )
353
+ return integers2bytes (v ^ key for v in bytes2integers (data ))
345
354
346
355
@staticmethod
347
356
def process_xor_many (data , key ):
@@ -350,10 +359,7 @@ def process_xor_many(data, key):
350
359
if len (key ) <= 64 and key == b'\x00 ' * len (key ):
351
360
return data
352
361
353
- if PY2 :
354
- return bytes (bytearray (a ^ b for a , b in zip (bytearray (data ), itertools .cycle (bytearray (key )))))
355
- else :
356
- return bytes (a ^ b for a , b in zip (data , itertools .cycle (key )))
362
+ return integers2bytes (a ^ b for a , b in zip (bytes2integers (data ), itertools .cycle (bytes2integers (key ))))
357
363
358
364
# formula taken from: http://stackoverflow.com/a/812039
359
365
precomputed_rotations = {amount :[(i << amount ) & 0xff | (i >> (- amount & 7 )) for i in range (256 )] for amount in range (8 )}
@@ -367,7 +373,4 @@ def process_rotate_left(data, amount, group_size):
367
373
return data
368
374
369
375
translate = KaitaiStream .precomputed_rotations [amount ]
370
- if PY2 :
371
- return bytes (bytearray (translate [a ] for a in bytearray (data )))
372
- else :
373
- return bytes (translate [a ] for a in data )
376
+ return integers2bytes (translate [a ] for a in bytes2integers (data ))
0 commit comments