aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imapp/camel-imapp-stream.c
blob: 20876beae2a43f8959ba064d38f375f005cbb981 (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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/* -*- 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 <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

#include <glib.h>

#include <camel/camel-stream-mem.h>

#include "camel-imapp-stream.h"
#include "camel-imapp-exception.h"

#define t(x) 
#define io(x) x

static void setup_table(void);

static CamelObjectClass *parent_class = NULL;

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

#define CAMEL_IMAPP_STREAM_SIZE (4096)
#define CAMEL_IMAPP_STREAM_TOKEN (4096) /* maximum token size */

static int
stream_fill(CamelIMAPPStream *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_IMAPP_STREAM_SIZE - (is->end - is->buf));
        if (left > 0) {
            is->end += left;
            io(printf("camel_imapp_read: buffer is '%.*s'\n", is->end - is->ptr, is->ptr));
            return is->end - is->ptr;
        } else {
            io(printf("camel_imapp_read: -1\n"));
            return -1;
        }
    }

    printf("camel_imapp_read: -1\n");

    return -1;
}

static ssize_t
stream_read(CamelStream *stream, char *buffer, size_t n)
{
    CamelIMAPPStream *is = (CamelIMAPPStream *)stream;
    ssize_t max;

    if (is->literal == 0 || n == 0)
        return 0;

    max = is->end - is->ptr;
    if (max > 0) {
        max = MIN(max, is->literal);
        max = MIN(max, n);
        memcpy(buffer, is->ptr, max);
        is->ptr += max;
    } else {
        max = MIN(is->literal, n);
        max = camel_stream_read(is->source, buffer, max);
        if (max <= 0)
            return max;
    }

    is->literal -= max;
    
    return max;
}

static ssize_t
stream_write(CamelStream *stream, const char *buffer, size_t n)
{
    CamelIMAPPStream *is = (CamelIMAPPStream *)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)
{
    CamelIMAPPStream *is = (CamelIMAPPStream *)stream;

    return is->literal == 0;
}

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

