Pull PGPPacket header parsing out into _parse_header
authorW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:46:48 +0000 (20:46 -0800)
committerW. Trevor King <wking@tremily.us>
Fri, 20 Dec 2013 05:20:49 +0000 (21:20 -0800)
This parsing is distinct from the payload parsing, so stash it in a
separate method.

gpg-migrate.py

index d227be71f8b03fd095236d8e92d7e346e80812c7..699b560ad4f2ace744173d6c92788c2220d138b2 100755 (executable)
@@ -26,6 +26,15 @@ class PGPPacket (dict):
         }
 
     def from_bytes(self, data):
+        offset = self._parse_header(data=data)
+        packet = data[offset:offset + self['length']]
+        if len(packet) < self['length']:
+            raise ValueError('packet too short ({} < {})'.format(
+                len(packet), self['length']))
+        offset += self['length']
+        return offset
+
+    def _parse_header(self, data):
         packet_tag = data[0]
         offset = 1
         always_one = packet_tag & 1 << 7
@@ -47,11 +56,6 @@ class PGPPacket (dict):
             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']]
-        if len(packet) < self['length']:
-            raise ValueError('packet too short ({} < {})'.format(
-                len(packet), self['length']))
-        offset += self['length']
         return offset
 
     def to_bytes(self):