Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f194546

Browse files
committed
two mime encoding schemes
1 parent 8ddd0ad commit f194546

2 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lib/base64.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Conversions to/from base64 transport encoding as per RFC-MIME (Dec 1991
2+
# version).
3+
4+
# Parameters set by RFX-XXXX.
5+
MAXLINESIZE = 76 # Excluding the CRLF
6+
INVAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
7+
PAD = '='
8+
9+
# Check that I typed that string correctly...
10+
if len(INVAR) <> 64: raise RuntimeError, 'wrong INVAR string!?!?'
11+
12+
# Compute the inverse table, for decode().
13+
inverse = {}
14+
for i in range(64): inverse[INVAR[i]] = i
15+
del i
16+
inverse[PAD] = 0
17+
18+
# Encode a file.
19+
def encode(input, output):
20+
line = ''
21+
BUFSIZE = 8192
22+
leftover = ''
23+
while 1:
24+
s = input.read(BUFSIZE)
25+
if not s: break
26+
s = leftover + s
27+
i = 0
28+
while i+3 <= len(s):
29+
quad = makequad(s[i:i+3])
30+
i = i+3
31+
if len(line) + 4 > MAXLINESIZE:
32+
output.write(line + '\n')
33+
line = ''
34+
line = line + quad
35+
leftover = s[i:]
36+
if leftover:
37+
quad = makeshortquad(leftover)
38+
if len(line) + 4 > MAXLINESIZE:
39+
output.write(line + '\n')
40+
line = ''
41+
line = line + quad
42+
if line:
43+
output.write(line + '\n')
44+
45+
def makequad(s): # Return the quad for a 3 character string
46+
x = ord(s[0])*0x10000 + ord(s[1])*0x100 + ord(s[2])
47+
x, c4 = divmod(x, 64)
48+
x, c3 = divmod(x, 64)
49+
c1, c2 = divmod(x, 64)
50+
return INVAR[c1] + INVAR[c2] +INVAR[c3] + INVAR[c4]
51+
52+
def makeshortquad(s): # Return the quad value for a 1 or 2 character string
53+
n = len(s)
54+
while len(s) < 3:
55+
s = s + '\0'
56+
quad = makequad(s)
57+
if n == 2:
58+
quad = quad[:3] + PAD
59+
elif n == 1:
60+
quad = quad[:2] + 2*PAD
61+
return quad
62+
63+
# Decode a file.
64+
def decode(input, output):
65+
BUFSIZE = 8192
66+
bits, n, bytes, prev = 0, 0, '', ''
67+
while 1:
68+
line = input.readline()
69+
if not line: break
70+
for c in line:
71+
if inverse.has_key(c):
72+
bits = bits*64 + inverse[c]
73+
n = n+6
74+
if n == 24:
75+
triplet = decodequad(bits)
76+
if c == PAD:
77+
if prev == PAD:
78+
triplet = triplet[:1]
79+
else:
80+
triplet = triplet[:2]
81+
bits, n = 0, 0
82+
bytes = bytes + triplet
83+
if len(bytes) > BUFSIZE:
84+
output.write(bytes[:BUFSIZE])
85+
bytes = bytes[BUFSIZE:]
86+
prev = c
87+
if bytes:
88+
output.write(bytes)
89+
90+
def decodequad(bits): # Turn 24 bits into 3 characters
91+
bits, c3 = divmod(bits, 256)
92+
c1, c2 = divmod(bits, 256)
93+
return chr(c1) + chr(c2) + chr(c3)
94+
95+
def encodestring(s):
96+
import StringIO
97+
f = StringIO.StringIO(s)
98+
g = StringIO.StringIO()
99+
encode(f, g)
100+
return g.getvalue()
101+
102+
def decodestring(s):
103+
import StringIO
104+
f = StringIO.StringIO(s)
105+
g = StringIO.StringIO()
106+
decode(f, g)
107+
return g.getvalue()
108+
109+
# Small test program, reads stdin, writes stdout.
110+
# no arg: encode, any arg: decode.
111+
def test():
112+
import sys
113+
if sys.argv[1:]:
114+
decode(sys.stdin, sys.stdout)
115+
else:
116+
encode(sys.stdin, sys.stdout)
117+
118+
def test1():
119+
s0 = "Aladdin:open sesame"
120+
s1 = encodestring(s0)
121+
s2 = decodestring(s1)
122+
print s0, `s1`, s2
123+
124+
if __name__ == '__main__':
125+
test()

Lib/quopri.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Conversions to/from quoted-printable transport encoding as per RFC-XXXX
2+
# (Dec 1991 version).
3+
4+
ESCAPE = '='
5+
MAXLINESIZE = 76
6+
HEX = '0123456789ABCDEF'
7+
8+
def needsquoting(c, quotetabs):
9+
if c == '\t':
10+
return not quotetabs
11+
return c == ESCAPE or not(' ' <= c <= '~')
12+
13+
def quote(c):
14+
if c == ESCAPE:
15+
return ESCAPE * 2
16+
else:
17+
i = ord(c)
18+
return ESCAPE + HEX[i/16] + HEX[i%16]
19+
20+
def encode(input, output, quotetabs):
21+
while 1:
22+
line = input.readline()
23+
if not line: break
24+
new = ''
25+
last = line[-1:]
26+
if last == '\n': line = line[:-1]
27+
else: last = ''
28+
prev = ''
29+
for c in line:
30+
if needsquoting(c, quotetabs):
31+
c = quote(c)
32+
if len(new) + len(c) >= MAXLINESIZE:
33+
output.write(new + ESCAPE + '\n')
34+
new = ''
35+
new = new + c
36+
prev = c
37+
if prev in (' ', '\t'):
38+
output.write(new + ESCAPE + '\n\n')
39+
else:
40+
output.write(new + '\n')
41+
42+
def decode(input, output):
43+
new = ''
44+
while 1:
45+
line = input.readline()
46+
if not line: break
47+
i, n = 0, len(line)
48+
if n > 0 and line[n-1] == '\n':
49+
partial = 0; n = n-1
50+
# Strip trailing whitespace
51+
while n > 0 and line[n-1] in (' ', '\t'):
52+
n = n-1
53+
else:
54+
partial = 1
55+
while i < n:
56+
c = line[i]
57+
if c <> ESCAPE:
58+
new = new + c; i = i+1
59+
elif i+1 == n and not partial:
60+
partial = 1; break
61+
elif i+1 < n and line[i+1] == ESCAPE:
62+
new = new + ESCAPE; i = i+2
63+
elif i+2 < n and ishex(line[i+1]) and ishex(line[i+2]):
64+
new = new + chr(unhex(line[i+1:i+3])); i = i+3
65+
else: # Bad escape sequence -- leave it in
66+
new = new + c; i = i+1
67+
if not partial:
68+
output.write(new + '\n')
69+
new = ''
70+
if new:
71+
output.write(new)
72+
73+
def ishex(c):
74+
return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F'
75+
76+
def unhex(s):
77+
bits = 0
78+
for c in s:
79+
if '0' <= c <= '9':
80+
i = ord('0')
81+
elif 'a' <= c <= 'f':
82+
i = ord('a')-10
83+
elif 'A' <= c <= 'F':
84+
i = ord('A')-10
85+
else:
86+
break
87+
bits = bits*16 + (ord(c) - i)
88+
return bits
89+
90+
def test():
91+
import sys
92+
if sys.argv[1:]:
93+
if sys.argv[1] == '-t': # Quote tabs
94+
encode(sys.stdin, sys.stdout, 1)
95+
else:
96+
decode(sys.stdin, sys.stdout)
97+
else:
98+
encode(sys.stdin, sys.stdout, 0)
99+
100+
if __name__ == '__main__':
101+
main()

0 commit comments

Comments
 (0)