a
    zeM                     @   sj  d dl Z d dlZd dlZg dZeefZdd Zd?ddZd@dd	Z	d
d Z
dd ZeddZeddZdd Zdd ZdZdadadd ZdAddZdd ZdBddZdadadZdZdCdd Zdd ddd!d"d#Zddd$d%d&d'Zd(Zda da!da"dDd)d*Z#d+d, Z$d-Z%e%d. d/ Z&d0d1 Z'd2d3 Z(d4d5 Z)d6d7 Z*d8d9 Z+d:d; Z,d<d= Z-e.d>krfe,  dS )E    N)encodedecodeencodebytesdecodebytes	b64encode	b64decode	b32encode	b32decode	b16encode	b16decode	b85encode	b85decode	a85encode	a85decodestandard_b64encodestandard_b64decodeurlsafe_b64encodeurlsafe_b64decodec                 C   sx   t | tr2z| dW S  ty0   tdY n0 t | tr@| S zt|  W S  tyr   td| j	j
 d Y n0 d S )Nasciiz4string argument should contain only ASCII charactersz>argument should be a bytes-like object or ASCII string, not %r)
isinstancestrr   UnicodeEncodeError
ValueErrorbytes_types
memoryviewtobytes	TypeError	__class____name__s r!   /usr/lib/python3.9/base64.py_bytes_from_decode_data"   s    

r#   c                 C   s,   t j| dd}|d ur(|td|S |S )NF)newline   +/)binascii
b2a_base64	translatebytes	maketrans)r    altcharsencodedr!   r!   r"   r   3   s    r   Fc                 C   sN   t | } |d ur*t |}| t|d} |rDtd| sDtdt| S )Nr%   s   [A-Za-z0-9+/]*={0,2}zNon-base64 digit found)	r#   r(   r)   r*   re	fullmatchr&   Error
a2b_base64)r    r+   validater!   r!   r"   r   A   s    
r   c                 C   s   t | S N)r   r   r!   r!   r"   r   Z   s    r   c                 C   s   t | S r2   )r   r   r!   r!   r"   r   a   s    r   r%   s   -_c                 C   s   t | tS r2   )r   r(   _urlsafe_encode_translationr   r!   r!   r"   r   o   s    r   c                 C   s   t | } | t} t| S r2   )r#   r(   _urlsafe_decode_translationr   r   r!   r!   r"   r   x   s    
r   s    ABCDEFGHIJKLMNOPQRSTUVWXYZ234567c                    s>  t d u r,dd tD   fdd D a d  t| tsBt|  } t| d }|rb| dd|   } t }tj	}t }t
dt| dD ]V}|| ||d  d}|||d?  ||d	? d
@   ||d? d
@   ||d
@   7 }q|dkrd|dd < nF|dkrd|dd < n.|dkr d|dd < n|dkr6d|dd < t|S )Nc                 S   s   g | ]}t |fqS r!   r)   .0ir!   r!   r"   
<listcomp>       zb32encode.<locals>.<listcomp>c                    s   g | ]} D ]}|| qqS r!   r!   r7   abZb32tabr!   r"   r9      r:          r   big      i  
      s   ======i   s   ====   s   ===      =)_b32tab2_b32alphabetr   r   r   r   len	bytearrayint
from_bytesranger)   )r    leftoverr,   rR   Zb32tab2r8   cr!   r>   r"   r      s<    




r   c              	   C   sx  t d u rdd ttD a t| } t| d r8td|d ur^t|}| t	dd| } |rj| 
 } t| }| d} |t|  }t }t }tdt| dD ]f}| ||d  }d}	z|D ]}
|	d	> ||
  }	qW n ty   td
d Y n0 ||	d	d7 }q|d s|dvr(td|rp|rp|	d	| K }	|	d	d}dd	|  d }|d | |dd < t|S )Nc                 S   s   i | ]\}}||qS r!   r!   )r7   kvr!   r!   r"   
<dictcomp>   r:   zb32decode.<locals>.<dictcomp>   zIncorrect paddings   01   OrK   r   r?   zNon-base32 digit foundrA   >   r   rE   rH   rJ      +   )_b32rev	enumeraterN   r#   rO   r&   r/   r(   r)   r*   upperrstriprP   rS   KeyErrorto_bytes)r    casefoldZmap01lZpadcharsdecodedZb32revr8   ZquantaaccrU   ZlastrT   r!   r!   r"   r	      s@    


r	   c                 C   s   t |  S r2   )r&   Zhexlifyr`   r   r!   r!   r"   r
      s    r
   c                 C   s4   t | } |r|  } td| r*tdt| S )Ns	   [^0-9A-F]zNon-base16 digit found)r#   r`   r-   searchr&   r/   Z	unhexlify)r    rd   r!   r!   r"   r      s    

r   s   <~s   ~>c           	         s   t | tst|  } t|  d }|r4| d|  } tdt| d  | } fdd|D }|r|s|d dkr d d	 |d< |d d |  |d< d
|S )NrJ   r@   z!%dIc                    sP   g | ]H}r|sd n6r$|dkr$dn&|d  |d d    |d   qS )   zi       yi^	 U   i9  r!   )r7   Zwordcharschars2foldnuls
foldspacesr!   r"   r9   !  s   

