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