aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/nntp/camel-nntp-stream.c
blob: 1e2dcb23f9e45ed53a4a84a78a486dbbebcb45db (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
 *
 * Author:
 *  Michael Zucchi <notzed@ximian.com>
 *
 * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; 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 <errno.h>

#include <string.h>
#include <stdio.h>

#include <glib.h>

#include "camel-nntp-stream.h"

extern int camel_verbose_debug;
#define dd(x) (camel_verbose_debug?(x):0)

static CamelObjectClass *parent_class = NULL;

/* Returns the class for a CamelStream */
#define CS_CLASS(so) CAMEL_NNTP_STREAM_CLASS(CAMEL_OBJECT_GET_CLASS(so))

#define CAMEL_NNTP_STREAM_SIZE (4096)
#define CAMEL_NNTP_STREAM_LINE (1024) /* maximum line size */

static int
stream_fill(CamelNNTPStream *is)
{
    int left = 0;

    if (is->source) {
        left = is->end - is->ptr;
        memcpy(is->buf, is->ptr, left);
        is->end = is->buf + left;
        is->ptr = is->buf;
        left = camel_stream_read(is->source, is->end, CAMEL_NNTP_STREAM_SIZE - (is->end - is->buf));
        if (left > 0) {
            is->end += left;
            is->end[0] = '\n';
            return is->end - is->ptr;
        } else {
            dd(printf("NNTP_STREAM_FILL(ERROR): '%s'\n", strerror(errno)));
            return -1;
        }
    }

    return 0;
}

static ssize_t
stream_read(CamelStream *stream, char *buffer, size_t n)
{
    CamelNNTPStream *is = (CamelNNTPStream *)stream;
    char *o, *oe;
    unsigned char *p, *e, c;
    int state;

    if (is->mode != CAMEL_NNTP_STREAM_DATA || n == 0)
        return 0;

    o = buffer;
    oe = buffer + n;
    state = is->state;

    /* Need to copy/strip '.'s and whatnot */
    p = is->ptr;
    e = is->end;

    switch(state) {
    state_0:
    case 0:     /* start of line, always read at least 3 chars */
        while (e - p < 3) {
            is->ptr = p;
            if (stream_fill(is) == -1)
                return -1;
            p = is->ptr;
            e = is->end;
        }
        if (p[0] == '.') {
            if (p[1] == '\r' && p[2] == '\n') {
                is->ptr = p+3;
                is->mode = CAMEL_NNTP_STREAM_EOD;
                is->state = 0;
                dd(printf("NNTP_STREAM_READ(%d):\n%.*s\n", o-buffer, o-buffer, buffer));
                return o-buffer;
            }
            p++;
        }
        state = 1;
        /* FALLS THROUGH */
    case 1:     /* looking for next sol */
        while (o < oe) {
            c = *p++;
            if (c == '\n') {
                /* end of input sentinal check */
                if (p > e) {
                    is->ptr = e;
                    if (stream_fill(is) == -1)
                        return -1;
                    p = is->ptr;
                    e = is->end;
                } else {
                    *o++ = '\n';
                    state = 0;
                    goto state_0;
                }
            } else if (c != '\r') {
                *o++ = c;
            }
        }
        break;
    }

    is->ptr = p;
    is->state = state;

    dd(printf("NNTP_STREAM_READ(%d):\n%.*s\n", o-buffer, o-buffer, buffer));

    return o-buffer;
}

static ssize_t
stream_write(CamelStream *stream, const char *buffer, size_t n)
{
    CamelNNTPStream *is = (CamelNNTPStream *)stream;

    return camel_stream_write(is->source, buffer, n);
}

static int
stream_close(CamelStream *stream)
{
    /* nop? */
    return 0;
}

static int
stream_flush(CamelStream *stream)
{
    /* nop? */
    return 0;
}

static gboolean
stream_eos(CamelStream *stream)
{
    CamelNNTPStream *is = (CamelNNTPStream *)stream;

    return is->mode != CAMEL_NNTP_STREAM_DATA;
}

static int
stream_reset(CamelStream *stream)
{
    /* nop?  reset literal mode? */
    return 0;
}

static void
camel_nntp_stream_class_init (CamelStreamClass *camel_nntp_stream_class)
{
    CamelStreamClass *camel_stream_class = (CamelStreamClass *)camel_nntp_stream_class;

    parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE );

    /* virtual method definition */
    camel_stream_class->read = stream_read;
    camel_stream_class->write = stream_write;
    camel_stream_class->close = stream_close;
    camel_stream_class->flush = stream_flush;
    camel_stream_class->eos = stream_eos;
    camel_stream_class->reset = stream_reset;
}

