<s> """<STR_LIT>""" <EOL> import struct <EOL> import binascii <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> _translation = [ chr ( _x ) for _x in range ( <NUM_LIT> ) ] <EOL> EMPTYSTRING = '<STR_LIT>' <EOL> def _translate ( s , altchars ) : <EOL> translation = _translation [ : ] <EOL> for k , v in altchars . items ( ) : <EOL> translation [ ord ( k ) ] = v <EOL> t = '<STR_LIT>' <EOL> for c in s : <EOL> t += translation [ ord ( c ) ] <EOL> return t <EOL> def b64encode ( s , altchars = None ) : <EOL> """<STR_LIT>""" <EOL> encoded = binascii . b2a_base64 ( s ) [ : - <NUM_LIT:1> ] <EOL> if altchars is not None : <EOL> return _translate ( encoded , { '<STR_LIT:+>' : altchars [ <NUM_LIT:0> ] , '<STR_LIT:/>' : altchars [ <NUM_LIT:1> ] } ) <EOL> return encoded <EOL> def b64decode ( s , altchars = None ) : <EOL> """<STR_LIT>""" <EOL> if altchars is not None : <EOL> s = _translate ( s , { altchars [ <NUM_LIT:0> ] : '<STR_LIT:+>' , altchars [ <NUM_LIT:1> ] : '<STR_LIT:/>' } ) <EOL> try : <EOL> return binascii . a2b_base64 ( s ) <EOL> except binascii . Error , msg : <EOL> raise TypeError ( msg ) <EOL> def standard_b64encode ( s ) : <EOL> """<STR_LIT>""" <EOL> return b64encode ( s ) <EOL> def standard_b64decode ( s ) : <EOL> """<STR_LIT>""" <EOL> return b64decode ( s ) <EOL> def urlsafe_b64encode ( s ) : <EOL> """<STR_LIT>""" <EOL> return b64encode ( s , '<STR_LIT>' ) <EOL> def urlsafe_b64decode ( s ) : <EOL> """<STR_LIT>""" <EOL> return b64decode ( s , '<STR_LIT>' ) <EOL> _b32alphabet = { <EOL> <NUM_LIT:0> : '<STR_LIT:A>' , <NUM_LIT:9> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT:S>' , <NUM_LIT> : '<STR_LIT:3>' , <EOL> <NUM_LIT:1> : '<STR_LIT:B>' , <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT:T>' , <NUM_LIT> : '<STR_LIT:4>' , <EOL> <NUM_LIT:2> : '<STR_LIT:C>' , <NUM_LIT:11> : '<STR_LIT:L>' , <NUM_LIT:20> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT:5>' , <EOL> <NUM_LIT:3> : '<STR_LIT:D>' , <NUM_LIT:12> : '<STR_LIT:M>' , <NUM_LIT> : '<STR_LIT>' , <NUM_LIT:30> : '<STR_LIT>' , <EOL> <NUM_LIT:4> : '<STR_LIT:E>' , <NUM_LIT> : '<STR_LIT:N>' , <NUM_LIT> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:5> : '<STR_LIT:F>' , <NUM_LIT> : '<STR_LIT:O>' , <NUM_LIT> : '<STR_LIT:X>' , <EOL> <NUM_LIT:6> : '<STR_LIT>' , <NUM_LIT:15> : '<STR_LIT:P>' , <NUM_LIT> : '<STR_LIT:Y>' , <EOL> <NUM_LIT:7> : '<STR_LIT:H>' , <NUM_LIT:16> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:8> : '<STR_LIT:I>' , <NUM_LIT> : '<STR_LIT:R>' , <NUM_LIT> : '<STR_LIT:2>' , <EOL> } <EOL> _b32tab = _b32alphabet . items ( ) <EOL> _b32tab . sort ( ) <EOL> _b32tab = [ v for k , v in _b32tab ] <EOL> _b32rev = dict ( [ ( v , k ) for k , v in _b32alphabet . items ( ) ] ) <EOL> def b32encode ( s ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> quanta , leftover = divmod ( len ( s ) , <NUM_LIT:5> ) <EOL> if leftover : <EOL> s += ( "<STR_LIT>" . ljust ( <NUM_LIT:5> - leftover , '<STR_LIT>' ) ) <EOL> quanta += <NUM_LIT:1> <EOL> for i in range ( quanta ) : <EOL> c1 , c2 , c3 = struct . unpack ( '<STR_LIT>' , s [ i * <NUM_LIT:5> : ( i + <NUM_LIT:1> ) * <NUM_LIT:5> ] ) <EOL> c2 += ( c1 & <NUM_LIT:1> ) << <NUM_LIT:16> <EOL> c3 += ( c2 & <NUM_LIT:3> ) << <NUM_LIT:8> <EOL> parts . extend ( [ _b32tab [ c1 >> <NUM_LIT:11> ] , <EOL> _b32tab [ ( c1 >> <NUM_LIT:6> ) & <NUM_LIT> ] , <EOL> _b32tab [ ( c1 >> <NUM_LIT:1> ) & <NUM_LIT> ] , <EOL> _b32tab [ c2 >> <NUM_LIT:12> ] , <EOL> _b32tab [ ( c2 >> <NUM_LIT:7> ) & <NUM_LIT> ] , <EOL> _b32tab [ ( c2 >> <NUM_LIT:2> ) & <NUM_LIT> ] , <EOL> _b32tab [ c3 >> <NUM_LIT:5> ] , <EOL> _b32tab [ c3 & <NUM_LIT> ] , <EOL> ] ) <EOL> encoded = EMPTYSTRING . join ( parts ) <EOL> if leftover == <NUM_LIT:1> : <EOL> return encoded [ : - <NUM_LIT:6> ] + '<STR_LIT>' <EOL> elif leftover == <NUM_LIT:2> : <EOL> return encoded [ : - <NUM_LIT:4> ] + '<STR_LIT>' <EOL> elif leftover == <NUM_LIT:3> : <EOL> return encoded [ : - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> elif leftover == <NUM_LIT:4> : <EOL> return encoded [ : - <NUM_LIT:1> ] + '<STR_LIT:=>' <EOL> return encoded <EOL> def b32decode ( s , casefold = False , map01 = None ) : <EOL> """<STR_LIT>""" <EOL> quanta , leftover = divmod ( len ( s ) , <NUM_LIT:8> ) <EOL> if leftover : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if map01 : <EOL> s = _translate ( s , { '<STR_LIT:0>' : '<STR_LIT:O>' , '<STR_LIT:1>' : map01 } ) <EOL> if casefold : <EOL> s = s . upper ( ) <EOL> padchars = <NUM_LIT:0> <EOL> padchars = len ( s ) <EOL> s = s . rstrip ( '<STR_LIT:=>' ) <EOL> padchars -= len ( s ) <EOL> parts = [ ] <EOL> acc = <NUM_LIT:0> <EOL> shift = <NUM_LIT> <EOL> for c in s : <EOL> val = _b32rev . get ( c ) <EOL> if val is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> acc += _b32rev [ c ] * ( <NUM_LIT:2> ** shift ) <EOL> shift -= <NUM_LIT:5> <EOL> if shift < <NUM_LIT:0> : <EOL> parts . append ( binascii . unhexlify ( '<STR_LIT>' % acc ) ) <EOL> acc = <NUM_LIT:0> <EOL> shift = <NUM_LIT> <EOL> last = binascii . unhexlify ( '<STR_LIT>' % acc ) <EOL> if padchars == <NUM_LIT:0> : <EOL> last = '<STR_LIT>' <EOL> elif padchars == <NUM_LIT:1> : <EOL> last = last [ : - <NUM_LIT:1> ] <EOL> elif padchars == <NUM_LIT:3> : <EOL> last = last [ : - <NUM_LIT:2> ] <EOL> elif padchars == <NUM_LIT:4> : <EOL> last = last [ : - <NUM_LIT:3> ] <EOL> elif padchars == <NUM_LIT:6> : <EOL> last = last [ : - <NUM_LIT:4> ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> parts . append ( last ) <EOL> return EMPTYSTRING . join ( parts ) <EOL> def b16encode ( s ) : <EOL> """<STR_LIT>""" <EOL> return binascii . hexlify ( s ) . upper ( ) <EOL> def b16decode ( s , casefold = False ) : <EOL> """<STR_LIT>""" <EOL> if casefold : <EOL> s = s . upper ( ) <EOL> from __javascript__ import RegExp <EOL> r = RegExp ( '<STR_LIT>' ) <EOL> if r . test ( s ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return binascii . unhexlify ( s ) <EOL> MAXLINESIZE = <NUM_LIT> <EOL> MAXBINSIZE = ( MAXLINESIZE // <NUM_LIT:4> ) * <NUM_LIT:3> <EOL> def encode ( input , output ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def decode ( input , output ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def encodestring ( s ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( s ) , MAXBINSIZE ) : <EOL> chunk = s [ i : i + MAXBINSIZE ] <EOL> pieces . append ( binascii . b2a_base64 ( chunk ) ) <EOL> return "<STR_LIT>" . join ( pieces ) <EOL> def decodestring ( s ) : <EOL> """<STR_LIT>""" <EOL> return binascii . a2b_base64 ( s ) <EOL> def test ( ) : <EOL> """<STR_LIT>""" <EOL> import sys , getopt <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . error , msg : <EOL> sys . stdout = sys . stderr <EOL> print msg <EOL> print """<STR_LIT>""" % sys . argv [ <NUM_LIT:0> ] <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> func = encode <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : func = encode <EOL> if o == '<STR_LIT>' : func = decode <EOL> if o == '<STR_LIT>' : func = decode <EOL> if o == '<STR_LIT>' : test1 ( ) ; return <EOL> if args and args [ <NUM_LIT:0> ] != '<STR_LIT:->' : <EOL> func ( open ( args [ <NUM_LIT:0> ] , '<STR_LIT:rb>' ) , sys . stdout ) <EOL> else : <EOL> func ( sys . stdin , sys . stdout ) <EOL> def test1 ( ) : <EOL> s0 = "<STR_LIT>" <EOL> s1 = encodestring ( s0 ) <EOL> s2 = decodestring ( s1 ) <EOL> print s0 , repr ( s1 ) , s2 <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
