U e5dO@sddlZddlZddlZdddddddd d d d d ddddddgZeefZddZdBddZdCddZ ddZ ddZ e ddZ e ddZddZddZdZdadad dZdDd!d Zd"d ZdEd#d Zdadad$Zd%ZdFd&d'Zddddd(d)dZddd*d+d,dZd-Zda da!da"dGd.d Z#d/d Z$d0Z%e%d1d2Z&d3dZ'd4dZ(d5d6Z)d7dZ*d8d9Z+d:dZ,d;d<Z-d=d>Z.d?d@Z/e0dAkre.dS)HNencodedecode encodebytes decodebytes b64encode b64decode b32encode b32decode b16encode b16decode b85encode b85decode a85encode a85decodestandard_b64encodestandard_b64decodeurlsafe_b64encodeurlsafe_b64decodecCs|t|tr4z |dWStk r2tdYnXt|trB|Szt|WStk rvtd|j j dYnXdS)Nasciiz4string argument should contain only ASCII charactersz>argument should be a bytes-like object or ASCII string, not %r) isinstancestrrUnicodeEncodeError ValueError bytes_types memoryviewtobytes TypeError __class____name__sr!/usr/lib64/python3.8/base64.py_bytes_from_decode_data"s   r#cCs,tj|dd}|dk r(|td|S|S)NF)newline+/)binascii b2a_base64 translatebytes maketrans)r altcharsencodedr!r!r"r3sFcCsNt|}|dk r*t|}|t|d}|rDtd|sDtdt|S)Nr%s[A-Za-z0-9+/]*={0,2}zNon-base64 digit found) r#r(r)r*re fullmatchr&Error a2b_base64)r r+Zvalidater!r!r"rAs cCst|SN)rrr!r!r"rZscCst|Sr1)rrr!r!r"rasr%s-_cCst|tSr1)rr(_urlsafe_encode_translationrr!r!r"roscCst|}|t}t|Sr1)r#r(_urlsafe_decode_translationrrr!r!r"rxs  s ABCDEFGHIJKLMNOPQRSTUVWXYZ234567cs>tdkr,ddtDfddDadt|tsBt|}t|d}|rb|dd|}t}tj }t}t dt|dD]V}||||dd}|||d?||d ?d @||d ?d @||d @7}q|d krd |dd<nF|dkrd|dd<n.|dkr d|dd<n|dkr6d|dd<t |S)NcSsg|]}t|fqSr!r).0ir!r!r" szb32encode..csg|]}D] }||q qSr!r!r6abZb32tabr!r"r8srbigi s======is====s====) _b32tab2 _b32alphabetrrrrlen bytearrayint from_bytesranger))r leftoverr,rPZb32tab2r7cr!r<r"rs<         c CsztdkrddttDat|}t|dr8td|dk r^t|}|t dd|}|rj| }t|}| d}|t|}t }t}t dt|dD]h}|||d}d} z|D]} | d >|| } qWn tk rtd dYnX|| d d 7}q|ds |d kr*td|rr|rr| d |K} | d d } d d |d} | d| |dd<t|S)NcSsi|]\}}||qSr!r!)r6kvr!r!r" szb32decode..zIncorrect paddings01OrIrr=zNon-base32 digit foundr?>rrCrFrH+)_b32rev enumeraterLr#rMr&r/r(r)r*upperrstriprNrQKeyErrorto_bytes) r casefoldZmap01lZpadcharsdecodedZb32revr7ZquantaaccrSZlastrRr!r!r"r s@        cCst|Sr1)r&Zhexlifyr^rr!r!r"r scCs4t|}|r|}td|r*tdt|S)Ns [^0-9A-F]zNon-base16 digit found)r#r^r-searchr&r/Z unhexlify)r rbr!r!r"r s   s<~s~>c st|tst|}t| d}|r4|d|}tdt|d|}fdd|D}|r|s|ddkrdd |d<|dd| |d<d |S) NrHr>z!%dIcsPg|]H}r|sdn6r$|dkr$dn&|d|dd|dqS)zi yi^ Ui9r!)r6Zwordcharschars2foldnuls foldspacesr!r"r8!s   z_85encode..rJrgrr=) rrrrrMstructStructZunpackjoin) r;rkrlpadrmrnpaddingZwordschunksr!rjr" _85encodes    rv)rnwrapcolrsadobecstdkr*ddtddDaddtDat|tt|d||rHtrt|rVdndfd dtd tD}|rt|d dkr|d d ||rt 7S)NcSsg|]}t|fqSr!r4r5r!r!r"r8Dsza85encode..!vcSsg|]}tD] }||q qSr!) _a85charsr9r!r!r"r8EsTrDrCcsg|]}||qSr!r!r5resultrwr!r"r8MsrrJro ) _a85chars2rQr{rv _A85STARTmaxrMappendrr_A85END)r;rnrwrsrxrur!r|r"r/s$   s )rnrx ignorecharsc Cst|}|rH|ts$tdt|tr<|dd}n |dd}tdj }g}|j }g}|j }|j } |dD]} d| krdkrnnl|| t |dkrd } |D]} d | | d} qz||| Wn tj k rtd dYnX| qv| d kr |rtd |dqv|rH| dkrH|r>td|dqv| |krVqvqvtd| qvd|} dt |} | r| d| } | S)Nz1Ascii85 encoded byte sequences must end with {!r}rD!Isuuuuryur=rrizAscii85 overflowzzz inside Ascii85 5-tuplesyzy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crorH)r#endswithrrformat startswithrrprqpackrclearrMerrorrr)r;rnrxrpackIrdZdecoded_appendZcurrZ curr_appendZ curr_clearxrer}rtr!r!r"rXsZ            sU0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~cCs2tdkr$ddtDaddtDat|tt|S)NcSsg|]}t|fqSr!r4r5r!r!r"r8szb85encode..cSsg|]}tD] }||q qSr!) _b85charsr9r!r!r"r8s) _b85chars2 _b85alphabetrrv)r;rsr!r!r"r s c CsJtdkr,dgdattD]\}}|t|<qt|}t| d}|d|}g}tdj}tdt|dD]}|||d}d}z|D]}|dt|}qWnFt k rt|D]&\}}t|dkrt d||dqYnXz| ||Wqntj k r$t d|dYqnXqnd |} |rF| d| } | S) Nr=~rrriz#bad base85 character at position %dz+base85 overflow in hunk starting at byte %dro)_b85decr]rr#rMrprqrrQrrrrrr) r;r7rSrtoutrchunkrejr}r!r!r"r sH       LrHrFcCsX|t}|sqTt|tkr>|tt|}|s4q>||7}qt|}||qdSr1)read MAXBINSIZErMr&r'write)inputoutputr nsliner!r!r"rs    cCs(|}|sq$t|}||qdSr1)readliner&r0r)rrrr r!r!r"rs  c Csz t|}Wn8tk rD}zd|jj}t||W5d}~XYnX|jdkrjd|j|jjf}t||jdkrd|j|jjf}t|dS)Nz"expected bytes-like object, not %s)rSr;Bz-expected single byte elements, not %r from %srCz(expected 1-D data, not %d-D data from %s)rrrrrndim)r merrmsgr!r!r"_input_type_checks      rcCsLt|g}tdt|tD]$}|||t}|t|qd|S)Nrro)rrQrMrrr&r'rr)r piecesr7rr!r!r"r s cCsddl}|dtdt|S)NrzAencodestring() is a deprecated alias since 3.1, use encodebytes()rD)warningswarnDeprecationWarningrr rr!r!r" encodestrings rcCst|t|Sr1)rr&r0rr!r!r"rscCsddl}|dtdt|S)NrzHdecodestring() is a deprecated alias since Python 3.1, use decodebytes()rD)rrrrrr!r!r" decodestring$s rc Csddl}ddl}z||jddd\}}WnP|jk r~}z0|j|_t|td|jd|dW5d}~XYnXt}|D]@\}}|dkrt}|dkrt }|dkrt }|d krt dSq|r|dd krt |dd }|||jj W5QRXn||j j |jj dS) NrrCZdeutzusage: %s [-d|-e|-u|-t] [file|-] -d, -u: decode -e: encode (default) -t: encode and decode string 'Aladdin:open sesame'rDz-ez-dz-uz-t-rb)sysgetoptargvrstderrstdoutprintexitrrtestopenbufferstdin) rrZoptsargsrfuncor:fr!r!r"main.s2 rcCs<d}tt|t|}tt|t|}tt|dS)NsAladdin:open sesame)rreprrr)Zs0s1s2r!r!r"rHs   r__main__)N)NF)FN)F)FFF)F)1r-rpr&__all__r)rNrr#rrrrr*r2r3rrrLrKr\rr r r r{rrrrvrrrrrrr r Z MAXLINESIZErrrrrrrrrrrr!r!r!r" s     & C  )H -