| 1 |
from pypy.interpreter.gateway import unwrap_spec |
| 2 |
from rpython.rlib.rstring import StringBuilder |
| 3 |
from pypy.module.binascii.interp_binascii import AsciiBufferUnwrapper |
| 4 |
|
| 5 |
MAXLINESIZE = 76 |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
def hexval(c): |
| 10 |
if c <= '9': |
| 11 |
return ord(c) - ord('0') |
| 12 |
elif c <= 'F': |
| 13 |
return ord(c) - (ord('A') - 10) |
| 14 |
else: |
| 15 |
return ord(c) - (ord('a') - 10) |
| 16 |
hexval._always_inline_ = True |
| 17 |
|
| 18 |
@unwrap_spec(data=AsciiBufferUnwrapper, header=int) |
| 19 |
def a2b_qp(space, data, header=0): |
| 20 |
"Decode a string of qp-encoded data." |
| 21 |
|
| 22 |
|
| 23 |
odata = StringBuilder(len(data)) |
| 24 |
inp = 0 |
| 25 |
|
| 26 |
while inp < len(data): |
| 27 |
c = data[inp] |
| 28 |
inp += 1 |
| 29 |
if c == '=': |
| 30 |
if inp >= len(data): |
| 31 |
break |
| 32 |
|
| 33 |
c = data[inp] |
| 34 |
if c == '\n' or c == '\r': |
| 35 |
if c != '\n': |
| 36 |
while inp < len(data) and data[inp] != '\n': |
| 37 |
inp += 1 |
| 38 |
inp += 1 |
| 39 |
elif c == '=': |
| 40 |
|
| 41 |
odata.append('=') |
| 42 |
inp += 1 |
| 43 |
elif (inp + 1 < len(data) and |
| 44 |
('A' <= c <= 'F' or |
| 45 |
'a' <= c <= 'f' or |
| 46 |
'0' <= c <= '9') and |
| 47 |
('A' <= data[inp+1] <= 'F' or |
| 48 |
'a' <= data[inp+1] <= 'f' or |
| 49 |
'0' <= data[inp+1] <= '9')): |
| 50 |
|
| 51 |
ch = chr(hexval(c) << 4 | hexval(data[inp+1])) |
| 52 |
inp += 2 |
| 53 |
odata.append(ch) |
| 54 |
else: |
| 55 |
odata.append('=') |
| 56 |
else: |
| 57 |
if header and c == '_': |
| 58 |
c = ' ' |
| 59 |
odata.append(c) |
| 60 |
return space.newbytes(odata.build()) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
class StringBuilderWithOneCharCancellable(object): |
| 65 |
|
| 66 |
def __init__(self, crlf, initial): |
| 67 |
self.crlf = crlf |
| 68 |
self.builder = StringBuilder(initial) |
| 69 |
self.pending = -1 |
| 70 |
|
| 71 |
def _flush(self): |
| 72 |
if self.pending >= 0: |
| 73 |
self.builder.append(chr(self.pending)) |
| 74 |
self.pending = -1 |
| 75 |
_flush._always_inline_ = True |
| 76 |
|
| 77 |
def append(self, c): |
| 78 |
self._flush() |
| 79 |
self.pending = ord(c) |
| 80 |
|
| 81 |
def newline(self): |
| 82 |
self._flush() |
| 83 |
if self.crlf: self.builder.append('\r') |
| 84 |
self.pending = ord('\n') |
| 85 |
|
| 86 |
def to_hex(self, c): |
| 87 |
self._flush() |
| 88 |
uvalue = ord(c) |
| 89 |
self.builder.append("0123456789ABCDEF"[uvalue >> 4]) |
| 90 |
self.builder.append("0123456789ABCDEF"[uvalue & 0xf]) |
| 91 |
|
| 92 |
def build(self): |
| 93 |
self._flush() |
| 94 |
return self.builder.build() |
| 95 |
|
| 96 |
@unwrap_spec(data='bufferstr', quotetabs=int, istext=int, header=int) |
| 97 |
def b2a_qp(space, data, quotetabs=0, istext=1, header=0): |
| 98 |
"""Encode a string using quoted-printable encoding. |
| 99 |
|
| 100 |
On encoding, when istext is set, newlines are not encoded, and white |
| 101 |
space at end of lines is. When istext is not set, \\r and \\n (CR/LF) are |
| 102 |
both encoded. When quotetabs is set, space and tabs are encoded.""" |
| 103 |
|
| 104 |
|
| 105 |
lf = data.find('\n') |
| 106 |
crlf = lf > 0 and data[lf-1] == '\r' |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
odata = StringBuilderWithOneCharCancellable(crlf, len(data)) |
| 111 |
inp = 0 |
| 112 |
linelen = 0 |
| 113 |
|
| 114 |
while inp < len(data): |
| 115 |
c = data[inp] |
| 116 |
if (c > '~' or |
| 117 |
c == '=' or |
| 118 |
(header and c == '_') or |
| 119 |
(c == '.' and linelen == 0 and (inp + 1 == len(data) or |
| 120 |
data[inp+1] in '\n\r\x00')) or |
| 121 |
(not istext and (c == '\r' or c == '\n')) or |
| 122 |
((c == '\t' or c == ' ') and (inp + 1 == len(data))) or |
| 123 |
(c <= ' ' and c != '\r' and c != '\n' and |
| 124 |
(quotetabs or (c != '\t' and c != ' ')))): |
| 125 |
linelen += 3 |
| 126 |
if linelen >= MAXLINESIZE: |
| 127 |
odata.append('=') |
| 128 |
odata.newline() |
| 129 |
linelen = 3 |
| 130 |
odata.append('=') |
| 131 |
odata.to_hex(c) |
| 132 |
inp += 1 |
| 133 |
else: |
| 134 |
if (istext and |
| 135 |
(c == '\n' or (inp+1 < len(data) and c == '\r' and |
| 136 |
data[inp+1] == '\n'))): |
| 137 |
|
| 138 |
pendingnum = odata.pending |
| 139 |
if pendingnum == ord(' ') or pendingnum == ord('\t'): |
| 140 |
odata.pending = ord('=') |
| 141 |
odata.to_hex(chr(pendingnum)) |
| 142 |
|
| 143 |
linelen = 0 |
| 144 |
odata.newline() |
| 145 |
if c == '\r': |
| 146 |
inp += 2 |
| 147 |
else: |
| 148 |
inp += 1 |
| 149 |
else: |
| 150 |
if (inp + 1 < len(data) and |
| 151 |
data[inp+1] != '\n' and |
| 152 |
(linelen + 1) >= MAXLINESIZE): |
| 153 |
odata.append('=') |
| 154 |
odata.newline() |
| 155 |
linelen = 0 |
| 156 |
|
| 157 |
linelen += 1 |
| 158 |
if header and c == ' ': |
| 159 |
c = '_' |
| 160 |
odata.append(c) |
| 161 |
inp += 1 |
| 162 |
|
| 163 |
return space.newbytes(odata.build()) |