-
Notifications
You must be signed in to change notification settings - Fork 1
/
identify.c
executable file
·165 lines (151 loc) · 4.13 KB
/
identify.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "stdxh.h"
#include "identify.h"
unsigned int markmask = 0xffffffff;
unsigned int maskfirstbit = 0;
unsigned int masknbits = 32;
int new_identifiter(char * filename,struct identifier ** hook){
FILE * init;
int num=0,i;
char str[STR_LEN];
char *pch,*preprocess;
struct identifier * ret;
if(init = fopen(filename,"r")){
/*
while(!feof(init)){
fgets(str,STR_LEN,init);
++num;
}
*/
while(1){
fgets(str,STR_LEN,init);
if(feof(init)) break;
++num;
}
fseek(init, 0 , SEEK_SET);
if(num > 0){
ret = (struct identifier *) malloc(sizeof(struct identifier)*num);
}
else{
return 0;
}
for(i=0 ; i<num ;++i){
fgets(str,STR_LEN,init);
pch = strtok(str," \n");
strcpy(ret[i].name,pch);
pch = strtok(NULL," \n");
if(atoi(pch)<3){
fprintf(stderr,"0,1,2 mark value is reserved!\n");
exit(1);
}
ret[i].mark = atoi(pch);
pch = strtok(NULL," \n");
strcpy(ret[i].pattern,pch);
preprocess = pre_process(ret[i].pattern);
if (regcomp(&(ret[i].preg),preprocess,REG_EXTENDED|REG_NEWLINE)!=0){
fprintf(stderr,"Compiling pre_process error!\n");
exit(1);
}
}
*hook = ret;
return num;
}
else{
fprintf(stderr,"Configure file open error!\n");
exit(1);
}
}
/*
int set_identifier(struct identifier * iden,
char *name,
char *pattern,
int mark
// int eflags, int cflags ,
){
if(!(strlen(name)<IDEN_NAME_LEN && strlen(pattern)<IDEN_PATTERN_LEN)){
fprintf(stderr,"The name or pattern of identifier is too long!\n");
return -1;
}
strcpy(iden->name , name);
strcpy(iden->pattern,pattern);
// iden->eflags = eflags;
//iden->cflags = cflags;
iden->mark = mark;
char * preprocessed = pre_process(iden->pattern);
int rc = regcomp(&preg , preprocessed , REG_EXTENDED);
if (rc != 0 ){
fprintf("Error compiling %s -- %s \n",iden->name , iden->pattern);
exit(1);
}
return 0
}
*/
char * pre_process(const char *s){
char * result = (char *)malloc(strlen(s) + 1);
unsigned int sindex = 0, rindex = 0;
while( sindex < strlen(s) ) {
if( sindex + 3 < strlen(s) && s[sindex] == '\\' && s[sindex+1] == 'x' &&
isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) ){
result[rindex] = hex2dec(s[sindex + 2])*16 + hex2dec(s[sindex + 3]);
switch ( result[rindex] ) {
case '$':
case '(':
case ')':
case '*':
case '+':
case '.':
case '?':
case '[':
case ']':
case '^':
case '|':
case '{':
case '}':
case '\\':
fprintf(stderr,"Warning: regexp contains a regexp control character");
break;
case '\0':
fprintf(stderr,"Warning: null (\\x00) in layer7 regexp. \
A null terminates the regexp string!\n");
break;
default:
break;
}
sindex += 3; /* 4 total */
}
else
result[rindex] = s[sindex];
sindex++;
rindex++;
}
result[rindex] = '\0';
return result;
}
int hex2dec(char c){
switch (c){
case '0' ... '9':
return c - '0';
case 'a' ... 'f':
return c - 'a' + 10;
case 'A' ... 'F':
return c - 'A' + 10;
default:
fprintf(stderr,"Bad regluar pattern encounter!\n");
exit(1);
}
return 0;
}
int iden_match(regex_t * pat , const char * buffer){
int ret = regexec(pat,buffer,0,NULL,0);
if(ret == 0 ) return 0;
else return -1;
}
int classify(struct identifier *iden , int iden_num,const char *data ){
int i;
//printf("%s\n",data);
for (i=0;i<iden_num;++i){
if( iden_match(&(iden[i].preg),data)==0){
return iden[i].mark;
}
}
return NO_MATCH_YET;
}