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