Read 'input', apply quoted-printable decoding, and write to 'output'. 'input' and 'output' are binary file objects. If 'header' is true, decode underscore as space (per RFC 1522).
(input, output, header=False)
| 115 | |
| 116 | |
| 117 | def decode(input, output, header=False): |
| 118 | """Read 'input', apply quoted-printable decoding, and write to 'output'. |
| 119 | 'input' and 'output' are binary file objects. |
| 120 | If 'header' is true, decode underscore as space (per RFC 1522).""" |
| 121 | |
| 122 | if a2b_qp is not None: |
| 123 | data = input.read() |
| 124 | odata = a2b_qp(data, header=header) |
| 125 | output.write(odata) |
| 126 | return |
| 127 | |
| 128 | new = b'' |
| 129 | while 1: |
| 130 | line = input.readline() |
| 131 | if not line: break |
| 132 | i, n = 0, len(line) |
| 133 | if n > 0 and line[n-1:n] == b'\n': |
| 134 | partial = 0; n = n-1 |
| 135 | # Strip trailing whitespace |
| 136 | while n > 0 and line[n-1:n] in b" \t\r": |
| 137 | n = n-1 |
| 138 | else: |
| 139 | partial = 1 |
| 140 | while i < n: |
| 141 | c = line[i:i+1] |
| 142 | if c == b'_' and header: |
| 143 | new = new + b' '; i = i+1 |
| 144 | elif c != ESCAPE: |
| 145 | new = new + c; i = i+1 |
| 146 | elif i+1 == n and not partial: |
| 147 | partial = 1; break |
| 148 | elif i+1 < n and line[i+1:i+2] == ESCAPE: |
| 149 | new = new + ESCAPE; i = i+2 |
| 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 |
| 152 | else: # Bad escape sequence -- leave it in |
| 153 | new = new + c; i = i+1 |
| 154 | if not partial: |
| 155 | output.write(new + b'\n') |
| 156 | new = b'' |
| 157 | if new: |
| 158 | output.write(new) |
| 159 | |
| 160 | def decodestring(s, header=False): |
| 161 | if a2b_qp is not None: |