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

« back to all changes in this revision

Viewing changes to message-source.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
/*  Functions to read lines or blocks of text from the message source.
 
24
    These functions allow the library to interface to the application
 
25
    using a callback function.  This is intended to allow the application
 
26
    maximum flexibility in managing its message storage.  */
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#include <config.h>
 
30
#endif
 
31
 
 
32
#include <assert.h>
 
33
 
 
34
#include <stdlib.h>
 
35
#include <string.h>
 
36
#include "message-source.h"
 
37
 
 
38
/* This is similar to code in siobuf.c */
 
39
 
 
40
struct msg_source
 
41
  {
 
42
    /* Callback to fill the input buffer */
 
43
    const char *(*cb) (void **ctx, int *len, void *arg);
 
44
    void *arg;
 
45
    void *ctx;
 
46
 
 
47
    /* Input buffer */
 
48
    const char *rp;             /* input buffer pointer */
 
49
    int rn;                     /* number of bytes unread in buffer */
 
50
 
 
51
    /* Output buffer (used by msg_gets()) */
 
52
    char *buf;
 
53
    size_t nalloc;
 
54
  };
 
55
 
 
56
msg_source_t
 
57
msg_source_create (void)
 
58
{
 
59
  msg_source_t source;
 
60
 
 
61
  if ((source = malloc (sizeof (struct msg_source))) != NULL)
 
62
    memset (source, 0, sizeof (struct msg_source));
 
63
  return source;
 
64
}
 
65
 
 
66
void
 
67
msg_source_destroy (msg_source_t source)
 
68
{
 
69
  assert (source != NULL);
 
70
 
 
71
  if (source->ctx != NULL)
 
72
    free (source->ctx);
 
73
  if (source->buf != NULL)
 
74
    free (source->buf);
 
75
  free (source);
 
76
}
 
77
 
 
78
void
 
79
msg_source_set_cb (msg_source_t source,
 
80
                   const char *(*cb) (void **ctx, int *len, void *arg),
 
81
                   void *arg)
 
82
{
 
83
  assert (source != NULL);
 
84
 
 
85
  if (source->ctx != NULL)
 
86
    {
 
87
      free (source->ctx);
 
88
      source->ctx = NULL;
 
89
    }
 
90
  source->cb = cb;
 
91
  source->arg = arg;
 
92
}
 
93
 
 
94
/* Use the callback to get data from the message source.
 
95
 */
 
96
static int
 
97
msg_fill (msg_source_t source)
 
98
{
 
99
  assert (source != NULL && source->cb != NULL);
 
100
 
 
101
  source->rp = (*source->cb) (&source->ctx, &source->rn, source->arg);
 
102
  return source->rn > 0;
 
103
}
 
104
 
 
105
void
 
106
msg_rewind (msg_source_t source)
 
107
{
 
108
  assert (source != NULL && source->cb != NULL);
 
109
 
 
110
  (*source->cb) (&source->ctx, NULL, source->arg);
 
111
}
 
112
 
 
113
/* Line oriented reader.  An output buffer is allocated as required.
 
114
   The return value is a pointer to the line and remains valid until the
 
115
   next call to msg_gets ().  The line is guaranteed to be terminated
 
116
   with a \r\n.  Len is set to the number of octets in the buffer.
 
117
   The line is *not* terminated with a \0.
 
118
 
 
119
   If concatenate is non-zero, the next line of input is concatenated
 
120
   with the existing line and the return value points to the original
 
121
   line in the buffer.  In this case *len is the number of octets in
 
122
   the buffer when called and is updated to the new count on return.
 
123
 */
 
124
const char *
 
125
msg_gets (msg_source_t source, int *len, int concatenate)
 
126
{
 
127
  int lastc, c, buflen;
 
128
  char *p, *nbuf;
 
129
 
 
130
  assert (source != NULL && len != NULL);
 
131
 
 
132
  if (source->rn <= 0 && !msg_fill (source))
 
133
    return NULL;
 
134
 
 
135
  if (source->buf == NULL)
 
136
    {
 
137
      source->nalloc = 1023;    /* RFC 2821 max line length + slack */
 
138
      source->buf = malloc (source->nalloc + 2);
 
139
      if (source->buf == NULL)
 
140
        return NULL;
 
141
    }
 
142
  p = source->buf;
 
143
  buflen = source->nalloc;
 
144
  if (concatenate)
 
145
    {
 
146
      p += *len;
 
147
      buflen -= *len;
 
148
    }
 
149
 
 
150
  lastc = 0;
 
151
  while (source->rn > 0 || msg_fill (source))
 
152
    {
 
153
      c = *source->rp++;
 
154
      source->rn--;
 
155
      if (buflen <= 0)
 
156
        {
 
157
          buflen = 512;
 
158
          source->nalloc += buflen;
 
159
          nbuf = realloc (source->buf, source->nalloc + 2);
 
160
          if (nbuf == NULL)
 
161
            {
 
162
              free (source->buf);
 
163
              return NULL;
 
164
            }
 
165
          p = nbuf + (p - source->buf);
 
166
          source->buf = nbuf;
 
167
        }
 
168
      *p++ = c;
 
169
      buflen--;
 
170
      if (c == '\n' && lastc == '\r')
 
171
        {
 
172
          *len = p - source->buf;
 
173
          return source->buf;
 
174
        }
 
175
      lastc = c;
 
176
    }
 
177
  /* Only get here if the input was not properly terminated with a \r\n.
 
178
     The handling of the DATA command in protocol.c relies on the \n
 
179
     so we add it here.  This is why there is 2 characters of slack in
 
180
     malloc and realloc above. */
 
181
  if (lastc != '\r')
 
182
    *p++ = '\r';
 
183
  *p++ = '\n';
 
184
  *len = p - source->buf;
 
185
  return source->buf;
 
186
}
 
187
 
 
188
/* Return the next character in the source, i.e. the first character
 
189
   that will be returned by the next call to msg_gets().  This is
 
190
   currently only used to check for RFC 2822 header continuation lines.
 
191
   It is not safe to use in conjunction with msg_getb().
 
192
 */
 
193
int
 
194
msg_nextc (msg_source_t source)
 
195
{
 
196
  assert (source != NULL);
 
197
 
 
198
  if (source->rn <= 0 && !msg_fill (source))
 
199
    return -1;
 
200
 
 
201
  return *source->rp;
 
202
}
 
203
 
 
204
/* Block oriented reader.  The output buffer is not used for efficiency.
 
205
 */
 
206
const char *
 
207
msg_getb (msg_source_t source, int *len)
 
208
{
 
209
  assert (source != NULL);
 
210
 
 
211
  if (source->rn <= 0 && !msg_fill (source))
 
212
    return NULL;
 
213
 
 
214
  /* Just return whatever is in the input buffer */
 
215
  *len = source->rn;
 
216
  source->rn = 0;
 
217
  return source->rp;
 
218
}