e fN@szdZddlZddlZddlZddddddd 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/dCdDZ0e1dEkrve/ndS)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|trDy|jdSWqDtk r@tdYqDXnt|trW|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/python34/lib64/python3.4/base64.py_bytes_from_decode_data"s  r"cCsBtj|dd}|dk r>|jtjd|S|S)aSEncode a byte string using Base64. s is the byte string to encode. Optional altchars must 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. The encoded byte string is returned. Ns+/)binascii b2a_base64 translatebytes maketrans)raltcharsencodedr r r!r3s  FcCszt|}|dk rBt|}|jtj|d}n|rmtjd| rmtjdntj|S)aDecode a Base64 encoded byte string. s is the byte string to decode. Optional altchars must be a string of length 2 which specifies the alternative alphabet used instead of the '+' and '/' characters. The decoded string is returned. A binascii.Error is raised if s is incorrectly padded. If validate is False (the default), non-base64-alphabet characters are discarded prior to the padding check. If validate is True, non-base64-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!rEs   cCs t|S)zEncode a byte string using the standard Base64 alphabet. s is the byte string to encode. The encoded byte string is returned. )r)rr r r!r]scCs t|S)aDecode a byte string encoded with the standard Base64 alphabet. s is the byte string to decode. The decoded byte string is returned. binascii.Error is raised if the input is incorrectly padded or if there are non-alphabet characters present in the input. )r)rr r r!rdss+/s-_cCst|jtS)zEncode a byte string using a url-safe Base64 alphabet. s is the byte string to encode. The encoded byte string is returned. The alphabet uses '-' instead of '+' and '_' instead of '/'. )rr'_urlsafe_encode_translation)rr r r!rrscCs%t|}|jt}t|S)aXDecode a byte string encoded with the standard Base64 alphabet. s is the byte string to decode. The decoded byte string is returned. binascii.Error is raised if the input is incorrectly padded or if there are non-alphabet characters present in the input. The alphabet uses '-' instead of '+' and '_' instead of '/'. )r"r'_urlsafe_decode_translationr)rr r r!r{s s ABCDEFGHIJKLMNOPQRSTUVWXYZ234567cstdkrAddtDfddDadnt|tset|j}nt|d}|r|td|}nt}t j }t}x{t dt|dD]a}||||dd}|||d?||d ?d @||d ?d @||d @7}qW|d krGd |dd|dkrd|dds zb32encode..cs'g|]}D]}||qqSr r )r2ab)b32tabr r!r4s rbigi r#s======s====s====ir$) _b32tab2 _b32alphabetrrrrlenr( bytearrayint from_bytesrange)rZleftoverr+rIZb32tab2r3cr )r7r!rs4    +    c Csftdkr(ddttDant|}t|drVtjdn|dk rt|}|jtj dd|}n|r|j }nt|}|j d}|t|}t }t}xt d t|dD]}|||d}d } y'x |D]} | d >|| } q"WWn$tk retjd dYnX|| jd d 7}qW|r\| d |K} | jd d } |d kr| dd|dds zb32decode..zIncorrect paddings01OrArr8zNon-base32 digit foundr9r#r@r>r?r=r$rQrCrQrBrQ)_b32rev enumeraterEr"rFr%r.r'r(r)upperrstriprGrJKeyErrorto_bytes) rcasefoldZmap01lZpadcharsdecodedZb32revr3ZquantaaccrKlastr r r!r sJ    "        cCstj|jS)zrEncode a byte string using Base16. s is the byte string to encode. The encoded byte string is returned. )r%ZhexlifyrU)rr r r!r scCsRt|}|r!|j}ntjd|rEtjdntj|S)aDecode a Base16 encoded byte string. s is the byte string to decode. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. The decoded byte string is returned. binascii.Error is raised if s were incorrectly padded or if there are non-alphabet characters present in the string. s [^0-9A-F]zNon-base16 digit found)r"rUr,searchr%r.Z unhexlify)rrYr r r!r s s<~s~>c st|ts$t|j}nt| d}|rL|d|}ntjdt|dj|}fdd|D}|r| r|d dkrdd |d .r#r_rr8r$r$r$r$) rrrrrFstructStructZunpackjoin) r6rbrcpadrdrepaddingZwordschunksr )rbrcrdrer! _85encode&s&  rmrewrapcolrjadobecstdkr>ddtddDaddtDant|tt|d||rltnrt|rdnd fd dtd tD}|rt|ddkr|jd qnd j|n|rt 7nS)a Encode a byte string using Ascii85. b is the byte string to encode. The encoded byte string is returned. 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 ('\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 string 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()r2r3r r r!r4Us za85encode..!vcSs'g|]}tD]}||qqSr ) _a85chars)r2r5r6r r r!r4Vs Tr>r#cs$g|]}||qSr r )r2r3)resultrnr r!r4^s rrfs r$) rrrJ _a85chars2rm _A85STARTmaxrFappendri_A85END)r6rernrjrorlr )rsrnr!r>s"    ignorecharss c Cst|}|r^|jto-|jtsKtdjttn|dd}ntjdj }g}|j }g}|j }|j } x?|dD]3} d| kodknrQ|| t |dkrd} x |D]} d | | d } qWy||| Wn$tj k rCtd d YnX| qq| dkr|rrtdn|dq|r| dkr|rtdn|dq| |krqqtd| qWdj|} dt |} | r| d | } n| S)acDecode an Ascii85 encoded byte string. s is the byte string to decode. 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. zAAscii85 encoded byte sequences must be bracketed by {!r} and {!r}r>z!Iur?!rr8rarpzAscii85 overflowNr_zz inside Ascii85 5-tuplesr`zy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crfrRsuuuurpuzy)r" startswithruendswithrxrformatrgrhpackrwclearrFerrorri)r6rerorypackIr[Zdecoded_appendcurrZ curr_appendZ curr_clearxr\rsrkr r r!risP            sU0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~cCsHtdkr5ddtDaddtDant|tt|S)zEncode an ASCII-encoded byte array in base85 format. If pad is true, the input is padded with "\0" so its length is a multiple of 4 characters before encoding. NcSsg|]}t|fqSr )r()r2r3r r r!r4s zb85encode..cSs'g|]}tD]}||qqSr ) _b85chars)r2r5r6r r r!r4s )r _b85alphabet _b85chars2rm)r6rjr r r!r s c CstdkrCdgdax'ttD]\}}|t|)warningswarnDeprecationWarningr)rrr r r! encodestrings   rcCst|tj|S)z6Decode a bytestring of base-64 data into a bytestring.)rr%r/)rr r r!r's cCs)ddl}|jdtdt|S)zLegacy alias of decodebytes().rNz7decodestring() is a deprecated alias, use decodebytes()r>)rrrr)rrr r r! decodestring,s   rc Cs|ddl}ddl}y)|j|jddd\}}Wn`|jk r}z=|j|_t|td|jd|jdWYdd}~XnXt}xj|D]b\}}|dkrt}n|dkrt }n|d krt }n|d krt dSqW|r_|dd kr_t |dd }|||jj WdQXn||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) rrZoptsargsrfuncor5fr r r!main5s0)       rcCsRd}tt|t|}tt|t|}tt|dS)NsAladdin:open sesame)rreprrr)Zs0s1s2r r r!rOs  r__main__)2__doc__r,rgr%__all__r(rGrr"rrrrr)r0r1rrrErDrSrr r r rrrtrurxrmrrrrrrr r Z MAXLINESIZErrrrrrrrrrrr r r r!sh             (K $+C *