Yf*P@swdZddlZddlZddlZddddddd d d d d dddddddgZeefZddZdddZ ddddZ ddZ ddZ ej ddZej ddZddZddZd Zdadad!d Zddd"d Zd#d Zdd$d Zdadad%Zd&Zdddd'd(Zd)dd*dd+dd,dd-dZd)dd,dd.d/d0dZd1Z da!da"da#dd2d Z$d3dZ%d4Z&e&d5d6Z'd7dZ(d8dZ)d9d:Z*d;dZ+d<d=Z,d>dZ-d?d@Z.dAdBZ/dCdDZ0e1dEkrse/dS)FzDBase16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodingsNencodedecode encodebytes decodebytes b64encode b64decode b32encode b32decode b16encode b16decode b85encode b85decode a85encode a85decodestandard_b64encodestandard_b64decodeurlsafe_b64encodeurlsafe_b64decodecCst|trAy|jdSWntk r@tdYnXt|trT|Syt|jSWn+tk rtd|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 +/opt/alt/python35/lib64/python3.5/base64.py_bytes_from_decode_data"s  r"cCsftj|dd}|dk rbt|dksItt||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. Ns+/)binascii b2a_base64lenAssertionErrorrepr translatebytes maketrans)raltcharsencodedr r r!r3s  $FcCst|}|dk rct|}t|dksHtt||jtj|d}|rtjd| rt 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!rBs   $cCs t|S)zrEncode bytes-like object s using the standard Base64 alphabet. The result is returned as a bytes object. )r)rr r r!r[scCs t|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!rbss+/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!rpscCs%t|}|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!rys s ABCDEFGHIJKLMNOPQRSTUVWXYZ234567cstdkr>ddtDfddDadt|ts_t|j}t|d}|r|td|}t}t j }t}x{t dt|dD]a}||||dd}|||d?||d ?d @||d ?d @||d @7}qW|d kr>d |dds zb32encode..cs'g|]}D]}||qqSr r )r6ab)b32tabr r!r8s rbigi r#s======r$s====s====ir%) _b32tab2 _b32alphabetrrrrr(r, bytearrayint from_bytesrange)rZleftoverr/rKZb32tab2r7cr )r;r!rs4   +    c Cs{tdkr%ddttDat|}t|drPtjd|dk rt|}t|dkstt||j t j dd|}|r|j }t|}|j d }|t|}t}t}xtd t|dD]}|||d}d } y'x |D]} | d >|| } q:WWn$tk r}tjd dYnX|| jd d 7}qW|rq| d |K} | jd d } |dkr| dd|dds zb32decode..zIncorrect paddingr#s01OrDrr<zNon-base32 digit foundr=rCr$rBrAr%rSrFrSrErS)_b32rev enumeraterHr"r(r&r2r)r*r+r,r-upperrstriprIrLKeyErrorto_bytes) rcasefoldZmap01lZpadcharsdecodedZb32revr7ZquantaaccrMZlastr r r!r sL    $         cCstj|jS)zKEncode the bytes-like object s using Base16 and return a bytes object. )r&ZhexlifyrW)rr r r!r scCsLt|}|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"rWr0searchr&r2Z unhexlify)rr[r r r!r s  s<~s~>c st|ts!t|j}t| d}|rF|d|}tjdt|dj|}fdd|D}|r| r|d dkrdd |d <|d d| |d.r#r`rr<r%r%r%r%) rrrrr(structStructZunpackjoin) r:rcrdpadrerfpaddingZwordschunksr )rcrdrerfr! _85encodes&  rnrfwrapcolrkadobecstdkr;ddtddDaddtDat|tt|d||rftrt|r{dnd 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,)r6r7r r r!r8Ls za85encode..!vcSs'g|]}tD]}||qqSr ) _a85chars)r6r9r:r r r!r8Ms Tr$r#cs$g|]}||qSr r )r6r7)resultror r!r8Us rrgs r%) rsrL _a85chars2rn _A85STARTmaxr(appendrj_A85END)r:rfrorkrprmr )rtror!r7s"     ignorecharss c Cst|}|rh|jts6tdjt|jtrX|dd}n|dd}tjdj }g}|j }g}|j }|j } x6|dD]*} d| kodknrX|| t |d krd} x |D]} d | | d } qWy||| Wn$tj k rMtd dYnX| q| dkr|rvtd|dq|r| dkr|rtd|dq| |krqqtd| qWdj|} 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!IurB!rr<rbrqzAscii85 overflowr`zz inside Ascii85 5-tuplesrazy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crgrTrTsuuuurquzy)r"endswithryrformat startswithrvrhripackrxclearr(errorrj)r:rfrprzpackIr]Zdecoded_appendZcurrZ curr_appendZ curr_clearxr^rtrlr r r!r`sV              sU0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~cCsEtdkr2ddtDaddtDat|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,)r6r7r r r!r8s zb85encode..cSs'g|]}tD]}||qqSr ) _b85chars)r6r9r:r r r!r8s )r _b85alphabet _b85chars2rn)r:rkr r r!r s c Cstdkr@dgdax$ttD]\}}|t|xAt|D]3\}}t|dkrt d||dqWYnXy|j ||Wqtj k rt d |dYqXqWd j |} |r| d| } | S) zqDecode the base85-encoded bytes-like object or ASCII string b The result is returned as a bytes object. Nr<~z!Irrbz#bad base85 character at position %dz+base85 overflow in hunk starting at byte %drg)_b85decrVrr"r(rhrirrLrrrxrrj) r:r7rMrloutrchunkr^jrtr r r!r s<     LrBrCcCsxy|jt}|sPx@t|tkr[|jtt|}|sNP||7}qWtj|}|j|qWdS)z1Encode a file; input and output are binary files.N)read MAXBINSIZEr(r&r'write)inputoutputrnsliner r r!rscCs:x3|j}|sPtj|}|j|qWdS)z1Decode a file; input and output are binary files.N)readliner&r3r)rrrrr r r!rs  cCsyt|}WnDtk rV}z$d|jj}t||WYdd}~XnX|jdkrd|j|jjf}t||jdkrd|j|jjf}t|dS) Nz"expected bytes-like object, not %srMr:Bz-expected single byte elements, not %r from %sr#z(expected 1-D data, not %d-D data from %s)rMr:r)rrrrrndim)rmerrmsgr r r!_input_type_checks" rcCsjt|g}xJtdt|tD]0}|||t}|jtj|q)Wdj|S)zVEncode a bytestring into a bytes object containing multiple lines of base-64 data.rrg)rrLr(rrxr&r'rj)rpiecesr7rr r r!rs  cCs)ddl}|jdtdt|S)zLegacy alias of encodebytes().rNzAencodestring() is a deprecated alias since 3.1, use encodebytes()r$)warningswarnDeprecationWarningr)rrr r r! encodestrings   rcCst|tj|S)z8Decode a bytestring of base-64 data into a bytes object.)rr&r3)rr r r!r's cCs)ddl}|jdtdt|S)zLegacy alias of decodebytes().rNzHdecodestring() is a deprecated alias since Python 3.1, use decodebytes()r$)rrrr)rrr r r! decodestring,s   rc Cstddl}ddl}y)|j|jddd\}}Wn`|jk r}z=|j|_t|td|jd|jdWYdd}~XnXt}xa|D]Y\}}|dkrt}|dkrt }|d krt }|d krt dSqW|rW|dd krWt |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) rrZoptsargsrfuncor9fr r r!main6s0)       rcCsdd}tt|t|}tt|t|}tt|||ks`tdS)NsAladdin:open sesame)rr*rrr))Zs0s1s2r r r!rPs  r__main__)2__doc__r0rhr&__all__r,rIrr"rrrrr-r4r5rrrHrGrUrr r r rsrurvryrnrrrrrrr r Z MAXLINESIZErrrrrrrrrrrr r r r!sh             &J $)H -