1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """Conversions to/from quoted-printable transport encoding as per RFC 1521.""" |
---|
4 | n/a | |
---|
5 | n/a | # (Dec 1991 version). |
---|
6 | n/a | |
---|
7 | n/a | __all__ = ["encode", "decode", "encodestring", "decodestring"] |
---|
8 | n/a | |
---|
9 | n/a | ESCAPE = b'=' |
---|
10 | n/a | MAXLINESIZE = 76 |
---|
11 | n/a | HEX = b'0123456789ABCDEF' |
---|
12 | n/a | EMPTYSTRING = b'' |
---|
13 | n/a | |
---|
14 | n/a | try: |
---|
15 | n/a | from binascii import a2b_qp, b2a_qp |
---|
16 | n/a | except ImportError: |
---|
17 | n/a | a2b_qp = None |
---|
18 | n/a | b2a_qp = None |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | def needsquoting(c, quotetabs, header): |
---|
22 | n/a | """Decide whether a particular byte ordinal needs to be quoted. |
---|
23 | n/a | |
---|
24 | n/a | The 'quotetabs' flag indicates whether embedded tabs and spaces should be |
---|
25 | n/a | quoted. Note that line-ending tabs and spaces are always encoded, as per |
---|
26 | n/a | RFC 1521. |
---|
27 | n/a | """ |
---|
28 | n/a | assert isinstance(c, bytes) |
---|
29 | n/a | if c in b' \t': |
---|
30 | n/a | return quotetabs |
---|
31 | n/a | # if header, we have to escape _ because _ is used to escape space |
---|
32 | n/a | if c == b'_': |
---|
33 | n/a | return header |
---|
34 | n/a | return c == ESCAPE or not (b' ' <= c <= b'~') |
---|
35 | n/a | |
---|
36 | n/a | def quote(c): |
---|
37 | n/a | """Quote a single character.""" |
---|
38 | n/a | assert isinstance(c, bytes) and len(c)==1 |
---|
39 | n/a | c = ord(c) |
---|
40 | n/a | return ESCAPE + bytes((HEX[c//16], HEX[c%16])) |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | |
---|
44 | n/a | def encode(input, output, quotetabs, header=False): |
---|
45 | n/a | """Read 'input', apply quoted-printable encoding, and write to 'output'. |
---|
46 | n/a | |
---|
47 | n/a | 'input' and 'output' are binary file objects. The 'quotetabs' flag |
---|
48 | n/a | indicates whether embedded tabs and spaces should be quoted. Note that |
---|
49 | n/a | line-ending tabs and spaces are always encoded, as per RFC 1521. |
---|
50 | n/a | The 'header' flag indicates whether we are encoding spaces as _ as per RFC |
---|
51 | n/a | 1522.""" |
---|
52 | n/a | |
---|
53 | n/a | if b2a_qp is not None: |
---|
54 | n/a | data = input.read() |
---|
55 | n/a | odata = b2a_qp(data, quotetabs=quotetabs, header=header) |
---|
56 | n/a | output.write(odata) |
---|
57 | n/a | return |
---|
58 | n/a | |
---|
59 | n/a | def write(s, output=output, lineEnd=b'\n'): |
---|
60 | n/a | # RFC 1521 requires that the line ending in a space or tab must have |
---|
61 | n/a | # that trailing character encoded. |
---|
62 | n/a | if s and s[-1:] in b' \t': |
---|
63 | n/a | output.write(s[:-1] + quote(s[-1:]) + lineEnd) |
---|
64 | n/a | elif s == b'.': |
---|
65 | n/a | output.write(quote(s) + lineEnd) |
---|
66 | n/a | else: |
---|
67 | n/a | output.write(s + lineEnd) |
---|
68 | n/a | |
---|
69 | n/a | prevline = None |
---|
70 | n/a | while 1: |
---|
71 | n/a | line = input.readline() |
---|
72 | n/a | if not line: |
---|
73 | n/a | break |
---|
74 | n/a | outline = [] |
---|
75 | n/a | # Strip off any readline induced trailing newline |
---|
76 | n/a | stripped = b'' |
---|
77 | n/a | if line[-1:] == b'\n': |
---|
78 | n/a | line = line[:-1] |
---|
79 | n/a | stripped = b'\n' |
---|
80 | n/a | # Calculate the un-length-limited encoded line |
---|
81 | n/a | for c in line: |
---|
82 | n/a | c = bytes((c,)) |
---|
83 | n/a | if needsquoting(c, quotetabs, header): |
---|
84 | n/a | c = quote(c) |
---|
85 | n/a | if header and c == b' ': |
---|
86 | n/a | outline.append(b'_') |
---|
87 | n/a | else: |
---|
88 | n/a | outline.append(c) |
---|
89 | n/a | # First, write out the previous line |
---|
90 | n/a | if prevline is not None: |
---|
91 | n/a | write(prevline) |
---|
92 | n/a | # Now see if we need any soft line breaks because of RFC-imposed |
---|
93 | n/a | # length limitations. Then do the thisline->prevline dance. |
---|
94 | n/a | thisline = EMPTYSTRING.join(outline) |
---|
95 | n/a | while len(thisline) > MAXLINESIZE: |
---|
96 | n/a | # Don't forget to include the soft line break `=' sign in the |
---|
97 | n/a | # length calculation! |
---|
98 | n/a | write(thisline[:MAXLINESIZE-1], lineEnd=b'=\n') |
---|
99 | n/a | thisline = thisline[MAXLINESIZE-1:] |
---|
100 | n/a | # Write out the current line |
---|
101 | n/a | prevline = thisline |
---|
102 | n/a | # Write out the last line, without a trailing newline |
---|
103 | n/a | if prevline is not None: |
---|
104 | n/a | write(prevline, lineEnd=stripped) |
---|
105 | n/a | |
---|
106 | n/a | def encodestring(s, quotetabs=False, header=False): |
---|
107 | n/a | if b2a_qp is not None: |
---|
108 | n/a | return b2a_qp(s, quotetabs=quotetabs, header=header) |
---|
109 | n/a | from io import BytesIO |
---|
110 | n/a | infp = BytesIO(s) |
---|
111 | n/a | outfp = BytesIO() |
---|
112 | n/a | encode(infp, outfp, quotetabs, header) |
---|
113 | n/a | return outfp.getvalue() |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | |
---|
117 | n/a | def decode(input, output, header=False): |
---|
118 | n/a | """Read 'input', apply quoted-printable decoding, and write to 'output'. |
---|
119 | n/a | 'input' and 'output' are binary file objects. |
---|
120 | n/a | If 'header' is true, decode underscore as space (per RFC 1522).""" |
---|
121 | n/a | |
---|
122 | n/a | if a2b_qp is not None: |
---|
123 | n/a | data = input.read() |
---|
124 | n/a | odata = a2b_qp(data, header=header) |
---|
125 | n/a | output.write(odata) |
---|
126 | n/a | return |
---|
127 | n/a | |
---|
128 | n/a | new = b'' |
---|
129 | n/a | while 1: |
---|
130 | n/a | line = input.readline() |
---|
131 | n/a | if not line: break |
---|
132 | n/a | i, n = 0, len(line) |
---|
133 | n/a | if n > 0 and line[n-1:n] == b'\n': |
---|
134 | n/a | partial = 0; n = n-1 |
---|
135 | n/a | # Strip trailing whitespace |
---|
136 | n/a | while n > 0 and line[n-1:n] in b" \t\r": |
---|
137 | n/a | n = n-1 |
---|
138 | n/a | else: |
---|
139 | n/a | partial = 1 |
---|
140 | n/a | while i < n: |
---|
141 | n/a | c = line[i:i+1] |
---|
142 | n/a | if c == b'_' and header: |
---|
143 | n/a | new = new + b' '; i = i+1 |
---|
144 | n/a | elif c != ESCAPE: |
---|
145 | n/a | new = new + c; i = i+1 |
---|
146 | n/a | elif i+1 == n and not partial: |
---|
147 | n/a | partial = 1; break |
---|
148 | n/a | elif i+1 < n and line[i+1:i+2] == ESCAPE: |
---|
149 | n/a | new = new + ESCAPE; i = i+2 |
---|
150 | n/a | elif i+2 < n and ishex(line[i+1:i+2]) and ishex(line[i+2:i+3]): |
---|
151 | n/a | new = new + bytes((unhex(line[i+1:i+3]),)); i = i+3 |
---|
152 | n/a | else: # Bad escape sequence -- leave it in |
---|
153 | n/a | new = new + c; i = i+1 |
---|
154 | n/a | if not partial: |
---|
155 | n/a | output.write(new + b'\n') |
---|
156 | n/a | new = b'' |
---|
157 | n/a | if new: |
---|
158 | n/a | output.write(new) |
---|
159 | n/a | |
---|
160 | n/a | def decodestring(s, header=False): |
---|
161 | n/a | if a2b_qp is not None: |
---|
162 | n/a | return a2b_qp(s, header=header) |
---|
163 | n/a | from io import BytesIO |
---|
164 | n/a | infp = BytesIO(s) |
---|
165 | n/a | outfp = BytesIO() |
---|
166 | n/a | decode(infp, outfp, header=header) |
---|
167 | n/a | return outfp.getvalue() |
---|
168 | n/a | |
---|
169 | n/a | |
---|
170 | n/a | |
---|
171 | n/a | # Other helper functions |
---|
172 | n/a | def ishex(c): |
---|
173 | n/a | """Return true if the byte ordinal 'c' is a hexadecimal digit in ASCII.""" |
---|
174 | n/a | assert isinstance(c, bytes) |
---|
175 | n/a | return b'0' <= c <= b'9' or b'a' <= c <= b'f' or b'A' <= c <= b'F' |
---|
176 | n/a | |
---|
177 | n/a | def unhex(s): |
---|
178 | n/a | """Get the integer value of a hexadecimal number.""" |
---|
179 | n/a | bits = 0 |
---|
180 | n/a | for c in s: |
---|
181 | n/a | c = bytes((c,)) |
---|
182 | n/a | if b'0' <= c <= b'9': |
---|
183 | n/a | i = ord('0') |
---|
184 | n/a | elif b'a' <= c <= b'f': |
---|
185 | n/a | i = ord('a')-10 |
---|
186 | n/a | elif b'A' <= c <= b'F': |
---|
187 | n/a | i = ord(b'A')-10 |
---|
188 | n/a | else: |
---|
189 | n/a | assert False, "non-hex digit "+repr(c) |
---|
190 | n/a | bits = bits*16 + (ord(c) - i) |
---|
191 | n/a | return bits |
---|
192 | n/a | |
---|
193 | n/a | |
---|
194 | n/a | |
---|
195 | n/a | def main(): |
---|
196 | n/a | import sys |
---|
197 | n/a | import getopt |
---|
198 | n/a | try: |
---|
199 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'td') |
---|
200 | n/a | except getopt.error as msg: |
---|
201 | n/a | sys.stdout = sys.stderr |
---|
202 | n/a | print(msg) |
---|
203 | n/a | print("usage: quopri [-t | -d] [file] ...") |
---|
204 | n/a | print("-t: quote tabs") |
---|
205 | n/a | print("-d: decode; default encode") |
---|
206 | n/a | sys.exit(2) |
---|
207 | n/a | deco = 0 |
---|
208 | n/a | tabs = 0 |
---|
209 | n/a | for o, a in opts: |
---|
210 | n/a | if o == '-t': tabs = 1 |
---|
211 | n/a | if o == '-d': deco = 1 |
---|
212 | n/a | if tabs and deco: |
---|
213 | n/a | sys.stdout = sys.stderr |
---|
214 | n/a | print("-t and -d are mutually exclusive") |
---|
215 | n/a | sys.exit(2) |
---|
216 | n/a | if not args: args = ['-'] |
---|
217 | n/a | sts = 0 |
---|
218 | n/a | for file in args: |
---|
219 | n/a | if file == '-': |
---|
220 | n/a | fp = sys.stdin.buffer |
---|
221 | n/a | else: |
---|
222 | n/a | try: |
---|
223 | n/a | fp = open(file, "rb") |
---|
224 | n/a | except OSError as msg: |
---|
225 | n/a | sys.stderr.write("%s: can't open (%s)\n" % (file, msg)) |
---|
226 | n/a | sts = 1 |
---|
227 | n/a | continue |
---|
228 | n/a | try: |
---|
229 | n/a | if deco: |
---|
230 | n/a | decode(fp, sys.stdout.buffer) |
---|
231 | n/a | else: |
---|
232 | n/a | encode(fp, sys.stdout.buffer, tabs) |
---|
233 | n/a | finally: |
---|
234 | n/a | if file != '-': |
---|
235 | n/a | fp.close() |
---|
236 | n/a | if sts: |
---|
237 | n/a | sys.exit(sts) |
---|
238 | n/a | |
---|
239 | n/a | |
---|
240 | n/a | |
---|
241 | n/a | if __name__ == '__main__': |
---|
242 | n/a | main() |
---|