o
    fc                     @   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	y%   dZdZY nw 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rWe  dS 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p#d|   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   /usr/lib/python3.10/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   s<  t dur|  }t |||d}|| dS |dfdd}d}	 |  }|s'nig }	d}
|dd dkr;|dd }d}
|D ]"}t|f}t|||rNt|}|rZ|d	krZ|	d
 q=|	| q=|durh|| t	|	}t
|tkr||dtd  dd |td d }t
|tkss|}q |dur|||
d dS 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   sn   | r| dd  dv r| | d d t| dd   |  d S | dkr.| t| |  d S | | |  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#   prevlinelineoutlinestrippedr   thisliner   r   r   r   ,   sF   	



!r   c                 C   sF   t d urt | ||dS ddlm} || }| }t|||| | S )Nr   r   BytesIO)r
   ior6   r   getvalue)r$   r   r   r6   infpoutfpr   r   r   r   j   s   r   c                 C   s  t dur|  }t ||d}|| dS d}	 |  }|sndt|}}|dkr[||d | dkr[d}	|d }|dkrZ||d | dv rZ|d }|dkrZ||d | dv sHnd}	||k r|||d  }
|
dkrx|rx|d	 }|d }no|
tkr||
 }|d }nb|d |kr|	sd}	n[|d |k r||d |d
  tkr|t }|d
 }n<|d
 |k rt||d |d
  rt||d
 |d  r|tt||d |d  f }|d }n||
 }|d }||k sa|	s||d  d}q|r|| dS 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/   newr1   inpartialr   r   r   r   r   u   sL   
$<,r   c                 C   sD   t d ur
t | |dS ddlm} || }| }t|||d | S )Nr;   r   r5   )r	   r7   r6   r   r8   )r$   r   r6   r9   r:   r   r   r   r      s   r   c                 C   sV   t | tsJ d|   kodkn  p*d|   kodkn  p*d|   ko(d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   Hr>   c                 C   s   d}| D ]P}t |f}d|  krdkrn ntd}n.d|  kr&dkr/n ntdd }nd	|  kr9d
krBn ntd	d }nJ dt| |d t||  }q|S )z.Get the integer value of a hexadecimal number.r   rD   rE   0rF   rG   a
   rH   rI   Fznon-hex digit r   )r   r   repr)r$   bitsr   rA   r   r   r   r?      s   

r?   c                  C   s  dd l } dd l}z|| jdd  d\}}W n, |jyC } z| j| _t| td td td | d W Y d }~nd }~ww d}d}|D ]\}}|d	krTd
}|dkrZd
}qJ|rl|rl| j| _td | d |sqdg}d}	|D ]W}
|
dkr| jj	}n&zt
|
d}W n ty } z| jd|
|f  d}	W Y d }~qud }~ww z|rt|| jj	 nt|| jj	| W |
dkr|  qu|
dkr|  w w |	r| |	 d S d S )Nr   r   tdz"usage: quopri [-t | -d] [file] ...z-t: quote tabsz-d: decode; default encoder<   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)rR   rS   optsargsmsgdecotabsorK   stsfilefpr   r   r   main   sd   



rh   __main__)F)FF)__doc____all__r   r,   r   r*   binasciir	   r
   ImportErrorr   r   r   r   r   r   r>   r?   rh   __name__r   r   r   r   <module>   s0   

>

+.
