Guido van Rossum | aa925a5 | 1997-04-02 05:47:39 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 3 | """Conversions to/from base64 transport encoding as per RFC-1521.""" |
| 4 | |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 5 | # Modified 04-Oct-95 by Jack to use binascii module |
| 6 | |
| 7 | import binascii |
| 8 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 9 | __all__ = ["encode","decode","encodestring","decodestring"] |
| 10 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 11 | MAXLINESIZE = 76 # Excluding the CRLF |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 12 | MAXBINSIZE = (MAXLINESIZE/4)*3 |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 14 | def encode(input, output): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 15 | """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 Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 26 | def decode(input, output): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 27 | """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 Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 33 | |
| 34 | def encodestring(s): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 35 | """Encode a string.""" |
| 36 | import StringIO |
| 37 | f = StringIO.StringIO(s) |
| 38 | g = StringIO.StringIO() |
| 39 | encode(f, g) |
| 40 | return g.getvalue() |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 41 | |
| 42 | def decodestring(s): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 43 | """Decode a string.""" |
| 44 | import StringIO |
| 45 | f = StringIO.StringIO(s) |
| 46 | g = StringIO.StringIO() |
| 47 | decode(f, g) |
| 48 | return g.getvalue() |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 50 | def test(): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 51 | """Small test program""" |
| 52 | import sys, getopt |
| 53 | try: |
| 54 | opts, args = getopt.getopt(sys.argv[1:], 'deut') |
| 55 | except getopt.error, msg: |
| 56 | sys.stdout = sys.stderr |
| 57 | print msg |
Jeremy Hylton | 0365180 | 2000-07-25 14:34:38 +0000 | [diff] [blame] | 58 | print """usage: %s [-d|-e|-u|-t] [file|-] |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 59 | -d, -u: decode |
| 60 | -e: encode (default) |
Jeremy Hylton | 0365180 | 2000-07-25 14:34:38 +0000 | [diff] [blame] | 61 | -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0] |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 62 | sys.exit(2) |
| 63 | func = encode |
| 64 | for o, a in opts: |
| 65 | if o == '-e': func = encode |
| 66 | if o == '-d': func = decode |
| 67 | if o == '-u': func = decode |
| 68 | if o == '-t': test1(); return |
| 69 | if args and args[0] != '-': |
| 70 | func(open(args[0], 'rb'), sys.stdout) |
| 71 | else: |
| 72 | func(sys.stdin, sys.stdout) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 73 | |
| 74 | def test1(): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 75 | s0 = "Aladdin:open sesame" |
| 76 | s1 = encodestring(s0) |
| 77 | s2 = decodestring(s1) |
| 78 | print s0, `s1`, s2 |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 79 | |
| 80 | if __name__ == '__main__': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 81 | test() |