1
# Copyright (C) 2009 Canonical Ltd
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
32
32
cdef extern from *:
33
33
ctypedef unsigned long size_t
35
void * realloc(void *, size_t)
37
void memcpy(void *, void *, 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
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)
47
delta_index * create_delta_index(source_info *src, delta_index *old) nogil
48
48
delta_index * create_delta_index_from_delta(source_info *delta,
50
void free_delta_index(delta_index *index)
49
delta_index *old) nogil
50
void free_delta_index(delta_index *index) nogil
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)
53
unsigned long *delta_size, unsigned long max_delta_size) nogil
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)
62
59
cdef void *safe_malloc(size_t count) except NULL:
148
145
src.buf = c_delta
149
146
src.size = c_delta_size
150
147
src.agg_offset = self._source_offset + unadded_bytes
151
index = create_delta_index_from_delta(src, self._index)
149
index = create_delta_index_from_delta(src, self._index)
152
150
self._source_offset = src.agg_offset + src.size
153
151
if index != NULL:
154
152
free_delta_index(self._index)
188
186
self._source_offset = src.agg_offset + src.size
189
187
# We delay creating the index on the first insert
190
188
if source_location != 0:
191
index = create_delta_index(src, self._index)
190
index = create_delta_index(src, self._index)
192
191
if index != NULL:
193
192
free_delta_index(self._index)
194
193
self._index = index
202
201
# We know that self._index is already NULL, so whatever
203
202
# 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):
234
235
# TODO: inline some of create_delta so we at least don't have to double
235
236
# malloc, and can instead use PyString_FromStringAndSize, to
236
237
# allocate the bytes into the final string
237
delta = create_delta(self._index,
239
&delta_size, max_delta_size)
238
c_max_delta_size = max_delta_size
240
delta = create_delta(self._index,
242
&delta_size, c_max_delta_size)
242
245
result = PyString_FromStringAndSize(<char *>delta, delta_size)
278
281
cdef unsigned char *_decode_copy_instruction(unsigned char *bytes,
279
unsigned char cmd, unsigned int *offset, unsigned int *length):
282
unsigned char cmd, unsigned int *offset,
283
unsigned int *length) nogil: # cannot_raise
280
284
"""Decode a copy instruction from the next few bytes.
282
286
A copy instruction is a variable number of bytes, so we will parse the
335
340
result = PyString_FromStringAndSize(NULL, size)
336
341
dst_buf = <unsigned char*>PyString_AS_STRING(result)
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)
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))
371
388
if (data != top or size != 0):