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