blob: f20a51a13e3420d833e7cd1314bd564238be384a [file] [log] [blame]
Guido van Rossumaa925a51997-04-02 05:47:39 +00001#! /usr/bin/env python
2
Guido van Rossumf1945461995-06-14 23:43:44 +00003# Conversions to/from base64 transport encoding as per RFC-MIME (Dec 1991
4# version).
5
Fred Drake5b34ec11998-05-22 18:18:08 +00006# Parameters set by RFC-1421.
Jack Jansen951213e1995-10-04 16:39:20 +00007#
8# Modified 04-Oct-95 by Jack to use binascii module
9
10import binascii
11
Guido van Rossumf1945461995-06-14 23:43:44 +000012MAXLINESIZE = 76 # Excluding the CRLF
Jack Jansen951213e1995-10-04 16:39:20 +000013MAXBINSIZE = (MAXLINESIZE/4)*3
Guido van Rossumf1945461995-06-14 23:43:44 +000014
15# Encode a file.
16def encode(input, output):
Guido van Rossumf1945461995-06-14 23:43:44 +000017 while 1:
Jack Jansen951213e1995-10-04 16:39:20 +000018 s = input.read(MAXBINSIZE)
Guido van Rossumf1945461995-06-14 23:43:44 +000019 if not s: break
Jack Jansen951213e1995-10-04 16:39:20 +000020 while len(s) < MAXBINSIZE:
21 ns = input.read(MAXBINSIZE-len(s))
22 if not ns: break
23 s = s + ns
24 line = binascii.b2a_base64(s)
25 output.write(line)
Guido van Rossumf1945461995-06-14 23:43:44 +000026
27# Decode a file.
28def decode(input, output):
Guido van Rossumf1945461995-06-14 23:43:44 +000029 while 1:
30 line = input.readline()
31 if not line: break
Jack Jansen951213e1995-10-04 16:39:20 +000032 s = binascii.a2b_base64(line)
33 output.write(s)
Guido van Rossumf1945461995-06-14 23:43:44 +000034
35def encodestring(s):
36 import StringIO
37 f = StringIO.StringIO(s)
38 g = StringIO.StringIO()
39 encode(f, g)
40 return g.getvalue()
41
42def decodestring(s):
43 import StringIO
44 f = StringIO.StringIO(s)
45 g = StringIO.StringIO()
46 decode(f, g)
47 return g.getvalue()
48
Guido van Rossum54c15101995-09-18 21:49:24 +000049# Small test program
Guido van Rossumf1945461995-06-14 23:43:44 +000050def test():
Guido van Rossum3b8e1601995-08-10 19:26:37 +000051 import sys, getopt
52 try:
53 opts, args = getopt.getopt(sys.argv[1:], 'deut')
54 except getopt.error, msg:
Guido van Rossum54c15101995-09-18 21:49:24 +000055 sys.stdout = sys.stderr
Guido van Rossum3b8e1601995-08-10 19:26:37 +000056 print msg
57 print """usage: basd64 [-d] [-e] [-u] [-t] [file|-]
58 -d, -u: decode
59 -e: encode (default)
60 -t: decode string 'Aladdin:open sesame'"""
Guido van Rossum54c15101995-09-18 21:49:24 +000061 sys.exit(2)
Guido van Rossum3b8e1601995-08-10 19:26:37 +000062 func = encode
63 for o, a in opts:
64 if o == '-e': func = encode
65 if o == '-d': func = decode
66 if o == '-u': func = decode
67 if o == '-t': test1(); return
68 if args and args[0] != '-':
Guido van Rossum25107de1997-05-08 23:10:42 +000069 func(open(args[0], 'rb'), sys.stdout)
Guido van Rossumf1945461995-06-14 23:43:44 +000070 else:
Guido van Rossum3b8e1601995-08-10 19:26:37 +000071 func(sys.stdin, sys.stdout)
Guido van Rossumf1945461995-06-14 23:43:44 +000072
73def test1():
74 s0 = "Aladdin:open sesame"
75 s1 = encodestring(s0)
76 s2 = decodestring(s1)
77 print s0, `s1`, s2
78
79if __name__ == '__main__':
80 test()