From: W. Trevor King Date: Thu, 19 Dec 2013 04:46:48 +0000 (-0800) Subject: Pull PGPPacket header parsing out into _parse_header X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=cf19c6f547ce366bcba88e581905efbeeec82a43;p=gpg-migrate.git Pull PGPPacket header parsing out into _parse_header This parsing is distinct from the payload parsing, so stash it in a separate method. --- 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):