]> git.proxmox.com Git - libgit2.git/blob - src/delta.h
f61987304b3eff14477c0b6101e1119bd01369cf
[libgit2.git] / src / delta.h
1 /*
2 * diff-delta code taken from git.git. See diff-delta.c for details.
3 *
4 */
5 #ifndef INCLUDE_git_delta_h__
6 #define INCLUDE_git_delta_h__
7
8 #include "common.h"
9
10 #include "pack.h"
11
12 typedef struct git_delta_index git_delta_index;
13
14 /*
15 * git_delta_index_init: compute index data from given buffer
16 *
17 * This returns a pointer to a struct delta_index that should be passed to
18 * subsequent create_delta() calls, or to free_delta_index(). A NULL pointer
19 * is returned on failure. The given buffer must not be freed nor altered
20 * before free_delta_index() is called. The returned pointer must be freed
21 * using free_delta_index().
22 */
23 extern int git_delta_index_init(
24 git_delta_index **out, const void *buf, size_t bufsize);
25
26 /*
27 * Free the index created by git_delta_index_init()
28 */
29 extern void git_delta_index_free(git_delta_index *index);
30
31 /*
32 * Returns memory usage of delta index.
33 */
34 extern size_t git_delta_index_size(git_delta_index *index);
35
36 /*
37 * create_delta: create a delta from given index for the given buffer
38 *
39 * This function may be called multiple times with different buffers using
40 * the same delta_index pointer. If max_delta_size is non-zero and the
41 * resulting delta is to be larger than max_delta_size then NULL is returned.
42 * On success, a non-NULL pointer to the buffer with the delta data is
43 * returned and *delta_size is updated with its size. The returned buffer
44 * must be freed by the caller.
45 */
46 extern int git_delta_create_from_index(
47 void **out,
48 size_t *out_size,
49 const struct git_delta_index *index,
50 const void *buf,
51 size_t bufsize,
52 size_t max_delta_size);
53
54 /*
55 * diff_delta: create a delta from source buffer to target buffer
56 *
57 * If max_delta_size is non-zero and the resulting delta is to be larger
58 * than max_delta_size then GIT_EBUFS is returned. On success, a non-NULL
59 * pointer to the buffer with the delta data is returned and *delta_size is
60 * updated with its size. The returned buffer must be freed by the caller.
61 */
62 GIT_INLINE(int) git_delta(
63 void **out, size_t *out_len,
64 const void *src_buf, size_t src_bufsize,
65 const void *trg_buf, size_t trg_bufsize,
66 size_t max_delta_size)
67 {
68 git_delta_index *index;
69 int error = 0;
70
71 *out = NULL;
72 *out_len = 0;
73
74 if ((error = git_delta_index_init(&index, src_buf, src_bufsize)) < 0)
75 return error;
76
77 if (index) {
78 error = git_delta_create_from_index(out, out_len,
79 index, trg_buf, trg_bufsize, max_delta_size);
80
81 git_delta_index_free(index);
82 }
83
84 return error;
85 }
86
87 /* the smallest possible delta size is 4 bytes */
88 #define GIT_DELTA_SIZE_MIN 4
89
90 /**
91 * Apply a git binary delta to recover the original content.
92 * The caller is responsible for freeing the returned buffer.
93 *
94 * @param out the output buffer
95 * @param out_len the length of the output buffer
96 * @param base the base to copy from during copy instructions.
97 * @param base_len number of bytes available at base.
98 * @param delta the delta to execute copy/insert instructions from.
99 * @param delta_len total number of bytes in the delta.
100 * @return 0 on success or an error code
101 */
102 extern int git_delta_apply(
103 void **out,
104 size_t *out_len,
105 const unsigned char *base,
106 size_t base_len,
107 const unsigned char *delta,
108 size_t delta_len);
109
110 /**
111 * Read the header of a git binary delta.
112 *
113 * @param base_out pointer to store the base size field.
114 * @param result_out pointer to store the result size field.
115 * @param delta the delta to execute copy/insert instructions from.
116 * @param delta_len total number of bytes in the delta.
117 * @return 0 on success or an error code
118 */
119 extern int git_delta_read_header(
120 size_t *base_out,
121 size_t *result_out,
122 const unsigned char *delta,
123 size_t delta_len);
124
125 /**
126 * Read the header of a git binary delta
127 *
128 * This variant reads just enough from the packfile stream to read the
129 * delta header.
130 */
131 extern int git_delta_read_header_fromstream(
132 size_t *base_out,
133 size_t *result_out,
134 git_packfile_stream *stream);
135
136 #endif