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