static void
camel_nntp_stream_init(CamelNNTPStream *is, CamelNNTPStreamClass *isclass)
{
    /* +1 is room for appending a 0 if we need to for a line */
    is->ptr = is->end = is->buf = g_malloc(CAMEL_NNTP_STREAM_SIZE+1);
    is->lineptr = is->linebuf = g_malloc(CAMEL_NNTP_STREAM_LINE+1);
    is->lineend = is->linebuf + CAMEL_NNTP_STREAM_LINE;

    /* init sentinal */
    is->ptr[0] = '\n';

    is->state = 0;
    is->mode = CAMEL_NNTP_STREAM_LINE;
}

static void
camel_nntp_stream_finalise(CamelNNTPStream *is)
{
    g_free(is->buf);
    g_free(is->linebuf);
    if (is->source)
        camel_object_unref((CamelObject *)is->source);
}

CamelType
camel_nntp_stream_get_type (void)
{
    static CamelType camel_nntp_stream_type = CAMEL_INVALID_TYPE;

    if (camel_nntp_stream_type == CAMEL_INVALID_TYPE) {
        camel_nntp_stream_type = camel_type_register( camel_stream_get_type(),
                                "CamelNNTPStream",
                                sizeof( CamelNNTPStream ),
                                sizeof( CamelNNTPStreamClass ),
                                (CamelObjectClassInitFunc) camel_nntp_stream_class_init,
                                NULL,
                                (CamelObjectInitFunc) camel_nntp_stream_init,
                                (CamelObjectFinalizeFunc) camel_nntp_stream_finalise );
    }

    return camel_nntp_stream_type;
}

/**
 * camel_nntp_stream_new:
 *
 * Returns a NULL stream.  A null stream is always at eof, and
 * always returns success for all reads and writes.
 *
 * Return value: the stream
 **/
CamelStream *
camel_nntp_stream_new(CamelStream *source)
{
    CamelNNTPStream *is;

    is = (CamelNNTPStream *)camel_object_new(camel_nntp_stream_get_type ());
    camel_object_ref((CamelObject *)source);
    is->source = source;

    return (CamelStream *)is;
}

/* Get one line from the nntp stream */
int
camel_nntp_stream_line(CamelNNTPStream *is, unsigned char **data, unsigned int *len)
{
    register unsigned char c, *p, *o, *oe;
    int newlen, oldlen;
    unsigned char *e;

    if (is->mode == CAMEL_NNTP_STREAM_EOD) {
        *data = is->linebuf;
        *len = 0;
        return 0;
    }

    o = is->linebuf;
    oe = is->lineend - 1;
    p = is->ptr;
    e = is->end;

    /* Data mode, convert leading '..' to '.', and stop when we reach a solitary '.' */
    if (is->mode == CAMEL_NNTP_STREAM_DATA) {
        /* need at least 3 chars in buffer */
        while (e-p < 3) {
            is->ptr = p;
            if (stream_fill(is) == -1)
                return -1;
            p = is->ptr;
            e = is->end;
        }

        /* check for isolated '.\r\n' or begging of line '.' */
        if (p[0] == '.') {
            if (p[1] == '\r' && p[2] == '\n') {
                is->ptr = p+3;
                is->mode = CAMEL_NNTP_STREAM_EOD;
                *data = is->linebuf;
                *len = 0;
                is->linebuf[0] = 0;

                dd(printf("NNTP_STREAM_LINE(END)\n"));

                return 0;
            }
            p++;
        }
    }

    while (1) {
        while (o < oe) {
            c = *p++;
            if (c == '\n') {
                /* sentinal? */
                if (p> e) {
                    is->ptr = e;
                    if (stream_fill(is) == -1)
                        return -1;
                    p = is->ptr;
                    e = is->end;
                } else {
                    is->ptr = p;
                    *data = is->linebuf;
                    *len = o - is->linebuf;
                    *o = 0;

                    dd(printf("NNTP_STREAM_LINE(%d): '%s'\n", *len, *data));

                    return 1;
                }
            } else if (c != '\r') {
                *o++ = c;
            }
        }

        /* limit this for bad server data? */
        oldlen = o - is->linebuf;
        newlen = (is->lineend - is->linebuf) * 3 / 2;
        is->lineptr = is->linebuf = g_realloc(is->linebuf, newlen);
        is->lineend = is->linebuf + newlen;
        oe = is->lineend - 1;
        o = is->linebuf + oldlen;
    }

    return -1;
}

