~ubuntu-branches/ubuntu/raring/clamav/raring

« back to all changes in this revision

Viewing changes to libclamav/hashtab.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Gran
  • Date: 2008-09-05 17:25:34 UTC
  • mfrom: (0.35.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080905172534-yi3f8fkye1o7u1r3
* New upstream version (closes: #497662, #497773)
  - lots of new options for clamd.conf
  - fixes CVEs CVE-2008-3912, CVE-2008-3913, CVE-2008-3914, and
    CVE-2008-1389
* No longer supports --unzip option, so typo is gone (closes: #496276)
* Translations:
  - sv (thanks Martin Bagge <brother@bsnet.se>) (closes: #491760)

Show diffs side-by-side

added added

removed removed

Lines of Context:
228
228
                        PROFILE_FIND_NOTFOUND(s, tries);
229
229
                        return NULL; /* element not found, place is empty*/
230
230
                }
231
 
                else if(element->key != DELETED_KEY && len == element->len && strncmp(key, element->key,len)==0) {
 
231
                else if(element->key != DELETED_KEY && len == element->len && (key == element->key || strncmp(key, element->key,len)==0)) {
232
232
                        PROFILE_FIND_FOUND(s, tries);
233
233
                        return element;/* found */
234
234
                }
235
235
                else {
236
 
                        idx = (idx + tries++) % s->capacity;
 
236
                        idx = (idx + tries++) & (s->capacity-1);
237
237
                        element = &s->htable[idx];
238
238
                }
239
239
        } while (tries <= s->capacity);
243
243
 
244
244
static int hashtab_grow(struct hashtable *s)
245
245
{
246
 
        const size_t new_capacity = nearest_power(s->capacity);
 
246
        const size_t new_capacity = nearest_power(s->capacity + 1);
247
247
        struct element* htable = cli_calloc(new_capacity, sizeof(*s->htable));
248
248
        size_t i,idx, used = 0;
 
249
        cli_dbgmsg("hashtab.c: new capacity: %lu\n",new_capacity);
249
250
        if(new_capacity == s->capacity || !htable)
250
251
                return CL_EMEM;
251
252
 
286
287
        return CL_SUCCESS;
287
288
}
288
289
 
289
 
int hashtab_insert(struct hashtable *s, const char* key, const size_t len, const element_data data)
 
290
const struct element* hashtab_insert(struct hashtable *s, const char* key, const size_t len, const element_data data)
290
291
{
291
292
        struct element* element;
292
293
        struct element* deleted_element = NULL;
293
294
        size_t tries = 1;
294
295
        size_t idx;
295
296
        if(!s)
296
 
                return CL_ENULLARG;
 
297
                return NULL;
 
298
        if(s->used > s->maxfill) {
 
299
                cli_dbgmsg("hashtab.c:Growing hashtable %p, because it has exceeded maxfill, old size:%ld\n",(void*)s,s->capacity);
 
300
                hashtab_grow(s);
 
301
        }
297
302
        do {
298
303
                PROFILE_CALC_HASH(s);
299
304
                idx = hash((const unsigned char*)key, len, s->capacity);
313
318
                                }
314
319
                                thekey = cli_malloc(len+1);
315
320
                                if(!thekey)
316
 
                                        return CL_EMEM;
 
321
                                        return NULL;
317
322
                                strncpy(thekey, key, len+1);
318
323
                                thekey[len]='\0';
319
324
                                element->key = thekey;
320
325
                                element->data = data;
321
326
                                element->len = len;
322
327
                                s->used++;
323
 
                                if(s->used > s->maxfill) {
324
 
                                        cli_dbgmsg("hashtab.c:Growing hashtable %p, because it has exceeded maxfill, old size:%ld\n",(void*)s,s->capacity);
325
 
                                        hashtab_grow(s);
326
 
                                }
327
 
                                return 0;
 
328
                                return element;
328
329
                        }
329
330
                        else if(element->key == DELETED_KEY) {
330
331
                                deleted_element = element;
332
333
                        else if(len == element->len && strncmp(key, element->key, len)==0) {
333
334
                                PROFILE_DATA_UPDATE(s, tries);
334
335
                                element->data = data;/* key found, update */
335
 
                                return 0;
 
336
                                return element;
336
337
                        }
337
338
                        else {
338
339
                                idx = (idx + tries++) % s->capacity;
344
345
                cli_dbgmsg("hashtab.c: Growing hashtable %p, because its full, old size:%ld.\n",(void*)s,s->capacity);
345
346
        } while( hashtab_grow(s) >= 0 );
346
347
        cli_warnmsg("hashtab.c: Unable to grow hashtable\n");
347
 
        return CL_EMEM;
348
 
}
349
 
 
350
 
void hashtab_delete(struct hashtable *s, const char* key, const size_t len)
351
 
{
352
 
        struct element* e = hashtab_find(s,key,len);
353
 
        if(e && e->key) {
354
 
                PROFILE_HASH_DELETE(s);
355
 
                free((void *)e->key);
356
 
                e->key = DELETED_KEY;
357
 
                s->used--;
358
 
        }
 
348
        return NULL;
359
349
}
360
350
 
361
351
void hashtab_clear(struct hashtable *s)
366
356
                if(s->htable[i].key && s->htable[i].key != DELETED_KEY)
367
357
                        free((void *)s->htable[i].key);
368
358
        }
369
 
        memset(s->htable, 0, s->capacity);
 
359
        if(s->htable)
 
360
                memset(s->htable, 0, s->capacity);
370
361
        s->used = 0;
371
362
}
372
363
 
 
364
void hashtab_free(struct hashtable *s)
 
365
{
 
366
        hashtab_clear(s);
 
367
        free(s->htable);
 
368
        s->htable = NULL;
 
369
        s->capacity = 0;
 
370
}
373
371
 
374
372
int hashtab_store(const struct hashtable *s,FILE* out)
375
373
{
558
556
        }
559
557
        return j;
560
558
}
561
 
 
562
 
struct uniq *uniq_init(uint32_t count) {
563
 
        struct uniq *U;
564
 
        if(!count) return NULL;
565
 
        count = count <= 256 ? 256 : count + (count*20/100);
566
 
        U = cli_malloc(sizeof(*U));
567
 
        if(!U) return NULL;
568
 
        U->uniques = cli_calloc(count, sizeof(uint32_t));
569
 
        if(!U->uniques) {
570
 
                free(U);
571
 
                return NULL;
572
 
        }
573
 
        U->count = count;
574
 
        return U;
575
 
}
576
 
 
577
 
uint32_t uniq_add(struct uniq *U, const char *key, uint32_t key_len, uint32_t *rhash) {
578
 
        uint32_t h = hash((const unsigned char *)key, key_len, U->count);
579
 
        if(rhash) *rhash = h;
580
 
        return U->uniques[h]++;
581
 
}
582
 
 
583
 
uint32_t uniq_get(struct uniq *U, const char *key, uint32_t key_len, uint32_t *rhash) {
584
 
        uint32_t h = hash((const unsigned char *)key, key_len, U->count);
585
 
        if(rhash) *rhash = h;
586
 
        return U->uniques[h];
587
 
}
588