home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / base64.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  2.8 KB  |  96 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import binascii
  5. __all__ = [
  6.     'encode',
  7.     'decode',
  8.     'encodestring',
  9.     'decodestring']
  10. MAXLINESIZE = 76
  11. MAXBINSIZE = (MAXLINESIZE // 4) * 3
  12.  
  13. def encode(input, output):
  14.     while 1:
  15.         s = input.read(MAXBINSIZE)
  16.         if not s:
  17.             break
  18.         
  19.         while len(s) < MAXBINSIZE:
  20.             ns = input.read(MAXBINSIZE - len(s))
  21.             if not ns:
  22.                 break
  23.             
  24.             s = s + ns
  25.         line = binascii.b2a_base64(s)
  26.         output.write(line)
  27.  
  28.  
  29. def decode(input, output):
  30.     while 1:
  31.         line = input.readline()
  32.         if not line:
  33.             break
  34.         
  35.         s = binascii.a2b_base64(line)
  36.         output.write(s)
  37.  
  38.  
  39. def encodestring(s):
  40.     pieces = []
  41.     for i in range(0, len(s), MAXBINSIZE):
  42.         chunk = s[i:i + MAXBINSIZE]
  43.         pieces.append(binascii.b2a_base64(chunk))
  44.     
  45.     return ''.join(pieces)
  46.  
  47.  
  48. def decodestring(s):
  49.     return binascii.a2b_base64(s)
  50.  
  51.  
  52. def test():
  53.     import sys
  54.     import getopt
  55.     
  56.     try:
  57.         (opts, args) = getopt.getopt(sys.argv[1:], 'deut')
  58.     except getopt.error:
  59.         msg = None
  60.         sys.stdout = sys.stderr
  61.         print msg
  62.         print "usage: %s [-d|-e|-u|-t] [file|-]\n        -d, -u: decode\n        -e: encode (default)\n        -t: encode and decode string 'Aladdin:open sesame'" % sys.argv[0]
  63.         sys.exit(2)
  64.  
  65.     func = encode
  66.     for o, a in opts:
  67.         if o == '-e':
  68.             func = encode
  69.         
  70.         if o == '-d':
  71.             func = decode
  72.         
  73.         if o == '-u':
  74.             func = decode
  75.         
  76.         if o == '-t':
  77.             test1()
  78.             return None
  79.         
  80.     
  81.     if args and args[0] != '-':
  82.         func(open(args[0], 'rb'), sys.stdout)
  83.     else:
  84.         func(sys.stdin, sys.stdout)
  85.  
  86.  
  87. def test1():
  88.     s0 = 'Aladdin:open sesame'
  89.     s1 = encodestring(s0)
  90.     s2 = decodestring(s1)
  91.     print s0, `s1`, s2
  92.  
  93. if __name__ == '__main__':
  94.     test()
  95.  
  96.