'Parse the header of a PGP packet to get the packet type, header length,
and data length.  Extra trailing characters in header are ignored.
prev_partial indicates that the previous packet was a partial packet.
An illegal header returns type -1, lengths 0.
Header format is defined in RFC4880 section 4.'

def pgp_packet_hdr_parse(self, header, prev_partial=False):

    hdr = bytearray(header.ljust(6, chr(0)))
    if (not prev_partial):
        hdr_len = 1
    else:
        hdr[1:] = hdr
        hdr[0] = 0
        hdr_len = 0
    is_partial = False
    if (prev_partial or ((hdr[0] & 192) == 192)):
        ptag = (hdr[0] & 63)
        body_len = hdr[1]
        lengthtype = 0
        hdr_len += 1
        if (body_len < 192):
            pass
        elif (body_len <= 223):
            hdr_len += 1
            body_len = ((((body_len - 192) << 8) + hdr[2]) + 192)
        elif (body_len == 255):
            hdr_len += 4
            body_len = ((((hdr[2] << 24) + (hdr[3] << 16)) + (hdr[4] << 8)) + hdr[5])
        else:
            if ((not prev_partial) and (not (ptag in {8, 9, 11, 18}))):
                return ((-1), 0, 0, False)
            is_partial = True
            body_len = (1 << (hdr[1] & 31))
    elif ((hdr[0] & 192) == 128):
        ptag = ((hdr[0] & 60) >> 2)
        lengthtype = (hdr[0] & 3)
        if (lengthtype < 3):
            hdr_len = 2
            body_len = hdr[1]
            if (lengthtype > 0):
                hdr_len = 3
                body_len = ((body_len << 8) + hdr[2])
            if (lengthtype > 1):
                hdr_len = 5
                body_len = (((body_len << 16) + (hdr[3] << 8)) + hdr[4])
        else:
            if ((ptag != 8) or ((hdr[1] < 1) or (hdr[1] > 3))):
                return ((-1), 0, 0, False)
            hdr_len = 1
            body_len = (-1)
    else:
        return ((-1), 0, 0, False)
    if (hdr_len > len(header)):
        return ((-1), 0, 0, False)
    return (ptag, hdr_len, body_len, is_partial)
