annotate Lib/base64.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 3c6db7165bc3
children ecdd4b468626
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5243
4f251b8e328f Add #! line.
Guido van Rossum <guido@python.org>
parents: 3145
diff changeset
1 #! /usr/bin/env python
4f251b8e328f Add #! line.
Guido van Rossum <guido@python.org>
parents: 3145
diff changeset
2
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Conversions to/from base64 transport encoding as per RFC-MIME (Dec 1991
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # version).
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
5441
3c6db7165bc3 Set correct RFC number. Open input in binary.
Guido van Rossum <guido@python.org>
parents: 5243
diff changeset
6 # Parameters set by RFX-1421.
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
7 #
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
8 # Modified 04-Oct-95 by Jack to use binascii module
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
10 import binascii
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
12 MAXLINESIZE = 76 # Excluding the CRLF
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
13 MAXBINSIZE = (MAXLINESIZE/4)*3
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # Encode a file.
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 def encode(input, output):
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 while 1:
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
18 s = input.read(MAXBINSIZE)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 if not s: break
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
20 while len(s) < MAXBINSIZE:
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
21 ns = input.read(MAXBINSIZE-len(s))
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
22 if not ns: break
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
23 s = s + ns
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
24 line = binascii.b2a_base64(s)
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
25 output.write(line)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # Decode a file.
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 def decode(input, output):
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 while 1:
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 line = input.readline()
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 if not line: break
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
32 s = binascii.a2b_base64(line)
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
33 output.write(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def encodestring(s):
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 import StringIO
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 f = StringIO.StringIO(s)
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 g = StringIO.StringIO()
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 encode(f, g)
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 return g.getvalue()
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def decodestring(s):
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 import StringIO
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 f = StringIO.StringIO(s)
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 g = StringIO.StringIO()
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 decode(f, g)
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 return g.getvalue()
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
3104
56dba037b8d6 improved test/main program
Guido van Rossum <guido@python.org>
parents: 2950
diff changeset
49 # Small test program
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def test():
2950
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
51 import sys, getopt
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
52 try:
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
53 opts, args = getopt.getopt(sys.argv[1:], 'deut')
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
54 except getopt.error, msg:
3104
56dba037b8d6 improved test/main program
Guido van Rossum <guido@python.org>
parents: 2950
diff changeset
55 sys.stdout = sys.stderr
2950
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
56 print msg
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
57 print """usage: basd64 [-d] [-e] [-u] [-t] [file|-]
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
58 -d, -u: decode
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
59 -e: encode (default)
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
60 -t: decode string 'Aladdin:open sesame'"""
3104
56dba037b8d6 improved test/main program
Guido van Rossum <guido@python.org>
parents: 2950
diff changeset
61 sys.exit(2)
2950
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
62 func = encode
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
63 for o, a in opts:
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
64 if o == '-e': func = encode
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
65 if o == '-d': func = decode
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
66 if o == '-u': func = decode
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
67 if o == '-t': test1(); return
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
68 if args and args[0] != '-':
5441
3c6db7165bc3 Set correct RFC number. Open input in binary.
Guido van Rossum <guido@python.org>
parents: 5243
diff changeset
69 func(open(args[0], 'rb'), sys.stdout)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 else:
2950
a1a9b7364bc3 upgdaded the test program
Guido van Rossum <guido@python.org>
parents: 2742
diff changeset
71 func(sys.stdin, sys.stdout)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def test1():
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 s0 = "Aladdin:open sesame"
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 s1 = encodestring(s0)
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 s2 = decodestring(s1)
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 print s0, `s1`, s2
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if __name__ == '__main__':
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 test()