17
17
"""Pyrex implementation for bencode coder/decoder"""
19
from __future__ import absolute_import
21
from cpython.bool cimport (
24
from cpython.bytes cimport (
26
PyBytes_FromStringAndSize,
30
from cpython.dict cimport (
33
from cpython.int cimport (
37
from cpython.list cimport (
41
from cpython.long cimport (
44
from cpython.mem cimport (
49
from cpython.tuple cimport (
53
from libc.stdlib cimport (
56
from libc.string cimport (
20
cdef extern from "stddef.h":
21
ctypedef unsigned int size_t
23
cdef extern from "Python.h":
24
ctypedef int Py_ssize_t
25
int PyInt_CheckExact(object o)
26
int PyLong_CheckExact(object o)
27
int PyString_CheckExact(object o)
28
int PyTuple_CheckExact(object o)
29
int PyList_CheckExact(object o)
30
int PyDict_CheckExact(object o)
31
int PyBool_Check(object o)
32
object PyString_FromStringAndSize(char *v, Py_ssize_t len)
33
char *PyString_AS_STRING(object o) except NULL
34
Py_ssize_t PyString_GET_SIZE(object o) except -1
35
object PyInt_FromString(char *str, char **pend, int base)
36
int Py_GetRecursionLimit()
37
int Py_EnterRecursiveCall(char *)
38
void Py_LeaveRecursiveCall()
40
int PyList_Append(object, object) except -1
42
cdef extern from "stdlib.h":
43
void free(void *memblock)
44
void *malloc(size_t size)
45
void *realloc(void *memblock, size_t size)
46
long strtol(char *, char **, int)
48
cdef extern from "string.h":
49
void *memcpy(void *dest, void *src, size_t count)
60
51
cdef extern from "python-compat.h":
61
52
int snprintf(char* buffer, size_t nsize, char* fmt, ...)
62
# Use wrapper with inverted error return so Cython can propogate
63
int BrzPy_EnterRecursiveCall(char *) except 0
65
cdef extern from "Python.h":
66
void Py_LeaveRecursiveCall()
90
71
"""Initialize decoder engine.
91
72
@param s: Python string.
93
if not PyBytes_CheckExact(s):
94
raise TypeError("bytes required")
74
if not PyString_CheckExact(s):
75
raise TypeError("String required")
97
self.tail = PyBytes_AS_STRING(s)
98
self.size = PyBytes_GET_SIZE(s)
78
self.tail = PyString_AS_STRING(s)
79
self.size = PyString_GET_SIZE(s)
99
80
self._yield_tuples = int(yield_tuples)
176
159
raise ValueError('leading zeros are not allowed')
177
160
D_UPDATE_TAIL(self, next_tail - self.tail + 1)
180
163
if n > self.size:
181
164
raise ValueError('stream underflow')
183
166
raise ValueError('string size below zero: %d' % n)
185
result = PyBytes_FromStringAndSize(self.tail, n)
168
result = PyString_FromStringAndSize(self.tail, n)
186
169
D_UPDATE_TAIL(self, n)
278
261
self.maxsize = maxsize
281
def __dealloc__(self):
282
PyMem_Free(self.buffer)
283
266
self.buffer = NULL
287
270
if self.buffer != NULL and self.size != 0:
288
return PyBytes_FromStringAndSize(self.buffer, self.size)
271
return PyString_FromStringAndSize(self.buffer, self.size)
291
275
cdef int _ensure_buffer(self, int required) except 0:
292
276
"""Ensure that tail of CharTail buffer has enough size.
319
303
self._ensure_buffer(INT_BUF_SIZE)
320
n = snprintf(self.tail, INT_BUF_SIZE, b"i%de", x)
304
n = snprintf(self.tail, INT_BUF_SIZE, "i%de", x)
322
306
raise MemoryError('int %d too big to encode' % x)
323
307
E_UPDATE_TAIL(self, n)
326
310
cdef int _encode_long(self, x) except 0:
327
return self._append_string(b'i%de' % x)
311
return self._append_string(''.join(('i', str(x), 'e')))
329
313
cdef int _append_string(self, s) except 0:
330
314
cdef Py_ssize_t n
331
n = PyBytes_GET_SIZE(s)
315
n = PyString_GET_SIZE(s)
332
316
self._ensure_buffer(n)
333
memcpy(self.tail, PyBytes_AS_STRING(s), n)
317
memcpy(self.tail, PyString_AS_STRING(s), n)
334
318
E_UPDATE_TAIL(self, n)
337
321
cdef int _encode_string(self, x) except 0:
339
323
cdef Py_ssize_t x_len
340
x_len = PyBytes_GET_SIZE(x)
324
x_len = PyString_GET_SIZE(x)
341
325
self._ensure_buffer(x_len + INT_BUF_SIZE)
342
n = snprintf(self.tail, INT_BUF_SIZE, b'%ld:', x_len)
326
n = snprintf(self.tail, INT_BUF_SIZE, '%d:', x_len)
344
328
raise MemoryError('string %s too big to encode' % x)
345
memcpy(<void *>(self.tail+n), PyBytes_AS_STRING(x), x_len)
329
memcpy(<void *>(self.tail+n), PyString_AS_STRING(x), x_len)
346
330
E_UPDATE_TAIL(self, n + x_len)
375
361
E_UPDATE_TAIL(self, 1)
378
cpdef object process(self, object x):
379
BrzPy_EnterRecursiveCall(" while bencode encoding")
364
def process(self, object x):
365
if Py_EnterRecursiveCall("encode"):
366
raise RuntimeError("too deeply nested")
381
if PyBytes_CheckExact(x):
368
if PyString_CheckExact(x):
382
369
self._encode_string(x)
383
elif PyInt_CheckExact(x) and x.bit_length() < 32:
370
elif PyInt_CheckExact(x):
384
371
self._encode_int(x)
385
372
elif PyLong_CheckExact(x):
386
373
self._encode_long(x)
387
elif (PyList_CheckExact(x) or PyTuple_CheckExact(x)
388
or isinstance(x, StaticTuple)):
374
elif PyList_CheckExact(x) or PyTuple_CheckExact(x):
389
375
self._encode_list(x)
390
376
elif PyDict_CheckExact(x):
391
377
self._encode_dict(x)