3 \O@sdZddlZddlZddlZddddddd d d d d dddddddgZeefZddZdCddZ dDddZ ddZ ddZ ej ddZej ddZddZddZd Zdadad!d ZdEd"d Zd#d ZdFd$d Zdadad%Zd&ZdGd'd(Zddddd)d*dZddd+d,d-dZd.Z da!da"da#dHd/d Z$d0dZ%d1Z&e&d2d3Z'd4dZ(d5dZ)d6d7Z*d8dZ+d9d:Z,d;dZ-dd?Z/d@dAZ0e1dBkre/dS)IzDBase16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodingsNencodedecode encodebytes decodebytes b64encode b64decode b32encode b32decode b16encode b16decode b85encode b85decode a85encode a85decodestandard_b64encodestandard_b64decodeurlsafe_b64encodeurlsafe_b64decodecCsxt|tr2y |jdStk r0tdYnXt|tr@|Sy t|jStk rrtd|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.6/base64.py_bytes_from_decode_data"s    r"cCsDtj|dd}|dk r@t|dks.tt||jtjd|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)newlineNs+/)binascii b2a_base64lenAssertionErrorrepr translatebytes maketrans)raltcharsencodedr r r!r3s FcCsht|}|dk rBt|}t|dks0tt||jtj|d}|r^tjd| r^t j dt j |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$s+/s^[A-Za-z0-9+/]*={0,2}$zNon-base64 digit found) r"r'r(r)r*r+r,rematchr%Error a2b_base64)rr-Zvalidater r r!rAs cCst|S)zrEncode bytes-like object s using the standard Base64 alphabet. The result is returned as a bytes object. )r)rr r r!rZscCst|S)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. )r)rr r r!rass+/s-_cCst|jtS)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_translation)rr r r!roscCst|}|jt}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_translationr)rr r r!rxs  s ABCDEFGHIJKLMNOPQRSTUVWXYZ234567csBtdkr,ddtDfddDadt|tsBt|j}t|d}|rb|dd|}t}tj }t}xjt dt|dD]V}||||dd}|||d ?||d ?d @||d ?d @||d @7}qW|d krd|dd<nF|dkr d|dd<n.|dkr$d|dd<n|dkr:d|dd<t |S)zKEncode the bytes-like object s using Base32 and return a bytes object. NcSsg|]}t|fqSr )r+).0ir r r! szb32encode..csg|]}D] }||q qSr r )r5ab)b32tabr r!r7srbigi s======r$s====s====i) _b32tab2 _b32alphabetrrrrr' bytearrayint from_bytesranger+)rleftoverr.rMZb32tab2r6cr )r:r!rs4   *    c CstdkrddttDat|}t|dr8tjd|dk rvt|}t|dks`tt||j t j dd|}|r|j }t|}|j d }|t|}t}t}xtd t|dD]n}|||d}d } y"x|D]} | d >|| } qWWn"tk rtjd dYnX|| jd d 7}qW|dsB|dkrLtjd|r|r| d |K} | jd d } dd |d} | d| |dd<t |S)aZDecode the Base32 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. 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. 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. NcSsi|]\}}||qSr r )r5kvr r r! szb32decode..zIncorrect paddingrAs01OrErr;zNon-base32 digit foundr=rDrCrB+>rrArDrCrB)_b32rev enumeraterJr"r'r%r1r(r)r*r+r,upperrstriprKrNKeyErrorto_bytes) rcasefoldZmap01lZpadcharsdecodedZb32revr6ZquantaaccrPZlastrOr r r!r sB         cCstj|jS)zKEncode the bytes-like object s using Base16 and return a bytes object. )r%ZhexlifyrZ)rr r r!r scCs4t|}|r|j}tjd|r*tjdtj|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"rZr/searchr%r1Z unhexlify)rr^r r r!r s   s<~s~>c st|tst|j}t| d}|r4|d|}tjdt|dj|}fdd|D}|r| r|d dkrdd |d <|d d| |d<d j|S)NrCr<z!%dIcsRg|]J}r| rdn6r&|dkr&dn&|d|dd|dqS)zi yi^ Ui9r )r5Zword)charschars2foldnuls foldspacesr r!r7!sz_85encode..rArcrr;rHrHrHrH) rrrrr'structStructunpackjoin) r9rfrgpadrhripaddingZwordschunksr )rfrgrhrir! _85encodes     rr)riwrapcolroadobecstdkr*ddtddDaddtDat|tt|d||rHtrt|rVdnd fd dtd tD}|rt|ddkr|jd d j||rt 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. NcSsg|]}t|fqSr )r+)r5r6r r r!r7Dsza85encode..!vcSsg|]}tD] }||q qSr ) _a85chars)r5r8r9r r r!r7EsTr$rAcsg|]}||qSr r )r5r6)resultrsr r!r7Msrrj rH) rwrN _a85chars2rr _A85STARTmaxr'appendrn_A85END)r9rirsrortrqr )rxrsr!r/s"   s )rirt ignorecharsc Cst|}|rH|jts$tdjt|jtr<|dd}n |dd}tjdj }g}|j }g}|j }|j } x|dD]} d| kodknr|| t |d krrd} x|D]} d | | d } qWy||| Wn"tj k rtd dYnX| qz| dkr0|r&td|dqz|rX| dkrX|rNtd|dqz| |krfqzqztd| qzWdj|} 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$Nz!IurC!rr;reruzAscii85 overflowrczz inside Ascii85 5-tuplesrdzy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crjrsuuuuruuzy)r"endswithr~rformat startswithr{rkrlpackr}clearr'errorrn)r9rirtrpackIr`Zdecoded_appendZcurrZ curr_appendZ curr_clearxrarxrpr r r!rXsV           sU0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~cCs2tdkr$ddtDaddtDat|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. NcSsg|]}t|fqSr )r+)r5r6r r r!r7szb85encode..cSsg|]}tD] }||q qSr ) _b85chars)r5r8r9r r r!r7s)r _b85alphabet _b85chars2rr)r9ror r r!r s c CsZtdkr0dgdaxttD]\}}|t|<qWt|}t| d}|d|}g}tjdj}xtdt|dD]}|||d}d}y"x|D]}|dt|}qWWnJt k rx2t|D]&\}}t|dkrt d||dqWYnXy|j ||Wqttj k r2t d |dYqtXqtWd j |} |rV| d| } | S) zqDecode the base85-encoded bytes-like object or ASCII string b The result is returned as a bytes object. Nr;~z!Irrez#bad base85 character at position %dz+base85 overflow in hunk starting at byte %drj)_b85decrYrr"r'rkrlrrNrrr}rrn) r9r6rPrpoutrchunkrajrxr r r!r s<       LrCrDcCs`xZ|jt}|sPx0t|tkrB|jtt|}|s8P||7}qWtj|}|j|qWdS)z1Encode a file; input and output are binary files.N)read MAXBINSIZEr'r%r&write)inputoutputrnsliner r r!rs   cCs,x&|j}|sPtj|}|j|qWdS)z1Decode a file; input and output are binary files.N)readliner%r2r)rrrrr r r!rs  cCsy t|}Wn8tk rD}zd|jj}t||WYdd}~XnX|jdkrjd|j|jjf}t||jdkrd|j|jjf}t|dS) Nz"expected bytes-like object, not %srPr9Bz-expected single byte elements, not %r from %srAz(expected 1-D data, not %d-D data from %s)rPr9r)rrrrrndim)rmerrmsgr r r!_input_type_checks    rcCsPt|g}x8tdt|tD]$}|||t}|jtj|qWdj|S)zVEncode a bytestring into a bytes object containing multiple lines of base-64 data.rrj)rrNr'rr}r%r&rn)rpiecesr6rr r r!r s cCsddl}|jdtdt|S)zLegacy alias of encodebytes().rNzAencodestring() is a deprecated alias since 3.1, use encodebytes()r$)warningswarnDeprecationWarningr)rrr r r! encodestringsrcCst|tj|S)z8Decode a bytestring of base-64 data into a bytes object.)rr%r2)rr r r!rscCsddl}|jdtdt|S)zLegacy alias of decodebytes().rNzHdecodestring() is a deprecated alias since Python 3.1, use decodebytes()r$)rrrr)rrr r r! decodestring$src Csddl}ddl}y|j|jddd\}}WnP|jk r~}z2|j|_t|td|jd|jdWYdd}~XnXt}xF|D]>\}}|dkrt}|dkrt }|d krt }|d krt dSqW|o|dd krt |dd }|||jj WdQRXn||j j |jj dS) zSmall main programrNrAZdeutzusage: %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) rrZoptsargsrfuncor8fr r r!main.s0rcCsHd}tt|t|}tt|t|}tt|||ksDtdS)NsAladdin:open sesame)rr)rrr()Zs0s1s2r r r!rHs   r__main__)N)NF)FN)F)FFF)F)2__doc__r/rkr%__all__r+rKrr"rrrrr,r3r4rrrJrIrXrr r r rwrzr{r~rrrrrrrrr r Z MAXLINESIZErrrrrrrrrrrr r r r!sh     & C  )H -