Guido van Rossum | aa925a5 | 1997-04-02 05:47:39 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 3 | # Conversions to/from base64 transport encoding as per RFC-MIME (Dec 1991 |
| 4 | # version). |
| 5 | |
Guido van Rossum | 25107de | 1997-05-08 23:10:42 +0000 | [diff] [blame] | 6 | # Parameters set by RFX-1421. |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 7 | # |
| 8 | # Modified 04-Oct-95 by Jack to use binascii module |
| 9 | |
| 10 | import binascii |
| 11 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 12 | MAXLINESIZE = 76 # Excluding the CRLF |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 13 | MAXBINSIZE = (MAXLINESIZE/4)*3 |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 14 | |
| 15 | # Encode a file. |
| 16 | def encode(input, output): |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 17 | while 1: |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 18 | s = input.read(MAXBINSIZE) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 19 | if not s: break |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 20 | 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 Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 26 | |
| 27 | # Decode a file. |
| 28 | def decode(input, output): |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 29 | while 1: |
| 30 | line = input.readline() |
| 31 | if not line: break |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 32 | s = binascii.a2b_base64(line) |
| 33 | output.write(s) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 34 | |
| 35 | def encodestring(s): |
| 36 | import StringIO |
| 37 | f = StringIO.StringIO(s) |
| 38 | g = StringIO.StringIO() |
| 39 | encode(f, g) |
| 40 | return g.getvalue() |
| 41 | |
| 42 | def 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 Rossum | 54c1510 | 1995-09-18 21:49:24 +0000 | [diff] [blame] | 49 | # Small test program |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 50 | def test(): |
Guido van Rossum | 3b8e160 | 1995-08-10 19:26:37 +0000 | [diff] [blame] | 51 | import sys, getopt |
| 52 | try: |
| 53 | opts, args = getopt.getopt(sys.argv[1:], 'deut') |
| 54 | except getopt.error, msg: |
Guido van Rossum | 54c1510 | 1995-09-18 21:49:24 +0000 | [diff] [blame] | 55 | sys.stdout = sys.stderr |
Guido van Rossum | 3b8e160 | 1995-08-10 19:26:37 +0000 | [diff] [blame] | 56 | 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 Rossum | 54c1510 | 1995-09-18 21:49:24 +0000 | [diff] [blame] | 61 | sys.exit(2) |
Guido van Rossum | 3b8e160 | 1995-08-10 19:26:37 +0000 | [diff] [blame] | 62 | 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 Rossum | 25107de | 1997-05-08 23:10:42 +0000 | [diff] [blame] | 69 | func(open(args[0], 'rb'), sys.stdout) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 70 | else: |
Guido van Rossum | 3b8e160 | 1995-08-10 19:26:37 +0000 | [diff] [blame] | 71 | func(sys.stdin, sys.stdout) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 72 | |
| 73 | def test1(): |
| 74 | s0 = "Aladdin:open sesame" |
| 75 | s1 = encodestring(s0) |
| 76 | s2 = decodestring(s1) |
| 77 | print s0, `s1`, s2 |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | test() |