32
32
cdef extern from *:
33
33
ctypedef unsigned long size_t
34
void * malloc(size_t) nogil
35
void * realloc(void *, size_t) nogil
36
void free(void *) nogil
37
void memcpy(void *, void *, size_t) nogil
35
void * realloc(void *, size_t)
37
void memcpy(void *, void *, size_t)
40
40
cdef extern from "delta.h":
44
44
unsigned long agg_offset
45
45
struct delta_index:
47
delta_index * create_delta_index(source_info *src, delta_index *old) nogil
47
delta_index * create_delta_index(source_info *src, delta_index *old)
48
48
delta_index * create_delta_index_from_delta(source_info *delta,
49
delta_index *old) nogil
50
void free_delta_index(delta_index *index) nogil
50
void free_delta_index(delta_index *index)
51
51
void *create_delta(delta_index *indexes,
52
52
void *buf, unsigned long bufsize,
53
unsigned long *delta_size, unsigned long max_delta_size) nogil
53
unsigned long *delta_size, unsigned long max_delta_size)
54
54
unsigned long get_delta_hdr_size(unsigned char **datap,
55
unsigned char *top) nogil
56
56
Py_ssize_t DELTA_SIZE_MIN
57
void *patch_delta(void *src_buf, unsigned long src_size,
58
void *delta_buf, unsigned long delta_size,
59
unsigned long *dst_size)
59
62
cdef void *safe_malloc(size_t count) except NULL:
145
148
src.buf = c_delta
146
149
src.size = c_delta_size
147
150
src.agg_offset = self._source_offset + unadded_bytes
149
index = create_delta_index_from_delta(src, self._index)
151
index = create_delta_index_from_delta(src, self._index)
150
152
self._source_offset = src.agg_offset + src.size
151
153
if index != NULL:
152
154
free_delta_index(self._index)
186
188
self._source_offset = src.agg_offset + src.size
187
189
# We delay creating the index on the first insert
188
190
if source_location != 0:
190
index = create_delta_index(src, self._index)
191
index = create_delta_index(src, self._index)
191
192
if index != NULL:
192
193
free_delta_index(self._index)
193
194
self._index = index
201
202
# We know that self._index is already NULL, so whatever
202
203
# create_delta_index returns is fine
204
self._index = create_delta_index(&self._source_infos[0], NULL)
204
self._index = create_delta_index(&self._source_infos[0], NULL)
205
205
assert self._index != NULL
207
207
cdef _expand_sources(self):
235
234
# TODO: inline some of create_delta so we at least don't have to double
236
235
# malloc, and can instead use PyString_FromStringAndSize, to
237
236
# allocate the bytes into the final string
238
c_max_delta_size = max_delta_size
240
delta = create_delta(self._index,
242
&delta_size, c_max_delta_size)
237
delta = create_delta(self._index,
239
&delta_size, max_delta_size)
245
242
result = PyString_FromStringAndSize(<char *>delta, delta_size)
281
278
cdef unsigned char *_decode_copy_instruction(unsigned char *bytes,
282
unsigned char cmd, unsigned int *offset,
283
unsigned int *length) nogil: # cannot_raise
279
unsigned char cmd, unsigned int *offset, unsigned int *length):
284
280
"""Decode a copy instruction from the next few bytes.
286
282
A copy instruction is a variable number of bytes, so we will parse the
340
335
result = PyString_FromStringAndSize(NULL, size)
341
336
dst_buf = <unsigned char*>PyString_AS_STRING(result)
351
data = _decode_copy_instruction(data, cmd, &cp_off, &cp_size)
352
if (cp_off + cp_size < cp_size or
353
cp_off + cp_size > source_size or
357
memcpy(out, source + cp_off, cp_size)
359
size = size - cp_size
363
# cmd == 0 is reserved for future encoding
364
# extensions. In the mean time we must fail when
365
# encountering them (might be data corruption).
371
memcpy(out, data, cmd)
377
raise ValueError('Something wrong with:'
378
' cp_off = %s, cp_size = %s'
379
' source_size = %s, size = %s'
380
% (cp_off, cp_size, source_size, size))
382
raise ValueError('Got delta opcode: 0, not supported')
384
raise ValueError('Insert instruction longer than remaining'
385
' bytes: %d > %d' % (cmd, size))
344
data = _decode_copy_instruction(data, cmd, &cp_off, &cp_size)
345
if (cp_off + cp_size < cp_size or
346
cp_off + cp_size > source_size or
348
raise RuntimeError('Something wrong with:'
349
' cp_off = %s, cp_size = %s'
350
' source_size = %s, size = %s'
351
% (cp_off, cp_size, source_size, size))
352
memcpy(out, source + cp_off, cp_size)
354
size = size - cp_size
358
# cmd == 0 is reserved for future encoding
359
# extensions. In the mean time we must fail when
360
# encountering them (might be data corruption).
361
raise RuntimeError('Got delta opcode: 0, not supported')
363
raise RuntimeError('Insert instruction longer than remaining'
364
' bytes: %d > %d' % (cmd, size))
365
memcpy(out, data, cmd)
388
371
if (data != top or size != 0):