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 const unsigned char *ptr;
31 static struct index ** delta_index(const unsigned char *buf,
32 unsigned long bufsize,
33 unsigned long trg_bufsize,
34 unsigned int *hash_shift)
37 unsigned int i, hshift, hlimit, *hash_count;
38 const unsigned char *data;
39 struct index *entry, **hash;
42 /* determine index hash size */
44 for (i = 8; (1 << i) < hsize && i < 24; i += 2);
49 /* allocate lookup index */
50 mem = malloc(hsize * sizeof(*hash) + bufsize * sizeof(*entry));
54 entry = mem + hsize * sizeof(*hash);
55 memset(hash, 0, hsize * sizeof(*hash));
57 /* allocate an array to count hash entries */
58 hash_count = calloc(hsize, sizeof(*hash_count));
64 /* then populate the index */
65 data = buf + bufsize - 2;
68 i = data[0] ^ ((data[1] ^ (data[2] << hshift)) << hshift);
69 entry->next = hash[i];
75 * Determine a limit on the number of entries in the same hash
76 * bucket. This guard us against patological data sets causing
77 * really bad hash distribution with most entries in the same hash
78 * bucket that would bring us to O(m*n) computing costs (m and n
79 * corresponding to reference and target buffer sizes).
81 * The more the target buffer is large, the more it is important to
82 * have small entry lists for each hash buckets. With such a limit
83 * the cost is bounded to something more like O(m+n).
85 hlimit = (1 << 26) / trg_bufsize;
90 * Now make sure none of the hash buckets has more entries than
91 * we're willing to test. Otherwise we cull the entry list to
92 * limit identical three byte prefixes to still preserve a good
93 * repartition across the reference buffer.
95 for (i = 0; i < hsize; i++) {
96 struct index **list, *bucket, *remaining;
98 if (hash_count[i] < hlimit)
105 while (cnt < hlimit && remaining) {
106 struct index *this = remaining, *that;
107 remaining = remaining->next;
108 for (that = bucket; that; that = that->next) {
109 if (!memcmp(that->ptr, this->ptr, 3))
113 continue; /* discard */
116 list = &(this->next);
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, inscnt, hash_shift;
140 const unsigned char *ref_data, *ref_top, *data, *top;
142 struct index *entry, **hash;
144 if (!from_size || !to_size)
146 hash = delta_index(from_buf, from_size, to_size, &hash_shift);
152 if (max_size && outsize >= max_size)
153 outsize = max_size + MAX_OP_SIZE + 1;
154 out = malloc(outsize);
161 ref_top = from_buf + from_size;
163 top = to_buf + to_size;
165 /* store reference buffer size */
166 out[outpos++] = from_size;
169 out[outpos - 1] |= 0x80;
170 out[outpos++] = from_size;
174 /* store target buffer size */
175 out[outpos++] = to_size;
178 out[outpos - 1] |= 0x80;
179 out[outpos++] = to_size;
186 unsigned int moff = 0, msize = 0;
187 if (data + 3 <= top) {
188 i = data[0] ^ ((data[1] ^ (data[2] << hash_shift)) << hash_shift);
189 for (entry = hash[i]; entry; entry = entry->next) {
190 const unsigned char *ref = entry->ptr;
191 const unsigned char *src = data;
192 unsigned int ref_size = ref_top - ref;
193 if (ref_size > top - src)
194 ref_size = top - src;
195 if (ref_size > 0x10000)
197 if (ref_size <= msize)
201 while (ref_size-- && *++src == *++ref);
202 if (msize < ref - entry->ptr) {
203 /* this is our best match so far */
204 msize = ref - entry->ptr;
205 moff = entry->ptr - ref_data;
210 if (!msize || msize < COPYOP_SIZE(moff, msize)) {
213 out[outpos++] = *data++;
215 if (inscnt == 0x7f) {
216 out[outpos - inscnt - 1] = inscnt;
223 out[outpos - inscnt - 1] = inscnt;
231 if (moff & 0xff) { out[outpos++] = moff; i |= 0x01; }
233 if (moff & 0xff) { out[outpos++] = moff; i |= 0x02; }
235 if (moff & 0xff) { out[outpos++] = moff; i |= 0x04; }
237 if (moff & 0xff) { out[outpos++] = moff; i |= 0x08; }
239 if (msize & 0xff) { out[outpos++] = msize; i |= 0x10; }
241 if (msize & 0xff) { out[outpos++] = msize; i |= 0x20; }
246 if (outpos >= outsize - MAX_OP_SIZE) {
248 outsize = outsize * 3 / 2;
249 if (max_size && outsize >= max_size)
250 outsize = max_size + MAX_OP_SIZE + 1;
251 if (max_size && outpos > max_size)
254 out = realloc(out, outsize);
264 out[outpos - inscnt - 1] = inscnt;
267 *delta_size = outpos;