-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageheader.c
206 lines (152 loc) · 3.15 KB
/
messageheader.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
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "messageheader.h"
#include "common.h"
int mh_errcode = MH_SUCCESS;
static int iswhitespace(char a)
{
// LWSP-char according to RFC 822
return (a == ' ' || a == '\t');
}
static int iscontrol(char a)
{
// according to RFC 822
return (a >= 0 && a <= 31);
}
static int isspecial(char a)
{
// according to RFC 2045
return (strchr("()<>@,;:\\\"/[]?=", a) != NULL);
}
static char *mh_error(enum MH_ERROR_TYPE et)
{
mh_errcode = et;
return NULL;
}
static char *mh_gettoken(const char *s)
{
static char *t = NULL;
static const char *ss = NULL;
char *prevt;
if (s != NULL) {
ss = s;
t = xrealloc(NULL, 2 * strlen(s) + 1);
}
prevt = t;
while (iswhitespace(*ss))
++ss;
if (*ss == '\0')
return mh_error(MH_EOF);
else if (*ss == '"') {
++ss;
while (*ss != '\"') {
if (*ss == '\0')
return mh_error(MH_ENDQUOTE);
// TODO maybe i should detect unquoted specials as error
// TODO does \0 can be quoted character?
if ((*ss == '\\') && *(++ss) == '\0')
return mh_error(MH_ENDQUOTE);
*(t++) = *(ss++);
}
++ss;
}
else if (isspecial(*ss)) {
*(t++) = *(ss++);
}
else {
while(!isspecial(*ss) && !iswhitespace(*ss))
*(t++) = *(ss++);
}
*(t++) = '\0';
return prevt;
}
static char *mh_gettokenkind(const char *s, char c)
{
char *t;
t = mh_gettoken(s);
if (t == NULL)
return NULL;
if (c == '\0' && isspecial(*t))
return mh_error(MH_WRONGTOKEN);
if (c != '\0' && *t != c)
return mh_error(MH_WRONGTOKEN);
return t;
}
char *mh_getfield()
{
char *fld;
size_t fldsz;
int fldlen;
char *s;
size_t ssz;
char c;
fld = NULL;
if ((fldlen = getline(&fld, &fldsz, stdin)) < 0
|| strcmp("\r\n", fld) == 0) {
free(fld);
return NULL;
}
s = NULL;
ssz = 0;
while ((c = fgetc(stdin), ungetc(c, stdin)) != EOF
&& iswhitespace(c)) {
size_t slen;
slen = getline(&s, &ssz, stdin);
if (strcmp("\r\n", fld + fldlen - 2) == 0)
fldlen -= 2;
fld = realloc(fld, fldsz + slen);
strcpy(fld + fldlen, s);
fldlen += slen;
}
if (strcmp("\r\n", fld + fldlen - 2) == 0)
fld[fldlen - 2] = '\0';
if (s != NULL)
free(s);
return fld;
}
char *mh_fieldbody(char *field)
{
char *body;
if ((body = strchr(field, ':')) == NULL)
return NULL;
*(body++) = '\0';
for (; *field != '\0'; ++field)
if (iscontrol(*field) || *field == ' ' || *field == ':')
return NULL;
return body;
}
char *mh_getcontenttype(const char *s)
{
char *type;
char *t;
if ((type = mh_gettokenkind(s, '\0')) == NULL)
return NULL;
if ((t = mh_gettokenkind(NULL, '/')) == NULL)
return NULL;
strcat(type, t);
if ((t = mh_gettokenkind(NULL, '\0')) == NULL)
return NULL;
strcat(type, t);
return type;
}
char *mh_getdisptype(const char *s)
{
char *type;
if ((type = mh_gettokenkind(s, '\0')) == NULL)
return NULL;
return type;
}
char *mh_getparam(char **val)
{
char *name;
if ((name = mh_gettokenkind(NULL, ';')) == NULL)
return NULL;
if ((name = mh_gettokenkind(NULL, '\0')) == NULL)
return NULL;
if (mh_gettokenkind(NULL, '=') == NULL)
return NULL;
if ((*val = mh_gettokenkind(NULL, '\0')) == NULL)
return NULL;
return name;
}