use delta index data when finding best delta matches
[git/git.git] / diff-delta.c
CommitLineData
a310d434
NP
1/*
2 * diff-delta.c: generate a delta between two buffers
3 *
4 * Many parts of this file have been lifted from LibXDiff version 0.10.
5 * http://www.xmailserver.org/xdiff-lib.html
6 *
7 * LibXDiff was written by Davide Libenzi <davidel@xmailserver.org>
8 * Copyright (C) 2003 Davide Libenzi
9 *
10 * Many mods for GIT usage by Nicolas Pitre <nico@cam.org>, (C) 2005.
11 *
12 * This file is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * Use of this within git automatically means that the LGPL
18 * licensing gets turned into GPLv2 within this project.
19 */
20
21#include <stdlib.h>
8e1454b5
NP
22#include <string.h>
23#include <zlib.h>
a310d434
NP
24#include "delta.h"
25
26
27/* block size: min = 16, max = 64k, power of 2 */
28#define BLK_SIZE 16
29
08abe669
NP
30/* maximum hash entry list for the same hash bucket */
31#define HASH_LIMIT 64
a310d434
NP
32
33#define GR_PRIME 0x9e370001
8e1454b5 34#define HASH(v, shift) (((unsigned int)(v) * GR_PRIME) >> (shift))
a310d434 35
08abe669 36struct index_entry {
a310d434 37 const unsigned char *ptr;
8e1454b5 38 unsigned int val;
08abe669 39 struct index_entry *next;
8e1454b5 40};
a310d434 41
08abe669
NP
42struct delta_index {
43 const void *src_buf;
44 unsigned long src_size;
45 unsigned int hash_shift;
46 struct index_entry *hash[0];
47};
48
49struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
a310d434 50{
08abe669
NP
51 unsigned int i, hsize, hshift, entries, *hash_count;
52 const unsigned char *data, *buffer = buf;
53 struct delta_index *index;
54 struct index_entry *entry, **hash;
8e1454b5
NP
55 void *mem;
56
08abe669
NP
57 if (!buf || !bufsize)
58 return NULL;
59
8e1454b5 60 /* determine index hash size */
c13c6bf7 61 entries = bufsize / BLK_SIZE;
8e1454b5 62 hsize = entries / 4;
c13c6bf7 63 for (i = 4; (1 << i) < hsize && i < 31; i++);
8e1454b5
NP
64 hsize = 1 << i;
65 hshift = 32 - i;
8e1454b5
NP
66
67 /* allocate lookup index */
08abe669
NP
68 mem = malloc(sizeof(*index) +
69 sizeof(*hash) * hsize +
70 sizeof(*entry) * entries);
8e1454b5
NP
71 if (!mem)
72 return NULL;
08abe669
NP
73 index = mem;
74 mem = index + 1;
8e1454b5 75 hash = mem;
08abe669
NP
76 mem = hash + hsize;
77 entry = mem;
78
79 index->src_buf = buf;
80 index->src_size = bufsize;
81 index->hash_shift = hshift;
8e1454b5
NP
82 memset(hash, 0, hsize * sizeof(*hash));
83
c13c6bf7
NP
84 /* allocate an array to count hash entries */
85 hash_count = calloc(hsize, sizeof(*hash_count));
86 if (!hash_count) {
08abe669 87 free(index);
c13c6bf7
NP
88 return NULL;
89 }
90
91 /* then populate the index */
08abe669
NP
92 data = buffer + entries * BLK_SIZE - BLK_SIZE;
93 while (data >= buffer) {
c13c6bf7 94 unsigned int val = adler32(0, data, BLK_SIZE);
8e1454b5
NP
95 i = HASH(val, hshift);
96 entry->ptr = data;
97 entry->val = val;
98 entry->next = hash[i];
99 hash[i] = entry++;
c13c6bf7 100 hash_count[i]++;
a310d434 101 data -= BLK_SIZE;
8e1454b5 102 }
a310d434 103
c13c6bf7
NP
104 /*
105 * Determine a limit on the number of entries in the same hash
106 * bucket. This guard us against patological data sets causing
107 * really bad hash distribution with most entries in the same hash
108 * bucket that would bring us to O(m*n) computing costs (m and n
109 * corresponding to reference and target buffer sizes).
110 *
08abe669 111 * Make sure none of the hash buckets has more entries than
c13c6bf7
NP
112 * we're willing to test. Otherwise we cull the entry list
113 * uniformly to still preserve a good repartition across
114 * the reference buffer.
115 */
116 for (i = 0; i < hsize; i++) {
08abe669 117 if (hash_count[i] < HASH_LIMIT)
c13c6bf7
NP
118 continue;
119 entry = hash[i];
120 do {
08abe669
NP
121 struct index_entry *keep = entry;
122 int skip = hash_count[i] / HASH_LIMIT / 2;
c13c6bf7
NP
123 do {
124 entry = entry->next;
125 } while(--skip && entry);
126 keep->next = entry;
127 } while(entry);
128 }
129 free(hash_count);
130
08abe669
NP
131 return index;
132}
133
134void free_delta_index(struct delta_index *index)
135{
136 free(index);
a310d434
NP
137}
138
fe474b58 139/* provide the size of the copy opcode given the block offset and size */
a310d434
NP
140#define COPYOP_SIZE(o, s) \
141 (!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
142 !!(s & 0xff) + !!(s & 0xff00) + 1)
143
fe474b58
NP
144/* the maximum size for any opcode */
145#define MAX_OP_SIZE COPYOP_SIZE(0xffffffff, 0xffffffff)
146
08abe669
NP
147void *
148create_delta(const struct delta_index *index,
149 const void *trg_buf, unsigned long trg_size,
150 unsigned long *delta_size, unsigned long max_size)
a310d434 151{
5a1fb2ca
NP
152 unsigned int i, outpos, outsize, hash_shift;
153 int inscnt;
8e1454b5
NP
154 const unsigned char *ref_data, *ref_top, *data, *top;
155 unsigned char *out;
a310d434 156
08abe669 157 if (!trg_buf || !trg_size)
8e1454b5
NP
158 return NULL;
159
a310d434
NP
160 outpos = 0;
161 outsize = 8192;
fe474b58
NP
162 if (max_size && outsize >= max_size)
163 outsize = max_size + MAX_OP_SIZE + 1;
a310d434 164 out = malloc(outsize);
08abe669 165 if (!out)
a310d434 166 return NULL;
a310d434
NP
167
168 /* store reference buffer size */
08abe669
NP
169 i = index->src_size;
170 while (i >= 0x80) {
171 out[outpos++] = i | 0x80;
172 i >>= 7;
69a2d426 173 }
08abe669 174 out[outpos++] = i;
a310d434
NP
175
176 /* store target buffer size */
08abe669
NP
177 i = trg_size;
178 while (i >= 0x80) {
179 out[outpos++] = i | 0x80;
180 i >>= 7;
69a2d426 181 }
08abe669 182 out[outpos++] = i;
a310d434 183
08abe669
NP
184 ref_data = index->src_buf;
185 ref_top = ref_data + index->src_size;
186 data = trg_buf;
187 top = trg_buf + trg_size;
188 hash_shift = index->hash_shift;
a310d434 189 inscnt = 0;
a310d434 190
8e1454b5
NP
191 while (data < top) {
192 unsigned int moff = 0, msize = 0;
08abe669
NP
193 struct index_entry *entry;
194 unsigned int val = adler32(0, data, BLK_SIZE);
195 i = HASH(val, hash_shift);
196 for (entry = index->hash[i]; entry; entry = entry->next) {
197 const unsigned char *ref = entry->ptr;
198 const unsigned char *src = data;
199 unsigned int ref_size = ref_top - ref;
200 if (entry->val != val)
201 continue;
202 if (ref_size > top - src)
203 ref_size = top - src;
204 if (ref_size > 0x10000)
205 ref_size = 0x10000;
206 if (ref_size <= msize)
207 break;
208 while (ref_size-- && *src++ == *ref)
209 ref++;
210 if (msize < ref - entry->ptr) {
211 /* this is our best match so far */
212 msize = ref - entry->ptr;
213 moff = entry->ptr - ref_data;
a310d434
NP
214 }
215 }
216
217 if (!msize || msize < COPYOP_SIZE(moff, msize)) {
218 if (!inscnt)
219 outpos++;
220 out[outpos++] = *data++;
221 inscnt++;
222 if (inscnt == 0x7f) {
223 out[outpos - inscnt - 1] = inscnt;
224 inscnt = 0;
225 }
226 } else {
8e1454b5
NP
227 unsigned char *op;
228
a310d434 229 if (inscnt) {
5a1fb2ca
NP
230 while (moff && ref_data[moff-1] == data[-1]) {
231 if (msize == 0x10000)
232 break;
233 /* we can match one byte back */
234 msize++;
235 moff--;
236 data--;
237 outpos--;
238 if (--inscnt)
239 continue;
240 outpos--; /* remove count slot */
241 inscnt--; /* make it -1 */
242 break;
243 }
a310d434
NP
244 out[outpos - inscnt - 1] = inscnt;
245 inscnt = 0;
246 }
247
248 data += msize;
8e1454b5 249 op = out + outpos++;
a310d434
NP
250 i = 0x80;
251
252 if (moff & 0xff) { out[outpos++] = moff; i |= 0x01; }
253 moff >>= 8;
254 if (moff & 0xff) { out[outpos++] = moff; i |= 0x02; }
255 moff >>= 8;
256 if (moff & 0xff) { out[outpos++] = moff; i |= 0x04; }
257 moff >>= 8;
258 if (moff & 0xff) { out[outpos++] = moff; i |= 0x08; }
259
260 if (msize & 0xff) { out[outpos++] = msize; i |= 0x10; }
261 msize >>= 8;
262 if (msize & 0xff) { out[outpos++] = msize; i |= 0x20; }
263
8e1454b5 264 *op = i;
a310d434
NP
265 }
266
fe474b58 267 if (outpos >= outsize - MAX_OP_SIZE) {
a310d434
NP
268 void *tmp = out;
269 outsize = outsize * 3 / 2;
fe474b58
NP
270 if (max_size && outsize >= max_size)
271 outsize = max_size + MAX_OP_SIZE + 1;
272 if (max_size && outpos > max_size)
273 out = NULL;
274 else
275 out = realloc(out, outsize);
a310d434
NP
276 if (!out) {
277 free(tmp);
a310d434
NP
278 return NULL;
279 }
280 }
281 }
282
283 if (inscnt)
284 out[outpos - inscnt - 1] = inscnt;
285
a310d434
NP
286 *delta_size = outpos;
287 return out;
288}