2 * diff-delta.c: generate a delta between two buffers
4 * Many parts of this file have been lifted from LibXDiff version 0.10.
5 * http://www.xmailserver.org/xdiff-lib.html
7 * LibXDiff was written by Davide Libenzi <davidel@xmailserver.org>
8 * Copyright (C) 2003 Davide Libenzi
10 * Many mods for GIT usage by Nicolas Pitre <nico@cam.org>, (C) 2005.
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.
17 * Use of this within git automatically means that the LGPL
18 * licensing gets turned into GPLv2 within this project.
27 /* block size: min = 16, max = 64k, power of 2 */
30 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32 #define GR_PRIME 0x9e370001
33 #define HASH(v, shift) (((unsigned int)(v) * GR_PRIME) >> (shift))
36 const unsigned char *ptr;
41 static struct index ** delta_index(const unsigned char *buf,
42 unsigned long bufsize,
43 unsigned long trg_bufsize,
44 unsigned int *hash_shift)
46 unsigned int i, hsize, hshift, hlimit, entries, *hash_count;
47 const unsigned char *data;
48 struct index *entry, **hash;
51 /* determine index hash size */
52 entries = bufsize / BLK_SIZE;
54 for (i = 4; (1 << i) < hsize && i < 31; i++);
59 /* allocate lookup index */
60 mem = malloc(hsize * sizeof(*hash) + entries * sizeof(*entry));
64 entry = mem + hsize * sizeof(*hash);
65 memset(hash, 0, hsize * sizeof(*hash));
67 /* allocate an array to count hash entries */
68 hash_count = calloc(hsize, sizeof(*hash_count));
74 /* then populate the index */
75 data = buf + entries * BLK_SIZE - BLK_SIZE;
77 unsigned int val = adler32(0, data, BLK_SIZE);
78 i = HASH(val, hshift);
81 entry->next = hash[i];
88 * Determine a limit on the number of entries in the same hash
89 * bucket. This guard us against patological data sets causing
90 * really bad hash distribution with most entries in the same hash
91 * bucket that would bring us to O(m*n) computing costs (m and n
92 * corresponding to reference and target buffer sizes).
94 * The more the target buffer is large, the more it is important to
95 * have small entry lists for each hash buckets. With such a limit
96 * the cost is bounded to something more like O(m+n).
98 hlimit = (1 << 26) / trg_bufsize;
99 if (hlimit < 4*BLK_SIZE)
103 * Now make sure none of the hash buckets has more entries than
104 * we're willing to test. Otherwise we cull the entry list
105 * uniformly to still preserve a good repartition across
106 * the reference buffer.
108 for (i = 0; i < hsize; i++) {
109 if (hash_count[i] < hlimit)
113 struct index *keep = entry;
114 int skip = hash_count[i] / hlimit / 2;
117 } while(--skip && entry);
126 /* provide the size of the copy opcode given the block offset and size */
127 #define COPYOP_SIZE(o, s) \
128 (!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
129 !!(s & 0xff) + !!(s & 0xff00) + 1)
131 /* the maximum size for any opcode */
132 #define MAX_OP_SIZE COPYOP_SIZE(0xffffffff, 0xffffffff)
134 void *diff_delta(void *from_buf, unsigned long from_size,
135 void *to_buf, unsigned long to_size,
136 unsigned long *delta_size,
137 unsigned long max_size)
139 unsigned int i, outpos, outsize, hash_shift;
141 const unsigned char *ref_data, *ref_top, *data, *top;
143 struct index *entry, **hash;
145 if (!from_size || !to_size)
147 hash = delta_index(from_buf, from_size, to_size, &hash_shift);
153 if (max_size && outsize >= max_size)
154 outsize = max_size + MAX_OP_SIZE + 1;
155 out = malloc(outsize);
162 ref_top = from_buf + from_size;
164 top = to_buf + to_size;
166 /* store reference buffer size */
167 out[outpos++] = from_size;
170 out[outpos - 1] |= 0x80;
171 out[outpos++] = from_size;
175 /* store target buffer size */
176 out[outpos++] = to_size;
179 out[outpos - 1] |= 0x80;
180 out[outpos++] = to_size;
187 unsigned int moff = 0, msize = 0;
188 if (data + BLK_SIZE <= top) {
189 unsigned int val = adler32(0, data, BLK_SIZE);
190 i = HASH(val, hash_shift);
191 for (entry = hash[i]; entry; entry = entry->next) {
192 const unsigned char *ref = entry->ptr;
193 const unsigned char *src = data;
194 unsigned int ref_size = ref_top - ref;
195 if (entry->val != val)
197 if (ref_size > top - src)
198 ref_size = top - src;
199 if (ref_size > 0x10000)
201 if (ref_size <= msize)
203 while (ref_size-- && *src++ == *ref)
205 if (msize < ref - entry->ptr) {
206 /* this is our best match so far */
207 msize = ref - entry->ptr;
208 moff = entry->ptr - ref_data;
213 if (!msize || msize < COPYOP_SIZE(moff, msize)) {
216 out[outpos++] = *data++;
218 if (inscnt == 0x7f) {
219 out[outpos - inscnt - 1] = inscnt;
226 while (moff && ref_data[moff-1] == data[-1]) {
227 if (msize == 0x10000)
229 /* we can match one byte back */
236 outpos--; /* remove count slot */
237 inscnt--; /* make it -1 */
240 out[outpos - inscnt - 1] = inscnt;
248 if (moff & 0xff) { out[outpos++] = moff; i |= 0x01; }
250 if (moff & 0xff) { out[outpos++] = moff; i |= 0x02; }
252 if (moff & 0xff) { out[outpos++] = moff; i |= 0x04; }
254 if (moff & 0xff) { out[outpos++] = moff; i |= 0x08; }
256 if (msize & 0xff) { out[outpos++] = msize; i |= 0x10; }
258 if (msize & 0xff) { out[outpos++] = msize; i |= 0x20; }
263 if (outpos >= outsize - MAX_OP_SIZE) {
265 outsize = outsize * 3 / 2;
266 if (max_size && outsize >= max_size)
267 outsize = max_size + MAX_OP_SIZE + 1;
268 if (max_size && outpos > max_size)
271 out = realloc(out, outsize);
281 out[outpos - inscnt - 1] = inscnt;
284 *delta_size = outpos;