added hooks for compression in token.c
[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
c627d613
AT
88static off_t last_match;
89
90
d9bea2dd
AT
91static void matched(int f,struct sum_struct *s,char *buf,off_t len,
92 int offset,int i)
c627d613
AT
93{
94 int n = offset - last_match;
95
96 if (verbose > 2)
97 if (i != -1)
dc5ddbcc 98 fprintf(FERROR,"match at %d last_match=%d j=%d len=%d n=%d\n",
c627d613
AT
99 (int)offset,(int)last_match,i,(int)s->sums[i].len,n);
100
70d794dc
AT
101 send_token(f,i,buf,last_match,n);
102
103 data_transfer += n;
104
105 if (i != -1)
106 last_match = offset + s->sums[i].len;
107
c627d613
AT
108 if (i != -1)
109 last_match = offset + s->sums[i].len;
110 if (n > 0)
111 write_flush(f);
112}
113
114
115static void hash_search(int f,struct sum_struct *s,char *buf,off_t len)
116{
117 int offset,j,k;
118 int end;
119 char sum2[SUM_LENGTH];
d9bea2dd
AT
120 uint32 s1, s2, sum;
121 char *map;
c627d613
AT
122
123 if (verbose > 2)
dc5ddbcc 124 fprintf(FERROR,"hash search b=%d len=%d\n",s->n,(int)len);
c627d613
AT
125
126 k = MIN(len, s->n);
d9bea2dd
AT
127
128 map = map_ptr(buf,0,k);
129
130 sum = get_checksum1(map, k);
c627d613
AT
131 s1 = sum & 0xFFFF;
132 s2 = sum >> 16;
133 if (verbose > 3)
dc5ddbcc 134 fprintf(FERROR, "sum=%.8x k=%d\n", sum, k);
c627d613
AT
135
136 offset = 0;
137
138 end = len + 1 - s->sums[s->count-1].len;
139
140 if (verbose > 3)
dc5ddbcc 141 fprintf(FERROR,"hash search s->n=%d len=%d count=%d\n",
c627d613
AT
142 s->n,(int)len,s->count);
143
144 do {
145 tag t = gettag2(s1,s2);
146 j = tag_table[t];
147 if (verbose > 4)
dc5ddbcc 148 fprintf(FERROR,"offset=%d sum=%08x\n",
c627d613
AT
149 offset,sum);
150
151 if (j != NULL_TAG) {
152 int done_csum2 = 0;
153
154 sum = (s1 & 0xffff) | (s2 << 16);
155 tag_hits++;
156 do {
157 int i = targets[j].i;
158
159 if (sum == s->sums[i].sum1) {
160 if (verbose > 3)
dc5ddbcc 161 fprintf(FERROR,"potential match at %d target=%d %d sum=%08x\n",
c627d613
AT
162 offset,j,i,sum);
163
164 if (!done_csum2) {
d9bea2dd
AT
165 int l = MIN(s->n,len-offset);
166 map = map_ptr(buf,offset,l);
167 get_checksum2(map,l,sum2);
c627d613
AT
168 done_csum2 = 1;
169 }
43a481dc 170 if (memcmp(sum2,s->sums[i].sum2,csum_length) == 0) {
c627d613
AT
171 matched(f,s,buf,len,offset,i);
172 offset += s->sums[i].len - 1;
173 k = MIN((len-offset), s->n);
d9bea2dd
AT
174 map = map_ptr(buf,offset,k);
175 sum = get_checksum1(map, k);
c627d613
AT
176 s1 = sum & 0xFFFF;
177 s2 = sum >> 16;
178 ++matches;
179 break;
180 } else {
181 false_alarms++;
182 }
183 }
184 j++;
185 } while (j<s->count && targets[j].t == t);
186 }
187
188 /* Trim off the first byte from the checksum */
d9bea2dd
AT
189 map = map_ptr(buf,offset,k+1);
190 s1 -= map[0];
191 s2 -= k * map[0];
c627d613
AT
192
193 /* Add on the next byte (if there is one) to the checksum */
194 if (k < (len-offset)) {
d9bea2dd 195 s1 += map[k];
c627d613
AT
196 s2 += s1;
197 } else {
198 --k;
199 }
200
c627d613
AT
201 } while (++offset < end);
202
203 matched(f,s,buf,len,len,-1);
204}
205
206
720b47f2 207void match_sums(int f,struct sum_struct *s,char *buf,off_t len)
c627d613
AT
208{
209 last_match = 0;
210 false_alarms = 0;
211 tag_hits = 0;
212 matches=0;
213 data_transfer=0;
214
215 if (len > 0 && s->count>0) {
216 build_hash_table(s);
217
218 if (verbose > 2)
dc5ddbcc 219 fprintf(FERROR,"built hash table\n");
c627d613
AT
220
221 hash_search(f,s,buf,len);
222
223 if (verbose > 2)
dc5ddbcc 224 fprintf(FERROR,"done hash search\n");
c627d613
AT
225 } else {
226 matched(f,s,buf,len,len,-1);
227 }
228
229 if (targets) {
230 free(targets);
231 targets=NULL;
232 }
233
234 if (verbose > 2)
dc5ddbcc 235 fprintf(FERROR, "false_alarms=%d tag_hits=%d matches=%d\n",
c627d613
AT
236 false_alarms, tag_hits, matches);
237
238 total_tag_hits += tag_hits;
239 total_false_alarms += false_alarms;
240 total_matches += matches;
241 total_data_transfer += data_transfer;
242}
243
244void match_report(void)
245{
246 if (verbose <= 1)
247 return;
248
dc5ddbcc 249 fprintf(FINFO,
c627d613
AT
250 "total: matches=%d tag_hits=%d false_alarms=%d data=%d\n",
251 total_matches,total_tag_hits,
252 total_false_alarms,total_data_transfer);
253}