~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to delta.h

  • Committer: John Arbash Meinel
  • Date: 2009-02-27 17:32:04 UTC
  • mto: (0.23.23 groupcompress_rabin)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090227173204-ce7djs6xbflluut1
Minor changes to get diff-delta.c and patch-delta.c to compile.
This includes bringing in 'delta.h'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef DELTA_H
 
2
#define DELTA_H
 
3
 
 
4
#include <stdlib.h>
 
5
#include <string.h>
 
6
/* opaque object for delta index */
 
7
struct delta_index;
 
8
 
 
9
/*
 
10
 * create_delta_index: compute index data from given buffer
 
11
 *
 
12
 * This returns a pointer to a struct delta_index that should be passed to
 
13
 * subsequent create_delta() calls, or to free_delta_index().  A NULL pointer
 
14
 * is returned on failure.  The given buffer must not be freed nor altered
 
15
 * before free_delta_index() is called.  The returned pointer must be freed
 
16
 * using free_delta_index().
 
17
 */
 
18
extern struct delta_index *
 
19
create_delta_index(const void *buf, unsigned long bufsize);
 
20
 
 
21
/*
 
22
 * free_delta_index: free the index created by create_delta_index()
 
23
 *
 
24
 * Given pointer must be what create_delta_index() returned, or NULL.
 
25
 */
 
26
extern void free_delta_index(struct delta_index *index);
 
27
 
 
28
/*
 
29
 * sizeof_delta_index: returns memory usage of delta index
 
30
 *
 
31
 * Given pointer must be what create_delta_index() returned, or NULL.
 
32
 */
 
33
extern unsigned long sizeof_delta_index(struct delta_index *index);
 
34
 
 
35
/*
 
36
 * create_delta: create a delta from given index for the given buffer
 
37
 *
 
38
 * This function may be called multiple times with different buffers using
 
39
 * the same delta_index pointer.  If max_delta_size is non-zero and the
 
40
 * resulting delta is to be larger than max_delta_size then NULL is returned.
 
41
 * On success, a non-NULL pointer to the buffer with the delta data is
 
42
 * returned and *delta_size is updated with its size.  The returned buffer
 
43
 * must be freed by the caller.
 
44
 */
 
45
extern void *
 
46
create_delta(const struct delta_index *index,
 
47
             const void *buf, unsigned long bufsize,
 
48
             unsigned long *delta_size, unsigned long max_delta_size);
 
49
 
 
50
/*
 
51
 * diff_delta: create a delta from source buffer to target buffer
 
52
 *
 
53
 * If max_delta_size is non-zero and the resulting delta is to be larger
 
54
 * than max_delta_size then NULL is returned.  On success, a non-NULL
 
55
 * pointer to the buffer with the delta data is returned and *delta_size is
 
56
 * updated with its size.  The returned buffer must be freed by the caller.
 
57
 */
 
58
static inline void *
 
59
diff_delta(const void *src_buf, unsigned long src_bufsize,
 
60
           const void *trg_buf, unsigned long trg_bufsize,
 
61
           unsigned long *delta_size, unsigned long max_delta_size)
 
62
{
 
63
        struct delta_index *index = create_delta_index(src_buf, src_bufsize);
 
64
        if (index) {
 
65
                void *delta = create_delta(index, trg_buf, trg_bufsize,
 
66
                                           delta_size, max_delta_size);
 
67
                free_delta_index(index);
 
68
                return delta;
 
69
        }
 
70
        return NULL;
 
71
}
 
72
 
 
73
/*
 
74
 * patch_delta: recreate target buffer given source buffer and delta data
 
75
 *
 
76
 * On success, a non-NULL pointer to the target buffer is returned and
 
77
 * *trg_bufsize is updated with its size.  On failure a NULL pointer is
 
78
 * returned.  The returned buffer must be freed by the caller.
 
79
 */
 
80
extern void *patch_delta(const void *src_buf, unsigned long src_size,
 
81
                         const void *delta_buf, unsigned long delta_size,
 
82
                         unsigned long *dst_size);
 
83
 
 
84
/* the smallest possible delta size is 4 bytes */
 
85
#define DELTA_SIZE_MIN  4
 
86
 
 
87
/*
 
88
 * This must be called twice on the delta data buffer, first to get the
 
89
 * expected source buffer size, and again to get the target buffer size.
 
90
 */
 
91
static inline unsigned long get_delta_hdr_size(const unsigned char **datap,
 
92
                                               const unsigned char *top)
 
93
{
 
94
        const unsigned char *data = *datap;
 
95
        unsigned char cmd;
 
96
        unsigned long size = 0;
 
97
        int i = 0;
 
98
        do {
 
99
                cmd = *data++;
 
100
                size |= (cmd & ~0x80) << i;
 
101
                i += 7;
 
102
        } while (cmd & 0x80 && data < top);
 
103
        *datap = data;
 
104
        return size;
 
105
}
 
106
 
 
107
#endif