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