@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 /usr/lib/python3.4/base64.py_bytes_from_decode_data"s  r"cCsftj|dd}|dk rbt|dksItt||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_base64lenAssertionErrorrepr translatebytes maketrans)raltcharsencodedr r r!r3s  $FcCst|}|dk rft|}t|dksHtt||jtj|d}n|rtjd| rt j dnt j |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. 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!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 )r6ab)b32tabr r!r8s rbigi r#s======r$s====s====ir%) _b32tab2 _b32alphabetrrrrr(r, bytearrayint from_bytesrange)rZleftoverr/rKZb32tab2r7cr )r;r!rs4    +    c Cstdkr(ddttDant|}t|drVtjdn|dk rt|}t|dkstt||j t j dd|}n|r|j }nt|}|j d }|t|}t}t}xtd t|dD]}|||d}d } y'x |D]} | d >|| } qFWWn$tk rtjd dYnX|| jd d 7}qW|r| 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) rcasefoldZmap01lZpadcharsdecodedZb32revr7ZquantaaccrMlastr r r!r sL    $"        cCstj|jS)zrEncode a byte string using Base16. s is the byte string to encode. The encoded byte string is returned. )r&ZhexlifyrW)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"rWr0searchr&r2Z unhexlify)rr[r 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#rarr<r%r%r%r%) rrrrr(structStructZunpackjoin) r:rdrepadrfrgpaddingZwordschunksr )rdrerfrgr! _85encode&s&  rorgwrapcolrladobecstdkr>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 (' ') 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,)r6r7r r r!r8Us za85encode..!vcSs'g|]}tD]}||qqSr ) _a85chars)r6r9r:r r r!r8Vs Tr$r#cs$g|]}||qSr r )r6r7)resultrpr r!r8^s rrhs r%) rtrL _a85chars2ro _A85STARTmaxr(appendrk_A85END)r:rgrprlrqrnr )rurpr!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!IurB!rr<rcrrzAscii85 overflowNrazz inside Ascii85 5-tuplesrbzy inside Ascii85 5-tuples zNon-Ascii85 digit found: %crhrTsuuuurruzy)r" startswithrwendswithrzrformatrirjpackryclearr(errorrk)r:rgrqr{packIr]Zdecoded_appendcurrZ curr_appendZ curr_clearxr^rurmr 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 "" so its length is a multiple of 4 characters before encoding. NcSsg|]}t|fqSr )r,)r6r7r r r!r8s zb85encode..cSs'g|]}tD]}||qqSr ) _b85chars)r6r9r:r r r!r8s )r _b85alphabet _b85chars2ro)r:rlr r r!r s c CstdkrCdgdax'ttD]\}}|t|sh             (K $+C *