/* returns -1 on error, 0 if last lot of data, >0 if more remaining */
int camel_nntp_stream_gets(CamelNNTPStream *is, unsigned char **start, unsigned int *len)
{
    int max;
    unsigned char *end;

    *len = 0;

    max = is->end - is->ptr;
    if (max == 0) {
        max = stream_fill(is);
        if (max <= 0)
            return max;
    }

    *start = is->ptr;
    end = memchr(is->ptr, '\n', max);
    if (end)
        max = (end - is->ptr) + 1;
    *start = is->ptr;
    *len = max;
    is->ptr += max;

    dd(printf("NNTP_STREAM_GETS(%s,%d): '%.*s'\n", end==NULL?"more":"last", *len, (int)*len, *start));

    return end == NULL?1:0;
}

void camel_nntp_stream_set_mode(CamelNNTPStream *is, camel_nntp_stream_mode_t mode)
{
    is->mode = mode;
}

/* returns -1 on erorr, 0 if last data, >0 if more data left */
int camel_nntp_stream_getd(CamelNNTPStream *is, unsigned char **start, unsigned int *len)
{
    unsigned char *p, *e, *s;
    int state;

    *len = 0;

    if (is->mode == CAMEL_NNTP_STREAM_EOD)
        return 0;

    if (is->mode == CAMEL_NNTP_STREAM_LINE) {
        g_warning("nntp_stream reading data in line mode\n");
        return 0;
    }

    state = is->state;
    p = is->ptr;
    e = is->end;

    while (e - p < 3) {
        is->ptr = p;
        if (stream_fill(is) == -1)
            return -1;
        p = is->ptr;
        e = is->end;
    }

    s = p;

    do {
        switch(state) {
        case 0:
            /* check leading '.', ... */
            if (p[0] == '.') {
                if (p[1] == '\r' && p[2] == '\n') {
                    is->ptr = p+3;
                    *len = p-s;
                    *start = s;
                    is->mode = CAMEL_NNTP_STREAM_EOD;
                    is->state = 0;

                    dd(printf("NNTP_STREAM_GETD(%s,%d): '%.*s'\n", "last", *len, (int)*len, *start));

                    return 0;
                }

                /* If at start, just skip '.', else return data upto '.' but skip it */
                if (p == s) {
                    s++;
                    p++;
                } else {
                    is->ptr = p+1;
                    *len = p-s;
                    *start = s;
                    is->state = 1;

                    dd(printf("NNTP_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start));

                    return 1;
                }
            }
            state = 1;
        case 1:
            /* Scan for sentinal */
            while ((*p++)!='\n')
                ;

            if (p > e) {
                p = e;
            } else {
                state = 0;
            }
            break;
        }
    } while ((e-p) >= 3);

    is->state = state;
    is->ptr = p;
    *len = p-s;
    *start = s;

    dd(printf("NNTP_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start));
    return 1;
}