2 * diff-delta code taken from git.git. See diff-delta.c for details.
5 #ifndef INCLUDE_git_delta_h__
6 #define INCLUDE_git_delta_h__
12 typedef struct git_delta_index git_delta_index
;
15 * git_delta_index_init: compute index data from given buffer
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().
23 extern int git_delta_index_init(
24 git_delta_index
**out
, const void *buf
, size_t bufsize
);
27 * Free the index created by git_delta_index_init()
29 extern void git_delta_index_free(git_delta_index
*index
);
32 * Returns memory usage of delta index.
34 extern size_t git_delta_index_size(git_delta_index
*index
);
37 * create_delta: create a delta from given index for the given buffer
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.
46 extern int git_delta_create_from_index(
49 const struct git_delta_index
*index
,
52 size_t max_delta_size
);
55 * diff_delta: create a delta from source buffer to target buffer
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.
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
)
68 git_delta_index
*index
;
74 if ((error
= git_delta_index_init(&index
, src_buf
, src_bufsize
)) < 0)
78 error
= git_delta_create_from_index(out
, out_len
,
79 index
, trg_buf
, trg_bufsize
, max_delta_size
);
81 git_delta_index_free(index
);
87 /* the smallest possible delta size is 4 bytes */
88 #define GIT_DELTA_SIZE_MIN 4
91 * Apply a git binary delta to recover the original content.
92 * The caller is responsible for freeing the returned buffer.
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
102 extern int git_delta_apply(
105 const unsigned char *base
,
107 const unsigned char *delta
,
111 * Read the header of a git binary delta.
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
119 extern int git_delta_read_header(
122 const unsigned char *delta
,
126 * Read the header of a git binary delta
128 * This variant reads just enough from the packfile stream to read the
131 extern int git_delta_read_header_fromstream(
134 git_packfile_stream
*stream
);