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, ...@@ -74,12 +74,6 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
74 if (mu_message_create (&msg, &tm)) 74 if (mu_message_create (&msg, &tm))
75 return 1; 75 return 1;
76 76
77 if (mu_memory_stream_create (&stream, MU_STREAM_RDWR))
78 {
79 mu_message_destroy (&msg, &tm);
80 return 1;
81 }
82
83 /* If a date_time is specified, the internal date SHOULD be set in the 77 /* If a date_time is specified, the internal date SHOULD be set in the
84 resulting message; otherwise, the internal date of the resulting 78 resulting message; otherwise, the internal date of the resulting
85 message is set to the current date and time by default. */ 79 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, ...@@ -96,10 +90,14 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
96 90
97 tm = gmtime (&t); 91 tm = gmtime (&t);
98 92
99 while (*text && mu_isblank (*text)) 93 text = mu_str_skip_class (text, MU_CTYPE_BLANK);
100 text++; 94
95 if (mu_static_memory_stream_create (&stream, text, strlen (text)))
96 {
97 mu_message_destroy (&msg, &tm);
98 return 1;
99 }
101 100
102 mu_stream_write (stream, text, strlen (text), NULL);
103 mu_message_set_stream (msg, stream, &tm); 101 mu_message_set_stream (msg, stream, &tm);
104 mu_message_set_size (msg, _append_size, &tm); 102 mu_message_set_size (msg, _append_size, &tm);
105 103
......
...@@ -192,10 +192,9 @@ decode64_buf (const char *name, unsigned char **pbuf, size_t *psize) ...@@ -192,10 +192,9 @@ decode64_buf (const char *name, unsigned char **pbuf, size_t *psize)
192 192
193 name++; 193 name++;
194 namelen = strlen (name) - 1; 194 namelen = strlen (name) - 1;
195 mu_memory_stream_create (&str, 0); 195 mu_static_memory_stream_create (&str, name, namelen);
196 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, 196 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
197 MU_STREAM_READ | MU_STREAM_AUTOCLOSE); 197 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
198 mu_stream_write (str, name, namelen, NULL);
199 mu_stream_read (flt, buf, sizeof buf, &size); 198 mu_stream_read (flt, buf, sizeof buf, &size);
200 mu_stream_destroy (&flt); 199 mu_stream_destroy (&flt);
201 *pbuf = malloc (size); 200 *pbuf = malloc (size);
......
...@@ -205,6 +205,9 @@ int mu_prog_stream_create (mu_stream_t *pstream, const char *progname, int flags ...@@ -205,6 +205,9 @@ int mu_prog_stream_create (mu_stream_t *pstream, const char *progname, int flags
205 int mu_filter_prog_stream_create (mu_stream_t *pstream, const char *progname, 205 int mu_filter_prog_stream_create (mu_stream_t *pstream, const char *progname,
206 mu_stream_t input); 206 mu_stream_t input);
207 int mu_memory_stream_create (mu_stream_t *pstream, int flags); 207 int mu_memory_stream_create (mu_stream_t *pstream, int flags);
208 int mu_static_memory_stream_create (mu_stream_t *pstream, const void *mem,
209 size_t size);
210
208 int mu_mapfile_stream_create (mu_stream_t *pstream, const char *filename, 211 int mu_mapfile_stream_create (mu_stream_t *pstream, const char *filename,
209 int flags); 212 int flags);
210 int mu_socket_stream_create (mu_stream_t *pstream, const char *filename, 213 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) ...@@ -160,8 +160,7 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
160 if (status != 0) 160 if (status != 0)
161 break; 161 break;
162 162
163 mu_memory_stream_create (&in_stream, 0); 163 mu_static_memory_stream_create (&in_stream, encoded_text, size);
164 mu_stream_write (in_stream, encoded_text, size, NULL);
165 mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL); 164 mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL);
166 status = mu_decode_filter (&filter, in_stream, filter_type, fromcode, 165 status = mu_decode_filter (&filter, in_stream, filter_type, fromcode,
167 tocode); 166 tocode);
...@@ -266,12 +265,9 @@ mu_rfc2047_encode (const char *charset, const char *encoding, ...@@ -266,12 +265,9 @@ mu_rfc2047_encode (const char *charset, const char *encoding,
266 else if (encoding[1] || !strchr ("BQ", encoding[0])) 265 else if (encoding[1] || !strchr ("BQ", encoding[0]))
267 return MU_ERR_BAD_2047_ENCODING; 266 return MU_ERR_BAD_2047_ENCODING;
268 267
269 rc = mu_memory_stream_create (&input_stream, 0); 268 rc = mu_static_memory_stream_create (&input_stream, text, strlen (text));
270 if (rc) 269 if (rc)
271 return rc; 270 return rc;
272
273 mu_stream_write (input_stream, text, strlen (text), NULL);
274 mu_stream_seek (input_stream, 0, MU_SEEK_SET, NULL);
275 rc = mu_filter_create (&output_stream, input_stream, 271 rc = mu_filter_create (&output_stream, input_stream,
276 encoding, MU_FILTER_ENCODE, 272 encoding, MU_FILTER_ENCODE,
277 MU_STREAM_READ | MU_STREAM_AUTOCLOSE); 273 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
......
...@@ -506,10 +506,8 @@ mu_mimehdr_decode_param (const char *value, int flags, ...@@ -506,10 +506,8 @@ mu_mimehdr_decode_param (const char *value, int flags,
506 size_t total = 0, pos; 506 size_t total = 0, pos;
507 size_t nbytes; 507 size_t nbytes;
508 508
509 rc = mu_memory_stream_create (&instr, 0); 509 rc = mu_static_memory_stream_create (&instr, data,
510 if (rc) 510 strlen (data));
511 break;
512 rc = mu_stream_write (instr, data, strlen (data), NULL);
513 if (rc) 511 if (rc)
514 break; 512 break;
515 513
......
...@@ -224,3 +224,33 @@ mu_memory_stream_create (mu_stream_t *pstream, int flags) ...@@ -224,3 +224,33 @@ mu_memory_stream_create (mu_stream_t *pstream, int flags)
224 return rc; 224 return rc;
225 } 225 }
226 226
227 int
228 mu_static_memory_stream_create (mu_stream_t *pstream, const void *mem,
229 size_t size)
230 {
231 mu_stream_t stream;
232 struct _mu_memory_stream *str;
233
234 str = (struct _mu_memory_stream *)
235 _mu_stream_create (sizeof (*str), MU_STREAM_READ | MU_STREAM_SEEK);
236
237 if (!str)
238 return ENOMEM;
239
240 str->ptr = (void*) mem;
241 str->size = size;
242 str->offset = 0;
243 str->capacity = size;
244
245 str->stream.flags |= _MU_STR_OPEN;
246 str->stream.read = _memory_read;
247 str->stream.size = _memory_size;
248 str->stream.ctl = _memory_ioctl;
249 str->stream.seek = _memory_seek;
250
251 stream = (mu_stream_t) str;
252 *pstream = stream;
253
254 return 0;
255 }
256
......
...@@ -598,10 +598,9 @@ chk_md5 (const char *db_pass, const char *pass) ...@@ -598,10 +598,9 @@ chk_md5 (const char *db_pass, const char *pass)
598 mu_md5_process_bytes (pass, strlen (pass), &md5context); 598 mu_md5_process_bytes (pass, strlen (pass), &md5context);
599 mu_md5_finish_ctx (&md5context, md5digest); 599 mu_md5_finish_ctx (&md5context, md5digest);
600 600
601 mu_memory_stream_create (&str, 0); 601 mu_static_memory_stream_create (&str, db_pass, strlen (db_pass));
602 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, 602 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
603 MU_STREAM_READ | MU_STREAM_AUTOCLOSE); 603 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
604 mu_stream_write (str, db_pass, strlen (db_pass), NULL);
605 604
606 mu_stream_read (flt, (char*) d1, sizeof d1, NULL); 605 mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
607 mu_stream_destroy (&flt); 606 mu_stream_destroy (&flt);
...@@ -620,11 +619,10 @@ chk_smd5 (const char *db_pass, const char *pass) ...@@ -620,11 +619,10 @@ chk_smd5 (const char *db_pass, const char *pass)
620 mu_stream_t str = NULL, flt = NULL; 619 mu_stream_t str = NULL, flt = NULL;
621 size_t size; 620 size_t size;
622 621
623 mu_memory_stream_create (&str, 0); 622 size = strlen (db_pass);
623 mu_static_memory_stream_create (&str, db_pass, size);
624 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, 624 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
625 MU_STREAM_READ | MU_STREAM_AUTOCLOSE); 625 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
626 size = strlen (db_pass);
627 mu_stream_write (str, db_pass, size, NULL);
628 626
629 d1 = malloc (size); 627 d1 = malloc (size);
630 if (!d1) 628 if (!d1)
...@@ -665,10 +663,9 @@ chk_sha (const char *db_pass, const char *pass) ...@@ -665,10 +663,9 @@ chk_sha (const char *db_pass, const char *pass)
665 mu_sha1_process_bytes (pass, strlen (pass), &sha1context); 663 mu_sha1_process_bytes (pass, strlen (pass), &sha1context);
666 mu_sha1_finish_ctx (&sha1context, sha1digest); 664 mu_sha1_finish_ctx (&sha1context, sha1digest);
667 665
668 mu_memory_stream_create (&str, 0); 666 mu_static_memory_stream_create (&str, db_pass, strlen (db_pass));
669 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, 667 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
670 MU_STREAM_READ | MU_STREAM_AUTOCLOSE); 668 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
671 mu_stream_write (str, db_pass, strlen (db_pass), NULL);
672 669
673 mu_stream_read (flt, (char*) d1, sizeof d1, NULL); 670 mu_stream_read (flt, (char*) d1, sizeof d1, NULL);
674 mu_stream_destroy (&flt); 671 mu_stream_destroy (&flt);
...@@ -687,11 +684,10 @@ chk_ssha (const char *db_pass, const char *pass) ...@@ -687,11 +684,10 @@ chk_ssha (const char *db_pass, const char *pass)
687 mu_stream_t str = NULL, flt = NULL; 684 mu_stream_t str = NULL, flt = NULL;
688 size_t size; 685 size_t size;
689 686
690 mu_memory_stream_create (&str, 0); 687 size = strlen (db_pass);
688 mu_static_memory_stream_create (&str, db_pass, size);
691 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE, 689 mu_filter_create (&flt, str, "base64", MU_FILTER_DECODE,
692 MU_STREAM_READ | MU_STREAM_AUTOCLOSE); 690 MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
693 size = strlen (db_pass);
694 mu_stream_write (str, db_pass, size, NULL);
695 691
696 d1 = malloc (size); 692 d1 = malloc (size);
697 if (!d1) 693 if (!d1)
......
...@@ -60,7 +60,7 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime, ...@@ -60,7 +60,7 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
60 mu_message_create (&newmsg, NULL); 60 mu_message_create (&newmsg, NULL);
61 mu_message_get_body (newmsg, &body); 61 mu_message_get_body (newmsg, &body);
62 62
63 if ((rc = mu_memory_stream_create (&input, MU_STREAM_RDWR))) 63 if ((rc = mu_static_memory_stream_create (&input, text, strlen (text))))
64 { 64 {
65 mu_sieve_error (mach, 65 mu_sieve_error (mach,
66 _("cannot create temporary stream: %s"), 66 _("cannot create temporary stream: %s"),
...@@ -70,8 +70,6 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime, ...@@ -70,8 +70,6 @@ build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime,
70 return 1; 70 return 1;
71 } 71 }
72 72
73 mu_stream_write (input, text, strlen (text), NULL);
74
75 if (mu_sieve_tag_lookup (tags, "mime", NULL)) 73 if (mu_sieve_tag_lookup (tags, "mime", NULL))
76 { 74 {
77 mu_stream_t fstr; 75 mu_stream_t fstr;
......