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