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.
25 /* block size: min = 16, max = 64k, power of 2 */
28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
30 #define GR_PRIME 0x9e370001
31 #define HASH(v, b) (((unsigned int)(v) * GR_PRIME) >> (32 - (b)))
33 /* largest prime smaller than 65536 */
36 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
39 #define DO1(buf, i) { s1 += buf[i]; s2 += s1; }
40 #define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
41 #define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
42 #define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
43 #define DO16(buf) DO8(buf, 0); DO8(buf, 8);
45 static unsigned int adler32(unsigned int adler
, const unsigned char *buf
, int len
)
48 unsigned int s1
= adler
& 0xffff;
49 unsigned int s2
= adler
>> 16;
68 return (s2
<< 16) | s1
;
71 static unsigned int hashbits(unsigned int size
)
73 unsigned int val
= 1, bits
= 0;
74 while (val
< size
&& bits
< 32) {
78 return bits
? bits
: 1;
81 typedef struct s_chanode
{
82 struct s_chanode
*next
;
86 typedef struct s_chastore
{
87 chanode_t
*head
, *tail
;
94 static void cha_init(chastore_t
*cha
, int isize
, int icount
)
96 cha
->head
= cha
->tail
= NULL
;
98 cha
->nsize
= icount
* isize
;
99 cha
->ancur
= cha
->sncur
= NULL
;
103 static void *cha_alloc(chastore_t
*cha
)
109 if (!ancur
|| ancur
->icurr
== cha
->nsize
) {
110 ancur
= malloc(sizeof(chanode_t
) + cha
->nsize
);
116 cha
->tail
->next
= ancur
;
123 data
= (void *)ancur
+ sizeof(chanode_t
) + ancur
->icurr
;
124 ancur
->icurr
+= cha
->isize
;
128 static void cha_free(chastore_t
*cha
)
130 chanode_t
*cur
= cha
->head
;
132 chanode_t
*tmp
= cur
;
138 typedef struct s_bdrecord
{
139 struct s_bdrecord
*next
;
141 const unsigned char *ptr
;
144 typedef struct s_bdfile
{
145 const unsigned char *data
, *top
;
147 unsigned int fphbits
;
151 static int delta_prepare(const unsigned char *buf
, int bufsize
, bdfile_t
*bdf
)
153 unsigned int fphbits
;
155 const unsigned char *base
, *data
, *top
;
159 fphbits
= hashbits(bufsize
/ BLK_SIZE
+ 1);
160 hsize
= 1 << fphbits
;
161 fphash
= malloc(hsize
* sizeof(bdrecord_t
*));
164 for (i
= 0; i
< hsize
; i
++)
166 cha_init(&bdf
->cha
, sizeof(bdrecord_t
), hsize
/ 4 + 1);
168 bdf
->data
= data
= base
= buf
;
169 bdf
->top
= top
= buf
+ bufsize
;
170 data
+= (bufsize
/ BLK_SIZE
) * BLK_SIZE
;
174 for ( ; data
>= base
; data
-= BLK_SIZE
) {
175 brec
= cha_alloc(&bdf
->cha
);
181 brec
->fp
= adler32(0, data
, MIN(BLK_SIZE
, top
- data
));
183 i
= HASH(brec
->fp
, fphbits
);
184 brec
->next
= fphash
[i
];
188 bdf
->fphbits
= fphbits
;
189 bdf
->fphash
= fphash
;
194 static void delta_cleanup(bdfile_t
*bdf
)
200 #define COPYOP_SIZE(o, s) \
201 (!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
202 !!(s & 0xff) + !!(s & 0xff00) + 1)
204 void *diff_delta(void *from_buf
, unsigned long from_size
,
205 void *to_buf
, unsigned long to_size
,
206 unsigned long *delta_size
,
207 unsigned long max_size
)
209 int i
, outpos
, outsize
, inscnt
, csize
, msize
, moff
;
211 const unsigned char *data
, *top
, *ptr1
, *ptr2
;
212 unsigned char *out
, *orig
;
216 if (delta_prepare(from_buf
, from_size
, &bdf
))
221 out
= malloc(outsize
);
228 top
= to_buf
+ to_size
;
230 /* store reference buffer size */
231 out
[outpos
++] = from_size
;
234 out
[outpos
- 1] |= 0x80;
235 out
[outpos
++] = from_size
;
239 /* store target buffer size */
240 out
[outpos
++] = to_size
;
243 out
[outpos
- 1] |= 0x80;
244 out
[outpos
++] = to_size
;
252 fp
= adler32(0, data
, MIN(top
- data
, BLK_SIZE
));
253 i
= HASH(fp
, bdf
.fphbits
);
254 for (brec
= bdf
.fphash
[i
]; brec
; brec
= brec
->next
) {
255 if (brec
->fp
== fp
) {
256 csize
= bdf
.top
- brec
->ptr
;
257 if (csize
> top
- data
)
259 for (ptr1
= brec
->ptr
, ptr2
= data
;
260 csize
&& *ptr1
== *ptr2
;
261 csize
--, ptr1
++, ptr2
++);
263 csize
= ptr1
- brec
->ptr
;
265 moff
= brec
->ptr
- bdf
.data
;
267 if (msize
>= 0x10000) {
275 if (!msize
|| msize
< COPYOP_SIZE(moff
, msize
)) {
278 out
[outpos
++] = *data
++;
280 if (inscnt
== 0x7f) {
281 out
[outpos
- inscnt
- 1] = inscnt
;
286 out
[outpos
- inscnt
- 1] = inscnt
;
291 orig
= out
+ outpos
++;
294 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x01; }
296 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x02; }
298 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x04; }
300 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x08; }
302 if (msize
& 0xff) { out
[outpos
++] = msize
; i
|= 0x10; }
304 if (msize
& 0xff) { out
[outpos
++] = msize
; i
|= 0x20; }
309 if (max_size
&& outpos
> max_size
) {
315 /* next time around the largest possible output is 1 + 4 + 3 */
316 if (outpos
> outsize
- 8) {
318 outsize
= outsize
* 3 / 2;
319 out
= realloc(out
, outsize
);
329 out
[outpos
- inscnt
- 1] = inscnt
;
332 *delta_size
= outpos
;