1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
4 * Michael Zucchi <notzed@ximian.com>
6 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
32 #include <glib/gi18n-lib.h>
34 #include <camel/camel-stream-mem.h>
36 #include "camel-imapx-utils.h"
37 #include "camel-imapx-stream.h"
39 #define CAMEL_IMAPX_STREAM_GET_PRIVATE(obj) \
40 (G_TYPE_INSTANCE_GET_PRIVATE \
41 ((obj), CAMEL_TYPE_IMAPX_STREAM, CamelIMAPXStreamPrivate))
43 #define t(...) camel_imapx_debug(token, __VA_ARGS__)
44 #define io(...) camel_imapx_debug(io, __VA_ARGS__)
46 struct _CamelIMAPXStreamPrivate {
49 guchar *buf, *ptr, *end;
53 camel_imapx_token_t unget_tok;
66 G_DEFINE_TYPE (CamelIMAPXStream, camel_imapx_stream, CAMEL_TYPE_STREAM)
69 imapx_stream_fill (CamelIMAPXStream *is,
70 GCancellable *cancellable,
75 if (is->priv->source != NULL) {
76 left = is->priv->end - is->priv->ptr;
77 memcpy (is->priv->buf, is->priv->ptr, left);
78 is->priv->end = is->priv->buf + left;
79 is->priv->ptr = is->priv->buf;
80 left = camel_stream_read (
82 (gchar *) is->priv->end,
83 is->priv->bufsize - (is->priv->end - is->priv->buf),
86 is->priv->end += left;
87 io (is->tagprefix, "camel_imapx_read: buffer is '%.*s'\n", (gint)(is->priv->end - is->priv->ptr), is->priv->ptr);
88 return is->priv->end - is->priv->ptr;
90 io (is->tagprefix, "camel_imapx_read: -1\n");
91 /* If returning zero, camel_stream_read() doesn't consider
92 * that to be an error. But we do -- we should only be here
93 * if we *know* there are data to receive. So set the error
97 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
98 _("Source stream returned no data"));
103 io (is->tagprefix, "camel_imapx_read: -1\n");
106 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
107 _("Source stream unavailable"));
113 imapx_stream_set_source (CamelIMAPXStream *stream,
116 g_return_if_fail (CAMEL_IS_STREAM (source));
117 g_return_if_fail (stream->priv->source == NULL);
119 stream->priv->source = g_object_ref (source);
123 imapx_stream_set_property (GObject *object,
128 switch (property_id) {
130 imapx_stream_set_source (
131 CAMEL_IMAPX_STREAM (object),
132 g_value_get_object (value));
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
140 imapx_stream_get_property (GObject *object,
145 switch (property_id) {
147 g_value_take_object (
149 camel_imapx_stream_ref_source (
150 CAMEL_IMAPX_STREAM (object)));
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 imapx_stream_dispose (GObject *object)
160 CamelIMAPXStream *stream = CAMEL_IMAPX_STREAM (object);
162 if (stream->priv->source != NULL) {
163 g_object_unref (stream->priv->source);
164 stream->priv->source = NULL;
167 /* Chain up to parent's dispose() method. */
168 G_OBJECT_CLASS (camel_imapx_stream_parent_class)->dispose (object);
172 imapx_stream_finalize (GObject *object)
174 CamelIMAPXStream *stream = CAMEL_IMAPX_STREAM (object);
176 g_free (stream->priv->buf);
177 g_free (stream->priv->tokenbuf);
179 /* Chain up to parent's finalize() method. */
180 G_OBJECT_CLASS (camel_imapx_stream_parent_class)->finalize (object);
184 imapx_stream_read (CamelStream *stream,
187 GCancellable *cancellable,
190 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
193 if (is->priv->literal == 0 || n == 0)
196 max = is->priv->end - is->priv->ptr;
198 max = MIN (max, is->priv->literal);
200 memcpy (buffer, is->priv->ptr, max);
201 is->priv->ptr += max;
203 max = MIN (is->priv->literal, n);
204 max = camel_stream_read (
206 buffer, max, cancellable, error);
211 io (is->tagprefix, "camel_imapx_read(literal): '%.*s'\n", (gint) max, buffer);
213 is->priv->literal -= max;
219 imapx_stream_write (CamelStream *stream,
222 GCancellable *cancellable,
225 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
227 if (g_strstr_len (buffer, n, "LOGIN")) {
228 io (is->tagprefix, "camel_imapx_write: 'LOGIN...'\n");
230 io (is->tagprefix, "camel_imapx_write: '%.*s'\n", (gint) n, buffer);
233 return camel_stream_write (
235 buffer, n, cancellable, error);
239 imapx_stream_close (CamelStream *stream,
240 GCancellable *cancellable,
243 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
245 return camel_stream_close (is->priv->source, cancellable, error);
249 imapx_stream_flush (CamelStream *stream,
250 GCancellable *cancellable,
258 imapx_stream_eos (CamelStream *stream)
260 CamelIMAPXStream *is = (CamelIMAPXStream *) stream;
262 return is->priv->literal == 0;
266 camel_imapx_stream_class_init (CamelIMAPXStreamClass *class)
268 GObjectClass *object_class;
269 CamelStreamClass *stream_class;
271 g_type_class_add_private (class, sizeof (CamelIMAPXStreamPrivate));
273 object_class = G_OBJECT_CLASS (class);
274 object_class->set_property = imapx_stream_set_property;
275 object_class->get_property = imapx_stream_get_property;
276 object_class->dispose = imapx_stream_dispose;
277 object_class->finalize = imapx_stream_finalize;
279 stream_class = CAMEL_STREAM_CLASS (class);
280 stream_class->read = imapx_stream_read;
281 stream_class->write = imapx_stream_write;
282 stream_class->close = imapx_stream_close;
283 stream_class->flush = imapx_stream_flush;
284 stream_class->eos = imapx_stream_eos;
286 g_object_class_install_property (
289 g_param_spec_object (
295 G_PARAM_CONSTRUCT_ONLY |
296 G_PARAM_STATIC_STRINGS));
300 camel_imapx_stream_init (CamelIMAPXStream *is)
302 is->priv = CAMEL_IMAPX_STREAM_GET_PRIVATE (is);
304 /* +1 is room for appending a 0 if we need to for a token */
305 is->priv->bufsize = 4096;
306 is->priv->buf = g_malloc (is->priv->bufsize + 1);
307 is->priv->ptr = is->priv->end = is->priv->buf;
308 is->priv->tokenbuf = g_malloc (is->priv->bufsize + 1);
312 camel_imapx_stream_grow (CamelIMAPXStream *is,
317 guchar *oldtok = is->priv->tokenbuf;
318 guchar *oldbuf = is->priv->buf;
321 is->priv->bufsize <<= 1;
322 } while (is->priv->bufsize <= len);
324 io (is->tagprefix, "Grow imapx buffers to %d bytes\n", is->priv->bufsize);
326 is->priv->tokenbuf = g_realloc (
328 is->priv->bufsize + 1);
330 *tokptr = is->priv->tokenbuf + (*tokptr - oldtok);
332 is->priv->unget_token =
334 (is->priv->unget_token - oldtok);
336 is->priv->buf = g_realloc (is->priv->buf, is->priv->bufsize + 1);
337 is->priv->ptr = is->priv->buf + (is->priv->ptr - oldbuf);
338 is->priv->end = is->priv->buf + (is->priv->end - oldbuf);
340 *bufptr = is->priv->buf + (*bufptr - oldbuf);
343 G_DEFINE_QUARK (camel-imapx-error-quark, camel_imapx_error)
346 * camel_imapx_stream_new:
348 * Returns a NULL stream. A null stream is always at eof, and
349 * always returns success for all reads and writes.
351 * Returns: the stream
354 camel_imapx_stream_new (CamelStream *source)
356 g_return_val_if_fail (CAMEL_IS_STREAM (source), NULL);
358 return g_object_new (
359 CAMEL_TYPE_IMAPX_STREAM,
360 "source", source, NULL);
364 camel_imapx_stream_ref_source (CamelIMAPXStream *is)
366 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), NULL);
368 return g_object_ref (is->priv->source);
371 /* Returns if there is any data buffered that is ready for processing */
373 camel_imapx_stream_buffered (CamelIMAPXStream *is)
375 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), 0);
377 return is->priv->end - is->priv->ptr;
380 /* FIXME: these should probably handle it themselves,
381 * and get rid of the token interface? */
383 camel_imapx_stream_atom (CamelIMAPXStream *is,
386 GCancellable *cancellable,
390 GError *local_error = NULL;
392 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), IMAPX_TOK_ERROR);
393 g_return_val_if_fail (data != NULL, IMAPX_TOK_ERROR);
394 g_return_val_if_fail (lenp != NULL, IMAPX_TOK_ERROR);
396 /* this is only 'approximate' atom */
397 switch (camel_imapx_stream_token (is, data, lenp, cancellable, &local_error)) {
398 case IMAPX_TOK_TOKEN:
404 case IMAPX_TOK_ERROR:
405 if (local_error != NULL)
406 g_propagate_error (error, local_error);
407 return IMAPX_TOK_ERROR;
409 if (local_error == NULL)
410 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting atom");
412 g_propagate_error (error, local_error);
413 io (is->tagprefix, "expecting atom!\n");
414 return IMAPX_TOK_PROTOCOL;
418 /* gets an atom, a quoted_string, or a literal */
420 camel_imapx_stream_astring (CamelIMAPXStream *is,
422 GCancellable *cancellable,
428 GError *local_error = NULL;
430 g_return_val_if_fail (CAMEL_IMAPX_STREAM (is), IMAPX_TOK_ERROR);
431 g_return_val_if_fail (data != NULL, IMAPX_TOK_ERROR);
433 switch (camel_imapx_stream_token (is, data, &len, cancellable, &local_error)) {
434 case IMAPX_TOK_TOKEN:
436 case IMAPX_TOK_STRING:
438 case IMAPX_TOK_LITERAL:
439 if (len >= is->priv->bufsize)
440 camel_imapx_stream_grow (is, len, NULL, NULL);
441 p = is->priv->tokenbuf;
442 camel_imapx_stream_set_literal (is, len);
444 ret = camel_imapx_stream_getl (is, &start, &inlen, cancellable, error);
447 memcpy (p, start, inlen);
451 *data = is->priv->tokenbuf;
453 case IMAPX_TOK_ERROR:
454 /* wont get unless no exception hanlder*/
455 if (local_error != NULL)
456 g_propagate_error (error, local_error);
457 return IMAPX_TOK_ERROR;
459 if (local_error == NULL)
460 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting astring");
462 g_propagate_error (error, local_error);
463 io (is->tagprefix, "expecting astring!\n");
464 return IMAPX_TOK_PROTOCOL;
468 /* check for NIL or (small) quoted_string or literal */
470 camel_imapx_stream_nstring (CamelIMAPXStream *is,
472 GCancellable *cancellable,
478 GError *local_error = NULL;
480 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), IMAPX_TOK_ERROR);
481 g_return_val_if_fail (data != NULL, IMAPX_TOK_ERROR);
483 switch (camel_imapx_stream_token (is, data, &len, cancellable, &local_error)) {
484 case IMAPX_TOK_STRING:
486 case IMAPX_TOK_LITERAL:
487 if (len >= is->priv->bufsize)
488 camel_imapx_stream_grow (is, len, NULL, NULL);
489 p = is->priv->tokenbuf;
490 camel_imapx_stream_set_literal (is, len);
492 ret = camel_imapx_stream_getl (is, &start, &inlen, cancellable, error);
495 memcpy (p, start, inlen);
499 *data = is->priv->tokenbuf;
501 case IMAPX_TOK_TOKEN:
503 if (toupper (p[0]) == 'N' && toupper (p[1]) == 'I' && toupper (p[2]) == 'L' && p[3] == 0) {
508 if (local_error == NULL)
509 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting nstring");
511 g_propagate_error (error, local_error);
512 return IMAPX_TOK_PROTOCOL;
513 case IMAPX_TOK_ERROR:
514 /* we'll never get this unless there are no exception handlers anyway */
515 if (local_error != NULL)
516 g_propagate_error (error, local_error);
517 return IMAPX_TOK_ERROR;
522 /* parse an nstring as a stream */
524 camel_imapx_stream_nstring_stream (CamelIMAPXStream *is,
525 CamelStream **stream,
526 GCancellable *cancellable,
532 CamelStream * mem = NULL;
533 GError *local_error = NULL;
535 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), -1);
536 g_return_val_if_fail (stream != NULL, -1);
540 switch (camel_imapx_stream_token (is, &token, &len, cancellable, &local_error)) {
541 case IMAPX_TOK_STRING:
542 mem = camel_stream_mem_new_with_buffer ((gchar *) token, len);
545 case IMAPX_TOK_LITERAL:
546 /* if len is big, we could automatically use a file backing */
547 camel_imapx_stream_set_literal (is, len);
548 mem = camel_stream_mem_new ();
549 if (camel_stream_write_to_stream ((CamelStream *) is, mem, cancellable, error) == -1) {
550 g_object_unref (mem);
557 G_SEEK_SET, NULL, NULL);
561 case IMAPX_TOK_TOKEN:
562 if (toupper (token[0]) == 'N' && toupper (token[1]) == 'I' && toupper (token[2]) == 'L' && token[3] == 0) {
568 if (local_error == NULL)
569 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "nstring: token not string");
571 g_propagate_error (error, local_error);
578 camel_imapx_stream_number (CamelIMAPXStream *is,
579 GCancellable *cancellable,
584 GError *local_error = NULL;
586 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), 0);
588 if (camel_imapx_stream_token (is, &token, &len, cancellable, &local_error) != IMAPX_TOK_INT) {
589 if (local_error == NULL)
590 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "expecting number");
592 g_propagate_error (error, local_error);
596 return strtoull ((gchar *) token, 0, 10);
600 camel_imapx_stream_text (CamelIMAPXStream *is,
602 GCancellable *cancellable,
605 GByteArray *build = g_byte_array_new ();
610 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), -1);
611 g_return_val_if_fail (text != NULL, -1);
613 while (is->priv->unget > 0) {
614 switch (is->priv->unget_tok) {
615 case IMAPX_TOK_TOKEN:
616 case IMAPX_TOK_STRING:
618 g_byte_array_append (
620 is->priv->unget_token,
621 is->priv->unget_len);
622 g_byte_array_append (
623 build, (guint8 *) " ", 1);
624 default: /* invalid, but we'll ignore */
631 tok = camel_imapx_stream_gets (is, &token, &len, cancellable, error);
634 g_byte_array_free (build, TRUE);
638 g_byte_array_append (build, token, len);
641 g_byte_array_append (build, (guint8 *) "", 1);
643 g_byte_array_free (build, FALSE);
648 /* Get one token from the imap stream */
650 camel_imapx_stream_token (CamelIMAPXStream *is,
653 GCancellable *cancellable,
656 register guchar c, *oe;
661 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), IMAPX_TOK_ERROR);
662 g_return_val_if_fail (data != NULL, IMAPX_TOK_ERROR);
663 g_return_val_if_fail (len != NULL, IMAPX_TOK_ERROR);
665 if (is->priv->unget > 0) {
667 *data = is->priv->unget_token;
668 *len = is->priv->unget_len;
669 return is->priv->unget_tok;
672 if (is->priv->literal > 0)
674 "stream_token called with literal %d",
680 /* skip whitespace/prefill buffer */
684 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
685 return IMAPX_TOK_ERROR;
690 } while (c == ' ' || c == '\r');
692 /*strchr("\n*()[]+", c)*/
693 if (imapx_is_token_char (c)) {
695 t (is->tagprefix, "token '%c'\n", c);
697 } else if (c == '{') {
703 if (isdigit (c) && literal < (UINT_MAX / 10)) {
704 literal = literal * 10 + (c - '0');
705 } else if (c == '}') {
712 is->priv->literal = literal;
713 t (is->tagprefix, "token LITERAL %d\n", literal);
714 return IMAPX_TOK_LITERAL;
718 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
719 return IMAPX_TOK_ERROR;
725 io (is->tagprefix, "Protocol error: literal too big\n");
727 io (is->tagprefix, "Protocol error: literal contains invalid gchar %02x '%c'\n", c, isprint (c) ? c : c);
733 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
734 return IMAPX_TOK_ERROR;
738 } else if (c == '"') {
739 o = is->priv->tokenbuf;
740 oe = is->priv->tokenbuf + is->priv->bufsize - 1;
747 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
748 return IMAPX_TOK_ERROR;
753 } else if (c == '\"') {
756 *data = is->priv->tokenbuf;
757 *len = o - is->priv->tokenbuf;
758 t (is->tagprefix, "token STRING '%s'\n", is->priv->tokenbuf);
759 return IMAPX_TOK_STRING;
761 if (c == '\n' || c == '\r') {
762 io (is->tagprefix, "Protocol error: truncated string\n");
766 camel_imapx_stream_grow (is, 0, &p, &o);
767 oe = is->priv->tokenbuf + is->priv->bufsize - 1;
773 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
774 return IMAPX_TOK_ERROR;
779 o = is->priv->tokenbuf;
780 oe = is->priv->tokenbuf + is->priv->bufsize - 1;
781 digits = isdigit (c);
786 /*if (strchr(" \r\n*()[]+", c) != NULL) {*/
787 if (imapx_is_notid_char (c)) {
788 if (c == ' ' || c == '\r')
791 is->priv->ptr = p - 1;
793 *data = is->priv->tokenbuf;
794 *len = o - is->priv->tokenbuf;
795 t (is->tagprefix, "token TOKEN '%s'\n", is->priv->tokenbuf);
796 return digits ? IMAPX_TOK_INT : IMAPX_TOK_TOKEN;
800 camel_imapx_stream_grow (is, 0, &p, &o);
801 oe = is->priv->tokenbuf + is->priv->bufsize - 1;
804 digits &= isdigit (c);
808 if (imapx_stream_fill (is, cancellable, error) == IMAPX_TOK_ERROR)
809 return IMAPX_TOK_ERROR;
815 /* Protocol error, skip until next lf? */
817 io (is->tagprefix, "Got protocol error\n");
820 is->priv->ptr = p - 1;
824 g_set_error (error, CAMEL_IMAPX_ERROR, 1, "protocol error");
825 return IMAPX_TOK_PROTOCOL;
829 camel_imapx_stream_ungettoken (CamelIMAPXStream *is,
830 camel_imapx_token_t tok,
834 g_return_if_fail (CAMEL_IS_IMAPX_STREAM (is));
836 is->priv->unget_tok = tok;
837 is->priv->unget_token = token;
838 is->priv->unget_len = len;
842 /* returns -1 on error, 0 if last lot of data, >0 if more remaining */
844 camel_imapx_stream_gets (CamelIMAPXStream *is,
847 GCancellable *cancellable,
853 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), -1);
854 g_return_val_if_fail (start != NULL, -1);
855 g_return_val_if_fail (len != NULL, -1);
859 max = is->priv->end - is->priv->ptr;
861 max = imapx_stream_fill (is, cancellable, error);
866 *start = is->priv->ptr;
867 end = memchr (is->priv->ptr, '\n', max);
869 max = (end - is->priv->ptr) + 1;
870 *start = is->priv->ptr;
872 is->priv->ptr += max;
874 return end == NULL ? 1 : 0;
878 camel_imapx_stream_set_literal (CamelIMAPXStream *is,
881 g_return_if_fail (CAMEL_IS_IMAPX_STREAM (is));
883 is->priv->literal = literal;
886 /* returns -1 on erorr, 0 if last data, >0 if more data left */
888 camel_imapx_stream_getl (CamelIMAPXStream *is,
891 GCancellable *cancellable,
896 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), -1);
897 g_return_val_if_fail (start != NULL, -1);
898 g_return_val_if_fail (len != NULL, -1);
902 if (is->priv->literal > 0) {
903 max = is->priv->end - is->priv->ptr;
905 max = imapx_stream_fill (is, cancellable, error);
910 max = MIN (max, is->priv->literal);
911 *start = is->priv->ptr;
913 is->priv->ptr += max;
914 is->priv->literal -= max;
917 if (is->priv->literal > 0)
923 /* skip the rest of the line of tokens */
925 camel_imapx_stream_skip (CamelIMAPXStream *is,
926 GCancellable *cancellable,
933 g_return_val_if_fail (CAMEL_IS_IMAPX_STREAM (is), -1);
936 tok = camel_imapx_stream_token (is, &token, &len, cancellable, error);
937 if (tok == IMAPX_TOK_LITERAL) {
938 camel_imapx_stream_set_literal (is, len);
939 while ((tok = camel_imapx_stream_getl (is, &token, &len, cancellable, error)) > 0) {
940 io (is->tagprefix, "Skip literal data '%.*s'\n", (gint) len, token);
943 } while (tok != '\n' && tok >= 0);