File: diction.c

package info (click to toggle)
dspam 3.10.1+dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,656 kB
  • sloc: ansic: 26,034; sh: 12,546; perl: 5,469; makefile: 690; sql: 379
file content (334 lines) | stat: -rw-r--r-- 7,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* $Id: diction.c,v 1.16 2011/06/28 00:13:48 sbajic Exp $ */

/*
 DSPAM
 COPYRIGHT (C) 2002-2011 DSPAM PROJECT

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
 *  diction.c - subset of lexical data
 *
 *  DESCRIPTION
 *    a diction is a subset of lexical data from a user's dictionary. in the
 *    context used within DSPAM, a diction is all of the matching lexical
 *    information from the current message being processed. the diction is
 *    loaded/stored by the storage driver and managed primarily by libdspam.
 */

#ifdef HAVE_CONFIG_H
#include <auto-config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "diction.h"

static unsigned long _ds_prime_list[] = {
  53ul, 97ul, 193ul, 389ul, 769ul,
  1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
  49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
  1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
  50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
  1610612741ul, 3221225473ul, 4294967291ul
};

ds_diction_t
ds_diction_create (unsigned long size)
{
  ds_diction_t diction = (ds_diction_t) calloc(1, sizeof(struct _ds_diction));
  int i = 0;

  if (!diction) {
    perror("ds_diction_create: calloc() failed");
    return NULL;
  }

  while (_ds_prime_list[i] < size) 
    { i++; }

  diction->size = _ds_prime_list[i];
  diction->items = 0;
  diction->tbl =
    (struct _ds_term **) calloc(diction->size, sizeof (struct _ds_term *));
  if (!diction->tbl)
  {
    perror("ds_diction_create: calloc() failed");
    free(diction);
    return NULL;
  }

  diction->order = nt_create(NT_INDEX);
  diction->chained_order = nt_create(NT_INDEX);
  if (!diction->order || !diction->chained_order) {
    nt_destroy(diction->order);
    nt_destroy(diction->chained_order);
    free(diction->tbl);
    free(diction);
    return NULL;
  }

  return diction;
}

void
ds_diction_destroy (ds_diction_t diction)
{
  ds_term_t term, next;
  ds_cursor_t cur;

  if (!diction) return;

  cur = ds_diction_cursor(diction);
  if (!cur) {
    perror("ds_diction_destroy: ds_diction_cursor() failed");
    return;
  } 

  term = ds_diction_next(cur);
  while(term)
  {
    next = ds_diction_next(cur);
    ds_diction_delete(diction, term->key);
    term = next;
  }
  ds_diction_close(cur);

  nt_destroy(diction->order);
  nt_destroy(diction->chained_order);
  free(diction->tbl);
  free(diction); 
  return;
}

ds_term_t
ds_diction_term_create (ds_key_t key, const char *name)
{
  ds_term_t term = (ds_term_t) calloc(1, sizeof(struct _ds_term));

  if (!term) {
    perror("ds_diction_term_create: calloc() failed");
  } else {
    term->key = key;
    term->frequency = 1;
    term->type = 'D';
    if (name)
      term->name = strdup(name);
  }
  return term;
}

ds_term_t
ds_diction_find (ds_diction_t diction, ds_key_t key)
{
  ds_term_t term;

  term = diction->tbl[key % diction->size];
  while (term)
  {
    if (key == term->key)
      return term;
    term = term->next;
  }

  return NULL;
}

ds_term_t
ds_diction_touch(
  ds_diction_t diction, 
  ds_key_t key, 
  const char *name, 
  int flags)
{
  unsigned long bucket = key % diction->size;
  ds_term_t parent = NULL;
  ds_term_t insert = NULL;
  ds_term_t term;

  term = diction->tbl[bucket];
  while (term) {
    if (key == term->key) {
      insert = term;
      break;
    }
    parent = term;
    term = term->next;
  }

  if (!insert) {
    insert = ds_diction_term_create(key, name);
    if (!insert) {
      perror("ds_diction_touch: ds_diction_term_create() failed");
      return NULL;
    }
    diction->items++;
    if (parent) 
      parent->next = insert;
    else
      diction->tbl[bucket] = insert;
  } else {
    if (!insert->name && name) 
      insert->name = strdup(name);
    insert->frequency++;
  }

  if (flags & DSD_CONTEXT) {
    if (flags & DSD_CHAINED)
      nt_add(diction->chained_order, insert);
    else
      nt_add(diction->order, insert);
  }

  return insert;
}

void
ds_diction_delete(ds_diction_t diction, ds_key_t key)
{
  unsigned long bucket = key % diction->size;
  ds_term_t parent = NULL;
  ds_term_t delete = NULL;
  ds_term_t term;

  term = diction->tbl[bucket];

  while(term) {
    if (key == term->key) {
      delete = term;
      break;
    }
    parent = term;
    term = term->next;
  }

  if (delete) {
    if (parent)
      parent->next = delete->next;
    else
      diction->tbl[bucket] = delete->next;

    free(delete->name);
    free(delete);
    diction->items--;
  }
  return;
}

ds_cursor_t
ds_diction_cursor (ds_diction_t diction)
{
  ds_cursor_t cur = (ds_cursor_t) calloc(1, sizeof(struct _ds_diction_c));

  if (!cur) {
    perror("ds_diction_cursor: calloc() failed");
    return NULL;
  }
  cur->diction    = diction;
  cur->iter_index = 0;
  cur->iter_next  = NULL;
  return cur;
}

ds_term_t
ds_diction_next (ds_cursor_t cur)
{
  unsigned long bucket;
  ds_term_t term;
  ds_term_t tbl_term;

  if (!cur)
    return NULL;

  term = cur->iter_next;
  if (term) {
    cur->iter_next = term->next;
    return term;
  }

  while (cur->iter_index < cur->diction->size) {
    bucket = cur->iter_index;
    cur->iter_index++;
    tbl_term = cur->diction->tbl[bucket];
    if (tbl_term) {
      cur->iter_next = tbl_term->next;
      return (tbl_term);
    }
  }

  return NULL;
}

void
ds_diction_close (ds_cursor_t cur)
{
  free(cur);
  return;
}

int
ds_diction_setstat (ds_diction_t diction, ds_key_t key, ds_spam_stat_t s)
{
  ds_term_t term = ds_diction_find(diction, key);

  if (term) {
    term->s.probability = s->probability;
    term->s.spam_hits = s->spam_hits;
    term->s.innocent_hits = s->innocent_hits;
    term->s.status = s->status;
    term->s.offset = s->offset;
    return 0;
  }
  return -1;
}

int ds_diction_addstat (ds_diction_t diction, ds_key_t key, ds_spam_stat_t s)
{
  ds_term_t term = ds_diction_find(diction, key);

  if (term) {
    term->s.probability += s->probability;
    term->s.spam_hits += s->spam_hits;
    term->s.innocent_hits += s->innocent_hits;
    if (!term->s.offset)
      term->s.offset = s->offset;
    if (s->status & TST_DISK)
      term->s.status |= TST_DISK;
    if (s->status & TST_DIRTY)
      term->s.status |= TST_DIRTY;
    return 0;
  }
  return -1;
}

int
ds_diction_getstat  (ds_diction_t diction, ds_key_t key, ds_spam_stat_t s)
{
  ds_term_t term = ds_diction_find(diction, key);

  if (term) {
    s->probability = term->s.probability;
    s->spam_hits = term->s.spam_hits;
    s->innocent_hits = term->s.innocent_hits;
    s->status = term->s.status;
    s->offset = term->s.offset;
    return 0;
  }
  return -1;
}