blob: 3158fdcee251925fc954cc271aa54d728f170786 [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
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00009__all__ = ["encode","decode","encodestring","decodestring"]
10
Guido van Rossumf1945461995-06-14 23:43:44 +000011MAXLINESIZE = 76 # Excluding the CRLF
Guido van Rossum54e54c62001-09-04 19:14:14 +000012MAXBINSIZE = (MAXLINESIZE//4)*3
Guido van Rossumf1945461995-06-14 23:43:44 +000013
Guido van Rossumf1945461995-06-14 23:43:44 +000014def encode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000015 """Encode a file."""
16 while 1:
17 s = input.read(MAXBINSIZE)
18 if not s: break
19 while len(s) < MAXBINSIZE:
20 ns = input.read(MAXBINSIZE-len(s))
21 if not ns: break
22 s = s + ns
23 line = binascii.b2a_base64(s)
24 output.write(line)
Guido van Rossumf1945461995-06-14 23:43:44 +000025
Guido van Rossumf1945461995-06-14 23:43:44 +000026def decode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000027 """Decode a file."""
28 while 1:
29 line = input.readline()
30 if not line: break
31 s = binascii.a2b_base64(line)
32 output.write(s)
Guido van Rossumf1945461995-06-14 23:43:44 +000033
34def encodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000035 """Encode a string."""
Peter Schneider-Kampfbb2b4c2001-06-07 18:56:13 +000036 pieces = []
37 for i in range(0, len(s), MAXBINSIZE):
38 chunk = s[i : i + MAXBINSIZE]
39 pieces.append(binascii.b2a_base64(chunk))
40 return "".join(pieces)
Guido van Rossumf1945461995-06-14 23:43:44 +000041
42def decodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000043 """Decode a string."""
Peter Schneider-Kampfbb2b4c2001-06-07 18:56:13 +000044 return binascii.a2b_base64(s)
Guido van Rossumf1945461995-06-14 23:43:44 +000045
Guido van Rossumf1945461995-06-14 23:43:44 +000046def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +000047 """Small test program"""
48 import sys, getopt
49 try:
50 opts, args = getopt.getopt(sys.argv[1:], 'deut')
51 except getopt.error, msg:
52 sys.stdout = sys.stderr
53 print msg
Jeremy Hylton03651802000-07-25 14:34:38 +000054 print """usage: %s [-d|-e|-u|-t] [file|-]
Guido van Rossum4acc25b2000-02-02 15:10:15 +000055 -d, -u: decode
56 -e: encode (default)
Jeremy Hylton03651802000-07-25 14:34:38 +000057 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
Guido van Rossum4acc25b2000-02-02 15:10:15 +000058 sys.exit(2)
59 func = encode
60 for o, a in opts:
61 if o == '-e': func = encode
62 if o == '-d': func = decode
63 if o == '-u': func = decode
64 if o == '-t': test1(); return
65 if args and args[0] != '-':
66 func(open(args[0], 'rb'), sys.stdout)
67 else:
68 func(sys.stdin, sys.stdout)
Guido van Rossumf1945461995-06-14 23:43:44 +000069
70def test1():
Guido van Rossum4acc25b2000-02-02 15:10:15 +000071 s0 = "Aladdin:open sesame"
72 s1 = encodestring(s0)
73 s2 = decodestring(s1)
74 print s0, `s1`, s2
Guido van Rossumf1945461995-06-14 23:43:44 +000075
76if __name__ == '__main__':
Guido van Rossum4acc25b2000-02-02 15:10:15 +000077 test()