U AT^L!c@sdZddlZddlZddlZddlZddlmZmZddlm Z ddddd d d d gZ e e 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})cCst|dS)Ni)ZbytesZfromhexZgroupr )Zmr ,/usr/lib64/python3.8/email/_encoded_words.pyArcCs|dd}t|gfS)Ns_s )Zreplace_q_byte_subber)encodedr r r rCs c@s,eZdZdededZddZdS) _QByteMaps-!*+/asciicCs.||jkrt|||<nd|||<||S)Nz={:02X})safeZchrformat)ZselfZkeyr r r __missing__Ms z_QByteMap.__missing__N)Z__name__Z __module__Z __qualname__rr rrrr r r r rIsr_z cCsddd|DS)Ncss|]}t|VqdSN) _q_byte_mapZ.0Zxr r r Zzencode_q..)Zjoinbstringr r r rYcCstdd|DS)Ncss|]}tt|VqdSr)lenrrr r r r]rzlen_q..)Zsumrr r r r\r cCst|d}|r ddd|nd}z&tj||dd|rDtgngfWStjk rztj|ddtgfWYStjk rz,tj|dddttgfWYYStjk r|tgfYYYSXYnXYnXdS)Ns===rT)ZvalidateFs==) r!base64Z b64decoderZInvalidBase64PaddingDefectbinasciiZErrorZInvalidBase64CharactersDefectZInvalidBase64LengthDefect)rZpad_errZmissing_paddingr r r rds(   cCst|dS)Nr)r#Z b64encoder rr r r rr cCs&tt|d\}}|d|r dndS)Nir"r)Zdivmodr!)rZ groups_of_3Zleftoverr r r r s)qbc Cs|d\}}}}}|d\}}}|}|dd}t||\}}z||}Wnvtk r|t d |||d}YnBt k r|dd}|dkr|t d |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. z?*rsurrogateescapez:Encoded word contains bytes not decodable using {} charset unknown-8bitzsL)   &+