Browse code

this commit is sponsored by Bar da Ciano

aCaB authored on 2011/01/08 12:11:38
Showing 2 changed files
... ...
@@ -23,6 +23,8 @@
23 23
 #include "str.h"
24 24
 
25 25
 #include <string.h>
26
+#include <stdlib.h>
27
+
26 28
 
27 29
 int hm_addhash(struct cli_matcher *root, const char *hash, uint32_t size, const char *virusname) {
28 30
     const struct cli_htu32_element *item;
... ...
@@ -126,9 +128,9 @@ static const unsigned int hashlen[] = {
126 126
 
127 127
 
128 128
 static inline int hm_cmp(const uint8_t *itm, const uint8_t *ref, unsigned int keylen) {
129
-    uint32_t i = *(uint32_t *)itm, r = *(uint32_t *)ref; /* safely aligned and faster than memcmp */
130
-    if(i != r) 
131
-	return (int)(i - r);
129
+    uint32_t i = *(uint32_t *)itm, r = *(uint32_t *)ref;
130
+    if(i!=r)
131
+	return (i<r) * 2 -1;
132 132
     return memcmp(&itm[4], &ref[4], keylen - 4);
133 133
 }
134 134
 
... ...
@@ -138,7 +140,7 @@ void hm_sort(struct cli_sz_hash *szh, size_t l, size_t r, unsigned int keylen) {
138 138
 
139 139
     const char *tmpv;
140 140
 
141
-    if(l > r - 1)
141
+    if(l + 1 >= r)
142 142
 	return;
143 143
 
144 144
     l1 = l+1, r1 = r;
... ...
@@ -158,17 +160,20 @@ void hm_sort(struct cli_sz_hash *szh, size_t l, size_t r, unsigned int keylen) {
158 158
     }
159 159
 
160 160
     l1--;
161
-    memcpy(tmph, &szh->hash_array[keylen * l1], keylen);
162
-    tmpv = szh->virusnames[l1];
163
-    memcpy(&szh->hash_array[keylen * l1], &szh->hash_array[keylen * l], keylen);
164
-    szh->virusnames[l1] = szh->virusnames[l];
165
-    memcpy(&szh->hash_array[keylen * l], tmph, keylen);
166
-    szh->virusnames[l] = tmpv;
161
+    if(l1!=l) {
162
+	memcpy(tmph, &szh->hash_array[keylen * l1], keylen);
163
+	tmpv = szh->virusnames[l1];
164
+	memcpy(&szh->hash_array[keylen * l1], &szh->hash_array[keylen * l], keylen);
165
+	szh->virusnames[l1] = szh->virusnames[l];
166
+	memcpy(&szh->hash_array[keylen * l], tmph, keylen);
167
+	szh->virusnames[l] = tmpv;
168
+    }
167 169
 
168 170
     hm_sort(szh, l, l1, keylen);
169 171
     hm_sort(szh, r1, r, keylen);
170 172
 }
171 173
 
174
+
172 175
 void hm_flush(struct cli_matcher *root) {
173 176
     enum CLI_HASH_TYPE type;
174 177
 
... ...
@@ -201,10 +206,7 @@ void hm_flush(struct cli_matcher *root) {
201 201
 
202 202
 
203 203
 int cli_hm_have_size(const struct cli_matcher *root, enum CLI_HASH_TYPE type, uint32_t size) {
204
-    if(!size || size == 0xffffffff || !root || !root->hm.htinint[type] || !cli_htu32_find(&root->hm.sizehashes[type], size))
205
-	return 0;
206
-
207
-    return 1;
204
+    return (size && size != 0xffffffff && root && root->hm.htinint[type] && cli_htu32_find(&root->hm.sizehashes[type], size));
208 205
 }
209 206
 
210 207
 int cli_hm_scan(const unsigned char *digest, uint32_t size, const char **virname, const struct cli_matcher *root, enum CLI_HASH_TYPE type) {
... ...
@@ -225,14 +227,16 @@ int cli_hm_scan(const unsigned char *digest, uint32_t size, const char **virname
225 225
 
226 226
     l = 0;
227 227
     r = szh->items;
228
-    while(l < r) {
228
+    while(l <= r) {
229 229
 	size_t c = (l + r) / 2;
230 230
 	int res = hm_cmp(digest, &szh->hash_array[keylen * c], keylen);
231 231
 
232
-	if(res < 0)
233
-	    r = c;
234
-	else if(res > 0)
235
-	    l = c;
232
+	if(res < 0) {
233
+	    if(!c)
234
+		break;
235
+	    r = c - 1;
236
+	} else if(res > 0)
237
+	    l = c + 1;
236 238
 	else {
237 239
 	    if(virname)
238 240
 		*virname = szh->virusnames[c];
... ...
@@ -651,7 +651,7 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
651 651
 	}
652 652
     }
653 653
 
654
-    if(!refhash && !ftonly && ctx->engine->md5_hdb)
654
+    if(!refhash && !ftonly && (ctx->engine->md5_hdb || ctx->engine->hm_hdb))
655 655
 	cli_md5_init(&md5ctx);
656 656
 
657 657
     while(offset < map->len) {
... ...
@@ -696,7 +696,7 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
696 696
 		    type = ret;
697 697
 	    }
698 698
 
699
-	    if(!refhash && ctx->engine->md5_hdb)
699
+	    if(!refhash && (ctx->engine->md5_hdb || ctx->engine->hm_hdb))
700 700
 		cli_md5_update(&md5ctx, buff + maxpatlen * (offset!=0), bytes - maxpatlen * (offset!=0));
701 701
 	}
702 702