a
    Ifd                     @   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re  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   sH   t | tsJ | dv r|S | dkr&|S | tkpFd|   ko@dkn   S )zDecide 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.
        	   _       ~)
isinstancebytesESCAPE)c	quotetabsheader r   lib/python3.9/quopri.pyneedsquoting   s    r   c                 C   sB   t | trt| dksJ t| } ttt| d  t| d  f S )zQuote a single character.      )r   r   lenordr   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|||rt|}|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 )Nr      .)writer   )soutputlineEndr   r   r   r#   ;   s
    (zencode.<locals>.writer   r!   r   r   r   s   =
)r&   )r
   readr#   readliner   r   r   appendEMPTYSTRINGjoinr   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
   ior3   r   getvalue)r$   r   r   r3   infpoutfpr   r   r   r   j   s    r   c                 C   s  t dur*|  }t ||d}|| dS d}|  }|s>qdt| }}|dkr||d | dkrd}	|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}	qq|d |k rJ||d |d
  tkrJ|t }|d
 }q|d
 |k rt||d |d
  rt||d
 |d  r|tt||d |d  f }|d }q||
 }|d }q|	s.||d  d}q.|r|| dS )zRead '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/   newr0   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 )Nr8   r   r2   )r	   r4   r3   r   r5   )r$   r   r3   r6   r7   r   r   r   r      s    r   c                 C   sV   t | tsJ d|   ko dkn  pTd|   ko8dkn  pTd|   koPdkS   S )zDReturn true if the byte ordinal 'c' is a hexadecimal digit in ASCII.   0   9   a   f   A   F)r   r   r   r   r   r   r;      s    r;   c                 C   s   d}| D ]}t |f}d|  kr*dkr8n n
td}n`d|  krLdkr^n ntdd }n:d	|  krrd
krn ntd	d }ndsJ dt| |d t||  }q|S )z.Get the integer value of a hexadecimal number.r   rA   rB   0rC   rD   a
   rE   rF   Fznon-hex digit r   )r   r   repr)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rd
}|dkrd
}q|r|r| j| _td | d |sdg}d}	|D ]}
|
dkr| jj	}nZzt
|
d}W nJ tyZ } z0| jd|
|f  d}	W Y d }~qW 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 encoder9   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)rN   rO   ZoptsargsmsgZdecoZtabsorH   stsfilefpr   r   r   main   sR     



"


ra   __main__)F)FF)F)F)__doc____all__r   r,   r   r*   Zbinasciir	   r
   ImportErrorr   r   r   r   r   r   r;   r<   ra   __name__r   r   r   r   <module>   s*   

>

+
.