-
Notifications
You must be signed in to change notification settings - Fork 4
/
tokens.c
128 lines (109 loc) · 2.95 KB
/
tokens.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
/*
* This file is part of libESMTP, a library for submission of RFC 2822
* formatted electronic mail messages using the SMTP protocol described
* in RFC 2821.
*
* Copyright (C) 2001,2002 Brian Stafford <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "tokens.h"
#define ATOMEXCLUDE "\"()<>[]@,;:\\."
#define XTEXTEXCLUDE " +="
#define WHITESPACE " \t\r\n\v"
#define SPACE 1
#define GRAPH 2
#define ATOM 4
#define XTEXT 8
static char atomchars[256];
#define is_atom(c) (atomchars[(int) (c)] & ATOM)
#define is_xtext(c) (atomchars[(int) (c)] & XTEXT)
#define is_space(c) (atomchars[(int) (c)] & SPACE)
#define initatom() do { if (!is_space (' ')) _initatom (); } while (0)
static void
_initatom (void)
{
const char *p;
int i;
/* Mark characters in the printable ASCII range */
for (i = ' ' + 1; i <= '~'; i++)
atomchars[i] |= (ATOM | GRAPH | XTEXT);
/* Turn off the ATOM flag on characters that are excluded */
for (p = ATOMEXCLUDE; *p != '\0'; p++)
atomchars[(int) *p] &= ~ATOM;
/* Turn off the XTEXT flag on printable characters that should be encoded */
for (p = XTEXTEXCLUDE; *p != '\0'; p++)
atomchars[(int) *p] &= ~XTEXT;
/* Mark space characters */
for (p = WHITESPACE; *p != '\0'; p++)
atomchars[(int) *p] |= SPACE;
}
const char *
skipblank (const char *s)
{
initatom ();
while (is_space (*s))
s++;
return s;
}
int
read_atom (const char *s, const char **es, char buf[], int len)
{
char *t;
initatom ();
if (!is_atom (*s))
return 0;
t = buf;
do
{
if (t < buf + len - 1)
*t++ = *s;
s++;
}
while (is_atom (*s));
*t = '\0';
if (es != NULL)
*es = s;
return t - buf;
}
static char xdigits[] = "0123456789ABCDEF";
/* Return a pointer to an xtext encoded string.
*/
char *
encode_xtext (char buf[], int len, const char *string)
{
const unsigned char *s;
char *t;
for (s = (const unsigned char *) string, t = buf; *s != '\0'; s++, t++)
{
if (t - buf > len - 1)
return NULL;
if (is_xtext (*s))
*t = *s;
else
{
*t++ = '+';
*t++ = xdigits[(*s & 0xF0) >> 4];
*t = xdigits[*s & 0x0F];
}
}
*t = '\0';
return buf;
}