-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.c
552 lines (450 loc) · 12.4 KB
/
header.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* $Id$ */
/* Routines to read & munge mail & news headers.
A header starts with a non space character in the first column.
Continuation lines start with a space character.
The first line of a message must not be a continuation line.
The headers are seprated from the body of the message with a space
line.
Most of these routines all work with 'header counts' and 'header
vectors' (headc, headv). These are similar to argument counts and
argument vectors (argc, argv).
Routines are:
int head_parse(headc, headv, fp)
Read header lines and stick them into a header
vector.
void head_norm(line)
Normalize a header line by removing
continuation lines, and extra blanks.
void head_blank(line)
Make sure that the first space character in a
header line is a blank (it could also be a tab
or newline or something). Do not need to do
this if head_norm() has been called.
char * head_find(headc, headv, header)
Find and return a pointer to the indicated
header in the header vector.
char * head_delete(headc, headv, header)
Delete and return a pointer to the indicated
header in the header vector.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sysexits.h>
#include "util.h"
#include "config.h"
#include "memory.h"
extern void logandexit();
/* Run through a message grabbing all of the header lines. Stop at the
end of the headers.
Note: This routine malloc's space for each header line.
This routine returns the number of header lines that it finds.
Note: The caller of this routine is responsible for calling it with
an (empty) head vector that is large enough for all of the expected
headers.
Bug: If the first line of the message is not a header, but starts
with a space character, then that line is LOST!
Bug: If there are more header lines than there is space in the header
vector, then the first header line that can NOT fit in the header
vector is LOST!
Bug: If a header is longer than MAXHEADERLEN, than the rest of it
is LOST!
Makes sure that all continuation lines start with a tab.
Deletes the final newline ('\n') from the end of all header lines.
*/
int head_parse(headc, headv, fp)
int headc; /* Number of spaces for headers in the headv. */
char ** headv; /* Header vector, filled out and returned. */
FILE * fp; /* File pointer to the start of the message. */
{
char head_line[MAXHEADERLEN+1];/* Current header line. */
char line[MAXHEADERLEN+1]; /* Current line. */
register int head_no; /* Number of headers so far. */
register int head_len; /* Length of the current header. */
/* Make sure that we were passed a place to put some header
lines.
*/
if ((headc <= 0) || (headv == (char **)NULL))
{
logandexit(EX_UNAVAILABLE, "head_parse: Null args");
}
/* Make sure the header vector is empty.
*/
for (head_no=0; head_no < headc; head_no++)
{
headv[head_no] = (char *)NULL;
}
/* We start off with the first header and the first character
of that header.
*/
head_no = 0;
head_len = 0;
/* Our loop reading lines & dealing with them. We break out of
this loop if we run out of places to put more header lines
(headv is not long enough), if we get a header line longer
than MAXHEADERLEN, if we reach the end of the file, or if
we reach the end of the header lines.
*/
while (1)
{
/* Read a line.
*/
if (fgets(line, MAXHEADERLEN, fp) == NULL)
{
/* We are at the end of the headers.
*/
break;
}
/* Check to see if this might be either a continuation
line or a blank line.
*/
if (isascii(*line) && isspace(*line))
{
/* The first header must not be a continuation
line.
*/
if ((head_no <= 0) && (head_len <= 0))
{
/* We are at the end of the headers.
*/
break;
}
/* According to RFC822, WHITESPACE CRLF sequnce
* is just a continuation line. So, Just ignore it.
* Also, RFC822 do not care about Form Feed.
*/
if (strspn(line, "\n\r") == strlen(line)) {
break; /* this is end of line */
}
if (strspn(line, " \t\n\r") == strlen(line)) {
continue; /* just ignore and continue */
}
/* This is a continuation line. If any of it
will fit into the current header line, add
it. Make sure that it starts with a tab.
Add as much as will fit to the current
header line. Toss anything beyond that.
*/
/* We do not think it is good idea to replace
* leading character to TAB. Thus, commented out.
* We try to avoid to touch header.
*/
if (head_len < MAXHEADERLEN)
{
/* *line = '\t'; */
(void)strncat(&head_line[head_len], line,
MAXHEADERLEN - head_len);
head_len += min(MAXHEADERLEN - head_len,
strlen(line));
}
else
{
}
}
else
{
/* This is a new header line.
*/
/* Save the old one, if any.
*/
if (head_len > 0)
{
/* Make sure that the header line does
not end with a newline. Make sure
that the header line does end with a
NUL.
*/
if (head_line[head_len - 1] == '\n')
{
head_len--;
}
head_line[head_len] = '\0';
/* Grab some space for it.
*/
headv[head_no] = xmalloc((unsigned)head_len+1);
/* Copy the header line into this space.
*/
(void)strcpy(headv[head_no], head_line);
/* We are done with this header line.
*/
head_len = 0;
/* Onto the next header line. If we
are out of space in the header
vector, then quit.
*/
if (++head_no >= headc)
{
/* We are at the end of the
headers.
*/
break;
}
}
/* Save this new header line.
*/
(void)strcpy(head_line, line);
head_len = strlen(line);
}
} /* End of while(1). */
/* Save the last header line, if any.
*/
if (head_len > 0)
{
/* Make sure that the header line does not end with a
newline. Make sure that the header line does end
with a NUL.
*/
if (head_line[head_len - 1] == '\n')
{
head_len--;
}
head_line[head_len] = '\0';
/* Grab some space for it.
*/
headv[head_no] = xmalloc((unsigned)head_len+1);
/* Copy the header line into this space.
*/
(void)strcpy(headv[head_no], head_line);
head_no++;
}
/* Return the number of header lines that we found.
*/
return (head_no);
}
/* This routine 'normalizes' header lines - it gets rid of continuation
lines, and makes sure that the only space characters are single
blanks.
Note: This overwrites the line!!
Note: It is NOT very efficient! If the line needs NOT to be
changed, it still copies it on top of itself.
Makes sure that the end of the header line is not a space character.
*/
void head_norm(line)
register char * line;
{
register char * wr_ptr; /* Write pointer in the line. */
/* Sanity check - we were not passed a NULL pointer.
*/
if ((line == (char *)NULL) || (*line == '\0'))
{
return;
}
/* Run down the line changing all space characters to a single
blank.
Note: we use 'line' as the read pointer.
*/
wr_ptr = line;
while (*line != '\0')
{
/* Replace all strings of space characters with one
blank.
*/
if (isascii(*line) && isspace(*line))
{
/* Put in one blank.
*/
*wr_ptr++ = ' ';
/* Skip past this character and all other space
characters.
Note: We do not need to check for NUL here
since NUL is not a space character.
*/
line++;
while (isascii(*line) && isspace(*line))
{
line++;
}
}
/* Copy the character that we now have and move up
one. Do not copy the NUL.
*/
if (*line != '\0')
{
*wr_ptr++ = *line++;
}
}
/* Stick a NUL at the end.
*/
*wr_ptr = '\0';
/* If the last character is a blank, get rid of it.
*/
if (*--wr_ptr == ' ')
{
*wr_ptr = '\0';
}
/* All done.
*/
return;
}
/* This routine makes sure that the first space charater in a header
line is a blank. This does not need to be called if head_norm() has
already been run on the line.
This is usefull if you want to search for the "From: " lines, and do
not want to normalize the header lines, but do not want to worry
about having the first space character be anything other than a
blank.
*/
void head_blank(line)
register char * line;
{
/* Sanity check - we were not passed a NULL pointer.
*/
if ((line == (char *)NULL) || (*line == '\0'))
{
return;
}
/* Run down the line until the end.
*/
while (*line != '\0')
{
/* If this character is a space character, replace it
with a blank and quit.
*/
if (isascii(*line) && isspace(*line))
{
/* Replace it with a blank.
*/
*line = ' ';
/* All done.
*/
break;
}
/* On to the next character in the line.
*/
line++;
}
/* All done.
*/
return;
}
/* This routine finds and returns a pointer to the indicated header in
a header vector.
This is similar to getenv(3) but it finds headers in the header
vector instead of environment variables in the environment.
If there is no such header, this routine returns NULL.
It finds the first such header only.
Note: If you are looking for a header such as "Subject: ", the given
header string must be exactly that: "Subject: ".
The routine actually matches the passed string with the first part
of each header line, and returns to first such header line.
It would actually be possible to look for "Subject: Re: ", even
though this is not (properly speaking) a real header.
Note: "Subject: " WILL NOT MATCH "Subject:\t". It is probably
useful to run all of the headers through head_norm() before using
this routine.
*/
char * head_find(headc, headv, header)
int headc; /* Number of headers in the header vector. */
register char ** headv; /* Header vector. */
char * header; /* Header to look for. */
{
/* Make sure that we were passed some header lines and that we
were passed a header to look for.
*/
if ((headc <= 0) || (headv == (char **)NULL)
|| (header == (char *)NULL) || (*header == '\0'))
{
return ((char *)NULL);
}
/* Run down the header vector looking for the first header that
matches.
*/
while (headc-- > 0)
{
/* If this header does not exist, skip it.
*/
if ((*headv == (char *)NULL) || (**headv == '\0'))
{
headv++;
continue;
}
/* Check this header against our target.
*/
if (strncasecmp(*headv, header, strlen(header)) == 0)
{
/* We found it!
*/
return (*headv);
}
/* On to the next header.
*/
headv++;
}
/* We did not find it.
*/
return ((char *)NULL);
}
/* This routine deletes the indicated header in a header vector. It
returns a pointer to the deleted header.
If there is no such header, this routine returns NULL.
It finds the first such header only.
Note: If you are looking for a header such as "Subject: ", the given
header string must be exactly that: "Subject: ".
The routine actually matches the passed string with the first part
of each header line, and returns to first such header line.
It would actually be possible to look for "Subject: Re: ", even
though this is not (properly speaking) a real header.
Note: "Subject: " WILL NOT MATCH "Subject:\t". It is probably
useful to run all of the headers through head_norm() before using
this routine.
*/
char * head_delete(headc, headv, header)
int headc; /* Number of headers in the header vector. */
register char ** headv; /* Header vector. */
char * header; /* Header to delete. */
{
/* Make sure that we were passed some header lines and that we
were passed a header to look for.
*/
if ((headc <= 0) || (headv == (char **)NULL)
|| (header == (char *)NULL) || (*header == '\0'))
{
return ((char *)NULL);
}
/* Run down the header vector looking for the first header that
matches.
*/
while (headc-- > 0)
{
/* If this header does not exist, skip it.
*/
if ((*headv == (char *)NULL) || (**headv == '\0'))
{
/* On to the next header.
*/
headv++;
continue;
}
/* Check this header against our target.
*/
if (strncasecmp(*headv, header, strlen(header)) == 0)
{
/* We found it! Remove it from the list &
return a pointer to it.
*/
header = *headv;
*headv = (char *)NULL;
return (header);
}
/* On to the next header.
*/
headv++;
}
/* We did not find it.
*/
return ((char *)NULL);
}
/* free whole header vector
*/
void head_free(headc, headv)
register int headc;
register char ** headv;
{
register int i;
for(i=0; i<headc; i++) {
free(headv[i]);
headv[i] = NULL;
}
}