'Checks arbitrary data to see if it is a PGP object and returns a set
that indicates the kind(s) of object found. The names of the set
elements are based on RFC3156 content types with \'pgp-\' stripped so
they can be used in sniffers for other protocols, e.g. S/MIME.
There are additional set elements \'armored\' and \'unencrypted\'.
This code should give no false negatives, but may give false positives.
For efficient handling of encoded data, only small segments are decoded.
Armored files are detected by their armor header alone.
Non-armored data is detected by looking for a sequence of valid PGP
packet headers.'

def sniff(self, data, encoding=None):

    found = set()
    is_base64 = False
    is_quopri = False
    line_len = 0
    line_end = 1
    enc_start = 0
    enc_end = 0
    dec_start = 0
    skip = 0
    ptag = 0
    hdr_len = 0
    body_len = 0
    partial = False
    offset_enc = 0
    offset_dec = 0
    offset_packet = 0
    if (encoding and (encoding.lower() == 'base64')):
        line_len = (data.find('\n') + 1)
        if (line_len < 0):
            line_len = len(data)
        elif ((line_len > 1) and (data[(line_len - 2)] == '\r')):
            line_end = 2
        if ((line_len - line_end) > 76):
            return found
        enc_end = line_len
        try:
            segment = base64.b64decode(data[enc_start:enc_end])
        except TypeError:
            return found
        is_base64 = True
    elif (encoding and (encoding.lower() == 'quoted-printable')):
        try:
            segment = quopri.decodestring(data[0:1500])
        except TypeError:
            return found
        is_quopri = True
    else:
        line_len = len(data)
        segment = data
    if (not segment):
        found = set()
    elif (not (ord(segment[0]) & 128)):
        found.add('armored')
        if segment.startswith(self.ARMOR_BEGIN_SIGNED):
            found.add('unencrypted')
            found.add('signature')
        elif segment.startswith(self.ARMOR_BEGIN_SIGNATURE):
            found.add('signature')
        elif segment.startswith(self.ARMOR_BEGIN_ENCRYPTED):
            found.add('encrypted')
        elif segment.startswith(self.ARMOR_BEGIN_PUB_KEY):
            found.add('key')
        else:
            found = set()
    else:
        while ((skip < len(segment)) and (body_len != (-1))):
            prev_partial = partial
            (ptag, hdr_len, body_len, partial) = self.pgp_packet_hdr_parse(segment[skip:], prev_partial)
            if (prev_partial or partial):
                pass
            elif (ptag == 11):
                found.add('unencrypted')
            elif (ptag == 1):
                found.add('encrypted')
            elif (ptag == 9):
                found.add('encrypted')
            elif (ptag == 18):
                found.add('encrypted')
            elif (ptag == 2):
                found.add('signature')
            elif (ptag == 4):
                found.add('signature')
            elif (ptag == 6):
                found.add('key')
            elif (ptag == 14):
                found.add('key')
            elif (ptag == 8):
                if ((dec_start == 0) and (body_len == (-1))):
                    found.add('signature')
                    found.add('unencrypted')
            elif ((ptag < 0) or (ptag > 19)):
                found = set()
                return found
            dec_start += (hdr_len + body_len)
            skip = dec_start
            if (is_base64 and (body_len != (-1))):
                (enc_start, enc_end, skip) = self.base64_segment(dec_start, (dec_start + 6), 0, line_len, line_end)
                segment = base64.b64decode(data[enc_start:enc_end])
        if (is_base64 and (body_len != (-1)) and (skip != len(segment))):
            found = set()
    return found
