]>
git.ipfire.org Git - thirdparty/git.git/blob - diffcore-delta.c
6 * Idea here is very simple.
8 * We have total of (sz-N+1) N-byte overlapping sequences in buf whose
9 * size is sz. If the same N-byte sequence appears in both source and
10 * destination, we say the byte that starts that sequence is shared
11 * between them (i.e. copied from source to destination).
13 * For each possible N-byte sequence, if the source buffer has more
14 * instances of it than the destination buffer, that means the
15 * difference are the number of bytes not copied from source to
16 * destination. If the counts are the same, everything was copied
17 * from source to destination. If the destination has more,
18 * everything was copied, and destination added more.
20 * We are doing an approximation so we do not really have to waste
21 * memory by actually storing the sequence. We just hash them into
22 * somewhere around 2^16 hashbuckets and count the occurrences.
24 * The length of the sequence is arbitrarily set to 8 for now.
27 #define HASHBASE 65537 /* next_prime(2^16) */
29 static void hash_chars(unsigned char *buf
, unsigned long sz
, int *count
)
31 unsigned int accum1
, accum2
, i
;
33 /* an 8-byte shift register made of accum1 and accum2. New
34 * bytes come at LSB of accum2, and shifted up to accum1
36 for (i
= accum1
= accum2
= 0; i
< 7; i
++, sz
--) {
37 accum1
= (accum1
<< 8) | (accum2
>> 24);
38 accum2
= (accum2
<< 8) | *buf
++;
41 accum1
= (accum1
<< 8) | (accum2
>> 24);
42 accum2
= (accum2
<< 8) | *buf
++;
43 /* We want something that hashes permuted byte
44 * sequences nicely; simpler hash like (accum1 ^
45 * accum2) does not perform as well.
47 i
= (accum1
+ accum2
* 0x61) % HASHBASE
;
53 int diffcore_count_changes(void *src
, unsigned long src_size
,
54 void *dst
, unsigned long dst_size
,
55 unsigned long delta_limit
,
56 unsigned long *src_copied
,
57 unsigned long *literal_added
)
59 int *src_count
, *dst_count
, i
;
62 if (src_size
< 8 || dst_size
< 8)
65 src_count
= xcalloc(HASHBASE
* 2, sizeof(int));
66 dst_count
= src_count
+ HASHBASE
;
67 hash_chars(src
, src_size
, src_count
);
68 hash_chars(dst
, dst_size
, dst_count
);
71 for (i
= 0; i
< HASHBASE
; i
++) {
72 if (src_count
[i
] < dst_count
[i
]) {
73 la
+= dst_count
[i
] - src_count
[i
];
76 else /* i.e. if (dst_count[i] <= src_count[i]) */