Kaitai Struct: shift rotate >1 byte groups

The name of the pictureThe name of the pictureThe name of the pictureClash 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)






share|improve this question



























    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)






    share|improve this question























      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)






      share|improve this question













      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)








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 5 at 16:47
























      asked Apr 4 at 21:13









      ArekBulski

      309111




      309111

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?