*** empty log message ***
[rsync/rsync.git] / match.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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
43a481dc
AT
22extern int csum_length;
23
c627d613
AT
24extern int verbose;
25extern int am_server;
26
27typedef unsigned short tag;
28
29#define TABLESIZE (1<<16)
30#define NULL_TAG ((tag)-1)
31
32static int false_alarms;
33static int tag_hits;
34static int matches;
35static int data_transfer;
36
37static int total_false_alarms=0;
38static int total_tag_hits=0;
39static int total_matches=0;
40static int total_data_transfer=0;
41
42
43struct target {
44 tag t;
45 int i;
46};
47
48static struct target *targets=NULL;
49
50static tag *tag_table = NULL;
51
52#define gettag2(s1,s2) (((s1) + (s2)) & 0xFFFF)
53#define gettag(sum) gettag2((sum)&0xFFFF,(sum)>>16)
54
55static int compare_targets(struct target *t1,struct target *t2)
56{
57 return(t1->t - t2->t);
58}
59
60
61static void build_hash_table(struct sum_struct *s)
62{
63 int i;
64
65 if (!tag_table)
66 tag_table = (tag *)malloc(sizeof(tag)*TABLESIZE);
67
68 targets = (struct target *)malloc(sizeof(targets[0])*s->count);
69 if (!tag_table || !targets)
70 out_of_memory("build_hash_table");
71
72 for (i=0;i<s->count;i++) {
73 targets[i].i = i;
74 targets[i].t = gettag(s->sums[i].sum1);
75 }
76
77 qsort(targets,s->count,sizeof(targets[0]),(int (*)())compare_targets);
78
79 for (i=0;i<TABLESIZE;i++)
80 tag_table[i] = NULL_TAG;
81
82 for (i=s->count-1;i>=0;i--) {
83 tag_table[targets[i].t] = i;
84 }
85}
86
87
41ba6938
AT
88static void send_match(int f,int i)
89{
90 static int matches[32];
91 static int match_count=0;
92
93 matches[match_count++] = i;
94 if (match_count == 32 || i==0) {
95 int i;
96 for (i=0;i<match_count;i++)
97 write_int(f,matches[i]);
98 match_count=0;
99 }
100}
c627d613
AT
101
102static off_t last_match;
103
104
d9bea2dd
AT
105static void matched(int f,struct sum_struct *s,char *buf,off_t len,
106 int offset,int i)
c627d613
AT
107{
108 int n = offset - last_match;
109
110 if (verbose > 2)
111 if (i != -1)
112 fprintf(stderr,"match at %d last_match=%d j=%d len=%d n=%d\n",
113 (int)offset,(int)last_match,i,(int)s->sums[i].len,n);
114
115 if (n > 0) {
d9bea2dd 116 int l = 0;
c627d613 117 write_int(f,n);
d9bea2dd 118 while (l < n) {
34ccb63e 119 int n1 = MIN(CHUNK_SIZE,n-l);
d9bea2dd
AT
120 write_buf(f,map_ptr(buf,last_match+l,n1),n1);
121 l += n1;
122 }
c627d613
AT
123 data_transfer += n;
124 }
41ba6938 125 send_match(f,-(i+1));
c627d613
AT
126 if (i != -1)
127 last_match = offset + s->sums[i].len;
128 if (n > 0)
129 write_flush(f);
130}
131
132
133static void hash_search(int f,struct sum_struct *s,char *buf,off_t len)
134{
135 int offset,j,k;
136 int end;
137 char sum2[SUM_LENGTH];
d9bea2dd
AT
138 uint32 s1, s2, sum;
139 char *map;
c627d613
AT
140
141 if (verbose > 2)
142 fprintf(stderr,"hash search b=%d len=%d\n",s->n,(int)len);
143
144 k = MIN(len, s->n);
d9bea2dd
AT
145
146 map = map_ptr(buf,0,k);
147
148 sum = get_checksum1(map, k);
c627d613
AT
149 s1 = sum & 0xFFFF;
150 s2 = sum >> 16;
151 if (verbose > 3)
152 fprintf(stderr, "sum=%.8x k=%d\n", sum, k);
153
154 offset = 0;
155
156 end = len + 1 - s->sums[s->count-1].len;
157
158 if (verbose > 3)
159 fprintf(stderr,"hash search s->n=%d len=%d count=%d\n",
160 s->n,(int)len,s->count);
161
162 do {
163 tag t = gettag2(s1,s2);
164 j = tag_table[t];
165 if (verbose > 4)
166 fprintf(stderr,"offset=%d sum=%08x\n",
167 offset,sum);
168
169 if (j != NULL_TAG) {
170 int done_csum2 = 0;
171
172 sum = (s1 & 0xffff) | (s2 << 16);
173 tag_hits++;
174 do {
175 int i = targets[j].i;
176
177 if (sum == s->sums[i].sum1) {
178 if (verbose > 3)
179 fprintf(stderr,"potential match at %d target=%d %d sum=%08x\n",
180 offset,j,i,sum);
181
182 if (!done_csum2) {
d9bea2dd
AT
183 int l = MIN(s->n,len-offset);
184 map = map_ptr(buf,offset,l);
185 get_checksum2(map,l,sum2);
c627d613
AT
186 done_csum2 = 1;
187 }
43a481dc 188 if (memcmp(sum2,s->sums[i].sum2,csum_length) == 0) {
c627d613
AT
189 matched(f,s,buf,len,offset,i);
190 offset += s->sums[i].len - 1;
191 k = MIN((len-offset), s->n);
d9bea2dd
AT
192 map = map_ptr(buf,offset,k);
193 sum = get_checksum1(map, k);
c627d613
AT
194 s1 = sum & 0xFFFF;
195 s2 = sum >> 16;
196 ++matches;
197 break;
198 } else {
199 false_alarms++;
200 }
201 }
202 j++;
203 } while (j<s->count && targets[j].t == t);
204 }
205
206 /* Trim off the first byte from the checksum */
d9bea2dd
AT
207 map = map_ptr(buf,offset,k+1);
208 s1 -= map[0];
209 s2 -= k * map[0];
c627d613
AT
210
211 /* Add on the next byte (if there is one) to the checksum */
212 if (k < (len-offset)) {
d9bea2dd 213 s1 += map[k];
c627d613
AT
214 s2 += s1;
215 } else {
216 --k;
217 }
218
c627d613
AT
219 } while (++offset < end);
220
221 matched(f,s,buf,len,len,-1);
222}
223
224
720b47f2 225void match_sums(int f,struct sum_struct *s,char *buf,off_t len)
c627d613
AT
226{
227 last_match = 0;
228 false_alarms = 0;
229 tag_hits = 0;
230 matches=0;
231 data_transfer=0;
232
233 if (len > 0 && s->count>0) {
234 build_hash_table(s);
235
236 if (verbose > 2)
237 fprintf(stderr,"built hash table\n");
238
239 hash_search(f,s,buf,len);
240
241 if (verbose > 2)
242 fprintf(stderr,"done hash search\n");
243 } else {
244 matched(f,s,buf,len,len,-1);
245 }
246
247 if (targets) {
248 free(targets);
249 targets=NULL;
250 }
251
252 if (verbose > 2)
253 fprintf(stderr, "false_alarms=%d tag_hits=%d matches=%d\n",
254 false_alarms, tag_hits, matches);
255
256 total_tag_hits += tag_hits;
257 total_false_alarms += false_alarms;
258 total_matches += matches;
259 total_data_transfer += data_transfer;
260}
261
262void match_report(void)
263{
264 if (verbose <= 1)
265 return;
266
267 fprintf(am_server?stderr:stdout,
268 "total: matches=%d tag_hits=%d false_alarms=%d data=%d\n",
269 total_matches,total_tag_hits,
270 total_false_alarms,total_data_transfer);
271}