1 # Conversions to/from quoted-printable transport encoding as per RFC-XXXX
6 HEX
= '0123456789ABCDEF'
8 def needsquoting(c
, quotetabs
):
11 return c
== ESCAPE
or not(' ' <= c
<= '~')
18 return ESCAPE
+ HEX
[i
/16] + HEX
[i
%16]
20 def encode(input, output
, quotetabs
):
22 line
= input.readline()
26 if last
== '\n': line
= line
[:-1]
30 if needsquoting(c
, quotetabs
):
32 if len(new
) + len(c
) >= MAXLINESIZE
:
33 output
.write(new
+ ESCAPE
+ '\n')
37 if prev
in (' ', '\t'):
38 output
.write(new
+ ESCAPE
+ '\n\n')
40 output
.write(new
+ '\n')
42 def decode(input, output
):
45 line
= input.readline()
48 if n
> 0 and line
[n
-1] == '\n':
50 # Strip trailing whitespace
51 while n
> 0 and line
[n
-1] in (' ', '\t'):
58 new
= new
+ c
; i
= i
+1
59 elif i
+1 == n
and not partial
:
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
68 output
.write(new
+ '\n')
74 return '0' <= c
<= '9' or 'a' <= c
<= 'f' or 'A' <= c
<= 'F'
87 bits
= bits
*16 + (ord(c
) - i
)
93 if sys
.argv
[1] == '-t': # Quote tabs
94 encode(sys
.stdin
, sys
.stdout
, 1)
96 decode(sys
.stdin
, sys
.stdout
)
98 encode(sys
.stdin
, sys
.stdout
, 0)
100 if __name__
== '__main__':