From cf19c6f547ce366bcba88e581905efbeeec82a43 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 18 Dec 2013 20:46:48 -0800 Subject: [PATCH] Pull PGPPacket header parsing out into _parse_header This parsing is distinct from the payload parsing, so stash it in a separate method. --- gpg-migrate.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/gpg-migrate.py b/gpg-migrate.py index d227be7..699b560 100755 --- a/gpg-migrate.py +++ b/gpg-migrate.py @@ -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): -- 2.26.2