-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathisspam.c
71 lines (60 loc) · 1.64 KB
/
isspam.c
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include "eprintf.h"
#define NPAT 6
#define NSTART 1024
char *pat[NPAT]; // patterns
int patlen[NPAT]; // length of pattern
int starting[UCHAR_MAX + 1][NSTART]; // pats starting with char
int nstarting[UCHAR_MAX + 1]; // number of such patterns
void buildStructures()
{
int i;
unsigned char c;
for (i = 0; i < NPAT; i++)
{
c = pat[i][0];
if (nstarting[c] >= NSTART)
eprintf("too many patterns (>=%d) begin '%c'", NSTART, c);
starting[c][nstarting[c]++] = i;
patlen[i] = strlen(pat[i]);
}
}
// isspam: test mesg for occurrence of any pat
int isspam(char *mesg)
{
int i, j, k;
unsigned char c;
for (j = 0; (c = mesg[j]) != '\0'; j++)
{
for (i = 0; i < nstarting[c]; i++)
{
k = starting[c][i];
if (memcmp(mesg + j, pat[k], patlen[k]) == 0)
{
printf("spam: match for '%s'\n", pat[k]);
return 1;
}
}
}
return 0;
}
int main()
{
pat[0] = "buy!";
pat[1] = "big bucks";
pat[2] = "best pictures";
pat[3] = "pretty girls";
pat[4] = "beautiful woman";
pat[5] = "big boob";
buildStructures();
char *mesg = "buy! now!";
printf("'%s' is spam? %s\n", mesg, isspam(mesg) == 1 ? "yes" : "no");
mesg = "there are lots of pRetty girls, come on!";
printf("'%s' is spam? %s\n", mesg, isspam(mesg) == 1 ? "yes" : "no");
mesg = "TOP secret";
printf("'%s' is spam? %s\n", mesg, isspam(mesg) == 1 ? "yes" : "no");
return 0;
}