Kaitai Struct: shift rotate >1 byte groups
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this code for correctness and performance. Both Python 2 and 3 need to be supported.
This code is part of Kaitai Struct framework. Current implementation can be found here. Code below is in PR #28, unmerged.
There is a test case for this feature here.
Support code:
if PY2:
range = xrange
def integers2bytes(ints):
return bytes(bytearray(ints))
def bytes2integers(data):
return bytearray(data)
else:
def integers2bytes(ints):
return bytes(ints)
def bytes2integers(data):
return data
Main code:
class KaitaiStream(object):
# formula taken from: http://stackoverflow.com/a/812039
precomputed_single_rotations = amount: [(i << amount) & 0xff
@staticmethod
def process_rotate_left(data, amount, group_size):
amount = amount % (group_size * 8)
if amount == 0:
return data
data_ints = bytes2integers(data)
if group_size == 1:
translate = KaitaiStream.precomputed_single_rotations[amount]
return integers2bytes(translate[a] for a in data_ints)
if len(data) % group_size != 0:
raise Exception("data length must be a multiple of group size")
if amount % 8 == 0:
indices = [(i + amount//8) % group_size for i in range(group_size)]
return integers2bytes(data_ints[i+k] for i in range(0,len(data),group_size) for k in indices)
amount1 = amount % 8
amount2 = 8 - amount1
indices_pairs = [ ((i+amount//8) % group_size, (i+1+amount//8) % group_size) for i in range(group_size)]
return integers2bytes((data_ints[i+k1] << amount1) & 0xff | (data_ints[i+k2] >> amount2) for i in range(0,len(data),group_size) for k1,k2 in indices_pairs)
python algorithm array bitwise
add a comment |Â
up vote
2
down vote
favorite
This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this code for correctness and performance. Both Python 2 and 3 need to be supported.
This code is part of Kaitai Struct framework. Current implementation can be found here. Code below is in PR #28, unmerged.
There is a test case for this feature here.
Support code:
if PY2:
range = xrange
def integers2bytes(ints):
return bytes(bytearray(ints))
def bytes2integers(data):
return bytearray(data)
else:
def integers2bytes(ints):
return bytes(ints)
def bytes2integers(data):
return data
Main code:
class KaitaiStream(object):
# formula taken from: http://stackoverflow.com/a/812039
precomputed_single_rotations = amount: [(i << amount) & 0xff
@staticmethod
def process_rotate_left(data, amount, group_size):
amount = amount % (group_size * 8)
if amount == 0:
return data
data_ints = bytes2integers(data)
if group_size == 1:
translate = KaitaiStream.precomputed_single_rotations[amount]
return integers2bytes(translate[a] for a in data_ints)
if len(data) % group_size != 0:
raise Exception("data length must be a multiple of group size")
if amount % 8 == 0:
indices = [(i + amount//8) % group_size for i in range(group_size)]
return integers2bytes(data_ints[i+k] for i in range(0,len(data),group_size) for k in indices)
amount1 = amount % 8
amount2 = 8 - amount1
indices_pairs = [ ((i+amount//8) % group_size, (i+1+amount//8) % group_size) for i in range(group_size)]
return integers2bytes((data_ints[i+k1] << amount1) & 0xff | (data_ints[i+k2] >> amount2) for i in range(0,len(data),group_size) for k1,k2 in indices_pairs)
python algorithm array bitwise
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this code for correctness and performance. Both Python 2 and 3 need to be supported.
This code is part of Kaitai Struct framework. Current implementation can be found here. Code below is in PR #28, unmerged.
There is a test case for this feature here.
Support code:
if PY2:
range = xrange
def integers2bytes(ints):
return bytes(bytearray(ints))
def bytes2integers(data):
return bytearray(data)
else:
def integers2bytes(ints):
return bytes(ints)
def bytes2integers(data):
return data
Main code:
class KaitaiStream(object):
# formula taken from: http://stackoverflow.com/a/812039
precomputed_single_rotations = amount: [(i << amount) & 0xff
@staticmethod
def process_rotate_left(data, amount, group_size):
amount = amount % (group_size * 8)
if amount == 0:
return data
data_ints = bytes2integers(data)
if group_size == 1:
translate = KaitaiStream.precomputed_single_rotations[amount]
return integers2bytes(translate[a] for a in data_ints)
if len(data) % group_size != 0:
raise Exception("data length must be a multiple of group size")
if amount % 8 == 0:
indices = [(i + amount//8) % group_size for i in range(group_size)]
return integers2bytes(data_ints[i+k] for i in range(0,len(data),group_size) for k in indices)
amount1 = amount % 8
amount2 = 8 - amount1
indices_pairs = [ ((i+amount//8) % group_size, (i+1+amount//8) % group_size) for i in range(group_size)]
return integers2bytes((data_ints[i+k1] << amount1) & 0xff | (data_ints[i+k2] >> amount2) for i in range(0,len(data),group_size) for k1,k2 in indices_pairs)
python algorithm array bitwise
This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this code for correctness and performance. Both Python 2 and 3 need to be supported.
This code is part of Kaitai Struct framework. Current implementation can be found here. Code below is in PR #28, unmerged.
There is a test case for this feature here.
Support code:
if PY2:
range = xrange
def integers2bytes(ints):
return bytes(bytearray(ints))
def bytes2integers(data):
return bytearray(data)
else:
def integers2bytes(ints):
return bytes(ints)
def bytes2integers(data):
return data
Main code:
class KaitaiStream(object):
# formula taken from: http://stackoverflow.com/a/812039
precomputed_single_rotations = amount: [(i << amount) & 0xff
@staticmethod
def process_rotate_left(data, amount, group_size):
amount = amount % (group_size * 8)
if amount == 0:
return data
data_ints = bytes2integers(data)
if group_size == 1:
translate = KaitaiStream.precomputed_single_rotations[amount]
return integers2bytes(translate[a] for a in data_ints)
if len(data) % group_size != 0:
raise Exception("data length must be a multiple of group size")
if amount % 8 == 0:
indices = [(i + amount//8) % group_size for i in range(group_size)]
return integers2bytes(data_ints[i+k] for i in range(0,len(data),group_size) for k in indices)
amount1 = amount % 8
amount2 = 8 - amount1
indices_pairs = [ ((i+amount//8) % group_size, (i+1+amount//8) % group_size) for i in range(group_size)]
return integers2bytes((data_ints[i+k1] << amount1) & 0xff | (data_ints[i+k2] >> amount2) for i in range(0,len(data),group_size) for k1,k2 in indices_pairs)
python algorithm array bitwise
edited Apr 5 at 16:47
asked Apr 4 at 21:13
ArekBulski
309111
309111
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191283%2fkaitai-struct-shift-rotate-1-byte-groups%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password