1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Pyrex implementation for bencode coder/decoder"""
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)
51
cdef extern from "python-compat.h":
52
int snprintf(char* buffer, size_t nsize, char* fmt, ...)
57
cdef extern from "_bencode_pyx.h":
58
void D_UPDATE_TAIL(Decoder, int n)
59
void E_UPDATE_TAIL(Encoder, int n)
61
# To maintain compatibility with older versions of pyrex, we have to use the
62
# relative import here, rather than 'bzrlib._static_tuple_c'
63
from _static_tuple_c cimport StaticTuple, StaticTuple_CheckExact, \
66
import_static_tuple_c()
72
cdef readonly char *tail
73
cdef readonly int size
74
cdef readonly int _yield_tuples
77
def __init__(self, s, yield_tuples=0):
78
"""Initialize decoder engine.
79
@param s: Python string.
81
if not PyString_CheckExact(s):
82
raise TypeError("String required")
85
self.tail = PyString_AS_STRING(s)
86
self.size = PyString_GET_SIZE(s)
87
self._yield_tuples = int(yield_tuples)
90
result = self._decode_object()
92
raise ValueError('junk in stream')
95
def decode_object(self):
96
return self._decode_object()
98
cdef object _decode_object(self):
102
raise ValueError('stream underflow')
104
if Py_EnterRecursiveCall("_decode_object"):
105
raise RuntimeError("too deeply nested")
108
if c'0' <= ch <= c'9':
109
return self._decode_string()
111
D_UPDATE_TAIL(self, 1)
112
return self._decode_list()
114
D_UPDATE_TAIL(self, 1)
115
return self._decode_int()
117
D_UPDATE_TAIL(self, 1)
118
return self._decode_dict()
120
raise ValueError('unknown object type identifier %r' % ch)
122
Py_LeaveRecursiveCall()
124
cdef int _read_digits(self, char stop_char) except -1:
127
while ((self.tail[i] >= c'0' and self.tail[i] <= c'9') or
128
self.tail[i] == c'-') and i < self.size:
131
if self.tail[i] != stop_char:
132
raise ValueError("Stop character %c not found: %c" %
133
(stop_char, self.tail[i]))
134
if (self.tail[0] == c'0' or
135
(self.tail[0] == c'-' and self.tail[1] == c'0')):
139
raise ValueError # leading zeroes are not allowed
142
cdef object _decode_int(self):
144
i = self._read_digits(c'e')
147
ret = PyInt_FromString(self.tail, NULL, 10)
150
D_UPDATE_TAIL(self, i+1)
153
cdef object _decode_string(self):
156
# strtol allows leading whitespace, negatives, and leading zeros
157
# however, all callers have already checked that '0' <= tail[0] <= '9'
158
# or they wouldn't have called _decode_string
159
# strtol will stop at trailing whitespace, etc
160
n = strtol(self.tail, &next_tail, 10)
161
if next_tail == NULL or next_tail[0] != c':':
162
raise ValueError('string len not terminated by ":"')
163
# strtol allows leading zeros, so validate that we don't have that
164
if (self.tail[0] == c'0'
165
and (n != 0 or (next_tail - self.tail != 1))):
166
raise ValueError('leading zeros are not allowed')
167
D_UPDATE_TAIL(self, next_tail - self.tail + 1)
171
raise ValueError('stream underflow')
173
raise ValueError('string size below zero: %d' % n)
175
result = PyString_FromStringAndSize(self.tail, n)
176
D_UPDATE_TAIL(self, n)
179
cdef object _decode_list(self):
183
if self.tail[0] == c'e':
184
D_UPDATE_TAIL(self, 1)
185
if self._yield_tuples:
190
# As a quick shortcut, check to see if the next object is a
191
# string, since we know that won't be creating recursion
192
# if self.tail[0] >= c'0' and self.tail[0] <= c'9':
193
PyList_Append(result, self._decode_object())
195
raise ValueError('malformed list')
197
cdef object _decode_dict(self):
206
D_UPDATE_TAIL(self, 1)
209
# keys should be strings only
210
if self.tail[0] < c'0' or self.tail[0] > c'9':
211
raise ValueError('key was not a simple string.')
212
key = self._decode_string()
214
raise ValueError('dict keys disordered')
217
value = self._decode_object()
220
raise ValueError('malformed dict')
223
def bdecode(object s):
224
"""Decode string x to Python object"""
225
return Decoder(s).decode()
228
def bdecode_as_tuple(object s):
229
"""Decode string x to Python object, using tuples rather than lists."""
230
return Decoder(s, True).decode()
233
class Bencached(object):
234
__slots__ = ['bencoded']
236
def __init__(self, s):
241
INITSIZE = 1024 # initial size for encoder buffer
246
"""Bencode encoder"""
248
cdef readonly char *tail
249
cdef readonly int size
250
cdef readonly char *buffer
251
cdef readonly int maxsize
253
def __init__(self, int maxsize=INITSIZE):
254
"""Initialize encoder engine
255
@param maxsize: initial size of internal char buffer
263
p = <char*>malloc(maxsize)
265
raise MemoryError('Not enough memory to allocate buffer '
268
self.maxsize = maxsize
271
def __dealloc__(self):
277
if self.buffer != NULL and self.size != 0:
278
return PyString_FromStringAndSize(self.buffer, self.size)
282
cdef int _ensure_buffer(self, int required) except 0:
283
"""Ensure that tail of CharTail buffer has enough size.
284
If buffer is not big enough then function try to
287
cdef char *new_buffer
290
if self.size + required < self.maxsize:
293
new_size = self.maxsize
294
while new_size < self.size + required:
295
new_size = new_size * 2
296
new_buffer = <char*>realloc(self.buffer, <size_t>new_size)
297
if new_buffer == NULL:
298
raise MemoryError('Cannot realloc buffer for encoder')
300
self.buffer = new_buffer
301
self.maxsize = new_size
302
self.tail = &new_buffer[self.size]
305
cdef int _encode_int(self, int x) except 0:
306
"""Encode int to bencode string iNNNe
307
@param x: value to encode
310
self._ensure_buffer(INT_BUF_SIZE)
311
n = snprintf(self.tail, INT_BUF_SIZE, "i%de", x)
313
raise MemoryError('int %d too big to encode' % x)
314
E_UPDATE_TAIL(self, n)
317
cdef int _encode_long(self, x) except 0:
318
return self._append_string(''.join(('i', str(x), 'e')))
320
cdef int _append_string(self, s) except 0:
322
n = PyString_GET_SIZE(s)
323
self._ensure_buffer(n)
324
memcpy(self.tail, PyString_AS_STRING(s), n)
325
E_UPDATE_TAIL(self, n)
328
cdef int _encode_string(self, x) except 0:
330
cdef Py_ssize_t x_len
331
x_len = PyString_GET_SIZE(x)
332
self._ensure_buffer(x_len + INT_BUF_SIZE)
333
n = snprintf(self.tail, INT_BUF_SIZE, '%d:', x_len)
335
raise MemoryError('string %s too big to encode' % x)
336
memcpy(<void *>(self.tail+n), PyString_AS_STRING(x), x_len)
337
E_UPDATE_TAIL(self, n + x_len)
340
cdef int _encode_list(self, x) except 0:
341
self._ensure_buffer(1)
343
E_UPDATE_TAIL(self, 1)
348
self._ensure_buffer(1)
350
E_UPDATE_TAIL(self, 1)
353
cdef int _encode_dict(self, x) except 0:
354
self._ensure_buffer(1)
356
E_UPDATE_TAIL(self, 1)
361
if not PyString_CheckExact(k):
362
raise TypeError('key in dict should be string')
363
self._encode_string(k)
366
self._ensure_buffer(1)
368
E_UPDATE_TAIL(self, 1)
371
def process(self, object x):
372
if Py_EnterRecursiveCall("encode"):
373
raise RuntimeError("too deeply nested")
375
if PyString_CheckExact(x):
376
self._encode_string(x)
377
elif PyInt_CheckExact(x):
379
elif PyLong_CheckExact(x):
381
elif (PyList_CheckExact(x) or PyTuple_CheckExact(x)
382
or StaticTuple_CheckExact(x)):
384
elif PyDict_CheckExact(x):
386
elif PyBool_Check(x):
387
self._encode_int(int(x))
388
elif isinstance(x, Bencached):
389
self._append_string(x.bencoded)
391
raise TypeError('unsupported type %r' % x)
393
Py_LeaveRecursiveCall()
397
"""Encode Python object x to string"""