static void
camel_imapp_stream_class_init (CamelStreamClass *camel_imapp_stream_class)
{
    CamelStreamClass *camel_stream_class = (CamelStreamClass *)camel_imapp_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_imapp_stream_init(CamelIMAPPStream *is, CamelIMAPPStreamClass *isclass)
{
    /* +1 is room for appending a 0 if we need to for a token */
    is->ptr = is->end = is->buf = g_malloc(CAMEL_IMAPP_STREAM_SIZE+1);
    is->tokenptr = is->tokenbuf = g_malloc(CAMEL_IMAPP_STREAM_SIZE+1);
    is->tokenend = is->tokenbuf + CAMEL_IMAPP_STREAM_SIZE;
}

static void
camel_imapp_stream_finalise(CamelIMAPPStream *is)
{
    g_free(is->buf);
    if (is->source)
        camel_object_unref((CamelObject *)is->source);
}

CamelType
camel_imapp_stream_get_type (void)
{
    static CamelType camel_imapp_stream_type = CAMEL_INVALID_TYPE;

    if (camel_imapp_stream_type == CAMEL_INVALID_TYPE) {
        setup_table();
        camel_imapp_stream_type = camel_type_register( camel_stream_get_type(),
                                "CamelIMAPPStream",
                                sizeof( CamelIMAPPStream ),
                                sizeof( CamelIMAPPStreamClass ),
                                (CamelObjectClassInitFunc) camel_imapp_stream_class_init,
                                NULL,
                                (CamelObjectInitFunc) camel_imapp_stream_init,
                                (CamelObjectFinalizeFunc) camel_imapp_stream_finalise );
    }

    return camel_imapp_stream_type;
}

/**
 * camel_imapp_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_imapp_stream_new(CamelStream *source)
{
    CamelIMAPPStream *is;

    is = (CamelIMAPPStream *)camel_object_new(camel_imapp_stream_get_type ());
    camel_object_ref((CamelObject *)source);
    is->source = source;

    return (CamelStream *)is;
}


/*
 From rfc2060

ATOM_CHAR       ::= <any CHAR except atom_specials>

atom_specials   ::= "(" / ")" / "{" / SPACE / CTL / list_wildcards /
                    quoted_specials

CHAR            ::= <any 7-bit US-ASCII character except NUL,
                     0x01 - 0x7f>

CTL             ::= <any ASCII control character and DEL,
                        0x00 - 0x1f, 0x7f>

SPACE           ::= <ASCII SP, space, 0x20>

list_wildcards  ::= "%" / "*"

quoted_specials ::= <"> / "\"
*/

static unsigned char imap_specials[256] = {
/* 00 */0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1,
/* 30 */1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 40 */1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 50 */1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
/* 60 */1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 70 */1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
};

#define imap_is_atom(c) ((imap_specials[(c)&0xff] & 0x01) != 0)
#define imap_is_simple(c) ((imap_specials[(c)&0xff] & 0x02) != 0)
#define imap_not_id(c) ((imap_specials[(c)&0xff] & 0x04) != 0)

/* could be pregenerated, but this is cheap */
static struct {
    unsigned char *chars;
    unsigned char mask;
} is_masks[] = {
    { "\n*()[]+", 2 },
    { " \r\n()[]+", 4 },
};

static void setup_table(void)
{
    int i;
    unsigned char *p, c;

    for (i=0;i<(int)(sizeof(is_masks)/sizeof(is_masks[0]));i++) {
        p = is_masks[i].chars;
        while ((c = *p++))
            imap_specials[c] |= is_masks[i].mask;
    }
}

#if 0

static int
skip_ws(CamelIMAPPStream *is, unsigned char *pp, unsigned char *pe)
{
    register unsigned char c, *p;
    unsigned char *e;

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

    do {
        while (p >= e ) {
            is->ptr = p;
            if (stream_fill(is) == IMAP_TOK_ERROR)
                return IMAP_TOK_ERROR;
            p = is->ptr;
            e = is->end;
        }
        c = *p++;
    } while (c == ' ' || c == '\r');

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

    return c;
}
#endif

/* FIXME: these should probably handle it themselves,
   and get rid of the token interface? */
int
camel_imapp_stream_atom(CamelIMAPPStream *is, unsigned char **data, unsigned int *lenp)
{
    unsigned char *p, c;

    /* this is only 'approximate' atom */
    switch(camel_imapp_stream_token(is, data, lenp)) {
    case IMAP_TOK_TOKEN:
        p = *data;
        while ((c = *p))
            *p++ = toupper(c);
    case IMAP_TOK_INT:
        return 0;
    case IMAP_TOK_ERROR:
        return IMAP_TOK_ERROR;
    default:
        camel_exception_throw(1, "expecting atom");
        printf("expecting atom!\n");
        return IMAP_TOK_PROTOCOL;
    }
}

/* gets an atom, a quoted_string, or a literal */
int
camel_imapp_stream_astring(CamelIMAPPStream *is, unsigned char **data)
{
    unsigned char *p, *start;
    unsigned int len, inlen;

    switch(camel_imapp_stream_token(is, data, &len)) {
    case IMAP_TOK_TOKEN:
    case IMAP_TOK_INT:
    case IMAP_TOK_STRING:
        return 0;
    case IMAP_TOK_LITERAL:
        /* FIXME: just grow buffer */
        if (len >= CAMEL_IMAPP_STREAM_TOKEN) {
            camel_exception_throw(1, "astring: literal too long");
            printf("astring too long\n");
            return IMAP_TOK_PROTOCOL;
        }
        p = is->tokenptr;
        camel_imapp_stream_set_literal(is, len);
        do {
            len = camel_imapp_stream_getl(is, &start, &inlen);
            if (len < 0)
                return len;
            memcpy(p, start, inlen);
            p += inlen;
        } while (len > 0);
        *data = is->tokenptr;
        return 0;
    case IMAP_TOK_ERROR:
        /* wont get unless no exception hanlder*/
        return IMAP_TOK_ERROR;
    default:
        camel_exception_throw(1, "expecting astring");
        printf("expecting astring!\n");
        return IMAP_TOK_PROTOCOL;
    }
}

/* check for NIL or (small) quoted_string or literal */
int
camel_imapp_stream_nstring(CamelIMAPPStream *is, unsigned char **data)
{
    unsigned char *p, *start;
    unsigned int len, inlen;

    switch(camel_imapp_stream_token(is, data, &len)) {
    case IMAP_TOK_STRING:
        return 0;
    case IMAP_TOK_LITERAL:
        /* FIXME: just grow buffer */
        if (len >= CAMEL_IMAPP_STREAM_TOKEN) {
            camel_exception_throw(1, "nstring: literal too long");
            return IMAP_TOK_PROTOCOL;
        }
        p = is->tokenptr;
        camel_imapp_stream_set_literal(is, len);
        do {
            len = camel_imapp_stream_getl(is, &start, &inlen);
            if (len < 0)
                return len;
            memcpy(p, start, inlen);
            p += inlen;
        } while (len > 0);
        *data = is->tokenptr;
        return 0;
    case IMAP_TOK_TOKEN:
        p = *data;
        if (toupper(p[0]) == 'N' && toupper(p[1]) == 'I' && toupper(p[2]) == 'L' && p[3] == 0) {
            *data = NULL;
            return 0;
        }
    default:
        camel_exception_throw(1, "expecting nstring");
        return IMAP_TOK_PROTOCOL;
    case IMAP_TOK_ERROR:
        /* we'll never get this unless there are no exception  handlers anyway */
        return IMAP_TOK_ERROR;

    }
}

/* parse an nstring as a stream */
int
camel_imapp_stream_nstring_stream(CamelIMAPPStream *is, CamelStream **stream)
/* throws IO,PARSE exception */
{
    unsigned char *token;
    unsigned int len;
    int ret = 0;
    CamelStream * volatile mem = NULL;

    *stream = NULL;

    CAMEL_TRY {
        switch(camel_imapp_stream_token(is, &token, &len)) {
        case IMAP_TOK_STRING:
            mem = camel_stream_mem_new_with_buffer(token, len);
            *stream = mem;
            break;
        case IMAP_TOK_LITERAL:
            /* if len is big, we could automatically use a file backing */
            camel_imapp_stream_set_literal(is, len);
            mem = camel_stream_mem_new();
            if (camel_stream_write_to_stream((CamelStream *)is, mem) == -1)
                camel_exception_throw(1, "nstring: io error: %s", strerror(errno));
            camel_stream_reset(mem);
            *stream = mem;
            break;
        case IMAP_TOK_TOKEN:
            if (toupper(token[0]) == 'N' && toupper(token[1]) == 'I' && toupper(token[2]) == 'L' && token[3] == 0) {
                *stream = NULL;
                break;
            }
        default:
            ret = -1;
            camel_exception_throw(1, "nstring: token not string");
        }
    } CAMEL_CATCH(ex) {
        if (mem)
            camel_object_unref((CamelObject *)mem);
        camel_exception_throw_ex(ex);
    } CAMEL_DONE;

    /* never reaches here anyway */
    return ret;
}

guint32
camel_imapp_stream_number(CamelIMAPPStream *is)
{
    unsigned char *token;
    unsigned int len;

    if (camel_imapp_stream_token(is, &token, &len) != IMAP_TOK_INT) {
        camel_exception_throw(1, "expecting number");
        return 0;
    }

    return strtoul(token, 0, 10);
}

int
camel_imapp_stream_text(CamelIMAPPStream *is, unsigned char **text)
{
    GByteArray *build = g_byte_array_new();
    unsigned char *token;
    unsigned int len;
    int tok;

    CAMEL_TRY {
        while (is->unget > 0) {
            switch (is->unget_tok) {
            case IMAP_TOK_TOKEN:
            case IMAP_TOK_STRING:
            case IMAP_TOK_INT:
                g_byte_array_append(build, is->unget_token, is->unget_len);
                g_byte_array_append(build, " ", 1);
            default: /* invalid, but we'll ignore */
                break;
            }
            is->unget--;
        }

        do {
            tok = camel_imapp_stream_gets(is, &token, &len);
            if (tok < 0)
                camel_exception_throw(1, "io error: %s", strerror(errno));
            if (len)
                g_byte_array_append(build, token, len);
        } while (tok > 0);
    } CAMEL_CATCH(ex) {
        *text = NULL;
        g_byte_array_free(build, TRUE);
        camel_exception_throw_ex(ex);
    } CAMEL_DONE;

    g_byte_array_append(build, "", 1);
    *text = build->data;
    g_byte_array_free(build, FALSE);

    return 0;
}

/* Get one token from the imap stream */
camel_imapp_token_t
/* throws IO,PARSE exception */
camel_imapp_stream_token(CamelIMAPPStream *is, unsigned char **data, unsigned int *len)
{
    register unsigned char c, *p, *o, *oe;
    unsigned char *e;
    unsigned int literal;
    int digits;

    if (is->unget > 0) {
        is->unget--;
        *data = is->unget_token;
        *len = is->unget_len;
        /*printf("token UNGET '%c' %s\n", is->unget_tok, is->unget_token);*/
        return is->unget_tok;
    }

    if (is->literal > 0)
        g_warning("stream_token called with literal %d", is->literal);

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

    /* skip whitespace/prefill buffer */
    do {
        while (p >= e ) {
            is->ptr = p;
            if (stream_fill(is) == IMAP_TOK_ERROR)
                goto io_error;
            p = is->ptr;
            e = is->end;
        }
        c = *p++;
    } while (c == ' ' || c == '\r');

    /*strchr("\n*()[]+", c)*/
    if (imap_is_simple(c)) {
        is->ptr = p;
        t(printf("token '%c'\n", c));
        return c;
    } else if (c == '{') {
        literal = 0;
        *data = p;
        while (1) {
            while (p < e) {
                c = *p++;
                if (isdigit(c) && literal < (UINT_MAX/10)) {
                    literal = literal * 10 + (c - '0');
                } else if (c == '}') {
                    while (1) {
                        while (p < e) {
                            c = *p++;
                            if (c == '\n') {
                                *len = literal;
                                is->ptr = p;
                                is->literal = literal;
                                t(printf("token LITERAL %d\n", literal));
                                return IMAP_TOK_LITERAL;
                            }
                        }
                        is->ptr = p;
                        if (stream_fill(is) == IMAP_TOK_ERROR)
                            goto io_error;
                        p = is->ptr;
                        e = is->end;
                    }
                } else {
                    if (isdigit(c))
                        printf("Protocol error: literal too big\n");
                    else
                        printf("Protocol error: literal contains invalid char %02x '%c'\n", c, isprint(c)?c:c);
                    goto protocol_error;
                }
            }
            is->ptr = p;
            if (stream_fill(is) == IMAP_TOK_ERROR)
                goto io_error;
            p = is->ptr;
            e = is->end;
        }
    } else if (c == '"') {
        o = is->tokenptr;
        oe = is->tokenptr + CAMEL_IMAPP_STREAM_TOKEN - 1;
        while (1) {
            while (p < e) {
                c = *p++;
                if (c == '\\') {
                    while (p >= e) {
                        is->ptr = p;
                        if (stream_fill(is) == IMAP_TOK_ERROR)
                            goto io_error;
                        p = is->ptr;
                        e = is->end;
                    }
                    c = *p++;
                } else if (c == '\"') {
                    is->ptr = p;
                    *o = 0;
                    *data = is->tokenbuf;
                    *len = o - is->tokenbuf;
                    t(printf("token STRING '%s'\n", is->tokenbuf));
                    return IMAP_TOK_STRING;
                }

                if (c == '\n' || c == '\r' || o>=oe) {
                    if (o >= oe)
                        printf("Protocol error: string too long\n");
                    else
                        printf("Protocol error: truncated string\n");
                    goto protocol_error;
                } else {
                    *o++ = c;
                }
            }
            is->ptr = p;
            if (stream_fill(is) == IMAP_TOK_ERROR)
                goto io_error;
            p = is->ptr;
            e = is->end;
        }
    } else {
        o = is->tokenptr;
        oe = is->tokenptr + CAMEL_IMAPP_STREAM_TOKEN - 1;
        digits = isdigit(c);
        *o++ = c;
        while (1) {
            while (p < e) {
                c = *p++;
                /*if (strchr(" \r\n*()[]+", c) != NULL) {*/
                if (imap_not_id(c)) {
                    if (c == ' ' || c == '\r')
                        is->ptr = p;
                    else
                        is->ptr = p-1;
                    *o = 0;
                    *data = is->tokenbuf;
                    *len = o - is->tokenbuf;
                    t(printf("token TOKEN '%s'\n", is->tokenbuf));
                    return digits?IMAP_TOK_INT:IMAP_TOK_TOKEN;
                } else if (o < oe) {
                    digits &= isdigit(c);
                    *o++ = c;
                } else {
                    printf("Protocol error: token too long\n");
                    goto protocol_error;
                }
            }
            is->ptr = p;
            if (stream_fill(is) == IMAP_TOK_ERROR)
                goto io_error;
            p = is->ptr;
            e = is->end;
        }
    }

    /* Had an i/o erorr */
io_error:
    printf("Got io error\n");
    camel_exception_throw(1, "io error");
    return IMAP_TOK_ERROR;

    /* Protocol error, skip until next lf? */
protocol_error:
    printf("Got protocol error\n");

    if (c == '\n')
        is->ptr = p-1;
    else
        is->ptr = p;

    camel_exception_throw(1, "protocol error");
    return IMAP_TOK_PROTOCOL;
}

void
camel_imapp_stream_ungettoken(CamelIMAPPStream *is, camel_imapp_token_t tok, unsigned char *token, unsigned int len)
{
    /*printf("ungettoken: '%c' '%s'\n", tok, token);*/
    is->unget_tok = tok;
    is->unget_token = token;
    is->unget_len = len;
    is->unget++;
}

/* returns -1 on error, 0 if last lot of data, >0 if more remaining */
int camel_imapp_stream_gets(CamelIMAPPStream *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;

    return end == NULL?1:0;
}

void camel_imapp_stream_set_literal(CamelIMAPPStream *is, unsigned int literal)
{
    is->literal = literal;
}

/* returns -1 on erorr, 0 if last data, >0 if more data left */
int camel_imapp_stream_getl(CamelIMAPPStream *is, unsigned char **start, unsigned int *len)
{
    int max;

    *len = 0;

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

        max = MIN(max, is->literal);
        *start = is->ptr;
        *len = max;
        is->ptr += max;
        is->literal -= max;
    }

    if (is->literal > 0)
        return 1;

    return 0;
}