~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_groupcompress_pyx.pyx

  • Committer: John Arbash Meinel
  • Date: 2009-03-27 22:29:55 UTC
  • mto: (3735.39.2 clean)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090327222955-utifmfm888zerixt
Implement apply_delta_to_source which doesn't have to malloc another string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
226
226
    cdef Py_ssize_t source_size
227
227
    cdef char *delta
228
228
    cdef Py_ssize_t delta_size
229
 
    cdef unsigned char *data, *top
230
 
    cdef unsigned char *dst_buf, *out, cmd
231
 
    cdef Py_ssize_t size
232
 
    cdef unsigned long cp_off, cp_size
233
229
 
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')
238
 
 
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)
243
 
 
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))
250
244
 
 
245
    return _apply_delta(source, source_size, delta, delta_size)
 
246
 
 
247
 
 
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
 
253
    cdef Py_ssize_t size
 
254
    cdef unsigned long cp_off, cp_size
 
255
 
251
256
    data = <unsigned char *>delta
252
257
    top = data + delta_size
253
258
 
328
333
    return result
329
334
 
330
335
 
 
336
def apply_delta_to_source(source, delta_start, delta_end):
 
337
    """Extract a delta from source bytes, and apply it."""
 
338
    cdef char *c_source
 
339
    cdef Py_ssize_t c_source_size
 
340
    cdef char *c_delta
 
341
    cdef Py_ssize_t c_delta_size
 
342
    cdef Py_ssize_t c_delta_start, c_delta_end
 
343
 
 
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')
 
355
 
 
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)
 
362
 
 
363
 
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