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