o ?Og|Q@sdZddlZddlZddlZgdZeefZddZdQddZ dRd d Z d d Z d dZ e ddZe ddZddZddZdZdZdZdZdZiaiaddZdSddZddZejd d!e_dSd"d#Zejd ed$e_d%d&Zejd'd!e_dTd(d)Zejd'd*d$e_d+d,Z dTd-d.Z!da"da#d/Z$d0Z%dUd1d2Z&ddddd3d4d5Z'ddd6d7d8d9Z(d:Z)da*da+da,dTd;d<Z-d=d>Z.d?Z/e/d@dAZ0dBdCZ1dDdEZ2dFdGZ3dHdIZ4dJdKZ5dLdMZ6dNdOZ7e8dPkre6dSdS)VzDBase16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodingsN)encodedecode encodebytes decodebytes b64encode b64decode b32encode b32decode b32hexencode b32hexdecode b16encode b16decode b85encode b85decode a85encode a85decodestandard_b64encodestandard_b64decodeurlsafe_b64encodeurlsafe_b64decodecCslt|trz|dWStytdwt|tr|Szt|WSty5td|j j dw)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#-/opt/alt/python310/lib64/python3.10/base64.py_bytes_from_decode_data"s"     r%cCsDtj|dd}|dur t|dksJt||td|S|S)a*Encode the bytes-like object s using Base64 and return a bytes object. Optional altchars should be a byte string of length 2 which specifies an alternative alphabet for the '+' and '/' characters. This allows an application to e.g. generate url or filesystem safe Base64 strings. F)newlineN+/)binascii b2a_base64lenrepr translatebytes maketrans)r"altcharsencodedr#r#r$r3s rFcCsft|}|dur!t|}t|dksJt||t|d}|r.td|s.t dt |S)anDecode the Base64 encoded bytes-like object or ASCII string s. Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the '+' and '/' characters. The result is returned as a bytes object. A binascii.Error is raised if s is incorrectly padded. If validate is False (the default), characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True, these non-alphabet characters in the input result in a binascii.Error. Nr'r(s[A-Za-z0-9+/]*={0,2}zNon-base64 digit found) r%r+r,r-r.r/re fullmatchr)Error a2b_base64)r"r0Zvalidater#r#r$rAs  rcCt|S)zrEncode bytes-like object s using the standard Base64 alphabet. The result is returned as a bytes object. )rr!r#r#r$rZsrcCr6)aQDecode bytes encoded with the standard Base64 alphabet. Argument s is a bytes-like object or ASCII string to decode. The result is returned as a bytes object. A binascii.Error is raised if the input is incorrectly padded. Characters that are not in the standard alphabet are discarded prior to the padding check. )rr!r#r#r$rasrr(s-_cCst|tS)zEncode bytes using the URL- and filesystem-safe Base64 alphabet. Argument s is a bytes-like object to encode. The result is returned as a bytes object. The alphabet uses '-' instead of '+' and '_' instead of '/'. )rr-_urlsafe_encode_translationr!r#r#r$rosrcCst|}|t}t|S)aDecode bytes using the URL- and filesystem-safe Base64 alphabet. Argument s is a bytes-like object or ASCII string to decode. The result is returned as a bytes object. A binascii.Error is raised if the input is incorrectly padded. Characters that are not in the URL-safe base-64 alphabet, and are not a plus '+' or slash '/', are discarded prior to the padding check. The alphabet uses '-' instead of '+' and '_' instead of '/'. )r%r-_urlsafe_decode_translationrr!r#r#r$rxs rzK Encode the bytes-like objects using {encoding} and return a bytes object. a Decode the {encoding} encoded bytes-like object or ASCII string s. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. {extra_args} The result is returned as a bytes object. A binascii.Error is raised if the input is incorrectly padded or if there are non-alphabet characters present in the input. a RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye) or letter L (el). The optional argument map01 when not None, specifies which letter the digit 1 should be mapped to (when map01 is not None, the digit 0 is always mapped to the letter O). For security purposes the default is None, so that 0 and 1 are not allowed in the input. s ABCDEFGHIJKLMNOPQRSTUVWXYZ234567s 0123456789ABCDEFGHIJKLMNOPQRSTUVcsR|tvrdd|DfddDt|<dt|ts#t|}t|d}|r3|dd|}t}tj}t|}t dt|dD]+}||||dd}|||d?||d ?d @||d ?d @||d @7}qE|d krd |dd<t |S|dkrd|dd<t |S|dkrd|dd<t |S|dkrd|dd<t |S)NcSg|]}t|fqSr#r..0ir#r#r$ z_b32encode..csg|] }D]}||qqSr#r#r<abZb32tabr#r$r>rbigi s======ir's====s====) _b32tab2rrrrr+ bytearrayint from_bytesranger.)alphabetr"leftoverr1rUZb32tab2r=cr#rCr$ _b32encodesB         rZc Cs|tvrddt|Dt|<t|}t|drtd|dur=t|}t|dks2Jt||t dd|}|rC| }t|}| d}|t|}t }t|}t d t|dD]1}|||d} d } z| D] } | d >|| } qpWn tytd dw|| d d 7}qa|ds|d vrtd|r|r| d |K} | d d } dd |d} | d| |dd<t|S)NcSsi|]\}}||qSr#r#)r<kvr#r#r$ r?z_b32decode..zIncorrect paddingrKs01OrPrrEzNon-base32 digit foundrG>rrKrMrO+)_b32rev enumerater%r+r)r4r,r-r.r/upperrstriprSrVKeyErrorto_bytes)rWr"casefoldmap01lZpadcharsdecodedZb32revr=ZquantaaccrYZlastrXr#r#r$ _b32decodesF         rncC tt|SN)rZ _b32alphabetr!r#r#r$r rZbase32)encodingcCstt|||Srp)rnrq)r"rirjr#r#r$r sr )rsZ extra_argscCrorp)rZ_b32hexalphabetr!r#r#r$r rrr Z base32hexcCs tt||Srp)rnrtr"rir#r#r$r s r cCst|S)zKEncode the bytes-like object s using Base16 and return a bytes object. )r)Zhexlifyrer!r#r#r$r sr cCs4t|}|r |}td|rtdt|S)aDecode the Base16 encoded bytes-like object or ASCII string s. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. The result is returned as a bytes object. A binascii.Error is raised if s is incorrectly padded or if there are non-alphabet characters present in the input. s [^0-9A-F]zNon-base16 digit found)r%rer2searchr)r4Z unhexlifyrur#r#r$r s     r s<~s~>c st|ts t|}t| d}|r|d|}tdt|d|}fdd|D}|rQ|sQ|ddkrFdd |d<|dd| |d<d |S) NrOrFz!%dIcsPg|]$}r |s dnr|dkrdn|d|dd|dqS)zi yi^ Ui9r#)r<Zwordcharschars2foldnuls foldspacesr#r$r>9s   z_85encode..rQrxrrE) rrrrr+structStructZunpackjoin) rBr|r}padr~rpaddingZwordschunksr#r{r$ _85encode/s     r)rwrapcolradobecstdurddtddDaddtDat|tt|d||r$trUt|r+dnd fd dtd tD}|rPt|d dkrP|d d||r[t 7S)aEncode bytes-like object b using Ascii85 and return a bytes object. foldspaces is an optional flag that uses the special short sequence 'y' instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This feature is not supported by the "standard" Adobe encoding. wrapcol controls whether the output should have newline (b'\n') characters added to it. If this is non-zero, each output line will be at most this many characters long. pad controls whether the input is padded to a multiple of 4 before encoding. Note that the btoa implementation always pads. adobe controls whether the encoded byte sequence is framed with <~ and ~>, which is used by the Adobe implementation. NcSr9r#r:r;r#r#r$r>\r?za85encode..!vcSg|] }tD]}||qqSr#) _a85charsr@r#r#r$r>]rDTr'rKcsg|] }||qSr#r#r;resultrr#r$r>esrrQr ) _a85chars2rVrr _A85STARTmaxr+appendr_A85END)rBrrrrrr#rr$rGs$   rs )rr ignorecharsc Cszt|}|r$|tstdt|tr|dd}n|dd}tdj }g}|j }g}|j }|j } |dD]k} d| krGdkr|nn3|| t |d kr{d } |D] } d | | d} qWz||| Wn tj ywtd dw| q;| d kr|rtd|dq;|r| dkr|rtd|dq;| |vrq;td| d|} dt |} | r| d| } | S)aDecode the Ascii85 encoded bytes-like object or ASCII string b. foldspaces is a flag that specifies whether the 'y' short sequence should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is not supported by the "standard" Adobe encoding. adobe controls whether the input sequence is in Adobe Ascii85 format (i.e. is framed with <~ and ~>). ignorechars should be a byte string containing characters to ignore from the input. This should only contain whitespace characters, and by default contains all whitespace characters in ASCII. The result is returned as a bytes object. z1Ascii85 encoded byte sequences must end with {!r}r'N!IsuuuururErrzzAscii85 overflowzzz inside Ascii85 5-tuplesyzy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crrO)r%endswithrrformat startswithrrrpackrclearr+errorr)rBrrrpackIrlZdecoded_appendZcurrZ curr_appendZ curr_clearxrmrrr#r#r$rps\             rsU0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~cCs2tdurddtDaddtDat|tt|S)zEncode bytes-like object b in base85 format and return a bytes object. If pad is true, the input is padded with b'\0' so its length is a multiple of 4 bytes before encoding. NcSr9r#r:r;r#r#r$r>r?zb85encode..cSrr#) _b85charsr@r#r#r$r>rD) _b85chars2 _b85alphabetrr)rBrr#r#r$rs rc Cs8tdurdgdattD]\}}|t|<q t|}t| d}|d|}g}tdj}tdt|dD]T}|||d}d}z|D] }|dt|}qFWn t yrt|D]\}}t|durpt d||dq]wz | ||Wq7tj yt d |dwd |} |r| d| } | S) zqDecode the base85-encoded bytes-like object or ASCII string b The result is returned as a bytes object. NrE~rrrzz#bad base85 character at position %dz+base85 overflow in hunk starting at byte %dr)_b85decrdrr%r+rrrrVrrrrr) rBr=rYroutrchunkrmjrr#r#r$rsP       rLrOrMcCsb |t}|s dSt|tkr&|tt|}|sn ||7}t|tkst|}||q)z1Encode a file; input and output are binary files.TN)read MAXBINSIZEr+r)r*write)inputoutputr"nsliner#r#r$rs    rcCs( |}|s dSt|}||q)z1Decode a file; input and output are binary files.TN)readliner)r5r)rrrr"r#r#r$r s  rc Cszt|}Wnty}z d|jj}t||d}~ww|jdvr0d|j|jjf}t||jdkrBd|j|jjf}t|dS)Nz"expected bytes-like object, not %s)rYrBBz-expected single byte elements, not %r from %srKz(expected 1-D data, not %d-D data from %s)rrrr rndim)r"merrmsgr#r#r$_input_type_checks$       rcCsLt|g}tdt|tD]}|||t}|t|qd|S)zVEncode a bytestring into a bytes object containing multiple lines of base-64 data.rr)rrVr+rrr)r*r)r"piecesr=rr#r#r$r$s  rcCst|t|S)z8Decode a bytestring of base-64 data into a bytes object.)rr)r5r!r#r#r$r/s rc Cs4ddl}ddl}z||jddd\}}Wn)|jy@}z|j|_t|td|jd|dWYd}~nd}~wwt}|D] \}}|dkrOt}|dkrUt }|d kr[t }|d kret dSqE|r|dd krt |dd }|||jj WddS1swYdS||j j |jj dS) zSmall main programrNrKZdeutzusage: %s [-d|-e|-u|-t] [file|-] -d, -u: decode -e: encode (default) -t: encode and decode string 'Aladdin:open sesame'r'z-ez-dz-uz-t-rb)sysgetoptargvrstderrstdoutprintexitrrtestopenbufferstdin) rrZoptsargsrfuncorAfr#r#r$main6s.    "rcCsHd}tt|t|}tt|t|}tt|||ks"JdS)NsAladdin:open sesame)rr,rr)Zs0s1s2r#r#r$rPs   r__main__rp)NF)FN)F)FFF)9__doc__r2rr)__all__r.rSrr%rrrrr/r7r8rrZ_B32_ENCODE_DOCSTRINGZ_B32_DECODE_DOCSTRINGZ_B32_DECODE_MAP01_DOCSTRINGrqrtrRrcrZrnrrr r r r r rrrrrrrrrrrrrZ MAXLINESIZErrrrrrrrr r#r#r#r$s~       $/    )H -