2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
25 typedef unsigned short tag;
27 #define TABLESIZE (1<<16)
28 #define NULL_TAG ((tag)-1)
30 static int false_alarms;
33 static int data_transfer;
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;
46 static struct target *targets=NULL;
48 static tag *tag_table = NULL;
50 #define gettag2(s1,s2) (((s1) + (s2)) & 0xFFFF)
51 #define gettag(sum) gettag2((sum)&0xFFFF,(sum)>>16)
53 static int compare_targets(struct target *t1,struct target *t2)
55 return(t1->t - t2->t);
59 static void build_hash_table(struct sum_struct *s)
64 tag_table = (tag *)malloc(sizeof(tag)*TABLESIZE);
66 targets = (struct target *)malloc(sizeof(targets[0])*s->count);
67 if (!tag_table || !targets)
68 out_of_memory("build_hash_table");
70 for (i=0;i<s->count;i++) {
72 targets[i].t = gettag(s->sums[i].sum1);
75 qsort(targets,s->count,sizeof(targets[0]),(int (*)())compare_targets);
77 for (i=0;i<TABLESIZE;i++)
78 tag_table[i] = NULL_TAG;
80 for (i=s->count-1;i>=0;i--) {
81 tag_table[targets[i].t] = i;
87 static off_t last_match;
90 static void matched(int f,struct sum_struct *s,char *buf,off_t len,
93 int n = offset - last_match;
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);
104 int n1 = MIN(WRITE_BLOCK_SIZE,n-l);
105 write_buf(f,map_ptr(buf,last_match+l,n1),n1);
112 last_match = offset + s->sums[i].len;
118 static void hash_search(int f,struct sum_struct *s,char *buf,off_t len)
122 char sum2[SUM_LENGTH];
127 fprintf(stderr,"hash search b=%d len=%d\n",s->n,(int)len);
131 map = map_ptr(buf,0,k);
133 sum = get_checksum1(map, k);
137 fprintf(stderr, "sum=%.8x k=%d\n", sum, k);
141 end = len + 1 - s->sums[s->count-1].len;
144 fprintf(stderr,"hash search s->n=%d len=%d count=%d\n",
145 s->n,(int)len,s->count);
148 tag t = gettag2(s1,s2);
151 fprintf(stderr,"offset=%d sum=%08x\n",
157 sum = (s1 & 0xffff) | (s2 << 16);
160 int i = targets[j].i;
162 if (sum == s->sums[i].sum1) {
164 fprintf(stderr,"potential match at %d target=%d %d sum=%08x\n",
168 int l = MIN(s->n,len-offset);
169 map = map_ptr(buf,offset,l);
170 get_checksum2(map,l,sum2);
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);
188 } while (j<s->count && targets[j].t == t);
191 /* Trim off the first byte from the checksum */
192 map = map_ptr(buf,offset,k+1);
196 /* Add on the next byte (if there is one) to the checksum */
197 if (k < (len-offset)) {
204 } while (++offset < end);
206 matched(f,s,buf,len,len,-1);
210 void match_sums(int f,struct sum_struct *s,char *buf,off_t len)
218 if (len > 0 && s->count>0) {
222 fprintf(stderr,"built hash table\n");
224 hash_search(f,s,buf,len);
227 fprintf(stderr,"done hash search\n");
229 matched(f,s,buf,len,len,-1);
238 fprintf(stderr, "false_alarms=%d tag_hits=%d matches=%d\n",
239 false_alarms, tag_hits, matches);
241 total_tag_hits += tag_hits;
242 total_false_alarms += false_alarms;
243 total_matches += matches;
244 total_data_transfer += data_transfer;
247 void match_report(void)
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);