Add `gnutls/dtls.h' to the distribution.
[gnutls.git] / lib / opencdk / stream.c
blob29bea09bac8e47d5edc0049b550bf41e6108faa4
1 /* stream.c - The stream implementation
2 * Copyright (C) 2002, 2003, 2007, 2008, 2010 Free Software Foundation,
3 * Inc.
5 * Author: Timo Schulz
7 * This file is part of OpenCDK.
9 * The OpenCDK library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <assert.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #include "opencdk.h"
39 #include "main.h"
40 #include "filters.h"
41 #include "stream.h"
42 #include "types.h"
44 /* This is the maximal amount of bytes we map. */
45 #define MAX_MAP_SIZE 16777216
47 static cdk_error_t stream_flush (cdk_stream_t s);
48 static cdk_error_t stream_filter_write (cdk_stream_t s);
49 static int stream_cache_flush (cdk_stream_t s, FILE * fp);
50 struct stream_filter_s *filter_add (cdk_stream_t s, filter_fnct_t fnc,
51 int type);
54 /* FIXME: The read/write/putc/getc function cannot directly
55 return an error code. It is stored in an error variable
56 inside the string. Right now there is no code to
57 return the error code or to reset it. */
59 /**
60 * cdk_stream_open:
61 * @file: The file to open
62 * @ret_s: The new STREAM object
64 * Creates a new stream based on an existing file. The stream is
65 * opened in read-only mode.
66 **/
67 cdk_error_t
68 cdk_stream_open (const char *file, cdk_stream_t * ret_s)
70 return _cdk_stream_open_mode (file, "rb", ret_s);
74 /* Helper function to allow to open a stream in different modes. */
75 cdk_error_t
76 _cdk_stream_open_mode (const char *file, const char *mode,
77 cdk_stream_t * ret_s)
79 cdk_stream_t s;
81 if (!file || !ret_s)
83 gnutls_assert ();
84 return CDK_Inv_Value;
87 _gnutls_read_log ("open stream `%s'\n", file);
88 *ret_s = NULL;
89 s = cdk_calloc (1, sizeof *s);
90 if (!s)
92 gnutls_assert ();
93 return CDK_Out_Of_Core;
95 s->fname = cdk_strdup (file);
96 if (!s->fname)
98 cdk_free (s);
99 gnutls_assert ();
100 return CDK_Out_Of_Core;
102 s->fp = fopen (file, mode);
103 if (!s->fp)
105 cdk_free (s->fname);
106 cdk_free (s);
107 gnutls_assert ();
108 return CDK_File_Error;
110 _gnutls_read_log ("open stream fd=%d\n", fileno (s->fp));
111 s->flags.write = 0;
112 *ret_s = s;
113 return 0;
118 * cdk_stream_new_from_cbs:
119 * @cbs: the callback context with all user callback functions
120 * @opa: opaque handle which is passed to all callbacks.
121 * @ret_s: the allocated stream
123 * This function creates a stream which uses user callback
124 * for the core operations (open, close, read, write, seek).
126 cdk_error_t
127 cdk_stream_new_from_cbs (cdk_stream_cbs_t cbs, void *opa,
128 cdk_stream_t * ret_s)
130 cdk_stream_t s;
132 if (!cbs || !opa || !ret_s)
134 gnutls_assert ();
135 return CDK_Inv_Value;
138 *ret_s = NULL;
139 s = cdk_calloc (1, sizeof *s);
140 if (!s)
142 gnutls_assert ();
143 return CDK_Out_Of_Core;
146 s->cbs.read = cbs->read;
147 s->cbs.write = cbs->write;
148 s->cbs.seek = cbs->seek;
149 s->cbs.release = cbs->release;
150 s->cbs.open = cbs->open;
151 s->cbs_hd = opa;
152 *ret_s = s;
154 /* If there is a user callback for open, we need to call it
155 here because read/write expects an open stream. */
156 if (s->cbs.open)
157 return s->cbs.open (s->cbs_hd);
158 return 0;
163 * cdk_stream_new:
164 * @file: The name of the new file
165 * @ret_s: The new STREAM object
167 * Create a new stream into the given file.
169 cdk_error_t
170 cdk_stream_new (const char *file, cdk_stream_t * ret_s)
172 cdk_stream_t s;
174 if (!ret_s)
176 gnutls_assert ();
177 return CDK_Inv_Value;
180 _gnutls_read_log ("new stream `%s'\n", file ? file : "[temp]");
181 *ret_s = NULL;
182 s = cdk_calloc (1, sizeof *s);
183 if (!s)
185 gnutls_assert ();
186 return CDK_Out_Of_Core;
188 s->flags.write = 1;
189 if (!file)
190 s->flags.temp = 1;
191 else
193 s->fname = cdk_strdup (file);
194 if (!s->fname)
196 cdk_free (s);
197 gnutls_assert ();
198 return CDK_Out_Of_Core;
201 s->fp = _cdk_tmpfile ();
202 if (!s->fp)
204 cdk_free (s->fname);
205 cdk_free (s);
206 gnutls_assert ();
207 return CDK_File_Error;
209 _gnutls_read_log ("new stream fd=%d\n", fileno (s->fp));
210 *ret_s = s;
211 return 0;
215 * cdk_stream_create:
216 * @file: the filename
217 * @ret_s: the object
219 * Creates a new stream.
220 * The difference to cdk_stream_new is, that no filtering can be used with
221 * this kind of stream and everything is written directly to the stream.
223 cdk_error_t
224 cdk_stream_create (const char *file, cdk_stream_t * ret_s)
226 cdk_stream_t s;
228 if (!file || !ret_s)
230 gnutls_assert ();
231 return CDK_Inv_Value;
234 _gnutls_read_log ("create stream `%s'\n", file);
235 *ret_s = NULL;
236 s = cdk_calloc (1, sizeof *s);
237 if (!s)
239 gnutls_assert ();
240 return CDK_Out_Of_Core;
242 s->flags.write = 1;
243 s->flags.filtrated = 1;
244 s->fname = cdk_strdup (file);
245 if (!s->fname)
247 cdk_free (s);
248 gnutls_assert ();
249 return CDK_Out_Of_Core;
251 s->fp = fopen (file, "w+b");
252 if (!s->fp)
254 cdk_free (s->fname);
255 cdk_free (s);
256 gnutls_assert ();
257 return CDK_File_Error;
259 _gnutls_read_log ("stream create fd=%d\n", fileno (s->fp));
260 *ret_s = s;
261 return 0;
266 * cdk_stream_tmp_new:
267 * @r_out: the new temp stream.
269 * Allocates a new tempory stream which is not associated with a file.
271 cdk_error_t
272 cdk_stream_tmp_new (cdk_stream_t * r_out)
274 return cdk_stream_new (NULL, r_out);
280 * cdk_stream_tmp_from_mem:
281 * @buf: the buffer which shall be written to the temp stream.
282 * @buflen: how large the buffer is
283 * @r_out: the new stream with the given contents.
285 * Creates a new tempory stream with the given contests.
287 cdk_error_t
288 cdk_stream_tmp_from_mem (const void *buf, size_t buflen, cdk_stream_t * r_out)
290 cdk_stream_t s;
291 cdk_error_t rc;
292 int nwritten;
294 *r_out = NULL;
295 rc = cdk_stream_tmp_new (&s);
296 if (rc)
298 gnutls_assert ();
299 return rc;
302 nwritten = cdk_stream_write (s, buf, buflen);
303 if (nwritten == EOF)
305 cdk_stream_close (s);
306 gnutls_assert ();
307 return s->error;
309 cdk_stream_seek (s, 0);
310 *r_out = s;
311 return 0;
315 cdk_error_t
316 _cdk_stream_fpopen (FILE * fp, unsigned write_mode, cdk_stream_t * ret_out)
318 cdk_stream_t s;
320 *ret_out = NULL;
321 s = cdk_calloc (1, sizeof *s);
322 if (!s)
324 gnutls_assert ();
325 return CDK_Out_Of_Core;
328 _gnutls_read_log ("stream ref fd=%d\n", fileno (fp));
329 s->fp = fp;
330 s->fp_ref = 1;
331 s->flags.filtrated = 1;
332 s->flags.write = write_mode;
334 *ret_out = s;
335 return 0;
339 cdk_error_t
340 _cdk_stream_append (const char *file, cdk_stream_t * ret_s)
342 cdk_stream_t s;
343 cdk_error_t rc;
345 if (!ret_s)
347 gnutls_assert ();
348 return CDK_Inv_Value;
350 *ret_s = NULL;
352 rc = _cdk_stream_open_mode (file, "a+b", &s);
353 if (rc)
355 gnutls_assert ();
356 return rc;
359 /* In the append mode, we need to write to the flag. */
360 s->flags.write = 1;
361 *ret_s = s;
362 return 0;
366 * cdk_stream_is_compressed:
367 * @s: the stream
369 * Check whether stream is compressed.
371 * Returns: 0 if the stream is uncompressed, otherwise the compression
372 * algorithm.
375 cdk_stream_is_compressed (cdk_stream_t s)
377 if (!s)
378 return 0;
379 return s->flags.compressed;
382 void
383 _cdk_stream_set_compress_algo (cdk_stream_t s, int algo)
385 if (!s)
386 return;
387 s->flags.compressed = algo;
391 cdk_error_t
392 cdk_stream_flush (cdk_stream_t s)
394 cdk_error_t rc;
396 if (!s)
398 gnutls_assert ();
399 return CDK_Inv_Value;
402 /* The user callback does not support flush */
403 if (s->cbs_hd)
404 return 0;
406 /* For read-only streams, no flush is needed. */
407 if (!s->flags.write)
408 return 0;
410 if (!s->flags.filtrated)
412 if (!cdk_stream_get_length (s))
413 return 0;
414 rc = cdk_stream_seek (s, 0);
415 if (!rc)
416 rc = stream_flush (s);
417 if (!rc)
418 rc = stream_filter_write (s);
419 s->flags.filtrated = 1;
420 if (rc)
422 s->error = rc;
423 gnutls_assert ();
424 return rc;
427 return 0;
431 void
432 cdk_stream_tmp_set_mode (cdk_stream_t s, int val)
434 if (s && s->flags.temp)
435 s->fmode = val;
440 * cdk_stream_close:
441 * @s: The STREAM object.
443 * Close a stream and flush all buffers. This function work different
444 * for read or write streams. When the stream is for reading, the
445 * filtering is already done and we can simply close the file and all
446 * buffers. But for the case it's a write stream, we need to apply
447 * all registered filters now. The file is closed in the filter
448 * function and not here.
450 cdk_error_t
451 cdk_stream_close (cdk_stream_t s)
453 struct stream_filter_s *f, *f2;
454 cdk_error_t rc;
456 if (!s)
458 gnutls_assert ();
459 return CDK_Inv_Value;
462 _gnutls_read_log ("close stream ref=%d `%s'\n",
463 s->fp_ref, s->fname ? s->fname : "[temp]");
465 /* In the user callback mode, we call the release cb if possible
466 and just free the stream. */
467 if (s->cbs_hd)
469 if (s->cbs.release)
470 rc = s->cbs.release (s->cbs_hd);
471 else
472 rc = 0;
473 cdk_free (s);
474 gnutls_assert ();
475 return rc;
479 rc = 0;
480 if (!s->flags.filtrated && !s->error)
481 rc = cdk_stream_flush (s);
482 if (!s->fp_ref && (s->fname || s->flags.temp))
484 int err;
486 _gnutls_read_log ("close stream fd=%d\n", fileno (s->fp));
487 err = fclose (s->fp);
488 s->fp = NULL;
489 if (err)
490 rc = CDK_File_Error;
493 /* Iterate over the filter list and use the cleanup flag to
494 free the allocated internal structures. */
495 f = s->filters;
496 while (f)
498 f2 = f->next;
499 if (f->fnct)
500 f->fnct (f->opaque, STREAMCTL_FREE, NULL, NULL);
501 cdk_free (f);
502 f = f2;
505 if (s->fname)
507 cdk_free (s->fname);
508 s->fname = NULL;
511 cdk_free (s->cache.buf);
512 s->cache.alloced = 0;
514 cdk_free (s);
515 gnutls_assert ();
516 return rc;
521 * cdk_stream_eof:
522 * @s: The STREAM object.
524 * Return if the associated file handle was set to EOF. This
525 * function will only work with read streams.
528 cdk_stream_eof (cdk_stream_t s)
530 return s ? s->flags.eof : -1;
534 const char *
535 _cdk_stream_get_fname (cdk_stream_t s)
537 if (!s)
538 return NULL;
539 if (s->flags.temp)
540 return NULL;
541 return s->fname ? s->fname : NULL;
545 /* Return the underlying FP of the stream.
546 WARNING: This handle should not be closed. */
547 FILE *
548 _cdk_stream_get_fp (cdk_stream_t s)
550 return s ? s->fp : NULL;
555 _cdk_stream_get_errno (cdk_stream_t s)
557 return s ? s->error : CDK_Inv_Value;
562 * cdk_stream_get_length:
563 * @s: The STREAM object.
565 * Return the length of the associated file handle. This function
566 * should work for both read and write streams. For write streams an
567 * additional flush is used to write possible pending data.
569 off_t
570 cdk_stream_get_length (cdk_stream_t s)
572 struct stat statbuf;
573 cdk_error_t rc;
575 if (!s)
577 gnutls_assert ();
578 return (off_t) - 1;
581 /* The user callback does not support stat. */
582 if (s->cbs_hd)
583 return 0;
585 rc = stream_flush (s);
586 if (rc)
588 s->error = rc;
589 gnutls_assert ();
590 return (off_t) - 1;
593 if (fstat (fileno (s->fp), &statbuf))
595 s->error = CDK_File_Error;
596 gnutls_assert ();
597 return (off_t) - 1;
600 return statbuf.st_size;
604 static struct stream_filter_s *
605 filter_add2 (cdk_stream_t s)
607 struct stream_filter_s *f;
609 assert (s);
611 f = cdk_calloc (1, sizeof *f);
612 if (!f)
613 return NULL;
614 f->next = s->filters;
615 s->filters = f;
616 return f;
620 static struct stream_filter_s *
621 filter_search (cdk_stream_t s, filter_fnct_t fnc)
623 struct stream_filter_s *f;
625 assert (s);
627 for (f = s->filters; f; f = f->next)
629 if (f->fnct == fnc)
630 return f;
633 return NULL;
636 static inline void
637 set_opaque (struct stream_filter_s *f)
639 switch (f->type)
641 case fARMOR:
642 f->opaque = &f->u.afx;
643 break;
644 case fCIPHER:
645 f->opaque = &f->u.cfx;
646 break;
647 case fLITERAL:
648 f->opaque = &f->u.pfx;
649 break;
650 case fCOMPRESS:
651 f->opaque = &f->u.zfx;
652 break;
653 case fHASH:
654 f->opaque = &f->u.mfx;
655 break;
656 case fTEXT:
657 f->opaque = &f->u.tfx;
658 break;
659 default:
660 f->opaque = NULL;
665 struct stream_filter_s *
666 filter_add (cdk_stream_t s, filter_fnct_t fnc, int type)
668 struct stream_filter_s *f;
670 assert (s);
672 s->flags.filtrated = 0;
673 f = filter_search (s, fnc);
674 if (f)
675 return f;
676 f = filter_add2 (s);
677 if (!f)
678 return NULL;
679 f->fnct = fnc;
680 f->flags.enabled = 1;
681 f->tmp = NULL;
682 f->type = type;
684 set_opaque (f);
686 return f;
689 static int
690 stream_get_mode (cdk_stream_t s)
692 assert (s);
694 if (s->flags.temp)
695 return s->fmode;
696 return s->flags.write;
700 static filter_fnct_t
701 stream_id_to_filter (int type)
703 switch (type)
705 case fARMOR:
706 return _cdk_filter_armor;
707 case fLITERAL:
708 return _cdk_filter_literal;
709 case fTEXT:
710 return _cdk_filter_text;
711 /* case fCIPHER : return _cdk_filter_cipher; */
712 /* case fCOMPRESS: return _cdk_filter_compress; */
713 default:
714 return NULL;
720 * cdk_stream_filter_disable:
721 * @s: The STREAM object
722 * @type: The numberic filter ID.
724 * Disables the filter with the type 'type'.
726 cdk_error_t
727 cdk_stream_filter_disable (cdk_stream_t s, int type)
729 struct stream_filter_s *f;
730 filter_fnct_t fnc;
732 if (!s)
734 gnutls_assert ();
735 return CDK_Inv_Value;
738 fnc = stream_id_to_filter (type);
739 if (!fnc)
741 gnutls_assert ();
742 return CDK_Inv_Value;
744 f = filter_search (s, fnc);
745 if (f)
746 f->flags.enabled = 0;
747 return 0;
751 /* WARNING: tmp should not be closed by the caller. */
752 static cdk_error_t
753 stream_fp_replace (cdk_stream_t s, FILE ** tmp)
755 int rc;
757 assert (s);
759 _gnutls_read_log ("replace stream fd=%d with fd=%d\n",
760 fileno (s->fp), fileno (*tmp));
761 rc = fclose (s->fp);
762 if (rc)
764 gnutls_assert ();
765 return CDK_File_Error;
767 s->fp = *tmp;
768 *tmp = NULL;
769 return 0;
773 /* This function is exactly like filter_read, except the fact that we can't
774 use tmpfile () all the time. That's why we open the real file when there
775 is no last filter. */
776 static cdk_error_t
777 stream_filter_write (cdk_stream_t s)
779 struct stream_filter_s *f;
780 cdk_error_t rc = 0;
782 assert (s);
784 if (s->flags.filtrated)
786 gnutls_assert ();
787 return CDK_Inv_Value;
790 for (f = s->filters; f; f = f->next)
792 if (!f->flags.enabled)
793 continue;
794 /* if there is no next filter, create the final output file */
795 _gnutls_read_log ("filter [write]: last filter=%d fname=%s\n",
796 f->next ? 1 : 0, s->fname);
797 if (!f->next && s->fname)
798 f->tmp = fopen (s->fname, "w+b");
799 else
800 f->tmp = _cdk_tmpfile ();
801 if (!f->tmp)
803 rc = CDK_File_Error;
804 break;
806 /* If there is no next filter, flush the cache. We also do this
807 when the next filter is the armor filter because this filter
808 is special and before it starts, all data should be written. */
809 if ((!f->next || f->next->type == fARMOR) && s->cache.size)
811 rc = stream_cache_flush (s, f->tmp);
812 if (rc)
813 break;
815 rc = f->fnct (f->opaque, f->ctl, s->fp, f->tmp);
816 _gnutls_read_log ("filter [write]: type=%d rc=%d\n", f->type, rc);
817 if (!rc)
818 rc = stream_fp_replace (s, &f->tmp);
819 if (!rc)
820 rc = cdk_stream_seek (s, 0);
821 if (rc)
823 _gnutls_read_log ("filter [close]: fd=%d\n", fileno (f->tmp));
824 fclose (f->tmp);
825 break;
828 return rc;
832 /* Here all data from the file handle is passed through all filters.
833 The scheme works like this:
834 Create a tempfile and use it for the output of the filter. Then the
835 original file handle will be closed and replace with the temp handle.
836 The file pointer will be set to the begin and the game starts again. */
837 static cdk_error_t
838 stream_filter_read (cdk_stream_t s)
840 struct stream_filter_s *f;
841 cdk_error_t rc = 0;
843 assert (s);
845 if (s->flags.filtrated)
846 return 0;
848 for (f = s->filters; f; f = f->next)
850 if (!f->flags.enabled)
851 continue;
852 if (f->flags.error)
854 _gnutls_read_log ("filter %s [read]: has the error flag; skipped\n",
855 s->fname ? s->fname : "[temp]");
856 continue;
859 f->tmp = _cdk_tmpfile ();
860 if (!f->tmp)
862 rc = CDK_File_Error;
863 break;
865 rc = f->fnct (f->opaque, f->ctl, s->fp, f->tmp);
866 _gnutls_read_log ("filter %s [read]: type=%d rc=%d\n",
867 s->fname ? s->fname : "[temp]", f->type, rc);
868 if (rc)
870 f->flags.error = 1;
871 break;
874 f->flags.error = 0;
875 /* If the filter is read-only, do not replace the FP because
876 the contents were not altered in any way. */
877 if (!f->flags.rdonly)
879 rc = stream_fp_replace (s, &f->tmp);
880 if (rc)
881 break;
883 else
885 fclose (f->tmp);
886 f->tmp = NULL;
888 rc = cdk_stream_seek (s, 0);
889 if (rc)
890 break;
891 /* Disable the filter after it was successfully used. The idea
892 is the following: let's say the armor filter was pushed and
893 later more filters were added. The second time the filter code
894 will be executed, only the new filter should be started but
895 not the old because we already used it. */
896 f->flags.enabled = 0;
899 return rc;
903 void *
904 _cdk_stream_get_opaque (cdk_stream_t s, int fid)
906 struct stream_filter_s *f;
908 if (!s)
909 return NULL;
911 for (f = s->filters; f; f = f->next)
913 if ((int) f->type == fid)
914 return f->opaque;
916 return NULL;
921 * cdk_stream_read:
922 * @s: The STREAM object.
923 * @buf: The buffer to insert the readed bytes.
924 * @count: Request so much bytes.
926 * Tries to read count bytes from the STREAM object.
927 * When this function is called the first time, it can take a while
928 * because all filters need to be processed. Please remember that you
929 * need to add the filters in reserved order.
932 cdk_stream_read (cdk_stream_t s, void *buf, size_t buflen)
934 int nread;
935 int rc;
937 if (!s)
939 gnutls_assert ();
940 return EOF;
943 if (s->cbs_hd)
945 if (s->cbs.read)
946 return s->cbs.read (s->cbs_hd, buf, buflen);
947 return 0;
950 if (s->flags.write && !s->flags.temp)
952 s->error = CDK_Inv_Mode;
953 gnutls_assert ();
954 return EOF; /* This is a write stream */
957 if (!s->flags.no_filter && !s->cache.on && !s->flags.filtrated)
959 rc = stream_filter_read (s);
960 if (rc)
962 s->error = rc;
963 if (feof (s->fp))
964 s->flags.eof = 1;
965 gnutls_assert ();
966 return EOF;
968 s->flags.filtrated = 1;
971 if (!buf && !buflen)
972 return 0;
974 nread = fread (buf, 1, buflen, s->fp);
975 if (!nread)
976 nread = EOF;
978 if (feof (s->fp))
980 s->error = 0;
981 s->flags.eof = 1;
983 return nread;
988 cdk_stream_getc (cdk_stream_t s)
990 unsigned char buf[2];
991 int nread;
993 if (!s)
995 gnutls_assert ();
996 return EOF;
998 nread = cdk_stream_read (s, buf, 1);
999 if (nread == EOF)
1001 s->error = CDK_File_Error;
1002 gnutls_assert ();
1003 return EOF;
1005 return buf[0];
1010 * cdk_stream_write:
1011 * @s: The STREAM object
1012 * @buf: The buffer with the values to write.
1013 * @count: The size of the buffer.
1015 * Tries to write count bytes into the stream.
1016 * In this function we simply write the bytes to the stream. We can't
1017 * use the filters here because it would mean they have to support
1018 * partial flushing.
1021 cdk_stream_write (cdk_stream_t s, const void *buf, size_t count)
1023 int nwritten;
1025 if (!s)
1027 gnutls_assert ();
1028 return EOF;
1031 if (s->cbs_hd)
1033 if (s->cbs.write)
1034 return s->cbs.write (s->cbs_hd, buf, count);
1035 return 0;
1038 if (!s->flags.write)
1040 s->error = CDK_Inv_Mode; /* this is a read stream */
1041 gnutls_assert ();
1042 return EOF;
1045 if (!buf && !count)
1046 return stream_flush (s);
1048 if (s->cache.on)
1050 /* We need to resize the buffer if the additional data wouldn't
1051 fit into it. We allocate more memory to avoid to resize it the
1052 next time the function is used. */
1053 if (s->cache.size + count > s->cache.alloced)
1055 byte *old = s->cache.buf;
1057 s->cache.buf =
1058 cdk_calloc (1, s->cache.alloced + count + STREAM_BUFSIZE);
1059 s->cache.alloced += (count + STREAM_BUFSIZE);
1060 memcpy (s->cache.buf, old, s->cache.size);
1061 cdk_free (old);
1062 _gnutls_read_log ("stream: enlarge cache to %d octets\n",
1063 (int) s->cache.alloced);
1065 memcpy (s->cache.buf + s->cache.size, buf, count);
1066 s->cache.size += count;
1067 return count;
1070 nwritten = fwrite (buf, 1, count, s->fp);
1071 if (!nwritten)
1072 nwritten = EOF;
1073 return nwritten;
1078 cdk_stream_putc (cdk_stream_t s, int c)
1080 byte buf[2];
1081 int nwritten;
1083 if (!s)
1085 gnutls_assert ();
1086 return EOF;
1088 buf[0] = c;
1089 nwritten = cdk_stream_write (s, buf, 1);
1090 if (nwritten == EOF)
1091 return EOF;
1092 return 0;
1096 off_t
1097 cdk_stream_tell (cdk_stream_t s)
1099 return s ? ftell (s->fp) : (off_t) - 1;
1103 cdk_error_t
1104 cdk_stream_seek (cdk_stream_t s, off_t offset)
1106 off_t len;
1108 if (!s)
1110 gnutls_assert ();
1111 return CDK_Inv_Value;
1114 if (s->cbs_hd)
1116 if (s->cbs.seek)
1117 return s->cbs.seek (s->cbs_hd, offset);
1118 return 0;
1121 /* Set or reset the EOF flag. */
1122 len = cdk_stream_get_length (s);
1123 if (len == offset)
1124 s->flags.eof = 1;
1125 else
1126 s->flags.eof = 0;
1128 if (fseek (s->fp, offset, SEEK_SET))
1130 gnutls_assert ();
1131 return CDK_File_Error;
1133 return 0;
1137 static cdk_error_t
1138 stream_flush (cdk_stream_t s)
1140 assert (s);
1142 /* For some constellations it cannot be assured that the
1143 return value is defined, thus we ignore it for now. */
1144 (void) fflush (s->fp);
1145 return 0;
1150 * cdk_stream_set_armor_flag:
1151 * @s: the stream object
1152 * @type: the type of armor to use
1154 * If the file is in read-mode, no armor type needs to be
1155 * defined (armor_type=0) because the armor filter will be
1156 * used for decoding existing armor data.
1157 * For the write mode, @armor_type can be set to any valid
1158 * armor type (message, key, sig).
1160 cdk_error_t
1161 cdk_stream_set_armor_flag (cdk_stream_t s, int armor_type)
1163 struct stream_filter_s *f;
1165 if (!s)
1167 gnutls_assert ();
1168 return CDK_Inv_Value;
1170 f = filter_add (s, _cdk_filter_armor, fARMOR);
1171 if (!f)
1173 gnutls_assert ();
1174 return CDK_Out_Of_Core;
1176 f->u.afx.idx = f->u.afx.idx2 = armor_type;
1177 f->ctl = stream_get_mode (s);
1178 return 0;
1183 * cdk_stream_set_literal_flag:
1184 * @s: the stream object
1185 * @mode: the mode to use (binary, text, unicode)
1186 * @fname: the file name to store in the packet.
1188 * In read mode it kicks off the literal decoding routine to
1189 * unwrap the data from the packet. The @mode parameter is ignored.
1190 * In write mode the function can be used to wrap the stream data
1191 * into a literal packet with the given mode and file name.
1193 cdk_error_t
1194 cdk_stream_set_literal_flag (cdk_stream_t s, cdk_lit_format_t mode,
1195 const char *fname)
1197 struct stream_filter_s *f;
1198 const char *orig_fname;
1200 _gnutls_read_log ("stream: enable literal mode.\n");
1202 if (!s)
1204 gnutls_assert ();
1205 return CDK_Inv_Value;
1208 orig_fname = _cdk_stream_get_fname (s);
1209 f = filter_add (s, _cdk_filter_literal, fLITERAL);
1210 if (!f)
1212 gnutls_assert ();
1213 return CDK_Out_Of_Core;
1215 f->u.pfx.mode = mode;
1216 f->u.pfx.filename = fname ? cdk_strdup (fname) : NULL;
1217 f->u.pfx.orig_filename = orig_fname ? cdk_strdup (orig_fname) : NULL;
1218 f->ctl = stream_get_mode (s);
1219 if (s->blkmode > 0)
1221 f->u.pfx.blkmode.on = 1;
1222 f->u.pfx.blkmode.size = s->blkmode;
1224 return 0;
1229 * cdk_stream_set_compress_flag:
1230 * @s: the stream object
1231 * @algo: the compression algo
1232 * @level: level of compression (0..9)
1234 * In read mode it kicks off the decompression filter to retrieve
1235 * the uncompressed data.
1236 * In write mode the stream data will be compressed with the
1237 * given algorithm at the given level.
1239 cdk_error_t
1240 cdk_stream_set_compress_flag (cdk_stream_t s, int algo, int level)
1243 gnutls_assert ();
1244 return CDK_Not_Implemented;
1246 #if 0
1247 struct stream_filter_s *f;
1249 if (!s)
1250 return CDK_Inv_Value;
1251 f = filter_add (s, _cdk_filter_compress, fCOMPRESS);
1252 if (!f)
1253 return CDK_Out_Of_Core;
1254 f->ctl = stream_get_mode (s);
1255 f->u.zfx.algo = algo;
1256 f->u.zfx.level = level;
1257 return 0;
1258 #endif
1263 * cdk_stream_set_text_flag:
1264 * @s: the stream object
1265 * @lf: line ending
1267 * Pushes the text filter to store the stream data in cannoncial format.
1269 cdk_error_t
1270 cdk_stream_set_text_flag (cdk_stream_t s, const char *lf)
1272 struct stream_filter_s *f;
1274 if (!s)
1276 gnutls_assert ();
1277 return CDK_Inv_Value;
1279 f = filter_add (s, _cdk_filter_text, fTEXT);
1280 if (!f)
1282 gnutls_assert ();
1283 return CDK_Out_Of_Core;
1285 f->ctl = stream_get_mode (s);
1286 f->u.tfx.lf = lf;
1287 return 0;
1292 * cdk_stream_set_hash_flag:
1293 * @s: the stream object
1294 * @digest_algo: the digest algorithm to use
1296 * This is for read-only streams. It pushes a digest filter to
1297 * calculate the digest of the given stream data.
1299 cdk_error_t
1300 cdk_stream_set_hash_flag (cdk_stream_t s, int digest_algo)
1302 struct stream_filter_s *f;
1304 if (!s)
1306 gnutls_assert ();
1307 return CDK_Inv_Value;
1309 if (stream_get_mode (s))
1311 gnutls_assert ();
1312 return CDK_Inv_Mode;
1314 f = filter_add (s, _cdk_filter_hash, fHASH);
1315 if (!f)
1317 gnutls_assert ();
1318 return CDK_Out_Of_Core;
1320 f->ctl = stream_get_mode (s);
1321 f->u.mfx.digest_algo = digest_algo;
1322 f->flags.rdonly = 1;
1323 return 0;
1328 * cdk_stream_enable_cache:
1329 * @s: the stream object
1330 * @val: 1=on, 0=off
1332 * Enables or disable the cache section of a stream object.
1334 cdk_error_t
1335 cdk_stream_enable_cache (cdk_stream_t s, int val)
1337 if (!s)
1339 gnutls_assert ();
1340 return CDK_Inv_Value;
1342 if (!s->flags.write)
1344 gnutls_assert ();
1345 return CDK_Inv_Mode;
1347 s->cache.on = val;
1348 if (!s->cache.buf)
1350 s->cache.buf = cdk_calloc (1, STREAM_BUFSIZE);
1351 s->cache.alloced = STREAM_BUFSIZE;
1352 _gnutls_read_log ("stream: allocate cache of %d octets\n",
1353 STREAM_BUFSIZE);
1355 return 0;
1359 static int
1360 stream_cache_flush (cdk_stream_t s, FILE * fp)
1362 int nwritten;
1364 assert (s);
1366 /* FIXME: We should find a way to use cdk_stream_write here. */
1367 if (s->cache.size > 0)
1369 nwritten = fwrite (s->cache.buf, 1, s->cache.size, fp);
1370 if (!nwritten)
1372 gnutls_assert ();
1373 return CDK_File_Error;
1375 s->cache.size = 0;
1376 s->cache.on = 0;
1377 wipemem (s->cache.buf, s->cache.alloced);
1379 return 0;
1384 * cdk_stream_kick_off:
1385 * @inp: the input stream
1386 * @out: the output stream.
1388 * Passes the entire data from @inp into the output stream @out
1389 * with all the activated filters.
1391 cdk_error_t
1392 cdk_stream_kick_off (cdk_stream_t inp, cdk_stream_t out)
1394 byte buf[BUFSIZE];
1395 int nread, nwritten;
1396 cdk_error_t rc;
1398 if (!inp || !out)
1400 gnutls_assert ();
1401 return CDK_Inv_Value;
1403 rc = CDK_Success;
1404 while (!cdk_stream_eof (inp))
1406 nread = cdk_stream_read (inp, buf, DIM (buf));
1407 if (!nread || nread == EOF)
1408 break;
1409 nwritten = cdk_stream_write (out, buf, nread);
1410 if (!nwritten || nwritten == EOF)
1411 { /* In case of errors, we leave the loop. */
1412 rc = inp->error;
1413 break;
1417 wipemem (buf, sizeof (buf));
1418 return rc;
1423 * cdk_stream_mmap_part:
1424 * @s: the stream
1425 * @off: the offset where to start
1426 * @len: how much bytes shall be mapped
1427 * @ret_buf: the buffer to store the content
1428 * @ret_buflen: length of the buffer
1430 * Maps the data of the given stream into a memory section. @ret_count
1431 * contains the length of the buffer.
1433 cdk_error_t
1434 cdk_stream_mmap_part (cdk_stream_t s, off_t off, size_t len,
1435 byte ** ret_buf, size_t * ret_buflen)
1437 cdk_error_t rc;
1438 off_t oldpos;
1439 unsigned int n;
1441 if (!ret_buf || !ret_buflen)
1443 gnutls_assert ();
1444 return CDK_Inv_Value;
1446 *ret_buf = NULL;
1447 *ret_buflen = 0;
1449 if (!s)
1451 gnutls_assert ();
1452 return CDK_Inv_Value;
1455 /* Memory mapping is not supported on custom I/O objects. */
1456 if (s->cbs_hd)
1458 _gnutls_read_log ("cdk_stream_mmap_part: not supported on callbacks\n");
1459 gnutls_assert ();
1460 return CDK_Inv_Mode;
1463 oldpos = cdk_stream_tell (s);
1464 rc = cdk_stream_flush (s);
1465 if (rc)
1467 gnutls_assert ();
1468 return rc;
1470 rc = cdk_stream_seek (s, off);
1471 if (rc)
1473 gnutls_assert ();
1474 return rc;
1476 if (!len)
1477 len = cdk_stream_get_length (s);
1478 if (!len)
1480 _gnutls_read_log ("cdk_stream_mmap_part: invalid file size %lu\n", len);
1481 gnutls_assert ();
1482 return s->error;
1484 if (len > MAX_MAP_SIZE)
1486 gnutls_assert ();
1487 return CDK_Too_Short;
1490 *ret_buf = cdk_calloc (1, len + 1);
1491 *ret_buflen = len;
1492 n = cdk_stream_read (s, *ret_buf, len);
1493 if (n != len)
1494 *ret_buflen = n;
1495 rc = cdk_stream_seek (s, oldpos);
1496 if (rc)
1497 gnutls_assert ();
1498 return rc;
1502 cdk_error_t
1503 cdk_stream_mmap (cdk_stream_t inp, byte ** buf, size_t * buflen)
1505 off_t len;
1507 /* We need to make sure all data is flushed before we retrieve the size. */
1508 cdk_stream_flush (inp);
1509 len = cdk_stream_get_length (inp);
1510 return cdk_stream_mmap_part (inp, 0, len, buf, buflen);
1515 * cdk_stream_peek:
1516 * @inp: the input stream handle
1517 * @s: buffer
1518 * @count: number of bytes to peek
1520 * The function acts like cdk_stream_read with the difference that
1521 * the file pointer is moved to the old position after the bytes were read.
1524 cdk_stream_peek (cdk_stream_t inp, byte * buf, size_t buflen)
1526 off_t off;
1527 int nbytes;
1529 if (!inp || !buf)
1530 return 0;
1531 if (inp->cbs_hd)
1532 return 0;
1534 off = cdk_stream_tell (inp);
1535 nbytes = cdk_stream_read (inp, buf, buflen);
1536 if (nbytes == -1)
1537 return 0;
1538 if (cdk_stream_seek (inp, off))
1539 return 0;
1540 return nbytes;
1544 /* Try to read a line from the given stream. */
1546 _cdk_stream_gets (cdk_stream_t s, char *buf, size_t count)
1548 int c, i;
1550 assert (s);
1552 i = 0;
1553 while (!cdk_stream_eof (s) && count > 0)
1555 c = cdk_stream_getc (s);
1556 if (c == EOF || c == '\r' || c == '\n')
1558 buf[i++] = '\0';
1559 break;
1561 buf[i++] = c;
1562 count--;
1564 return i;
1568 /* Try to write string into the stream @s. */
1570 _cdk_stream_puts (cdk_stream_t s, const char *buf)
1572 return cdk_stream_write (s, buf, strlen (buf));
1576 /* Activate the block mode for the given stream. */
1577 cdk_error_t
1578 _cdk_stream_set_blockmode (cdk_stream_t s, size_t nbytes)
1580 assert (s);
1582 _gnutls_read_log ("stream: activate block mode with blocksize %d\n",
1583 (int) nbytes);
1584 s->blkmode = nbytes;
1585 return 0;
1589 /* Return the block mode state of the given stream. */
1591 _cdk_stream_get_blockmode (cdk_stream_t s)
1593 return s ? s->blkmode : 0;