1 /* stream.c - The stream implementation
2 * Copyright (C) 2002, 2003, 2007, 2008, 2010 Free Software Foundation,
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,
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,
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. */
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.
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. */
76 _cdk_stream_open_mode (const char *file, const char *mode,
87 _gnutls_read_log ("open stream `%s'\n", file);
89 s = cdk_calloc (1, sizeof *s);
93 return CDK_Out_Of_Core;
95 s->fname = cdk_strdup (file);
100 return CDK_Out_Of_Core;
102 s->fp = fopen (file, mode);
108 return CDK_File_Error;
110 _gnutls_read_log ("open stream fd=%d\n", fileno (s->fp));
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).
127 cdk_stream_new_from_cbs (cdk_stream_cbs_t cbs, void *opa,
128 cdk_stream_t * ret_s)
132 if (!cbs || !opa || !ret_s)
135 return CDK_Inv_Value;
139 s = cdk_calloc (1, sizeof *s);
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;
154 /* If there is a user callback for open, we need to call it
155 here because read/write expects an open stream. */
157 return s->cbs.open (s->cbs_hd);
164 * @file: The name of the new file
165 * @ret_s: The new STREAM object
167 * Create a new stream into the given file.
170 cdk_stream_new (const char *file, cdk_stream_t * ret_s)
177 return CDK_Inv_Value;
180 _gnutls_read_log ("new stream `%s'\n", file ? file : "[temp]");
182 s = cdk_calloc (1, sizeof *s);
186 return CDK_Out_Of_Core;
193 s->fname = cdk_strdup (file);
198 return CDK_Out_Of_Core;
201 s->fp = _cdk_tmpfile ();
207 return CDK_File_Error;
209 _gnutls_read_log ("new stream fd=%d\n", fileno (s->fp));
216 * @file: the filename
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.
224 cdk_stream_create (const char *file, cdk_stream_t * ret_s)
231 return CDK_Inv_Value;
234 _gnutls_read_log ("create stream `%s'\n", file);
236 s = cdk_calloc (1, sizeof *s);
240 return CDK_Out_Of_Core;
243 s->flags.filtrated = 1;
244 s->fname = cdk_strdup (file);
249 return CDK_Out_Of_Core;
251 s->fp = fopen (file, "w+b");
257 return CDK_File_Error;
259 _gnutls_read_log ("stream create fd=%d\n", fileno (s->fp));
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.
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.
288 cdk_stream_tmp_from_mem (const void *buf, size_t buflen, cdk_stream_t * r_out)
295 rc = cdk_stream_tmp_new (&s);
302 nwritten = cdk_stream_write (s, buf, buflen);
305 cdk_stream_close (s);
309 cdk_stream_seek (s, 0);
316 _cdk_stream_fpopen (FILE * fp, unsigned write_mode, cdk_stream_t * ret_out)
321 s = cdk_calloc (1, sizeof *s);
325 return CDK_Out_Of_Core;
328 _gnutls_read_log ("stream ref fd=%d\n", fileno (fp));
331 s->flags.filtrated = 1;
332 s->flags.write = write_mode;
340 _cdk_stream_append (const char *file, cdk_stream_t * ret_s)
348 return CDK_Inv_Value;
352 rc = _cdk_stream_open_mode (file, "a+b", &s);
359 /* In the append mode, we need to write to the flag. */
366 * cdk_stream_is_compressed:
369 * Check whether stream is compressed.
371 * Returns: 0 if the stream is uncompressed, otherwise the compression
375 cdk_stream_is_compressed (cdk_stream_t s)
379 return s->flags.compressed;
383 _cdk_stream_set_compress_algo (cdk_stream_t s, int algo)
387 s->flags.compressed = algo;
392 cdk_stream_flush (cdk_stream_t s)
399 return CDK_Inv_Value;
402 /* The user callback does not support flush */
406 /* For read-only streams, no flush is needed. */
410 if (!s->flags.filtrated)
412 if (!cdk_stream_get_length (s))
414 rc = cdk_stream_seek (s, 0);
416 rc = stream_flush (s);
418 rc = stream_filter_write (s);
419 s->flags.filtrated = 1;
432 cdk_stream_tmp_set_mode (cdk_stream_t s, int val)
434 if (s && s->flags.temp)
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.
451 cdk_stream_close (cdk_stream_t s)
453 struct stream_filter_s *f, *f2;
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. */
470 rc = s->cbs.release (s->cbs_hd);
480 if (!s->flags.filtrated && !s->error)
481 rc = cdk_stream_flush (s);
482 if (!s->fp_ref && (s->fname || s->flags.temp))
486 _gnutls_read_log ("close stream fd=%d\n", fileno (s->fp));
487 err = fclose (s->fp);
493 /* Iterate over the filter list and use the cleanup flag to
494 free the allocated internal structures. */
500 f->fnct (f->opaque, STREAMCTL_FREE, NULL, NULL);
511 cdk_free (s->cache.buf);
512 s->cache.alloced = 0;
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;
535 _cdk_stream_get_fname (cdk_stream_t s)
541 return s->fname ? s->fname : NULL;
545 /* Return the underlying FP of the stream.
546 WARNING: This handle should not be closed. */
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.
570 cdk_stream_get_length (cdk_stream_t s)
581 /* The user callback does not support stat. */
585 rc = stream_flush (s);
593 if (fstat (fileno (s->fp), &statbuf))
595 s->error = CDK_File_Error;
600 return statbuf.st_size;
604 static struct stream_filter_s *
605 filter_add2 (cdk_stream_t s)
607 struct stream_filter_s *f;
611 f = cdk_calloc (1, sizeof *f);
614 f->next = s->filters;
620 static struct stream_filter_s *
621 filter_search (cdk_stream_t s, filter_fnct_t fnc)
623 struct stream_filter_s *f;
627 for (f = s->filters; f; f = f->next)
637 set_opaque (struct stream_filter_s *f)
642 f->opaque = &f->u.afx;
645 f->opaque = &f->u.cfx;
648 f->opaque = &f->u.pfx;
651 f->opaque = &f->u.zfx;
654 f->opaque = &f->u.mfx;
657 f->opaque = &f->u.tfx;
665 struct stream_filter_s *
666 filter_add (cdk_stream_t s, filter_fnct_t fnc, int type)
668 struct stream_filter_s *f;
672 s->flags.filtrated = 0;
673 f = filter_search (s, fnc);
680 f->flags.enabled = 1;
690 stream_get_mode (cdk_stream_t s)
696 return s->flags.write;
701 stream_id_to_filter (int type)
706 return _cdk_filter_armor;
708 return _cdk_filter_literal;
710 return _cdk_filter_text;
711 /* case fCIPHER : return _cdk_filter_cipher; */
712 /* case fCOMPRESS: return _cdk_filter_compress; */
720 * cdk_stream_filter_disable:
721 * @s: The STREAM object
722 * @type: The numberic filter ID.
724 * Disables the filter with the type 'type'.
727 cdk_stream_filter_disable (cdk_stream_t s, int type)
729 struct stream_filter_s *f;
735 return CDK_Inv_Value;
738 fnc = stream_id_to_filter (type);
742 return CDK_Inv_Value;
744 f = filter_search (s, fnc);
746 f->flags.enabled = 0;
751 /* WARNING: tmp should not be closed by the caller. */
753 stream_fp_replace (cdk_stream_t s, FILE ** tmp)
759 _gnutls_read_log ("replace stream fd=%d with fd=%d\n",
760 fileno (s->fp), fileno (*tmp));
766 return CDK_File_Error;
774 /* This function is exactly like filter_read, except the fact that we can't
775 use tmpfile () all the time. That's why we open the real file when there
776 is no last filter. */
778 stream_filter_write (cdk_stream_t s)
780 struct stream_filter_s *f;
785 if (s->flags.filtrated)
788 return CDK_Inv_Value;
791 for (f = s->filters; f; f = f->next)
793 if (!f->flags.enabled)
795 /* if there is no next filter, create the final output file */
796 _gnutls_read_log ("filter [write]: last filter=%d fname=%s\n",
797 f->next ? 1 : 0, s->fname);
798 if (!f->next && s->fname)
799 f->tmp = fopen (s->fname, "w+b");
801 f->tmp = _cdk_tmpfile ();
807 /* If there is no next filter, flush the cache. We also do this
808 when the next filter is the armor filter because this filter
809 is special and before it starts, all data should be written. */
810 if ((!f->next || f->next->type == fARMOR) && s->cache.size)
812 rc = stream_cache_flush (s, f->tmp);
816 rc = f->fnct (f->opaque, f->ctl, s->fp, f->tmp);
817 _gnutls_read_log ("filter [write]: type=%d rc=%d\n", f->type, rc);
819 rc = stream_fp_replace (s, &f->tmp);
821 rc = cdk_stream_seek (s, 0);
824 _gnutls_read_log ("filter [close]: fd=%d\n", fileno (f->tmp));
834 /* Here all data from the file handle is passed through all filters.
835 The scheme works like this:
836 Create a tempfile and use it for the output of the filter. Then the
837 original file handle will be closed and replace with the temp handle.
838 The file pointer will be set to the begin and the game starts again. */
840 stream_filter_read (cdk_stream_t s)
842 struct stream_filter_s *f;
847 if (s->flags.filtrated)
850 for (f = s->filters; f; f = f->next)
852 if (!f->flags.enabled)
856 _gnutls_read_log ("filter %s [read]: has the error flag; skipped\n",
857 s->fname ? s->fname : "[temp]");
861 f->tmp = _cdk_tmpfile ();
867 rc = f->fnct (f->opaque, f->ctl, s->fp, f->tmp);
868 _gnutls_read_log ("filter %s [read]: type=%d rc=%d\n",
869 s->fname ? s->fname : "[temp]", f->type, rc);
877 /* If the filter is read-only, do not replace the FP because
878 the contents were not altered in any way. */
879 if (!f->flags.rdonly)
881 rc = stream_fp_replace (s, &f->tmp);
890 rc = cdk_stream_seek (s, 0);
893 /* Disable the filter after it was successfully used. The idea
894 is the following: let's say the armor filter was pushed and
895 later more filters were added. The second time the filter code
896 will be executed, only the new filter should be started but
897 not the old because we already used it. */
898 f->flags.enabled = 0;
906 _cdk_stream_get_opaque (cdk_stream_t s, int fid)
908 struct stream_filter_s *f;
913 for (f = s->filters; f; f = f->next)
915 if ((int) f->type == fid)
924 * @s: The STREAM object.
925 * @buf: The buffer to insert the readed bytes.
926 * @count: Request so much bytes.
928 * Tries to read count bytes from the STREAM object.
929 * When this function is called the first time, it can take a while
930 * because all filters need to be processed. Please remember that you
931 * need to add the filters in reserved order.
934 cdk_stream_read (cdk_stream_t s, void *buf, size_t buflen)
948 return s->cbs.read (s->cbs_hd, buf, buflen);
952 if (s->flags.write && !s->flags.temp)
954 s->error = CDK_Inv_Mode;
956 return EOF; /* This is a write stream */
959 if (!s->flags.no_filter && !s->cache.on && !s->flags.filtrated)
961 rc = stream_filter_read (s);
965 if (s->fp && feof (s->fp))
970 s->flags.filtrated = 1;
976 nread = fread (buf, 1, buflen, s->fp);
990 cdk_stream_getc (cdk_stream_t s)
992 unsigned char buf[2];
1000 nread = cdk_stream_read (s, buf, 1);
1003 s->error = CDK_File_Error;
1013 * @s: The STREAM object
1014 * @buf: The buffer with the values to write.
1015 * @count: The size of the buffer.
1017 * Tries to write count bytes into the stream.
1018 * In this function we simply write the bytes to the stream. We can't
1019 * use the filters here because it would mean they have to support
1023 cdk_stream_write (cdk_stream_t s, const void *buf, size_t count)
1036 return s->cbs.write (s->cbs_hd, buf, count);
1040 if (!s->flags.write)
1042 s->error = CDK_Inv_Mode; /* this is a read stream */
1048 return stream_flush (s);
1052 /* We need to resize the buffer if the additional data wouldn't
1053 fit into it. We allocate more memory to avoid to resize it the
1054 next time the function is used. */
1055 if (s->cache.size + count > s->cache.alloced)
1057 byte *old = s->cache.buf;
1060 cdk_calloc (1, s->cache.alloced + count + STREAM_BUFSIZE);
1061 s->cache.alloced += (count + STREAM_BUFSIZE);
1062 memcpy (s->cache.buf, old, s->cache.size);
1064 _gnutls_read_log ("stream: enlarge cache to %d octets\n",
1065 (int) s->cache.alloced);
1067 memcpy (s->cache.buf + s->cache.size, buf, count);
1068 s->cache.size += count;
1072 nwritten = fwrite (buf, 1, count, s->fp);
1080 cdk_stream_putc (cdk_stream_t s, int c)
1091 nwritten = cdk_stream_write (s, buf, 1);
1092 if (nwritten == EOF)
1099 cdk_stream_tell (cdk_stream_t s)
1101 return s ? ftell (s->fp) : (off_t) - 1;
1106 cdk_stream_seek (cdk_stream_t s, off_t offset)
1113 return CDK_Inv_Value;
1119 return s->cbs.seek (s->cbs_hd, offset);
1123 /* Set or reset the EOF flag. */
1124 len = cdk_stream_get_length (s);
1130 if (fseek (s->fp, offset, SEEK_SET))
1133 return CDK_File_Error;
1140 stream_flush (cdk_stream_t s)
1144 /* For some constellations it cannot be assured that the
1145 return value is defined, thus we ignore it for now. */
1146 (void) fflush (s->fp);
1152 * cdk_stream_set_armor_flag:
1153 * @s: the stream object
1154 * @type: the type of armor to use
1156 * If the file is in read-mode, no armor type needs to be
1157 * defined (armor_type=0) because the armor filter will be
1158 * used for decoding existing armor data.
1159 * For the write mode, @armor_type can be set to any valid
1160 * armor type (message, key, sig).
1163 cdk_stream_set_armor_flag (cdk_stream_t s, int armor_type)
1165 struct stream_filter_s *f;
1170 return CDK_Inv_Value;
1172 f = filter_add (s, _cdk_filter_armor, fARMOR);
1176 return CDK_Out_Of_Core;
1178 f->u.afx.idx = f->u.afx.idx2 = armor_type;
1179 f->ctl = stream_get_mode (s);
1185 * cdk_stream_set_literal_flag:
1186 * @s: the stream object
1187 * @mode: the mode to use (binary, text, unicode)
1188 * @fname: the file name to store in the packet.
1190 * In read mode it kicks off the literal decoding routine to
1191 * unwrap the data from the packet. The @mode parameter is ignored.
1192 * In write mode the function can be used to wrap the stream data
1193 * into a literal packet with the given mode and file name.
1196 cdk_stream_set_literal_flag (cdk_stream_t s, cdk_lit_format_t mode,
1199 struct stream_filter_s *f;
1200 const char *orig_fname;
1202 _gnutls_read_log ("stream: enable literal mode.\n");
1207 return CDK_Inv_Value;
1210 orig_fname = _cdk_stream_get_fname (s);
1211 f = filter_add (s, _cdk_filter_literal, fLITERAL);
1215 return CDK_Out_Of_Core;
1217 f->u.pfx.mode = mode;
1218 f->u.pfx.filename = fname ? cdk_strdup (fname) : NULL;
1219 f->u.pfx.orig_filename = orig_fname ? cdk_strdup (orig_fname) : NULL;
1220 f->ctl = stream_get_mode (s);
1223 f->u.pfx.blkmode.on = 1;
1224 f->u.pfx.blkmode.size = s->blkmode;
1231 * cdk_stream_set_compress_flag:
1232 * @s: the stream object
1233 * @algo: the compression algo
1234 * @level: level of compression (0..9)
1236 * In read mode it kicks off the decompression filter to retrieve
1237 * the uncompressed data.
1238 * In write mode the stream data will be compressed with the
1239 * given algorithm at the given level.
1242 cdk_stream_set_compress_flag (cdk_stream_t s, int algo, int level)
1246 return CDK_Not_Implemented;
1249 struct stream_filter_s *f;
1252 return CDK_Inv_Value;
1253 f = filter_add (s, _cdk_filter_compress, fCOMPRESS);
1255 return CDK_Out_Of_Core;
1256 f->ctl = stream_get_mode (s);
1257 f->u.zfx.algo = algo;
1258 f->u.zfx.level = level;
1265 * cdk_stream_set_text_flag:
1266 * @s: the stream object
1269 * Pushes the text filter to store the stream data in cannoncial format.
1272 cdk_stream_set_text_flag (cdk_stream_t s, const char *lf)
1274 struct stream_filter_s *f;
1279 return CDK_Inv_Value;
1281 f = filter_add (s, _cdk_filter_text, fTEXT);
1285 return CDK_Out_Of_Core;
1287 f->ctl = stream_get_mode (s);
1294 * cdk_stream_set_hash_flag:
1295 * @s: the stream object
1296 * @digest_algo: the digest algorithm to use
1298 * This is for read-only streams. It pushes a digest filter to
1299 * calculate the digest of the given stream data.
1302 cdk_stream_set_hash_flag (cdk_stream_t s, int digest_algo)
1304 struct stream_filter_s *f;
1309 return CDK_Inv_Value;
1311 if (stream_get_mode (s))
1314 return CDK_Inv_Mode;
1316 f = filter_add (s, _cdk_filter_hash, fHASH);
1320 return CDK_Out_Of_Core;
1322 f->ctl = stream_get_mode (s);
1323 f->u.mfx.digest_algo = digest_algo;
1324 f->flags.rdonly = 1;
1330 * cdk_stream_enable_cache:
1331 * @s: the stream object
1334 * Enables or disable the cache section of a stream object.
1337 cdk_stream_enable_cache (cdk_stream_t s, int val)
1342 return CDK_Inv_Value;
1344 if (!s->flags.write)
1347 return CDK_Inv_Mode;
1352 s->cache.buf = cdk_calloc (1, STREAM_BUFSIZE);
1353 s->cache.alloced = STREAM_BUFSIZE;
1354 _gnutls_read_log ("stream: allocate cache of %d octets\n",
1362 stream_cache_flush (cdk_stream_t s, FILE * fp)
1368 /* FIXME: We should find a way to use cdk_stream_write here. */
1369 if (s->cache.size > 0)
1371 nwritten = fwrite (s->cache.buf, 1, s->cache.size, fp);
1375 return CDK_File_Error;
1379 wipemem (s->cache.buf, s->cache.alloced);
1386 * cdk_stream_kick_off:
1387 * @inp: the input stream
1388 * @out: the output stream.
1390 * Passes the entire data from @inp into the output stream @out
1391 * with all the activated filters.
1394 cdk_stream_kick_off (cdk_stream_t inp, cdk_stream_t out)
1397 int nread, nwritten;
1403 return CDK_Inv_Value;
1406 while (!cdk_stream_eof (inp))
1408 nread = cdk_stream_read (inp, buf, DIM (buf));
1409 if (!nread || nread == EOF)
1411 nwritten = cdk_stream_write (out, buf, nread);
1412 if (!nwritten || nwritten == EOF)
1413 { /* In case of errors, we leave the loop. */
1419 wipemem (buf, sizeof (buf));
1425 * cdk_stream_mmap_part:
1427 * @off: the offset where to start
1428 * @len: how much bytes shall be mapped
1429 * @ret_buf: the buffer to store the content
1430 * @ret_buflen: length of the buffer
1432 * Maps the data of the given stream into a memory section. @ret_count
1433 * contains the length of the buffer.
1436 cdk_stream_mmap_part (cdk_stream_t s, off_t off, size_t len,
1437 byte ** ret_buf, size_t * ret_buflen)
1443 if (!ret_buf || !ret_buflen)
1446 return CDK_Inv_Value;
1454 return CDK_Inv_Value;
1457 /* Memory mapping is not supported on custom I/O objects. */
1460 _gnutls_read_log ("cdk_stream_mmap_part: not supported on callbacks\n");
1462 return CDK_Inv_Mode;
1465 oldpos = cdk_stream_tell (s);
1466 rc = cdk_stream_flush (s);
1472 rc = cdk_stream_seek (s, off);
1479 len = cdk_stream_get_length (s);
1482 _gnutls_read_log ("cdk_stream_mmap_part: invalid file size %lu\n", len);
1486 if (len > MAX_MAP_SIZE)
1489 return CDK_Too_Short;
1492 *ret_buf = cdk_calloc (1, len + 1);
1494 n = cdk_stream_read (s, *ret_buf, len);
1497 rc = cdk_stream_seek (s, oldpos);
1505 cdk_stream_mmap (cdk_stream_t inp, byte ** buf, size_t * buflen)
1509 /* We need to make sure all data is flushed before we retrieve the size. */
1510 cdk_stream_flush (inp);
1511 len = cdk_stream_get_length (inp);
1512 return cdk_stream_mmap_part (inp, 0, len, buf, buflen);
1518 * @inp: the input stream handle
1520 * @count: number of bytes to peek
1522 * The function acts like cdk_stream_read with the difference that
1523 * the file pointer is moved to the old position after the bytes were read.
1526 cdk_stream_peek (cdk_stream_t inp, byte * buf, size_t buflen)
1536 off = cdk_stream_tell (inp);
1537 nbytes = cdk_stream_read (inp, buf, buflen);
1540 if (cdk_stream_seek (inp, off))
1546 /* Try to read a line from the given stream. */
1548 _cdk_stream_gets (cdk_stream_t s, char *buf, size_t count)
1555 while (!cdk_stream_eof (s) && count > 0)
1557 c = cdk_stream_getc (s);
1558 if (c == EOF || c == '\r' || c == '\n')
1570 /* Try to write string into the stream @s. */
1572 _cdk_stream_puts (cdk_stream_t s, const char *buf)
1574 return cdk_stream_write (s, buf, strlen (buf));
1578 /* Activate the block mode for the given stream. */
1580 _cdk_stream_set_blockmode (cdk_stream_t s, size_t nbytes)
1584 _gnutls_read_log ("stream: activate block mode with blocksize %d\n",
1586 s->blkmode = nbytes;
1591 /* Return the block mode state of the given stream. */
1593 _cdk_stream_get_blockmode (cdk_stream_t s)
1595 return s ? s->blkmode : 0;