chiark / gitweb /
Upstream qmail 1.01
[qmail] / hfield.c
1 #include "hfield.h"
2
3 static char *(hname[]) = {
4   "unknown-header"
5 , "sender"
6 , "from"
7 , "reply-to"
8 , "to"
9 , "cc"
10 , "bcc"
11 , "date"
12 , "message-id"
13 , "subject"
14 , "resent-sender"
15 , "resent-from"
16 , "resent-reply-to"
17 , "resent-to"
18 , "resent-cc"
19 , "resent-bcc"
20 , "resent-date"
21 , "resent-message-id"
22 , "return-receipt-to"
23 , "errors-to"
24 , "apparently-to"
25 , "received"
26 , "return-path"
27 , "delivered-to"
28 , "content-length"
29 , "content-type"
30 , "content-transfer-encoding"
31 , "notice-requested-upon-delivery-to"
32 , 0
33 };
34
35 static int hmatch(s,len,t)
36 char *s;
37 int len;
38 char *t;
39 {
40  int i;
41  char ch;
42
43  for (i = 0;ch = t[i];++i)
44   {
45    if (i >= len) return 0;
46    if (ch != s[i])
47     {
48      if (ch == '-') return 0;
49      if (ch - 32 != s[i]) return 0;
50     }
51   }
52  for (;;)
53   {
54    if (i >= len) return 0;
55    ch = s[i];
56    if (ch == ':') return 1;
57    if ((ch != ' ') && (ch != '\t')) return 0;
58    ++i;
59   }
60 }
61
62 int hfield_known(s,len)
63 char *s;
64 int len;
65 {
66  int i;
67  char *t;
68
69  for (i = 1;t = hname[i];++i)
70    if (hmatch(s,len,t))
71      return i;
72  return 0;
73 }
74
75 int hfield_valid(s,len)
76 char *s;
77 int len;
78 {
79  int i;
80  int j;
81  char ch;
82
83  for (j = 0;j < len;++j)
84    if (s[j] == ':')
85      break;
86  if (j >= len) return 0;
87  while (j)
88   {
89    ch = s[j - 1];
90    if ((ch != ' ') && (ch != '\t'))
91      break;
92    --j;
93   }
94  if (!j) return 0;
95
96  for (i = 0;i < j;++i)
97   {
98    ch = s[i];
99    if (ch <= 32) return 0;
100    if (ch >= 127) return 0;
101   }
102  return 1;
103 }
104
105 unsigned int hfield_skipname(s,len)
106 char *s;
107 int len;
108 {
109  int i;
110  char ch;
111
112  for (i = 0;i < len;++i)
113    if (s[i] == ':')
114      break;
115  if (i < len) ++i;
116  while (i < len)
117   {
118    ch = s[i];
119    if ((ch != '\t') && (ch != '\n') && (ch != '\r') && (ch != ' '))
120      break;
121    ++i;
122   }
123  return i;
124 }