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