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"cCs,tj|dd}|dk r(|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_base64 translatebytes maketrans)raltcharsencodedr r r!r3sFcCsPt|}|dk r*t|}|jtj|d}|rFtjd| rFtjdtj|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. Ns+/s^[A-Za-z0-9+/]*={0,2}$zNon-base64 digit found) 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 )r1ab)b32tabr r!r3srbigi s======s====s====i) _b32tab2 _b32alphabetrrrrlen bytearrayint from_bytesranger')rleftoverr*rKZb32tab2r2cr )r6r!rs4   *    c CstdkrddttDat|}t|dr8tjd|dk r^t|}|jtj dd|}|rj|j }t|}|j d}|t|}t }t}xt d t|dD]l}|||d}d } y"x|D]} | d >|| } qWWn tk rtjd dYnX|| jd d 7}qW|ds(|dkr2tjd|rz|rz| 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 )r1kvr r r! szb32decode..zIncorrect paddings01OrBrr7zNon-base32 digit foundr9r=rAr@r>+>rr=rAr@r>)_b32rev enumeraterGr"rHr$r-r&r'r(upperrstriprIrLKeyErrorto_bytes) rcasefoldZmap01lZpadcharsdecodedZb32revr2ZquantaaccrNZlastrMr r r!r s@         cCstj|jS)zKEncode the bytes-like object s using Base16 and return a bytes object. )r$ZhexlifyrX)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"rXr+searchr$r-Z 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)Nr@r8z!%dIcsRg|]J}r| rdn6r&|dkr&dn&|d|dd|dqS)zi yi^ Ui9r )r1Zword)charschars2foldnuls foldspacesr r!r3!sz_85encode..r=rarr7rErErErE) rrrrrHstructStructZunpackjoin) r5rdrepadrfrgpaddingZwordschunksr )rdrerfrgr! _85encodes     ro)rgwrapcolrladobecstdkr*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')r1r2r r r!r3Dsza85encode..!vcSsg|]}tD] }||q qSr ) _a85chars)r1r4r5r r r!r3EsTr?r=csg|]}||qSr r )r1r2)resultrpr r!r3Msrrh rE) rtrL _a85chars2ro _A85STARTmaxrHappendrk_A85END)r5rgrprlrqrnr )rurpr!r/s"   s )rgrq 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!Iur@!rr7rcrrzAscii85 overflowrazz inside Ascii85 5-tuplesrbzy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crhrsuuuurruzy)r"endswithr{rformat startswithrxrirjpackrzclearrHerrorrk)r5rgrqr|packIr^Zdecoded_appendZcurrZ curr_appendZ curr_clearxr_rurmr 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')r1r2r r r!r3szb85encode..cSsg|]}tD] }||q qSr ) _b85chars)r1r4r5r r r!r3s)r _b85alphabet _b85chars2ro)r5rlr 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. Nr7~z!Irrcz#bad base85 character at position %dz+base85 overflow in hunk starting at byte %drh)_b85decrWrr"rHrirjrrLrrrzrrk) r5r2rNrmoutrchunkr_jrur r r!r s<       Lr@rAcCs`xZ|jt}|sPx0t|tkrB|jtt|}|s8P||7}qWtj|}|j|qWdS)z1Encode a file; input and output are binary files.N)read MAXBINSIZErHr$r%write)inputoutputrnsliner r r!rs   cCs,x&|j}|sPtj|}|j|qWdS)z1Decode a file; input and output are binary files.N)readliner$r.r)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 %srNr5Bz-expected single byte elements, not %r from %sr=z(expected 1-D data, not %d-D data from %s)rNr5r)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.rrh)rrLrHrrzr$r%rk)rpiecesr2rr 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$r.)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 programrNr=Zdeutzusage: %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) rrZoptsargsrfuncor4fr r r!main.s0rcCs<d}tt|t|}tt|t|}tt|dS)NsAladdin:open sesame)rreprrr)Zs0s1s2r r r!rHs   r__main__)N)NF)FN)F)FFF)F)2__doc__r+rir$__all__r'rIrr"rrrrr(r/r0rrrGrFrVrr r r rtrwrxr{rorrrrrrr r Z MAXLINESIZErrrrrrrrrrrr r r r!sh     & C  )H -