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 int *hash_shift
)
45 unsigned int hsize
, hshift
, entries
, blksize
, i
;
46 const unsigned char *data
;
47 struct index
*entry
, **hash
;
50 /* determine index hash size */
51 entries
= (bufsize
+ BLK_SIZE
- 1) / BLK_SIZE
;
53 for (i
= 4; (1 << i
) < hsize
&& i
< 16; i
++);
58 /* allocate lookup index */
59 mem
= malloc(hsize
* sizeof(*hash
) + entries
* sizeof(*entry
));
63 entry
= mem
+ hsize
* sizeof(*hash
);
64 memset(hash
, 0, hsize
* sizeof(*hash
));
66 /* then populate it */
67 data
= buf
+ entries
* BLK_SIZE
- BLK_SIZE
;
68 blksize
= bufsize
- (data
- buf
);
70 unsigned int val
= adler32(0, data
, blksize
);
71 i
= HASH(val
, hshift
);
74 entry
->next
= hash
[i
];
83 /* provide the size of the copy opcode given the block offset and size */
84 #define COPYOP_SIZE(o, s) \
85 (!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
86 !!(s & 0xff) + !!(s & 0xff00) + 1)
88 /* the maximum size for any opcode */
89 #define MAX_OP_SIZE COPYOP_SIZE(0xffffffff, 0xffffffff)
91 void *diff_delta(void *from_buf
, unsigned long from_size
,
92 void *to_buf
, unsigned long to_size
,
93 unsigned long *delta_size
,
94 unsigned long max_size
)
96 unsigned int i
, outpos
, outsize
, inscnt
, hash_shift
;
97 const unsigned char *ref_data
, *ref_top
, *data
, *top
;
99 struct index
*entry
, **hash
;
101 if (!from_size
|| !to_size
)
103 hash
= delta_index(from_buf
, from_size
, &hash_shift
);
109 if (max_size
&& outsize
>= max_size
)
110 outsize
= max_size
+ MAX_OP_SIZE
+ 1;
111 out
= malloc(outsize
);
118 ref_top
= from_buf
+ from_size
;
120 top
= to_buf
+ to_size
;
122 /* store reference buffer size */
123 out
[outpos
++] = from_size
;
126 out
[outpos
- 1] |= 0x80;
127 out
[outpos
++] = from_size
;
131 /* store target buffer size */
132 out
[outpos
++] = to_size
;
135 out
[outpos
- 1] |= 0x80;
136 out
[outpos
++] = to_size
;
143 unsigned int moff
= 0, msize
= 0;
144 unsigned int blksize
= MIN(top
- data
, BLK_SIZE
);
145 unsigned int val
= adler32(0, data
, blksize
);
146 i
= HASH(val
, hash_shift
);
147 for (entry
= hash
[i
]; entry
; entry
= entry
->next
) {
148 const unsigned char *ref
= entry
->ptr
;
149 const unsigned char *src
= data
;
150 unsigned int ref_size
= ref_top
- ref
;
151 if (entry
->val
!= val
)
153 if (ref_size
> top
- src
)
154 ref_size
= top
- src
;
155 while (ref_size
&& *src
++ == *ref
) {
159 ref_size
= ref
- entry
->ptr
;
160 if (ref_size
> msize
) {
161 /* this is our best match so far */
162 moff
= entry
->ptr
- ref_data
;
164 if (msize
>= 0x10000) {
171 if (!msize
|| msize
< COPYOP_SIZE(moff
, msize
)) {
174 out
[outpos
++] = *data
++;
176 if (inscnt
== 0x7f) {
177 out
[outpos
- inscnt
- 1] = inscnt
;
184 out
[outpos
- inscnt
- 1] = inscnt
;
192 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x01; }
194 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x02; }
196 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x04; }
198 if (moff
& 0xff) { out
[outpos
++] = moff
; i
|= 0x08; }
200 if (msize
& 0xff) { out
[outpos
++] = msize
; i
|= 0x10; }
202 if (msize
& 0xff) { out
[outpos
++] = msize
; i
|= 0x20; }
207 if (outpos
>= outsize
- MAX_OP_SIZE
) {
209 outsize
= outsize
* 3 / 2;
210 if (max_size
&& outsize
>= max_size
)
211 outsize
= max_size
+ MAX_OP_SIZE
+ 1;
212 if (max_size
&& outpos
> max_size
)
215 out
= realloc(out
, outsize
);
225 out
[outpos
- inscnt
- 1] = inscnt
;
228 *delta_size
= outpos
;