4
* Copyright (C) 2008 Sourcefire, Inc.
6
* Authors: aCaB <acab@clamav.net>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License version 2 as
10
* published by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24
#include "clamav-config.h"
36
struct uniq *uniq_init(uint32_t count) {
39
if(!count) return NULL;
40
U = cli_calloc(1, sizeof(*U));
43
U->md5s = cli_malloc(count * sizeof(*U->md5s));
52
void uniq_free(struct uniq *U) {
57
uint32_t uniq_add(struct uniq *U, const char *key, uint32_t key_len, char **rhash) {
61
struct UNIQMD5 *m = NULL;
64
cli_md5_update(&md5, key, key_len);
65
cli_md5_final(digest, &md5);
67
if(U->items && U->md5s[U->idx[*digest]].md5[0]==*digest)
68
for(m=&U->md5s[U->idx[*digest]]; m; m=m->next)
69
if(!memcmp(&digest[1], &m->md5[1], 15)) break;
72
const char HEX[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
74
m = &U->md5s[U->items];
77
if(U->items && U->md5s[U->idx[*digest]].md5[0]==*digest)
78
m->next = &U->md5s[U->idx[*digest]];
82
U->idx[*digest]=U->items;
84
for(i = 0; i < 16; i++) {
85
m->name[i*2] = HEX[digest[i]>>4 & 0xf];
86
m->name[i*2+1] = HEX[digest[i] & 0xf];
87
m->md5[i] = digest[i];
93
if(rhash) *rhash = m->name;
97
uint32_t uniq_get(struct uniq *U, const char *key, uint32_t key_len, char **rhash) {
100
struct UNIQMD5 *m = NULL;
103
cli_md5_update(&md5, key, key_len);
104
cli_md5_final(digest, &md5);
106
if(!U->items || U->md5s[U->idx[*digest]].md5[0]!=*digest)
109
for(m=&U->md5s[U->idx[*digest]]; m; m=m->next) {
110
if(memcmp(&digest[1], &m->md5[1], 15)) continue;
111
if(rhash) *rhash = m->name;