3 """Conversions to/from base64 transport encoding as per RFC-1521."""
5 # Modified 04-Oct-95 by Jack to use binascii module
9 MAXLINESIZE
= 76 # Excluding the CRLF
10 MAXBINSIZE
= (MAXLINESIZE
/4)*3
12 def encode(input, output
):
15 s
= input.read(MAXBINSIZE
)
17 while len(s
) < MAXBINSIZE
:
18 ns
= input.read(MAXBINSIZE
-len(s
))
21 line
= binascii
.b2a_base64(s
)
24 def decode(input, output
):
27 line
= input.readline()
29 s
= binascii
.a2b_base64(line
)
33 """Encode a string."""
35 f
= StringIO
.StringIO(s
)
36 g
= StringIO
.StringIO()
41 """Decode a string."""
43 f
= StringIO
.StringIO(s
)
44 g
= StringIO
.StringIO()
49 """Small test program"""
52 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'deut')
53 except getopt
.error
, msg
:
54 sys
.stdout
= sys
.stderr
56 print """usage: basd64 [-d] [-e] [-u] [-t] [file|-]
59 -t: decode string 'Aladdin:open sesame'"""
63 if o
== '-e': func
= encode
64 if o
== '-d': func
= decode
65 if o
== '-u': func
= decode
66 if o
== '-t': test1(); return
67 if args
and args
[0] != '-':
68 func(open(args
[0], 'rb'), sys
.stdout
)
70 func(sys
.stdin
, sys
.stdout
)
73 s0
= "Aladdin:open sesame"
78 if __name__
== '__main__':