226
226
cdef Py_ssize_t source_size
228
228
cdef Py_ssize_t delta_size
229
cdef unsigned char *data, *top
230
cdef unsigned char *dst_buf, *out, cmd
232
cdef unsigned long cp_off, cp_size
234
230
if not PyString_CheckExact(source_bytes):
235
231
raise TypeError('source is not a str')
236
232
if not PyString_CheckExact(delta_bytes):
237
233
raise TypeError('delta is not a str')
239
234
source = PyString_AS_STRING(source_bytes)
240
235
source_size = PyString_GET_SIZE(source_bytes)
241
236
delta = PyString_AS_STRING(delta_bytes)
242
237
delta_size = PyString_GET_SIZE(delta_bytes)
244
238
# Code taken from patch-delta.c, only brought here to give better error
245
239
# handling, and to avoid double allocating memory
246
240
if (delta_size < DELTA_SIZE_MIN):
248
242
raise RuntimeError('delta_size %d smaller than min delta size %d'
249
243
% (delta_size, DELTA_SIZE_MIN))
245
return _apply_delta(source, source_size, delta, delta_size)
248
cdef object _apply_delta(char *source, Py_ssize_t source_size,
249
char *delta, Py_ssize_t delta_size):
250
"""common functionality between apply_delta and apply_delta_to_source."""
251
cdef unsigned char *data, *top
252
cdef unsigned char *dst_buf, *out, cmd
254
cdef unsigned long cp_off, cp_size
251
256
data = <unsigned char *>delta
252
257
top = data + delta_size
336
def apply_delta_to_source(source, delta_start, delta_end):
337
"""Extract a delta from source bytes, and apply it."""
339
cdef Py_ssize_t c_source_size
341
cdef Py_ssize_t c_delta_size
342
cdef Py_ssize_t c_delta_start, c_delta_end
344
if not PyString_CheckExact(source):
345
raise TypeError('source is not a str')
346
c_source_size = PyString_GET_SIZE(source)
347
c_delta_start = delta_start
348
c_delta_end = delta_end
349
if c_delta_start >= c_source_size:
350
raise ValueError('delta starts after source')
351
if c_delta_end > c_source_size:
352
raise ValueError('delta ends after source')
353
if c_delta_start >= c_delta_end:
354
raise ValueError('delta starts after it ends')
356
c_delta_size = c_delta_end - c_delta_start
357
c_source = PyString_AS_STRING(source)
358
c_delta = c_source + c_delta_start
359
# We don't use source_size, because we know the delta should not refer to
360
# any bytes after it starts
361
return _apply_delta(c_source, c_delta_start, c_delta, c_delta_size)
331
364
def encode_base128_int(val):
332
365
"""Convert an integer into a 7-bit lsb encoding."""
333
366
cdef unsigned int c_val