Extract the packet length from the old-format length header
authorW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:31:15 +0000 (20:31 -0800)
committerW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:39:59 +0000 (20:39 -0800)
From RFC 4880 [1]:

  Scalar numbers are unsigned and are always stored in big-endian
  format.  Using n[k] to refer to the kth octet being interpreted, the
  value of a two-octet scalar is ((n[0] << 8) + n[1]).  The value of a
  four-octet scalar is ((n[0] << 24) + (n[1] << 16) + (n[2] << 8) +
  n[3]).

The struct big-endian byte-order character is '>' [2].

[1]: http://tools.ietf.org/search/rfc4880#section-3.1
[2]: http://docs.python.org/3/library/struct.html#byte-order-size-and-alignment

gpg-migrate.py

index b836926d70ebbed403f710e8ecdc36dacdefc202..f61fb670dca006875c075e6dc93e21c40c478d20 100755 (executable)
@@ -42,6 +42,9 @@ class PGPPacket (dict):
             if not length_bytes:
                 raise NotImplementedError(
                     'old-format packet of indeterminate length')
+            length_format = '>{}'.format(length_type)
+            length_data = data[1: 1 + length_bytes]
+            self['length'] = _struct.unpack(length_format, length_data)[0]
 
     def to_bytes(self):
         pass