o
    f]!                     @   s   d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 g dZ
eedjdd Zd	d
 ZG dd deZe Zdeed< dd Zdd Zdd Zdd Zdd ZeedZdd ZeedZeedZd ddZdS )!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_lettersdigits)errors)decode_qencode_qdecode_bencode_blen_qlen_bdecodeencodes   =([a-fA-F0-9]{2})c                 C   s   t | d S )N   )bytesfromhexgroupr   )m r   +/usr/lib/python3.10/email/_encoded_words.py<lambda>A   s    r   c                 C   s   |  dd} t| g fS )N   _    )replace_q_byte_subber)encodedr   r   r   r   C   s   r   c                   @   s,   e Zd Zded ed Zdd ZdS )	_QByteMaps   -!*+/asciic                 C   s4   || j v rt|| |< | | S d|| |< | | S )Nz={:02X})safechrformat)selfkeyr   r   r   __missing__M   s
   
z_QByteMap.__missing__N)__name__
__module____qualname__r   r   r   r   r!   r   r   r   r   r   I   s    r   _ c                 C   s   d dd | D S )N c                 s   s    | ]}t | V  qd S N)_q_byte_map.0xr   r   r   	<genexpr>Z   s    zencode_q.<locals>.<genexpr>)joinbstringr   r   r   r   Y   s   r   c                 C   s   t dd | D S )Nc                 s   s    | ]	}t t| V  qd S r(   )lenr)   r*   r   r   r   r-   ]   s    zlen_q.<locals>.<genexpr>)sumr/   r   r   r   r	   \   s   r	   c                 C   s   t | d }|rdd d|  nd}ztj| | dd|r$t gfW S g fW S  tjys   ztj| ddt gfW  Y S  tjyr   ztj| d ddt t gfW  Y  Y S  tjyq   | t gf Y  Y  Y S w w w )N   s   ===    T)validateFs   ==)	r1   base64	b64decoder   InvalidBase64PaddingDefectbinasciiErrorInvalidBase64CharactersDefectInvalidBase64LengthDefect)r   pad_errmissing_paddingr   r   r   r   d   s2   
r   c                 C   s   t | dS )Nr   )r6   	b64encoder   r/   r   r   r   r      s   r   c                 C   s(   t t| d\}}|d |rd S d S )N   r3   r   )divmodr1   )r0   groups_of_3leftoverr   r   r   r
      s   r
   )qbc           	   
   C   s   |  d\}}}}}|d\}}}| }|dd}t| |\}}z||}W n= tyF   |t	d|d ||d}Y n# t
tfyh   |dd}| dkrf|td|d	 Y nw ||||fS )
a  Decode 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.

    ?*r   surrogateescapez0Encoded word contains bytes not decodable using z charsetunknown-8bitzUnknown charset z* in encoded word; decoded as unknown bytes)split	partitionlowerr   _cte_decodersr   UnicodeDecodeErrorappendr   UndecodableBytesDefectLookupErrorUnicodeEncodeErrorCharsetError)	ewr%   charsetcte
cte_stringlangr0   defectsstringr   r   r   r      s&   
r   utf-8r'   c                 C   s|   |dkr|  dd}n|  |}|du r*td |}td |}|| dk r(dnd}t| |}|r6d| }d	||||S )
a  Encode string using the CTE encoding that produces the shorter result.

    Produces an RFC 2047/2243 encoded word of the form:

        =?charset*lang?cte?encoded_string?=

    where '*lang' is omitted unless the 'lang' parameter is given a value.
    Optional argument charset (defaults to utf-8) specifies the charset to use
    to encode the string to binary before CTE encoding it.  Optional argument
    'encoding' is the cte specifier for the encoding that should be used ('q'
    or 'b'); if it is None (the default) the encoding which produces the
    shortest encoded sequence is used, except that 'q' is preferred if it is up
    to five characters longer.  Optional argument 'lang' (default '') gives the
    RFC 2243 language string to specify in the encoded word.

    rI   r   rH   NrD   rE      rG   z=?{}{}?{}?{}?=)r   _cte_encode_length_cte_encodersr   )rZ   rU   encodingrX   r0   qlenblenr   r   r   r   r      s   
r   )r[   Nr'   )__doc__rer6   r9   	functoolsrZ   r   r   emailr   __all__partialcompilesubr   r   dictr   r)   ordr   r	   r   r   r
   rM   r   r^   r]   r   r   r   r   r   <module>   s>    )&+