3 \F!@sdZddlZddlZddlZddlZddlmZmZddlm Z ddddd d d d gZ ej ej d j ddZddZGdddeZeZdeed<ddZdd ZddZddZdd ZeedZdd ZeedZeedZddd ZdS) z Routines for manipulating RFC2047 encoded words. This is currently a package-private API, but will be considered for promotion to a public API if there is demand. N) ascii_lettersdigits)errorsdecode_qencode_qdecode_bencode_blen_qlen_bdecodeencodes=([a-fA-F0-9]{2})cCstt|jddgS)N)bytesintgroup)mr,/usr/lib64/python3.6/email/_encoded_words.pyAsrcCs|jdd}t|gfS)N_ )replace_q_byte_subber)encodedrrrrCs c@s,eZdZdejdejdZddZdS) _QByteMaps-!*+/asciicCs.||jkrt|||<ndj|||<||S)Nz={:02X})safechrformat)selfkeyrrr __missing__Ms z_QByteMap.__missing__N)__name__ __module__ __qualname__rr rrr"rrrrrIsr_ cCsdjdd|DS)Ncss|]}t|VqdS)N) _q_byte_map).0xrrr Zszencode_q..)join)bstringrrrrYscCstdd|DS)Ncss|]}tt|VqdS)N)lenr))r*r+rrrr,]szlen_q..)sum)r.rrrr \scCst|d}|r ddd|nd}y$tj||dd|rDtjgngfStjk rytj|ddtjgfStjk ry"tj|dddtjtjgfStjk r|tjgfSXYnXYnXdS)Ns===T)ZvalidateFs==) r/base64Z b64decoderZInvalidBase64PaddingDefectbinasciiErrorZInvalidBase64CharactersDefectZInvalidBase64LengthDefect)rZpad_errZmissing_paddingrrrrds     cCstj|jdS)Nr)r3Z b64encoder )r.rrrrscCs&tt|d\}}|d|r dndS)Nr1r)divmodr/)r.Z groups_of_3Zleftoverrrrr s)qbc Cs|jd\}}}}}|jd\}}}|j}|jdd}t||\}}y|j|}Wnvtk r|jtj dj ||j|d}YnBt k r|jdd}|jdkr|jtj dj |YnX||||fS)aDecode encoded word and return (string, charset, lang, defects) tuple. An RFC 2047/2243 encoded word has the form: =?charset*lang?cte?encoded_string?= where '*lang' may be omitted but the other parts may not be. This function expects exactly such a string (that is, it does not check the syntax and may raise errors if the string is not well formed), and returns the encoded_string decoded first from its Content Transfer Encoding and then from the resulting bytes into unicode using the specified charset. If the cte-decoded string does not successfully decode using the specified character set, a defect is added to the defects list and the unknown octets are replaced by the unicode 'unknown' character \uFDFF. The specified charset and language are returned. The default for language, which is rarely if ever encountered, is the empty string. ?*rsurrogateescapez:Encoded word contains bytes not decodable using {} charsetz unknown-8bitzsB$    &+