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