Track data offset in bytes, and extract the packet payload
authorW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:37:33 +0000 (20:37 -0800)
committerW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:39:59 +0000 (20:39 -0800)
Return the offset when we're done, because there may be multiple
packets in a stream.

gpg-migrate.py

index f61fb670dca006875c075e6dc93e21c40c478d20..905f8b64aacb63bf7189069d1d23dabd3c6d4625 100755 (executable)
@@ -27,6 +27,7 @@ class PGPPacket (dict):
 
     def from_bytes(self, data):
         packet_tag = data[0]
+        offset = 1
         always_one = packet_tag & 1 << 7
         if not always_one:
             raise ValueError('most significant packet tag bit not set')
@@ -43,8 +44,12 @@ class PGPPacket (dict):
                 raise NotImplementedError(
                     'old-format packet of indeterminate length')
             length_format = '>{}'.format(length_type)
-            length_data = data[1: 1 + length_bytes]
+            length_data = data[offset: offset + length_bytes]
+            offset += length_bytes
             self['length'] = _struct.unpack(length_format, length_data)[0]
+        packet = data[offset:offset + self['length']]
+        offset += self['length']
+        return offset
 
     def to_bytes(self):
         pass