z_85encode.<locals>.<listcomp>rL   ri   r   r?   r:   )	r   r   r   r   rO   structStructunpackjoin)	r=   rm   rn   padro   rp   paddingwordschunksr!   rl   r"   	_85encode  s    
ry   )rp   wrapcolru   adobec                   s   t d u r*dd tddD add tD a t| tt |d| |rHt   rt|rVdnd fd	dtd
t D }|rt|d d kr|d d| |r t	7   S )Nc                 S   s   g | ]}t |fqS r!   r5   r6   r!   r!   r"   r9   D  r:   za85encode.<locals>.<listcomp>!   v   c                 S   s   g | ]}t D ]}|| qqS r!   )	_a85charsr;   r!   r!   r"   r9   E  r:   TrF   rE   c                    s   g | ]} ||  qS r!   r!   r6   resultrz   r!   r"   r9   M  s   r   rL   r:      
)

_a85chars2rS   r~   ry   	_A85STARTmaxrO   appendrt   _A85END)r=   rp   rz   ru   r{   rx   r!   r   r"   r   /  s$    

r   s    	
)rp   r{   ignorecharsc             	   C   s  t | } |rH| ts$tdt| tr<| dd } n| d d } tdj	}g }|j
}g }|j
}|j}	| d D ]}
d|
  krdkrn nj||
 t|dkrd	}|D ]}
d
| |
d  }qz||| W n tjy   tdd Y n0 |	  qv|
dkr|rtd|d qv|rF|
dkrF|r<td|d qv|
|v rTqvqvtd|
 qvd|}dt| }|r|d |  }|S )Nz1Ascii85 encoded byte sequences must end with {!r}rF   !Is   uuuur|   u   r?   r   rk   zAscii85 overflowz   zz inside Ascii85 5-tuples       y   zy inside Ascii85 5-tuples       zNon-Ascii85 digit found: %cr:   rJ   )r#   endswithr   r   format
startswithr   rq   rr   packr   clearrO   errorrt   )r=   rp   r{   r   packIrf   Zdecoded_appendZcurrZcurr_appendZ
curr_clearxrg   r   rv   r!   r!   r"   r   X  sZ    






r   sU   0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~c                 C   s2   t d u r$dd tD add tD a t| tt |S )Nc                 S   s   g | ]}t |fqS r!   r5   r6   r!   r!   r"   r9     r:   zb85encode.<locals>.<listcomp>c                 S   s   g | ]}t D ]}|| qqS r!   )	_b85charsr;   r!   r!   r"   r9     r:   )
_b85chars2_b85alphabetr   ry   )r=   ru   r!   r!   r"   r     s    	r   c           
   	   C   sF  t d u r,d gd a ttD ]\}}|t |< qt| } t|  d }| d|  } g }tdj}tdt| dD ]}| ||d  }d}z|D ]}|d t |  }qW nD t	y   t|D ]&\}}t | d u rt
d||  d q Y n0 z||| W qn tjy    t
d| d Y qn0 qnd	|}	|rB|	d |  }	|	S )
N   r?      ~r   r   rk   z#bad base85 character at position %dz+base85 overflow in hunk starting at byte %dr:   )_b85decr_   r   r#   rO   rq   rr   r   rS   r   r   r   r   rt   )
r=   r8   rU   rv   outr   chunkrg   jr   r!   r!   r"   r     sH    



r   L   rJ   rH   c                 C   sX   |  t}|sqTt|tk r>|  tt| }|s4q>||7 }qt|}|| q d S r2   )read
MAXBINSIZErO   r&   r'   write)inputoutputr    nsliner!   r!   r"   r     s    


r   c                 C   s(   |   }|sq$t|}|| q d S r2   )readliner&   r0   r   )r   r   r   r    r!   r!   r"   r     s
    
r   c              
   C   s   zt | }W n: tyF } z"d| jj }t||W Y d }~n
d }~0 0 |jdvrld|j| jjf }t||jdkrd|j| jjf }t|d S )Nz"expected bytes-like object, not %s)rU   r=   Bz-expected single byte elements, not %r from %srE   z(expected 1-D data, not %d-D data from %s)r   r   r   r   r   ndim)r    merrmsgr!   r!   r"   _input_type_check  s     

r   c                 C   sL   t |  g }tdt| tD ]$}| ||t  }|t| qd|S )Nr   r:   )r   rS   rO   r   r   r&   r'   rt   )r    piecesr8   r   r!   r!   r"   r     s    r   c                 C   s   t |  t| S r2   )r   r&   r0   r   r!   r!   r"   r     s    r   c            	   
   C   s6  dd l } dd l}z|| jdd  d\}}W nR |jy } z8| j| _t| td| jd   | d W Y d }~n
d }~0 0 t}|D ]@\}}|dkrt}|dkrt	}|dkrt	}|d	krt
   d S q|r |d d
kr t|d d}||| jj W d    n1 s0    Y  n|| jj| jj d S )Nr   rE   Zdeutzusage: %s [-d|-e|-u|-t] [file|-]
        -d, -u: decode
        -e: encode (default)
        -t: encode and decode string 'Aladdin:open sesame'rF   z-ez-dz-uz-t-rb)sysgetoptargvr   stderrstdoutprintexitr   r   testopenbufferstdin)	r   r   Zoptsargsr   funcor<   fr!   r!   r"   main  s(     0r   c                  C   s<   d} t t|  t| }t t| t|}t t| d S )Ns   Aladdin:open sesame)r   reprr   r   )Zs0s1s2r!   r!   r"   r   8  s    r   __main__)N)NF)FN)F)FFF)F)/r-   rq   r&   __all__r)   rP   r   r#   r   r   r   r   r*   r3   r4   r   r   rN   rM   r^   r   r	   r
   r   r~   r   r   r   ry   r   r   r   r   r   r   r   r   ZMAXLINESIZEr   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>	   sX   

	&
C

)H
-	

