| 1 |
import py |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 4 |
from rpython.translator import cdir |
| 5 |
from rpython.rlib import rutf8 |
| 6 |
|
| 7 |
UNICODE_REPLACEMENT_CHARACTER = u'\uFFFD'.encode("utf8") |
| 8 |
|
| 9 |
|
| 10 |
class EncodeDecodeError(Exception): |
| 11 |
def __init__(self, start, end, reason): |
| 12 |
self.start = start |
| 13 |
self.end = end |
| 14 |
self.reason = reason |
| 15 |
def __repr__(self): |
| 16 |
return 'EncodeDecodeError(%r, %r, %r)' % (self.start, self.end, |
| 17 |
self.reason) |
| 18 |
|
| 19 |
srcdir = py.path.local(__file__).dirpath() |
| 20 |
|
| 21 |
codecs = [ |
| 22 |
|
| 23 |
'gb2312', 'gbk', 'gb18030', 'hz', |
| 24 |
|
| 25 |
|
| 26 |
'big5hkscs', |
| 27 |
|
| 28 |
|
| 29 |
'iso2022_kr', 'iso2022_jp', 'iso2022_jp_1', 'iso2022_jp_2', |
| 30 |
'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext', |
| 31 |
|
| 32 |
|
| 33 |
'shift_jis', 'cp932', 'euc_jp', 'shift_jis_2004', |
| 34 |
'euc_jis_2004', 'euc_jisx0213', 'shift_jisx0213', |
| 35 |
|
| 36 |
|
| 37 |
'euc_kr', 'cp949', 'johab', |
| 38 |
|
| 39 |
|
| 40 |
'big5', 'cp950', |
| 41 |
] |
| 42 |
|
| 43 |
eci = ExternalCompilationInfo( |
| 44 |
separate_module_files = [ |
| 45 |
srcdir.join('src', 'cjkcodecs', '_codecs_cn.c'), |
| 46 |
srcdir.join('src', 'cjkcodecs', '_codecs_hk.c'), |
| 47 |
srcdir.join('src', 'cjkcodecs', '_codecs_iso2022.c'), |
| 48 |
srcdir.join('src', 'cjkcodecs', '_codecs_jp.c'), |
| 49 |
srcdir.join('src', 'cjkcodecs', '_codecs_kr.c'), |
| 50 |
srcdir.join('src', 'cjkcodecs', '_codecs_tw.c'), |
| 51 |
srcdir.join('src', 'cjkcodecs', 'multibytecodec.c'), |
| 52 |
], |
| 53 |
includes = ['src/cjkcodecs/multibytecodec.h'], |
| 54 |
include_dirs = [str(srcdir), cdir], |
| 55 |
) |
| 56 |
|
| 57 |
MBERR_TOOSMALL = -1 |
| 58 |
MBERR_TOOFEW = -2 |
| 59 |
MBERR_INTERNAL = -3 |
| 60 |
MBERR_NOMEMORY = -4 |
| 61 |
|
| 62 |
MULTIBYTECODEC_P = rffi.COpaquePtr('struct MultibyteCodec_s', |
| 63 |
compilation_info=eci) |
| 64 |
|
| 65 |
def llexternal(*args, **kwds): |
| 66 |
kwds.setdefault('compilation_info', eci) |
| 67 |
kwds.setdefault('sandboxsafe', True) |
| 68 |
kwds.setdefault('_nowrapper', True) |
| 69 |
return rffi.llexternal(*args, **kwds) |
| 70 |
|
| 71 |
def getter_for(name): |
| 72 |
return llexternal('pypy_cjkcodec_%s' % name, [], MULTIBYTECODEC_P) |
| 73 |
|
| 74 |
_codecs_getters = dict([(name, getter_for(name)) for name in codecs]) |
| 75 |
assert len(_codecs_getters) == len(codecs) |
| 76 |
|
| 77 |
def getcodec(name): |
| 78 |
getter = _codecs_getters[name] |
| 79 |
return getter() |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
DECODEBUF_P = rffi.COpaquePtr('struct pypy_cjk_dec_s', compilation_info=eci) |
| 85 |
pypy_cjk_dec_new = llexternal('pypy_cjk_dec_new', |
| 86 |
[MULTIBYTECODEC_P], DECODEBUF_P) |
| 87 |
pypy_cjk_dec_init = llexternal('pypy_cjk_dec_init', |
| 88 |
[DECODEBUF_P, rffi.CCHARP, rffi.SSIZE_T], |
| 89 |
rffi.SSIZE_T) |
| 90 |
pypy_cjk_dec_free = llexternal('pypy_cjk_dec_free', [DECODEBUF_P], |
| 91 |
lltype.Void) |
| 92 |
pypy_cjk_dec_chunk = llexternal('pypy_cjk_dec_chunk', [DECODEBUF_P], |
| 93 |
rffi.SSIZE_T) |
| 94 |
pypy_cjk_dec_outbuf = llexternal('pypy_cjk_dec_outbuf', [DECODEBUF_P], |
| 95 |
rffi.CWCHARP) |
| 96 |
pypy_cjk_dec_outlen = llexternal('pypy_cjk_dec_outlen', [DECODEBUF_P], |
| 97 |
rffi.SSIZE_T) |
| 98 |
pypy_cjk_dec_inbuf_remaining = llexternal('pypy_cjk_dec_inbuf_remaining', |
| 99 |
[DECODEBUF_P], rffi.SSIZE_T) |
| 100 |
pypy_cjk_dec_inbuf_consumed = llexternal('pypy_cjk_dec_inbuf_consumed', |
| 101 |
[DECODEBUF_P], rffi.SSIZE_T) |
| 102 |
pypy_cjk_dec_replace_on_error = llexternal('pypy_cjk_dec_replace_on_error', |
| 103 |
[DECODEBUF_P, rffi.CWCHARP, |
| 104 |
rffi.SSIZE_T, rffi.SSIZE_T], |
| 105 |
rffi.SSIZE_T) |
| 106 |
|
| 107 |
def decode(space, codec, stringdata, errors="strict", errorcb=None, namecb=None): |
| 108 |
decodebuf = pypy_cjk_dec_new(codec) |
| 109 |
if not decodebuf: |
| 110 |
raise MemoryError |
| 111 |
try: |
| 112 |
return decodeex(space, decodebuf, stringdata, errors, errorcb, namecb) |
| 113 |
finally: |
| 114 |
pypy_cjk_dec_free(decodebuf) |
| 115 |
|
| 116 |
def decodeex(space, decodebuf, stringdata, errors="strict", errorcb=None, namecb=None, |
| 117 |
ignore_error=0): |
| 118 |
inleft = len(stringdata) |
| 119 |
with rffi.scoped_nonmovingbuffer(stringdata) as inbuf: |
| 120 |
if pypy_cjk_dec_init(decodebuf, inbuf, inleft) < 0: |
| 121 |
raise MemoryError |
| 122 |
while True: |
| 123 |
r = pypy_cjk_dec_chunk(decodebuf) |
| 124 |
if r == 0 or r == ignore_error: |
| 125 |
break |
| 126 |
multibytecodec_decerror(space, decodebuf, r, errors, |
| 127 |
errorcb, namecb, stringdata) |
| 128 |
src = pypy_cjk_dec_outbuf(decodebuf) |
| 129 |
length = pypy_cjk_dec_outlen(decodebuf) |
| 130 |
return rffi.wcharpsize2utf8(src, length) |
| 131 |
|
| 132 |
def multibytecodec_decerror(space, decodebuf, e, errors, |
| 133 |
errorcb, namecb, stringdata): |
| 134 |
if e > 0: |
| 135 |
reason = "illegal multibyte sequence" |
| 136 |
esize = e |
| 137 |
elif e == MBERR_TOOFEW: |
| 138 |
reason = "incomplete multibyte sequence" |
| 139 |
esize = pypy_cjk_dec_inbuf_remaining(decodebuf) |
| 140 |
elif e == MBERR_NOMEMORY: |
| 141 |
raise MemoryError |
| 142 |
else: |
| 143 |
raise RuntimeError |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
start = pypy_cjk_dec_inbuf_consumed(decodebuf) |
| 148 |
end = start + esize |
| 149 |
if errors == "strict": |
| 150 |
raise EncodeDecodeError(start, end, reason) |
| 151 |
elif errors == "ignore": |
| 152 |
replace = "" |
| 153 |
elif errors == "replace": |
| 154 |
replace = UNICODE_REPLACEMENT_CHARACTER |
| 155 |
else: |
| 156 |
assert errorcb |
| 157 |
w_s = space.newbytes(stringdata) |
| 158 |
replace, end, rettype, _, _ = errorcb(errors, namecb, reason, |
| 159 |
w_s, start, end) |
| 160 |
|
| 161 |
lgt = rutf8.codepoints_in_utf8(replace) |
| 162 |
inbuf = rffi.utf82wcharp(replace, lgt) |
| 163 |
try: |
| 164 |
r = pypy_cjk_dec_replace_on_error(decodebuf, inbuf, lgt, end) |
| 165 |
finally: |
| 166 |
lltype.free(inbuf, flavor='raw') |
| 167 |
if r == MBERR_NOMEMORY: |
| 168 |
raise MemoryError |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
ENCODEBUF_P = rffi.COpaquePtr('struct pypy_cjk_enc_s', compilation_info=eci) |
| 173 |
pypy_cjk_enc_new = llexternal('pypy_cjk_enc_new', |
| 174 |
[MULTIBYTECODEC_P], ENCODEBUF_P) |
| 175 |
pypy_cjk_enc_init = llexternal('pypy_cjk_enc_init', |
| 176 |
[ENCODEBUF_P, rffi.CWCHARP, rffi.SSIZE_T], |
| 177 |
rffi.SSIZE_T) |
| 178 |
pypy_cjk_enc_free = llexternal('pypy_cjk_enc_free', [ENCODEBUF_P], |
| 179 |
lltype.Void) |
| 180 |
pypy_cjk_enc_chunk = llexternal('pypy_cjk_enc_chunk', |
| 181 |
[ENCODEBUF_P, rffi.SSIZE_T], rffi.SSIZE_T) |
| 182 |
pypy_cjk_enc_reset = llexternal('pypy_cjk_enc_reset', [ENCODEBUF_P], |
| 183 |
rffi.SSIZE_T) |
| 184 |
pypy_cjk_enc_outbuf = llexternal('pypy_cjk_enc_outbuf', [ENCODEBUF_P], |
| 185 |
rffi.CCHARP) |
| 186 |
pypy_cjk_enc_outlen = llexternal('pypy_cjk_enc_outlen', [ENCODEBUF_P], |
| 187 |
rffi.SSIZE_T) |
| 188 |
pypy_cjk_enc_inbuf_remaining = llexternal('pypy_cjk_enc_inbuf_remaining', |
| 189 |
[ENCODEBUF_P], rffi.SSIZE_T) |
| 190 |
pypy_cjk_enc_inbuf_consumed = llexternal('pypy_cjk_enc_inbuf_consumed', |
| 191 |
[ENCODEBUF_P], rffi.SSIZE_T) |
| 192 |
pypy_cjk_enc_replace_on_error = llexternal('pypy_cjk_enc_replace_on_error', |
| 193 |
[ENCODEBUF_P, rffi.CCHARP, |
| 194 |
rffi.SSIZE_T, rffi.SSIZE_T], |
| 195 |
rffi.SSIZE_T) |
| 196 |
pypy_cjk_enc_getcodec = llexternal('pypy_cjk_enc_getcodec', |
| 197 |
[ENCODEBUF_P], MULTIBYTECODEC_P) |
| 198 |
pypy_cjk_enc_copystate = llexternal('pypy_cjk_enc_copystate', |
| 199 |
[ENCODEBUF_P, ENCODEBUF_P], lltype.Void) |
| 200 |
_pypy_cjk_enc_getstate = llexternal('pypy_cjk_enc_getstate', |
| 201 |
[ENCODEBUF_P, rffi.CCHARP], lltype.Void) |
| 202 |
_pypy_cjk_enc_setstate = llexternal('pypy_cjk_enc_setstate', |
| 203 |
[ENCODEBUF_P, rffi.CCHARP], lltype.Void) |
| 204 |
|
| 205 |
def enc_getstate(encodebuf): |
| 206 |
"""Return the 8-byte codec state as a byte string.""" |
| 207 |
with rffi.scoped_alloc_buffer(8) as buf: |
| 208 |
_pypy_cjk_enc_getstate(encodebuf, rffi.cast(rffi.CCHARP, buf.raw)) |
| 209 |
return buf.str(8) |
| 210 |
|
| 211 |
def enc_setstate(encodebuf, state8): |
| 212 |
"""Restore the 8-byte codec state from a byte string.""" |
| 213 |
with rffi.scoped_str2charp(state8) as buf: |
| 214 |
_pypy_cjk_enc_setstate(encodebuf, buf) |
| 215 |
|
| 216 |
MBENC_FLUSH = 1 |
| 217 |
MBENC_RESET = 2 |
| 218 |
|
| 219 |
def encode(space, codec, unicodedata, length, errors="strict", errorcb=None, |
| 220 |
namecb=None, copystate=lltype.nullptr(ENCODEBUF_P.TO)): |
| 221 |
encodebuf = pypy_cjk_enc_new(codec) |
| 222 |
if not encodebuf: |
| 223 |
raise MemoryError |
| 224 |
if copystate: |
| 225 |
pypy_cjk_enc_copystate(encodebuf, copystate) |
| 226 |
try: |
| 227 |
return encodeex(space, encodebuf, unicodedata, length, errors, errorcb, namecb) |
| 228 |
finally: |
| 229 |
if copystate: |
| 230 |
pypy_cjk_enc_copystate(copystate, encodebuf) |
| 231 |
pypy_cjk_enc_free(encodebuf) |
| 232 |
|
| 233 |
def encodeex(space, encodebuf, utf8data, length, errors="strict", errorcb=None, |
| 234 |
namecb=None, ignore_error=0): |
| 235 |
inleft = length |
| 236 |
inbuf = rffi.utf82wcharp(utf8data, length) |
| 237 |
w_s = space.newtext(utf8data) |
| 238 |
try: |
| 239 |
if pypy_cjk_enc_init(encodebuf, inbuf, inleft) < 0: |
| 240 |
raise MemoryError |
| 241 |
if ignore_error == 0: |
| 242 |
flags = MBENC_FLUSH | MBENC_RESET |
| 243 |
else: |
| 244 |
flags = 0 |
| 245 |
while True: |
| 246 |
r = pypy_cjk_enc_chunk(encodebuf, flags) |
| 247 |
if r == 0 or r == ignore_error: |
| 248 |
break |
| 249 |
multibytecodec_encerror(space, encodebuf, r, errors, |
| 250 |
errorcb, namecb, w_s) |
| 251 |
while flags & MBENC_RESET: |
| 252 |
r = pypy_cjk_enc_reset(encodebuf) |
| 253 |
if r == 0: |
| 254 |
break |
| 255 |
multibytecodec_encerror(space, encodebuf, r, errors, |
| 256 |
errorcb, namecb, w_s) |
| 257 |
src = pypy_cjk_enc_outbuf(encodebuf) |
| 258 |
length = pypy_cjk_enc_outlen(encodebuf) |
| 259 |
return rffi.charpsize2str(src, length) |
| 260 |
finally: |
| 261 |
lltype.free(inbuf, flavor='raw') |
| 262 |
|
| 263 |
def multibytecodec_encerror(space, encodebuf, e, errors, |
| 264 |
errorcb, namecb, w_s): |
| 265 |
if e > 0: |
| 266 |
reason = "illegal multibyte sequence" |
| 267 |
esize = e |
| 268 |
elif e == MBERR_TOOFEW: |
| 269 |
reason = "incomplete multibyte sequence" |
| 270 |
esize = pypy_cjk_enc_inbuf_remaining(encodebuf) |
| 271 |
elif e == MBERR_NOMEMORY: |
| 272 |
raise MemoryError |
| 273 |
else: |
| 274 |
raise RuntimeError |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
start = pypy_cjk_enc_inbuf_consumed(encodebuf) |
| 279 |
end = start + esize |
| 280 |
if errors == "strict": |
| 281 |
raise EncodeDecodeError(start, end, reason) |
| 282 |
elif errors == "ignore": |
| 283 |
replace = "" |
| 284 |
rettype = 'b' |
| 285 |
elif errors == "replace": |
| 286 |
replace = "?" |
| 287 |
rettype = 'u' |
| 288 |
else: |
| 289 |
assert errorcb |
| 290 |
replace, end, rettype, _, _ = errorcb(errors, namecb, reason, |
| 291 |
w_s, start, end) |
| 292 |
if rettype == 'u': |
| 293 |
codec = pypy_cjk_enc_getcodec(encodebuf) |
| 294 |
lgt = rutf8.check_utf8(replace, False) |
| 295 |
replace = encode(space, codec, replace, lgt, copystate=encodebuf) |
| 296 |
|
| 297 |
|
| 298 |
with rffi.scoped_nonmovingbuffer(replace) as inbuf: |
| 299 |
r = pypy_cjk_enc_replace_on_error(encodebuf, inbuf, len(replace), end) |
| 300 |
if r == MBERR_NOMEMORY: |
| 301 |
raise MemoryError |