~ubuntu-branches/ubuntu/dapper/postfix/dapper-security

« back to all changes in this revision

Viewing changes to src/global/is_header.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      is_header 3
 
4
/* SUMMARY
 
5
/*      message header classification
 
6
/* SYNOPSIS
 
7
/*      #include <is_header.h>
 
8
/*
 
9
/*      int     is_header(string)
 
10
/*      const char *string;
 
11
/* DESCRIPTION
 
12
/*      is_header() examines the given string and returns non-zero (true)
 
13
/*      when it begins with a mail header name + optional space + colon.
 
14
/*      The result is the length of the mail header name.
 
15
/* STANDARDS
 
16
/*      RFC 822 (ARPA Internet Text Messages)
 
17
/* LICENSE
 
18
/* .ad
 
19
/* .fi
 
20
/*      The Secure Mailer license must be distributed with this software.
 
21
/* AUTHOR(S)
 
22
/*      Wietse Venema
 
23
/*      IBM T.J. Watson Research
 
24
/*      P.O. Box 704
 
25
/*      Yorktown Heights, NY 10598, USA
 
26
/*--*/
 
27
 
 
28
/* System library. */
 
29
 
 
30
#include "sys_defs.h"
 
31
#include <ctype.h>
 
32
 
 
33
/* Global library. */
 
34
 
 
35
#include "is_header.h"
 
36
 
 
37
/* is_header - determine if this can be a header line */
 
38
 
 
39
int     is_header(const char *str)
 
40
{
 
41
    const unsigned char *cp;
 
42
    int     state;
 
43
    int     c;
 
44
    int     len;
 
45
 
 
46
#define INIT            0
 
47
#define IN_CHAR         1
 
48
#define IN_CHAR_SPACE   2
 
49
#define CU_CHAR_PTR(x)  ((const unsigned char *) (x))
 
50
 
 
51
    /*
 
52
     * XXX RFC 2822 Section 4.5, Obsolete header fields: whitespace may
 
53
     * appear between header label and ":" (see: RFC 822, Section 3.4.2.).
 
54
     */
 
55
    for (len = 0, state = INIT, cp = CU_CHAR_PTR(str); (c = *cp) != 0; cp++) {
 
56
        switch (c) {
 
57
        default:
 
58
            if (!ISASCII(c) || ISCNTRL(c))
 
59
                return (0);
 
60
            if (state == INIT)
 
61
                state = IN_CHAR;
 
62
            if (state == IN_CHAR) {
 
63
                len++;
 
64
                continue;
 
65
            }
 
66
            return (0);
 
67
        case ' ':
 
68
        case '\t':
 
69
            if (state == IN_CHAR)
 
70
                state = IN_CHAR_SPACE;
 
71
            if (state == IN_CHAR_SPACE)
 
72
                continue;
 
73
            return (0);
 
74
        case ':':
 
75
            return ((state == IN_CHAR || state == IN_CHAR_SPACE) ? len : 0);
 
76
        }
 
77
    }
 
78
    return (0);
 
79
}