-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectfp.c
executable file
·147 lines (107 loc) · 4.09 KB
/
detectfp.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int detectfp(char ttype, char *fpdatapath) {
FILE *fpdatafp;
char *temp, fpline[MAXLINELENGTH];
Word_t *Rolecount=0, *attrcount=0, *Cmdcount=0, *columnval, *value;
short attrid, columnindex=0, i;
uint8_t Roleindex[3], *attr, *attrindex, *attridbuf, *Cmdindex, *numcolumns, *columnindexbuf, *Cmdval, *QueryRoleId;
char index[3];
int numfp=0, maxcount, maxprobrole;
double ln_postprob=0.0, ln_maxprob;
if ((fpdatafp = fopen(fpdatapath, "r")) == NULL) {
printf("\n Error opening false positives data file = %s \n", fpdatapath);
return -1;
}
/*Browse through the false positives data line by line*/
while(fgets(fpline, MAXLINELENGTH, fpdatafp) != NULL) {
numfprecords++;
maxcount = 0;
ln_postprob = 0.0;
/* do this for every role */
strcpy(Roleindex, "");
JSLF(Rolecount, Rolecountarray, Roleindex);
while (Rolecount != NULL) {
temp = strdup(fpline);
/* Get current Role id */
QueryRoleId = (char *)strtok(temp, delimtab);
ln_postprob = log((double)(*Rolecount)/numtrngrecords);
if (ttype == 'c' || ttype == 's') {
attrid = 0; /* Reinitialise attribute index */
while ((attr = (char *)strtok(NULL, delimtab)) != NULL) {
attrindex = (char *)malloc(ATTRINDEXSIZE_cs*sizeof(char));
attridbuf = (char *)malloc(10*sizeof(char));
/* Form the attribute index */
strcpy(attrindex, Roleindex);
strcat(attrindex, "_");
sprintf(attridbuf, "%d", attrid++);
strcat(attrindex, attridbuf);
strcat(attrindex, "_");
strcat(attrindex, attr);
JSLG(attrcount, Attrcountarray, attrindex);
if (attrcount != NULL) {
ln_postprob = ln_postprob + log((double)(*attrcount)/(*Rolecount));
// printf("\n %s count = %d", attrindex, *attrcount);
}
else ln_postprob = ln_postprob + log((double)(1/2*(*Rolecount)));
free(attrindex);
free(attridbuf);
}
}
else {
attrid = 0; /* Reinitialise attribute index */
attridbuf = (char *)malloc(10*sizeof(char));
sprintf(attridbuf, "%d", attrid);
/* Calculate command probability */
Cmdval = (char *)strtok(NULL, delimtab);
Cmdindex = (char *)malloc(ATTRINDEXSIZE_cs*sizeof(char));
strcpy(Cmdindex, Roleindex);
strcat(Cmdindex, "_");
strcat(Cmdindex, attridbuf); /* Command is attribute 0 */
strcat(Cmdindex, "_");
strcat(Cmdindex, Cmdval);
JSLG(Cmdcount, Attrcountarray, Cmdindex);
if (Cmdcount != NULL) ln_postprob = ln_postprob + log((double)(*Cmdcount)/(*Rolecount));
else ln_postprob = ln_postprob + log((double)(1/2*(*Rolecount)));
attrid++; /* Increment attribute counter */
/* Calculate attribute probabilities */
strcpy(index, "");
JSLF(columnval, Columncountarray, index);
while (columnval != NULL) {
attrindex = (char *)malloc(ATTRINDEXSIZE_f*sizeof(char));
attridbuf = (char *)malloc(10*sizeof(char));
strcpy(attrindex, Roleindex);
strcat(attrindex, "_");
sprintf(attridbuf, "%d", attrid++);
strcat(attrindex, attridbuf);
strcat(attrindex, "_");
i = *columnval;
while(i-- > 0) {
attr = (char *)strtok(NULL, delimtab);
strcat(attrindex, attr);
}
JSLI(attrcount, Attrcountarray, attrindex);
if (attrcount != NULL) ln_postprob = ln_postprob + log((double)(*attrcount)/(*Rolecount));
else ln_postprob = ln_postprob + log((double)(1/2*(*Rolecount)));
free(attrindex);
free(attridbuf);
JSLN(columnval, Columncountarray, index);
}
}
/*Code for determining the max prob role*/
// printf("\n ln_postprob = %f", ln_postprob);
if (maxcount++ == 0) {
ln_maxprob = ln_postprob;
maxprobrole = atoi(Roleindex);
}
else if (ln_postprob > ln_maxprob) {
ln_maxprob = ln_postprob;
maxprobrole = atoi(Roleindex);
}
JSLN(Rolecount, Rolecountarray, Roleindex); // get next rolecount
}
// printf("\n Maxprob role id = %d, log prob = %f", maxprobrole, ln_maxprob );
if (maxprobrole != atoi(QueryRoleId)) fpcount++;
}
}