- Fixed a problem with the new gettag2() macro by getting rid of the
[rsync/rsync.git] / match.c
CommitLineData
1e4f48d6 1/*
c627d613
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
1e4f48d6 4
c627d613
AT
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
1e4f48d6 9
c627d613
AT
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
1e4f48d6 14
c627d613
AT
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
21
22extern int verbose;
16417f8b 23extern int do_progress;
ba582f75 24extern int checksum_seed;
6cc11982 25extern int append_mode;
a36ffd39
WD
26
27int updating_basis_file;
c627d613 28
c627d613 29static int false_alarms;
900cfcb5 30static int hash_hits;
c627d613 31static int matches;
a800434a 32static int64 data_transfer;
c627d613 33
3a6a366f 34static int total_false_alarms;
900cfcb5 35static int total_hash_hits;
3a6a366f 36static int total_matches;
c627d613 37
a800434a 38extern struct stats stats;
c627d613 39
d9bca0c3
WD
40static uint32 tablesize;
41static int32 *sum_table;
c627d613 42
900cfcb5 43#define GETTAG(sum) ((sum)%tablesize)
c627d613
AT
44
45static void build_hash_table(struct sum_struct *s)
46{
7cacd47e 47 int32 i;
d9bca0c3
WD
48 uint32 prior_size = tablesize;
49
50 /* Dynamically calculate the hash table size so that the hash load
51 * for big files is about 80%. This number must be odd or s2 will
52 * not be able to span the entire set. */
53 tablesize = (uint32)(s->count/8) * 10 + 11;
54 if (tablesize < 65537)
55 tablesize = 65537; /* a prime number */
56 if (tablesize != prior_size) {
57 free(sum_table);
58 sum_table = new_array(int32, tablesize);
59 if (!sum_table)
60 out_of_memory("build_hash_table");
61 }
c627d613 62
d9bca0c3 63 memset(sum_table, 0xFF, tablesize * sizeof sum_table[0]);
c627d613 64
0e36d9da 65 for (i = 0; i < s->count; i++) {
900cfcb5 66 uint32 t = GETTAG(s->sums[i].sum1);
d9bca0c3
WD
67 s->sums[i].chain = sum_table[t];
68 sum_table[t] = i;
1e4f48d6 69 }
c627d613
AT
70}
71
72
bcacc18b 73static OFF_T last_match;
c627d613
AT
74
75
f5f95a38
MP
76/**
77 * Transmit a literal and/or match token.
78 *
79 * This delightfully-named function is called either when we find a
80 * match and need to transmit all the unmatched data leading up to it,
81 * or when we get bored of accumulating literal data and just need to
82 * transmit it. As a result of this second case, it is called even if
83 * we have not matched at all!
84 *
85 * @param i If >0, the number of a matched token. If 0, indicates we
86 * have only literal data.
87 **/
a06b419d 88static void matched(int f, struct sum_struct *s, struct map_struct *buf,
37a56445 89 OFF_T offset, int32 i)
c627d613 90{
a06b419d
WD
91 int32 n = offset - last_match; /* max value: block_size (int32) */
92 int32 j;
93
94 if (verbose > 2 && i >= 0) {
95 rprintf(FINFO,
96 "match at %.0f last_match=%.0f j=%d len=%ld n=%ld\n",
97 (double)offset, (double)last_match, i,
98 (long)s->sums[i].len, (long)n);
99 }
c627d613 100
a06b419d 101 send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len);
e7ebc36c 102 data_transfer += n;
70d794dc 103
a800434a
AT
104 if (i >= 0) {
105 stats.matched_data += s->sums[i].len;
e7ebc36c 106 n += s->sums[i].len;
a800434a 107 }
1e4f48d6
WD
108
109 for (j = 0; j < n; j += CHUNK_SIZE) {
a06b419d
WD
110 int32 n1 = MIN(CHUNK_SIZE, n - j);
111 sum_update(map_ptr(buf, last_match + j, n1), n1);
e7ebc36c 112 }
9e31c482 113
0d0e2e93 114 if (i >= 0)
e7ebc36c 115 last_match = offset + s->sums[i].len;
0d0e2e93
AT
116 else
117 last_match = offset;
eb86d661 118
0394e34a 119 if (buf && do_progress)
4440b8aa 120 show_progress(last_match, buf->file_size);
9e31c482
AT
121}
122
123
c6e7fcb4 124static void hash_search(int f,struct sum_struct *s,
da9d12f5 125 struct map_struct *buf, OFF_T len)
c627d613 126{
7560c17a 127 OFF_T offset, end, backup;
7cacd47e 128 int32 k, want_i;
e7ebc36c 129 char sum2[SUM_LENGTH];
1e4f48d6 130 uint32 s1, s2, sum;
7560c17a 131 int more;
debb4505 132 schar *map;
e7ebc36c 133
a04d77bc
WD
134 /* want_i is used to encourage adjacent matches, allowing the RLL
135 * coding of the output to work more efficiently. */
136 want_i = 0;
923fa978 137
0e36d9da 138 if (verbose > 2) {
a06b419d
WD
139 rprintf(FINFO, "hash search b=%ld len=%.0f\n",
140 (long)s->blength, (double)len);
0e36d9da 141 }
e7ebc36c 142
a06b419d 143 k = (int32)MIN(len, (OFF_T)s->blength);
1e4f48d6 144
0e36d9da 145 map = (schar *)map_ptr(buf, 0, k);
1e4f48d6 146
e7ebc36c
AT
147 sum = get_checksum1((char *)map, k);
148 s1 = sum & 0xFFFF;
149 s2 = sum >> 16;
150 if (verbose > 3)
a06b419d 151 rprintf(FINFO, "sum=%.8x k=%ld\n", sum, (long)k);
1e4f48d6 152
e7ebc36c 153 offset = 0;
1e4f48d6 154
e7ebc36c 155 end = len + 1 - s->sums[s->count-1].len;
1e4f48d6 156
0e36d9da 157 if (verbose > 3) {
a06b419d
WD
158 rprintf(FINFO, "hash search s->blength=%ld len=%.0f count=%.0f\n",
159 (long)s->blength, (double)len, (double)s->count);
0e36d9da 160 }
1e4f48d6 161
e7ebc36c 162 do {
e7ebc36c 163 int done_csum2 = 0;
d9bca0c3 164 int32 i;
900cfcb5 165 uint32 t;
1e4f48d6 166
e7ebc36c 167 if (verbose > 4)
5f808dfb 168 rprintf(FINFO,"offset=%.0f sum=%08x\n",(double)offset,sum);
1e4f48d6 169
900cfcb5
WD
170 t = GETTAG(sum);
171 i = sum_table[t];
172 if (i < 0)
173 goto null_hash;
174
175 hash_hits++;
176 do {
d9bca0c3 177 int32 l;
1e4f48d6 178
0e36d9da
WD
179 if (sum != s->sums[i].sum1)
180 continue;
1e4f48d6 181
536541d5 182 /* also make sure the two blocks are the same length */
a06b419d 183 l = (int32)MIN((OFF_T)s->blength, len-offset);
0e36d9da
WD
184 if (l != s->sums[i].len)
185 continue;
536541d5 186
a36ffd39 187 /* in-place: ensure chunk's offset is either >= our
a3221d2a 188 * offset or that the data didn't move. */
a36ffd39 189 if (updating_basis_file && s->sums[i].offset < offset
a3221d2a
WD
190 && !(s->sums[i].flags & SUMFLG_SAME_OFFSET))
191 continue;
192
d9bca0c3
WD
193 if (verbose > 3) {
194 rprintf(FINFO,
195 "potential match at %.0f i=%ld sum=%08x\n",
196 (double)offset, (long)i, sum);
197 }
1e4f48d6 198
e7ebc36c 199 if (!done_csum2) {
debb4505 200 map = (schar *)map_ptr(buf,offset,l);
e7ebc36c
AT
201 get_checksum2((char *)map,l,sum2);
202 done_csum2 = 1;
203 }
1e4f48d6 204
fc0257c9 205 if (memcmp(sum2,s->sums[i].sum2,s->s2length) != 0) {
e7ebc36c
AT
206 false_alarms++;
207 continue;
208 }
923fa978 209
a36ffd39 210 /* When updating in-place, the best possible match is
a3221d2a
WD
211 * one with an identical offset, so we prefer that over
212 * the following want_i optimization. */
a36ffd39 213 if (updating_basis_file) {
d9bca0c3
WD
214 int32 i2;
215 for (i2 = i; i2 >= 0; i2 = s->sums[i2].chain) {
a3221d2a
WD
216 if (s->sums[i2].offset != offset)
217 continue;
218 if (i2 != i) {
219 if (sum != s->sums[i2].sum1)
220 break;
221 if (memcmp(sum2, s->sums[i2].sum2,
222 s->s2length) != 0)
223 break;
224 i = i2;
225 }
226 /* This chunk was at the same offset on
227 * both the sender and the receiver. */
228 s->sums[i].flags |= SUMFLG_SAME_OFFSET;
229 goto set_want_i;
d9bca0c3 230 }
a3221d2a
WD
231 }
232
923fa978 233 /* we've found a match, but now check to see
a04d77bc
WD
234 * if want_i can hint at a better match. */
235 if (i != want_i && want_i < s->count
a36ffd39 236 && (!updating_basis_file || s->sums[want_i].offset >= offset
a3221d2a 237 || s->sums[want_i].flags & SUMFLG_SAME_OFFSET)
a04d77bc
WD
238 && sum == s->sums[want_i].sum1
239 && memcmp(sum2, s->sums[want_i].sum2, s->s2length) == 0) {
5e252dea
WD
240 /* we've found an adjacent match - the RLL coder
241 * will be happy */
a04d77bc 242 i = want_i;
923fa978 243 }
a3221d2a 244 set_want_i:
a04d77bc 245 want_i = i + 1;
1e4f48d6 246
e7ebc36c
AT
247 matched(f,s,buf,offset,i);
248 offset += s->sums[i].len - 1;
a06b419d 249 k = (int32)MIN((OFF_T)s->blength, len-offset);
0e36d9da 250 map = (schar *)map_ptr(buf, offset, k);
e7ebc36c
AT
251 sum = get_checksum1((char *)map, k);
252 s1 = sum & 0xFFFF;
253 s2 = sum >> 16;
254 matches++;
255 break;
900cfcb5 256 } while ((i = s->sums[i].chain) >= 0);
1e4f48d6 257
900cfcb5 258 null_hash:
7560c17a
WD
259 backup = offset - last_match;
260 /* We sometimes read 1 byte prior to last_match... */
261 if (backup < 0)
262 backup = 0;
263
e7ebc36c 264 /* Trim off the first byte from the checksum */
7560c17a
WD
265 more = offset + k < len;
266 map = (schar *)map_ptr(buf, offset - backup, k + more + backup)
267 + backup;
e7ebc36c
AT
268 s1 -= map[0] + CHAR_OFFSET;
269 s2 -= k * (map[0]+CHAR_OFFSET);
1e4f48d6 270
e7ebc36c 271 /* Add on the next byte (if there is one) to the checksum */
7560c17a
WD
272 if (more) {
273 s1 += map[k] + CHAR_OFFSET;
e7ebc36c 274 s2 += s1;
0e36d9da 275 } else
e7ebc36c 276 --k;
900cfcb5 277 sum = (s1 & 0xffff) | (s2 << 16);
45f133b9 278
5914bf15
PM
279 /* By matching early we avoid re-reading the
280 data 3 times in the case where a token
281 match comes a long way after last
282 match. The 3 reads are caused by the
283 running match, the checksum update and the
284 literal send. */
a06b419d
WD
285 if (backup >= s->blength+CHUNK_SIZE && end-offset > CHUNK_SIZE)
286 matched(f, s, buf, offset - s->blength, -2);
e7ebc36c 287 } while (++offset < end);
1e4f48d6 288
a06b419d
WD
289 matched(f, s, buf, len, -1);
290 map_ptr(buf, len-1, 1);
c627d613
AT
291}
292
293
f5f95a38
MP
294/**
295 * Scan through a origin file, looking for sections that match
296 * checksums from the generator, and transmit either literal or token
297 * data.
298 *
538ba24f
MP
299 * Also calculates the MD4 checksum of the whole file, using the md
300 * accumulator. This is transmitted with the file as protection
301 * against corruption on the wire.
302 *
f5f95a38
MP
303 * @param s Checksums received from the generator. If <tt>s->count ==
304 * 0</tt>, then there are actually no checksums for this file.
305 *
306 * @param len Length of the file to send.
307 **/
308void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len)
c627d613 309{
e7ebc36c
AT
310 char file_sum[MD4_SUM_LENGTH];
311
312 last_match = 0;
313 false_alarms = 0;
900cfcb5 314 hash_hits = 0;
1e4f48d6
WD
315 matches = 0;
316 data_transfer = 0;
e7ebc36c 317
ba582f75 318 sum_init(checksum_seed);
e7ebc36c 319
6cc11982
WD
320 if (append_mode) {
321 OFF_T j = 0;
322 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
1f86fcf5
WD
323 if (buf && do_progress)
324 show_progress(last_match, buf->file_size);
6cc11982
WD
325 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
326 CHUNK_SIZE);
327 last_match = j;
328 }
329 if (last_match < s->flength) {
330 int32 len = s->flength - last_match;
1f86fcf5
WD
331 if (buf && do_progress)
332 show_progress(last_match, buf->file_size);
6cc11982
WD
333 sum_update(map_ptr(buf, last_match, len), len);
334 last_match = s->flength;
335 }
336 s->count = 0;
337 }
338
a06b419d 339 if (len > 0 && s->count > 0) {
e7ebc36c 340 build_hash_table(s);
1e4f48d6
WD
341
342 if (verbose > 2)
9486289c 343 rprintf(FINFO,"built hash table\n");
1e4f48d6 344
e7ebc36c 345 hash_search(f,s,buf,len);
1e4f48d6
WD
346
347 if (verbose > 2)
9486289c 348 rprintf(FINFO,"done hash search\n");
e7ebc36c 349 } else {
eb86d661
AT
350 OFF_T j;
351 /* by doing this in pieces we avoid too many seeks */
6cc11982 352 for (j = last_match + CHUNK_SIZE; j < len; j += CHUNK_SIZE)
a06b419d
WD
353 matched(f, s, buf, j, -2);
354 matched(f, s, buf, len, -1);
e7ebc36c 355 }
9e31c482 356
e7ebc36c 357 sum_end(file_sum);
edecdad5
WD
358 /* If we had a read error, send a bad checksum. */
359 if (buf && buf->status != 0)
360 file_sum[0]++;
c627d613 361
bc63ae3f
S
362 if (verbose > 2)
363 rprintf(FINFO,"sending file_sum\n");
364 write_buf(f,file_sum,MD4_SUM_LENGTH);
c627d613 365
e7ebc36c 366 if (verbose > 2)
900cfcb5
WD
367 rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n",
368 false_alarms, hash_hits, matches);
1e4f48d6 369
900cfcb5 370 total_hash_hits += hash_hits;
e7ebc36c
AT
371 total_false_alarms += false_alarms;
372 total_matches += matches;
a800434a 373 stats.literal_data += data_transfer;
c627d613
AT
374}
375
376void match_report(void)
377{
e7ebc36c
AT
378 if (verbose <= 1)
379 return;
c627d613 380
9486289c 381 rprintf(FINFO,
900cfcb5
WD
382 "total: matches=%d hash_hits=%d false_alarms=%d data=%.0f\n",
383 total_matches, total_hash_hits, total_false_alarms,
a800434a 384 (double)stats.literal_data);
c627d613 385}