Browse code

minor optimization

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@858 77e5149b-7576-45b1-b177-96237e5ba77b

Tomasz Kojm authored on 2004/09/14 05:03:46
Showing 2 changed files
... ...
@@ -1,6 +1,10 @@
1
+Mon Sep 13 21:57:12 CEST 2004 (tk)
2
+----------------------------------
3
+  * libclamav/matcher-bm.c: minor optimization
4
+
1 5
 Mon Sep 13 18:41:34 BST 2004 (njh)
2 6
 ----------------------------------
3
-  clamav-milter:	Use pthread_cond_broadcast() instead of
7
+  * clamav-milter:	Use pthread_cond_broadcast() instead of
4 8
 				pthread_cond_signal
5 9
 
6 10
 Mon Sep 13 18:41:05 CEST 2004 (tk)
... ...
@@ -10,7 +14,7 @@ Mon Sep 13 18:41:05 CEST 2004 (tk)
10 10
 Mon Sep 13 14:17:01 BST 2004 (njh)
11 11
 ----------------------------------
12 12
   * libclamav/mbox.c:	return with CL_EFORMAT if mail can't be parsed
13
-    clamav-milter:	Updated SESSION code. Not enabled by default - don't
13
+  * clamav-milter:	Updated SESSION code. Not enabled by default - don't
14 14
 				use in a production environment, but testing
15 15
 				feedback would be welcome
16 16
 
... ...
@@ -21,10 +21,12 @@
21 21
 #include "others.h"
22 22
 #include "cltypes.h"
23 23
 
24
-#define BM_MIN_LENGTH 10
25
-#define BM_BLOCK_SIZE 3
24
+#define BM_MIN_LENGTH	10
25
+#define BM_TEST_OFFSET	5
26
+#define BM_BLOCK_SIZE	3
26 27
 
27 28
 #define MIN(a,b) (a < b) ? a : b
29
+#define HASH(a,b,c) 211 * (unsigned char) a + 37 * (unsigned char) b + (unsigned char) c
28 30
 
29 31
 
30 32
 int cli_bm_addpatt(struct cl_node *root, struct cli_bm_patt *pattern)
... ...
@@ -41,12 +43,12 @@ int cli_bm_addpatt(struct cl_node *root, struct cli_bm_patt *pattern)
41 41
     }
42 42
 
43 43
     for(i = BM_MIN_LENGTH - BM_BLOCK_SIZE; i >= 0; i--) {
44
-	idx = 211 * ((unsigned char) pt[i]) + 37 * ((unsigned char) pt[i + 1]) + (unsigned char) pt[i + 2];
44
+	idx = HASH(pt[i], pt[i + 1], pt[i + 2]);
45 45
 	root->bm_shift[idx] = MIN(root->bm_shift[idx], BM_MIN_LENGTH - BM_BLOCK_SIZE - i);
46 46
     }
47 47
 
48 48
     i = BM_MIN_LENGTH - BM_BLOCK_SIZE;
49
-    idx = 211 * ((unsigned char) pt[i]) + 37 * ((unsigned char) pt[i + 1]) + (unsigned char) pt[i + 2];
49
+    idx = HASH(pt[i], pt[i + 1], pt[i + 2]);
50 50
 
51 51
     prev = next = root->bm_suffix[idx];
52 52
 
... ...
@@ -131,7 +133,7 @@ int cli_bm_scanbuff(const char *buffer, unsigned int length, const char **virnam
131 131
 	return CL_CLEAN;
132 132
 
133 133
     for(i = BM_MIN_LENGTH - BM_BLOCK_SIZE; i < length - BM_BLOCK_SIZE + 1; ) {
134
-	idx = 211 * ((unsigned char) buffer[i]) + 37 * ((unsigned char) buffer[i + 1]) + (unsigned char) buffer[i + 2];
134
+	idx = HASH(buffer[i], buffer[i + 1], buffer[i + 2]);
135 135
 
136 136
 	shift = root->bm_shift[idx];
137 137
 
... ...
@@ -146,6 +148,12 @@ int cli_bm_scanbuff(const char *buffer, unsigned int length, const char **virnam
146 146
 	    while(p && p->pattern[0] == prefix) {
147 147
 		off = i - BM_MIN_LENGTH + BM_BLOCK_SIZE;
148 148
 		bp = buffer + off;
149
+
150
+		if(bp[BM_TEST_OFFSET] != p->pattern[BM_TEST_OFFSET]) {
151
+		    p = p->next;
152
+		    continue;
153
+		}
154
+
149 155
 		found = 1;
150 156
 		for(j = 0; j < p->length && off < length; j++, off++) {
151 157
 		    if(bp[j] != p->pattern[j]) {