~ubuntu-branches/ubuntu/trusty/libesmtp/trusty-proposed

« back to all changes in this revision

Viewing changes to tokens.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy T. Bouse
  • Date: 2002-03-06 08:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020306083748-ihmt32mddsslvg5i
Tags: upstream-0.8.11
ImportĀ upstreamĀ versionĀ 0.8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of libESMTP, a library for submission of RFC 2822
 
3
 *  formatted electronic mail messages using the SMTP protocol described
 
4
 *  in RFC 2821.
 
5
 *
 
6
 *  Copyright (C) 2001,2002  Brian Stafford  <brian@stafford.uklinux.net>
 
7
 *
 
8
 *  This library is free software; you can redistribute it and/or
 
9
 *  modify it under the terms of the GNU Lesser General Public
 
10
 *  License as published by the Free Software Foundation; either
 
11
 *  version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 *  This library is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 *  Lesser General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU Lesser General Public
 
19
 *  License along with this library; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
 
 
30
#include "tokens.h"
 
31
 
 
32
#define ATOMEXCLUDE     "\"()<>[]@,;:\\."
 
33
#define XTEXTEXCLUDE    " +="
 
34
#define WHITESPACE      " \t\r\n\v"
 
35
 
 
36
#define SPACE           1
 
37
#define GRAPH           2
 
38
#define ATOM            4
 
39
#define XTEXT           8
 
40
 
 
41
static char atomchars[256];
 
42
#define is_atom(c)      (atomchars[(int) (c)] & ATOM)
 
43
#define is_xtext(c)     (atomchars[(int) (c)] & XTEXT)
 
44
#define is_space(c)     (atomchars[(int) (c)] & SPACE)
 
45
 
 
46
#define initatom()      do { if (!is_space (' ')) _initatom (); } while (0) 
 
47
 
 
48
static void
 
49
_initatom (void)
 
50
{
 
51
  const char *p;
 
52
  int i;
 
53
 
 
54
  /* Mark characters in the printable ASCII range */
 
55
  for (i = ' ' + 1; i <= '~'; i++)
 
56
    atomchars[i] |= (ATOM | GRAPH | XTEXT);
 
57
 
 
58
  /* Turn off the ATOM flag on characters that are excluded */
 
59
  for (p = ATOMEXCLUDE; *p != '\0'; p++)
 
60
    atomchars[(int) *p] &= ~ATOM;
 
61
 
 
62
  /* Turn off the XTEXT flag on printable characters that should be encoded */
 
63
  for (p = XTEXTEXCLUDE; *p != '\0'; p++)
 
64
    atomchars[(int) *p] &= ~XTEXT;
 
65
 
 
66
  /* Mark space characters */
 
67
  for (p = WHITESPACE; *p != '\0'; p++)
 
68
    atomchars[(int) *p] |= SPACE;
 
69
}
 
70
 
 
71
const char *
 
72
skipblank (const char *s)
 
73
{
 
74
  initatom ();
 
75
  while (is_space (*s))
 
76
    s++;
 
77
  return s;
 
78
}
 
79
 
 
80
int
 
81
read_atom (const char *s, const char **es, char buf[], int len)
 
82
{
 
83
  char *t;
 
84
 
 
85
  initatom ();
 
86
  if (!is_atom (*s))
 
87
    return 0;
 
88
  t = buf;
 
89
  do
 
90
    {
 
91
      if (t < buf + len - 1)
 
92
        *t++ = *s;
 
93
      s++;
 
94
    }
 
95
  while (is_atom (*s));
 
96
  *t = '\0';
 
97
  if (es != NULL)
 
98
    *es = s;
 
99
  return t - buf;
 
100
}
 
101
 
 
102
static char xdigits[] = "0123456789ABCDEF";
 
103
 
 
104
/* Return a pointer to an xtext encoded string.
 
105
 */
 
106
char *
 
107
encode_xtext (char buf[], int len, const char *string)
 
108
{
 
109
  const unsigned char *s;
 
110
  char *t;
 
111
 
 
112
  for (s = (const unsigned char *) string, t = buf; *s != '\0'; s++, t++)
 
113
    {
 
114
      if (t - buf > len - 1)
 
115
        return NULL;
 
116
      if (is_xtext (*s))
 
117
        *t = *s;
 
118
      else
 
119
        {
 
120
          *t++ = '+';
 
121
          *t++ = xdigits[(*s & 0xF0) >> 4];
 
122
          *t = xdigits[*s & 0x0F];
 
123
        }
 
124
    }
 
125
  *t = '\0';
 
126
  return buf;
 
127
}
 
128