~yolanda.robla/ubuntu/saucy/clamav/dep-8-tests

« back to all changes in this revision

Viewing changes to libclamav/uniq.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:
 
1
/*
 
2
 *  md5 based hashtab
 
3
 *
 
4
 *  Copyright (C) 2008 Sourcefire, Inc.
 
5
 *
 
6
 *  Authors: aCaB <acab@clamav.net>
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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,
 
20
 *  MA 02110-1301, USA.
 
21
 */
 
22
 
 
23
#if HAVE_CONFIG_H
 
24
#include "clamav-config.h"
 
25
#endif
 
26
 
 
27
#include <stdlib.h>
 
28
#if HAVE_STRING_H
 
29
#include <string.h>
 
30
#endif
 
31
 
 
32
#include "uniq.h"
 
33
#include "others.h"
 
34
#include "md5.h"
 
35
 
 
36
struct uniq *uniq_init(uint32_t count) {
 
37
  struct uniq *U;
 
38
 
 
39
  if(!count) return NULL;
 
40
  U = cli_calloc(1, sizeof(*U));
 
41
  if(!U) return NULL;
 
42
 
 
43
  U->md5s = cli_malloc(count * sizeof(*U->md5s));
 
44
  if(!U->md5s) {
 
45
    uniq_free(U);
 
46
    return NULL;
 
47
  }
 
48
 
 
49
  return U;
 
50
}
 
51
 
 
52
void uniq_free(struct uniq *U) {
 
53
  free(U->md5s);
 
54
  free(U);
 
55
}
 
56
 
 
57
uint32_t uniq_add(struct uniq *U, const char *key, uint32_t key_len, char **rhash) {
 
58
  unsigned int i;
 
59
  uint8_t digest[16];
 
60
  cli_md5_ctx md5;
 
61
  struct UNIQMD5 *m = NULL;
 
62
 
 
63
  cli_md5_init(&md5);
 
64
  cli_md5_update(&md5, key, key_len);
 
65
  cli_md5_final(digest, &md5);
 
66
 
 
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;
 
70
  
 
71
  if(!m) {
 
72
    const char HEX[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
73
 
 
74
    m = &U->md5s[U->items];
 
75
    m->count = 0;
 
76
 
 
77
    if(U->items && U->md5s[U->idx[*digest]].md5[0]==*digest)
 
78
      m->next = &U->md5s[U->idx[*digest]];
 
79
    else
 
80
      m->next = NULL;
 
81
 
 
82
    U->idx[*digest]=U->items;
 
83
 
 
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];
 
88
    }
 
89
    m->name[32] = '\0';
 
90
  }
 
91
 
 
92
  U->items++;
 
93
  if(rhash) *rhash = m->name;
 
94
  return m->count++;
 
95
}
 
96
 
 
97
uint32_t uniq_get(struct uniq *U, const char *key, uint32_t key_len, char **rhash) {
 
98
  uint8_t digest[16];
 
99
  cli_md5_ctx md5;
 
100
  struct UNIQMD5 *m = NULL;
 
101
 
 
102
  cli_md5_init(&md5);
 
103
  cli_md5_update(&md5, key, key_len);
 
104
  cli_md5_final(digest, &md5);
 
105
 
 
106
  if(!U->items || U->md5s[U->idx[*digest]].md5[0]!=*digest)
 
107
    return 0;
 
108
 
 
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;
 
112
    return m->count;
 
113
  }
 
114
 
 
115
  return 0;
 
116
}