blob: ae67f68eccb38d597966a01d7fa936d02e4f3f14 [file] [log] [blame]
Guido van Rossumaa925a51997-04-02 05:47:39 +00001#! /usr/bin/env python
2
Guido van Rossum4acc25b2000-02-02 15:10:15 +00003"""Conversions to/from base64 transport encoding as per RFC-1521."""
4
Jack Jansen951213e1995-10-04 16:39:20 +00005# Modified 04-Oct-95 by Jack to use binascii module
6
7import binascii
8
Guido van Rossumf1945461995-06-14 23:43:44 +00009MAXLINESIZE = 76 # Excluding the CRLF
Jack Jansen951213e1995-10-04 16:39:20 +000010MAXBINSIZE = (MAXLINESIZE/4)*3
Guido van Rossumf1945461995-06-14 23:43:44 +000011
Guido van Rossumf1945461995-06-14 23:43:44 +000012def encode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000013 """Encode a file."""
14 while 1:
15 s = input.read(MAXBINSIZE)
16 if not s: break
17 while len(s) < MAXBINSIZE:
18 ns = input.read(MAXBINSIZE-len(s))
19 if not ns: break
20 s = s + ns
21 line = binascii.b2a_base64(s)
22 output.write(line)
Guido van Rossumf1945461995-06-14 23:43:44 +000023
Guido van Rossumf1945461995-06-14 23:43:44 +000024def decode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000025 """Decode a file."""
26 while 1:
27 line = input.readline()
28 if not line: break
29 s = binascii.a2b_base64(line)
30 output.write(s)
Guido van Rossumf1945461995-06-14 23:43:44 +000031
32def encodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000033 """Encode a string."""
34 import StringIO
35 f = StringIO.StringIO(s)
36 g = StringIO.StringIO()
37 encode(f, g)
38 return g.getvalue()
Guido van Rossumf1945461995-06-14 23:43:44 +000039
40def decodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000041 """Decode a string."""
42 import StringIO
43 f = StringIO.StringIO(s)
44 g = StringIO.StringIO()
45 decode(f, g)
46 return g.getvalue()
Guido van Rossumf1945461995-06-14 23:43:44 +000047
Guido van Rossumf1945461995-06-14 23:43:44 +000048def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +000049 """Small test program"""
50 import sys, getopt
51 try:
52 opts, args = getopt.getopt(sys.argv[1:], 'deut')
53 except getopt.error, msg:
54 sys.stdout = sys.stderr
55 print msg
Jeremy Hylton03651802000-07-25 14:34:38 +000056 print """usage: %s [-d|-e|-u|-t] [file|-]
Guido van Rossum4acc25b2000-02-02 15:10:15 +000057 -d, -u: decode
58 -e: encode (default)
Jeremy Hylton03651802000-07-25 14:34:38 +000059 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
Guido van Rossum4acc25b2000-02-02 15:10:15 +000060 sys.exit(2)
61 func = encode
62 for o, a in opts:
63 if o == '-e': func = encode
64 if o == '-d': func = decode
65 if o == '-u': func = decode
66 if o == '-t': test1(); return
67 if args and args[0] != '-':
68 func(open(args[0], 'rb'), sys.stdout)
69 else:
70 func(sys.stdin, sys.stdout)
Guido van Rossumf1945461995-06-14 23:43:44 +000071
72def test1():
Guido van Rossum4acc25b2000-02-02 15:10:15 +000073 s0 = "Aladdin:open sesame"
74 s1 = encodestring(s0)
75 s2 = decodestring(s1)
76 print s0, `s1`, s2
Guido van Rossumf1945461995-06-14 23:43:44 +000077
78if __name__ == '__main__':
Guido van Rossum4acc25b2000-02-02 15:10:15 +000079 test()