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