Commit 2ef58f17 2ef58f17e48b99f5c95c2c90e8ba9a4190663dd8 by Sergey Poznyakoff

New type of stream: static memory stream.

Static memory stream is a read-only stream whose transport is
an arbitrary area of memory.  Such streams allow the programmer
to access C strings using streams interface.

* include/mailutils/stream.h (mu_static_memory_stream_create): New
proto.
* libmailutils/stream/memory_stream.c (mu_static_memory_stream_create): New
function.
* imap4d/append.c (imap4d_append0): Use static memory stream to
create a temporary message.
* imap4d/preauth.c (decode64_buf): Use static memory stream.
* libmailutils/base/rfc2047.c (mu_rfc2047_decode): Likewise.
* libmailutils/mime/mimehdr.c (mu_mimehdr_decode_param): Likewise.
* libmu_auth/ldap.c (chk_md5, chk_sha, chk_ssha): Likewise.
* libmu_sieve/extensions/vacation.c (build_mime): Likewise.
1 parent 27f9a867
......@@ -74,12 +74,6 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
if (mu_message_create (&msg, &tm))
return 1;
if (mu_memory_stream_create (&stream, MU_STREAM_RDWR))
{
mu_message_destroy (&msg, &tm);
return 1;
}
/* If a date_time is specified, the internal date SHOULD be set in the
resulting message; otherwise, the internal date of the resulting
message is set to the current date and time by default. */
......@@ -96,10 +90,14 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
tm = gmtime (&t);
while (*text && mu_isblank (*text))
text++;
text = mu_str_skip_class (text, MU_CTYPE_BLANK);
if (mu_static_memory_stream_create (&stream, text, strlen (text)))
{
mu_message_destroy (&msg, &tm);
return 1;
}
mu_stream_write (stream, text, strlen (text), NULL);
mu_message_set_stream (msg, stream, &tm);
mu_message_set_size (msg, _append_size, &tm);
......
......@@ -192,10 +192,9 @@ decode64_buf (const char *name, unsigned char **pbuf, size_t *psize)
name++;
namelen = strlen (name) - 1;
mu_memory_stream_create (&str, 0);
mu_static_memory_stream_create (&str, name, namelen);
mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
mu_stream_write (str, name, namelen, NULL);
mu_stream_read (flt, buf, sizeof buf, &size);
mu_stream_destroy (&flt);
*pbuf = malloc (size);
......
......@@ -205,6 +205,9 @@ int mu_prog_stream_create (mu_stream_t *pstream, const char *progname, int flags
int mu_filter_prog_stream_create (mu_stream_t *pstream, const char *progname,
mu_stream_t input);
int mu_memory_stream_create (mu_stream_t *pstream, int flags);
int mu_static_memory_stream_create (mu_stream_t *pstream, const void *mem,
size_t size);
int mu_mapfile_stream_create (mu_stream_t *pstream, const char *filename,
int flags);
int mu_socket_stream_create (mu_stream_t *pstream, const char *filename,
......
......@@ -160,8 +160,7 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
if (status != 0)
break;
mu_memory_stream_create (&in_stream, 0);
mu_stream_write (in_stream, encoded_text, size, NULL);
mu_static_memory_stream_create (&in_stream, encoded_text, size);
mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL);
status = mu_decode_filter (&filter, in_stream, filter_type, fromcode,
tocode);
......@@ -266,12 +265,9 @@ mu_rfc2047_encode (const char *charset, const char *encoding,
else if (encoding[1] || !strchr ("BQ", encoding[0]))
return MU_ERR_BAD_2047_ENCODING;
rc = mu_memory_stream_create (&input_stream, 0);
rc = mu_static_memory_stream_create (&input_stream, text, strlen (text));
if (rc)
return rc;
mu_stream_write (input_stream, text, strlen (text), NULL);
mu_stream_seek (input_stream, 0, MU_SEEK_SET, NULL);
rc = mu_filter_create (&output_stream, input_stream,
encoding, MU_FILTER_ENCODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
......
......@@ -506,10 +506,8 @@ mu_mimehdr_decode_param (const char *value, int flags,
size_t total = 0, pos;
size_t nbytes;
rc = mu_memory_stream_create (&instr, 0);
if (rc)
break;
rc = mu_stream_write (instr, data, strlen (data), NULL);
rc = mu_static_memory_stream_create (&instr, data,
strlen (data));
if (rc)
break;
......
......@@ -224,3 +224,33 @@ mu_memory_stream_create (mu_stream_t *pstream, int flags)
return rc;
}
int
mu_static_memory_stream_create (mu_stream_t *pstream, const void *mem,
size_t size)
{
mu_stream_t stream;
struct _mu_memory_stream *str;
str = (struct _mu_memory_stream *)
_mu_stream_create (sizeof (*str), MU_STREAM_READ | MU_STREAM_SEEK);
if (!str)
return ENOMEM;
str->ptr = (void*) mem;
str->size = size;
str->offset = 0;
str->capacity = size;
str->stream.flags |= _MU_STR_OPEN;
str->stream.read = _memory_read;
str->stream.size = _memory_size;
str->stream.ctl = _memory_ioctl;
str->stream.seek = _memory_seek;
stream = (mu_stream_t) str;
*pstream = stream;
return 0;
}
......
......@@ -598,10 +598,9 @@ chk_md5 (const char *db_pass, const char *pass)
mu_md5_process_bytes (pass, strlen (pass), &md5context);
mu_md5_finish_ctx (&md5context, md5digest);
mu_memory_stream_create (&str, 0);
mu_static_memory_stream_create (&str, db_pass, strlen (db_pass));
mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
mu_stream_write (str, db_pass, strlen (db_pass), NULL);
mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
mu_stream_destroy (&flt);
......@@ -620,11 +619,10 @@ chk_smd5 (const char *db_pass, const char *pass)
mu_stream_t str = NULL, flt = NULL;
size_t size;
mu_memory_stream_create (&str, 0);
size = strlen (db_pass);
mu_static_memory_stream_create (&str, db_pass, size);
mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
size = strlen (db_pass);
mu_stream_write (str, db_pass, size, NULL);
d1 = malloc (size);
if (!d1)
......@@ -665,10 +663,9 @@ chk_sha (const char *db_pass, const char *pass)
mu_sha1_process_bytes (pass, strlen (pass), &sha1context);
mu_sha1_finish_ctx (&sha1context, sha1digest);
mu_memory_stream_create (&str, 0);
mu_static_memory_stream_create (&str, db_pass, strlen (db_pass));
mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
mu_stream_write (str, db_pass, strlen (db_pass), NULL);
mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
mu_stream_destroy (&flt);
......@@ -687,11 +684,10 @@ chk_ssha (const char *db_pass, const char *pass)
mu_stream_t str = NULL, flt = NULL;
size_t size;
mu_memory_stream_create (&str, 0);
size = strlen (db_pass);
mu_static_memory_stream_create (&str, db_pass, size);
mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
size = strlen (db_pass);
mu_stream_write (str, db_pass, size, NULL);
d1 = malloc (size);
if (!d1)
......
......@@ -60,7 +60,7 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
mu_message_create (&newmsg, NULL);
mu_message_get_body (newmsg, &body);
if ((rc = mu_memory_stream_create (&input, MU_STREAM_RDWR)))
if ((rc = mu_static_memory_stream_create (&input, text, strlen (text))))
{
mu_sieve_error (mach,
_("cannot create temporary stream: %s"),
......@@ -70,8 +70,6 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
return 1;
}
mu_stream_write (input, text, strlen (text), NULL);
if (mu_sieve_tag_lookup (tags, "mime", NULL))
{
mu_stream_t fstr;
......