a
    ´ib  ã                   @   s®   d Z g d¢ZdZdZdZdZzddlmZmZ W n e	yJ   dZdZY n0 d	d
„ Z
dd„ Zddd„Zddd„Zddd„Zd dd„Zdd„ Zdd„ Zdd„ Zedkrªeƒ  dS )!zHConversions to/from quoted-printable transport encoding as per RFC 1521.)ÚencodeÚdecodeÚencodestringÚdecodestringó   =éL   s   0123456789ABCDEFó    é    )Úa2b_qpÚb2a_qpNc                 C   s:   | dv r|S | dkr|S | t kp8d|   ko2dkn   S )zìDecide whether a particular byte ordinal needs to be quoted.

    The 'quotetabs' flag indicates whether embedded tabs and spaces should be
    quoted.  Note that line-ending tabs and spaces are always encoded, as per
    RFC 1521.
    ó    	ó   _ó    ó   ~)ÚESCAPE)ÚcÚ	quotetabsÚheader© r   ú/usr/lib64/python3.9/quopri.pyÚneedsquoting   s
    r   c                 C   s(   t | ƒ} ttt| d  t| d  fƒ S )zQuote a single character.é   )Úordr   ÚbytesÚHEX©r   r   r   r   Úquote$   s    r   Fc                 C   s2  t dur,|  ¡ }t |||d�}| |¡ dS |dfdd„}d}|  ¡ }|sN�qg }	d}
|dd… dkrv|dd… }d}
|D ]D}t|fƒ}t|||ƒrœt|ƒ}|r´|dkr´|	 d	¡ qz|	 |¡ qz|durÐ||ƒ t 	|	¡}t
|ƒtk�r||dtd
 … dd� |td
 d… }qÚ|}q>|du�r.|||
d� dS )av  Read 'input', apply quoted-printable encoding, and write to 'output'.

    'input' and 'output' are binary file objects. The 'quotetabs' flag
    indicates whether embedded tabs and spaces should be quoted. Note that
    line-ending tabs and spaces are always encoded, as per RFC 1521.
    The 'header' flag indicates whether we are encoding spaces as _ as per RFC
    1522.N©r   r   ó   
c                 S   sj   | r<| dd … dv r<|  | d d… t| dd … ƒ | ¡ n*| dkrX|  t| ƒ| ¡ n|  | | ¡ d S )Néÿÿÿÿr   ó   .)Úwriter   )ÚsÚoutputÚlineEndr   r   r   r    ;   s
    (zencode.<locals>.writer   r   r   r   é   s   =
)r#   )r
   Úreadr    Úreadliner   r   r   ÚappendÚEMPTYSTRINGÚjoinÚlenÚMAXLINESIZE)Úinputr"   r   r   ÚdataÚodatar    ZprevlineÚlineZoutlineÚstrippedr   Zthisliner   r   r   r   ,   s>    	




r   c                 C   sF   t d urt | ||d�S ddlm} || ƒ}|ƒ }t||||ƒ | ¡ S )Nr   r   ©ÚBytesIO)r
   Úior2   r   Úgetvalue)r!   r   r   r2   ÚinfpÚoutfpr   r   r   r   j   s    r   c                 C   sö  t dur*|  ¡ }t ||d�}| |¡ dS d}|  ¡ }|s>�qâdt|ƒ }}|dkrœ||d |… dkrœd}	|d }|dkr ||d |… dv r |d }qtnd}	||k �rÊ|||d … }
|
dkrØ|rØ|d	 }|d }q |
tkrò||
 }|d }q |d |k�r|	�sd}	�qÊq |d |k �rJ||d |d
 … tk�rJ|t }|d
 }q |d
 |k �r¸t||d |d
 … ƒ�r¸t||d
 |d … ƒ�r¸|tt||d |d … ƒfƒ }|d }q ||
 }|d }q |	s.| |d ¡ d}q.|�rò| |¡ dS )z»Read 'input', apply quoted-printable decoding, and write to 'output'.
    'input' and 'output' are binary file objects.
    If 'header' is true, decode underscore as space (per RFC 1522).N©r   r   r   r$   r   s    	r   r   é   é   )	r	   r%   r    r&   r*   r   Úishexr   Úunhex)r,   r"   r   r-   r.   Únewr/   ÚiÚnÚpartialr   r   r   r   r   u   s@    


(B,r   c                 C   sD   t d urt | |d�S ddlm} || ƒ}|ƒ }t|||d� | ¡ S )Nr7   r   r1   )r	   r3   r2   r   r4   )r!   r   r2   r5   r6   r   r   r   r       s    r   c                 C   sH   d|   kodkn  pFd|   ko*dkn  pFd|   koBdkS   S )zDReturn true if the byte ordinal 'c' is a hexadecimal digit in ASCII.ó   0ó   9ó   aó   fó   Aó   Fr   r   r   r   r   r:   ¬   s    r:   c                 C   sž   d}| D ]�}t |fƒ}d|  kr*dkr8n n
tdƒ}nLd|  krLdkr^n ntdƒd }n&d	|  krrd
kr„n ntd	ƒd }n |d t|ƒ|  }q|S )z.Get the integer value of a hexadecimal number.r   r@   rA   Ú0rB   rC   Úaé
   rD   rE   r   )r   r   )r!   Úbitsr   r=   r   r   r   r;   ±   s    

r;   c                  C   sÄ  dd l } dd l}z| | jdd … d¡\}}W nX |jy† } z>| j| _t|ƒ tdƒ tdƒ tdƒ |  d¡ W Y d }~n
d }~0 0 d}d}|D ] \}}|d	kr¨d
}|dkr”d
}q”|rØ|rØ| j| _tdƒ |  d¡ |sâdg}d}	|D ]Ä}
|
dk�r| jj	}nZzt
|
dƒ}W nJ t�yZ } z0| j d|
|f ¡ d}	W Y d }~qêW Y d }~n
d }~0 0 z<|�rtt|| jj	ƒ nt|| jj	|ƒ W |
dk�r®| ¡  qê|
dk�r¬| ¡  0 qê|	�rÀ|  |	¡ d S )Nr   r$   Ztdz"usage: quopri [-t | -d] [file] ...z-t: quote tabsz-d: decode; default encoder8   Fz-tTz-dz -t and -d are mutually exclusiveú-Úrbz%s: can't open (%s)
)ÚsysÚgetoptÚargvÚerrorÚstderrÚstdoutÚprintÚexitÚstdinÚbufferÚopenÚOSErrorr    r   r   Úclose)rL   rM   ZoptsÚargsÚmsgZdecoZtabsÚorG   ÚstsÚfileÚfpr   r   r   ÚmainÃ   sR     



"

ÿ
r_   Ú__main__)F)FF)F)F)Ú__doc__Ú__all__r   r+   r   r(   Zbinasciir	   r
   ÚImportErrorr   r   r   r   r   r   r:   r;   r_   Ú__name__r   r   r   r   Ú<module>   s*   

>

+
.