-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsm.c
More file actions
155 lines (127 loc) · 4.04 KB
/
fsm.c
File metadata and controls
155 lines (127 loc) · 4.04 KB
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
#include <stdlib.h>
#include <locale.h>
#include "fcgi_stdio.h"
//#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <ctype.h>
#include <sys/types.h>
#include <regex.h>
#include "wmail.h"
#include "message.h"
void inline zero_buf(char *buf,unsigned int s,unsigned int e) {
unsigned int i;
for (i = s; i < e; ++i) {
buf[i] = 0x20;
}
}
void inline trunc_buf(char *buf,unsigned int s) {
buf[s] = 0;
}
void inline malform_buf(char *buf,unsigned int s,unsigned int e) {
unsigned int i;
for (i = s; i < e; ++i) {
if (buf[i] == 0x20 || buf[i] == '\t') continue;
buf[i++] = '_';
buf[i] = '_';
break;
}
}
void inline malform_clean_buf(char *buf,unsigned int s,unsigned int e) {
buf[e-3]='X';
buf[e-2]='S';
buf[e-1]='S';
}
void inline zero_tag(char *buf,unsigned int s) {
char *p;
p = &buf[s];
while (*p!= '>') {
*p++ = 0x20;
}
}
enum {
PZERO,
PTRUNC,
PMALFORM,
PMALFORM_CLEAN,
PZEROTAG,
};
#define R_MAX 512
int do_reg(char *buf,char *reg,int atype) {
regex_t preg;
size_t nmatch = 1;
regmatch_t rm[1];
char ebuf[256];
int rv;
static int rec = 0;
if (rec > R_MAX) {
SETERR(E_PARSE,NULL);
return 0;
}
rv = regcomp(&preg,reg,REG_EXTENDED|REG_ICASE);
if (rv) {
regerror(rv,&preg,ebuf,256);
SETERR(E_PARSE,ebuf);
return 0;
}
//printf("test %s<br>",buf);
rv = regexec(&preg,buf, nmatch, rm,REG_NOTBOL);
if (rv == REG_NOMATCH) {
// printf("NOT_MATCHED<br>");
regfree(&preg);
return 0;
} else if (!rv) {
char *ptr;
int s,e;
s = rm[0].rm_so;
e = rm[0].rm_eo;
if (atype == PZERO) zero_buf(buf,s,e);
if (atype == PTRUNC) trunc_buf(buf,s);
if (atype == PMALFORM) {
malform_buf(buf,s,e);
}
if (atype == PMALFORM_CLEAN) {
malform_clean_buf(buf,s,e);
}
if (atype == PZEROTAG) zero_tag(buf,s);
ptr = buf + rm[0].rm_eo;
regfree(&preg);
++rec;
do_reg(ptr,reg,atype);
--rec;
// printf("MATCHED %d %d-%d<br>",nmatch,rm[0].rm_so,rm[0].rm_eo);
return 1;
} else {
regerror(rv,&preg,ebuf,256);
SETERR(E_PARSE,ebuf);
return 0;
}
}
char *do_html_subst(char *buf, int *len) {
char *sbuf;
int plen,tlen;
sbuf = buf;
plen = *len;
//printf("Content-type:text/html\n\n");
do_reg(buf,".*<body[^>]*>",PZERO);
do_reg(buf,"</body>.*",PTRUNC);
do_reg(buf,"<script[^>]*>.*</script>",PZERO);
do_reg(buf,"<style[^>]*>.*</style>",PZERO);
do_reg(buf,"<link[^>]*>",PZERO);
do_reg(buf,"[[:space:]/]+style[[:space:]]*=",PMALFORM);
do_reg(buf,"((�*61;?|�*3D;?|=)|((u|�*85;?|�*55;?|�*117;?|�*75;?)[[:space:]]*(r|�*82;?|�*52;?|�*114;?|�*72;?)[[:space:]]*(l|�*76;?|�*4c;?|�*108;?|�*6c;?)[[:space:]]*(\\()))[[:space:]]*(�*34;?|�*22;?|\"|�*39;?|�*27;?|\')?[^>]*[[:space:]]*(s|�*83;?|�*53;?|�*115;?|�*73;?)[[:space:]]*(c|�*67;?|�*43;?|�*99;?|�*63;?)[[:space:]]*(r|�*82;?|�*52;?|�*114;?|�*72;?)[[:space:]]*(i|�*73;?|�*49;?|�*105;?|�*69;?)[[:space:]]*(p|�*80;?|�*50;?|�*112;?|�*70;?)[[:space:]]*(t|�*84;?|�*54;?|�*116;?|�*74;?)[[:space:]]*(:|�*58;?|�*3a;?)",PZEROTAG);
do_reg(buf,"[[:space:]\"'/](o|�*79;?|�*4f;?|�*111;?|�*6f;?)(n|�*78;?|�*4e;?|�*110;?|�*6e;?)[[:alnum:]]+[[:space:]]*=",PZEROTAG);
do_reg(buf,"<[^>a-z]*s[[:space:]]*c[[:space:]]*r[[:space:]]*i[[:space:]]*p[[:space:]]*t",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*e[[:space:]]*m[[:space:]]*b[[:space:]]*e[[:space:]]*d",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*m[[:space:]]*e[[:space:]]*t[[:space:]]*a",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*x[[:space:]]*m[[:space:]]*l",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*b[[:space:]]*a[[:space:]]*s[[:space:]]*e",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*j[[:space:]]*a[[:space:]]*v[[:space:]]*a",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*o[[:space:]]*b[[:space:]]*j[[:space:]]*e[[:space:]]*c[[:space:]]*t",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*i[[:space:]]*f[[:space:]]*r[[:space:]]*a[[:space:]]*m[[:space:]]*e",PMALFORM_CLEAN);
do_reg(buf,"<[^>a-z]*f[[:space:]]*r[[:space:]]*a[[:space:]]*m[[:space:]]*e",PMALFORM_CLEAN);
tlen = strlen(buf);
*len += (tlen - plen);
return sbuf;
}