]> git.proxmox.com Git - libgit2.git/blame - src/delta.h
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / delta.h
CommitLineData
ec1d42b7
MS
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"
eae0bfdc 9
6a2d2f8a 10#include "pack.h"
ec1d42b7 11
1cd65991 12typedef struct git_delta_index git_delta_index;
ec1d42b7
MS
13
14/*
1cd65991 15 * git_delta_index_init: compute index data from given buffer
ec1d42b7
MS
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 */
1cd65991
ET
23extern int git_delta_index_init(
24 git_delta_index **out, const void *buf, size_t bufsize);
ec1d42b7
MS
25
26/*
1cd65991 27 * Free the index created by git_delta_index_init()
ec1d42b7 28 */
1cd65991 29extern void git_delta_index_free(git_delta_index *index);
ec1d42b7
MS
30
31/*
1cd65991 32 * Returns memory usage of delta index.
ec1d42b7 33 */
1cd65991 34extern size_t git_delta_index_size(git_delta_index *index);
ec1d42b7
MS
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 */
1cd65991
ET
46extern int git_delta_create_from_index(
47 void **out,
48 size_t *out_size,
a8122b5d
RB
49 const struct git_delta_index *index,
50 const void *buf,
1cd65991
ET
51 size_t bufsize,
52 size_t max_delta_size);
ec1d42b7
MS
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
1cd65991 58 * than max_delta_size then GIT_EBUFS is returned. On success, a non-NULL
ec1d42b7
MS
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 */
1cd65991
ET
62GIT_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)
ec1d42b7 67{
1cd65991
ET
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
ec1d42b7 77 if (index) {
1cd65991
ET
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);
ec1d42b7 82 }
ec1d42b7 83
1cd65991
ET
84 return error;
85}
ec1d42b7
MS
86
87/* the smallest possible delta size is 4 bytes */
88#define GIT_DELTA_SIZE_MIN 4
89
6a2d2f8a
ET
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*/
102extern 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*/
119extern 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 */
131extern int git_delta_read_header_fromstream(
132 size_t *base_out,
133 size_t *result_out,
134 git_packfile_stream *stream);
135
ec1d42b7 136#endif