1 /* stream.c - The stream implementation
2 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
6 * This file is part of OpenCDK.
8 * The OpenCDK library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
42 /* This is the maximal amount of bytes we map. */
43 #define MAX_MAP_SIZE 16777216
45 static cdk_error_t
stream_flush (cdk_stream_t s
);
46 static cdk_error_t
stream_filter_write (cdk_stream_t s
);
47 static int stream_cache_flush (cdk_stream_t s
, FILE * fp
);
48 struct stream_filter_s
*filter_add (cdk_stream_t s
, filter_fnct_t fnc
,
52 /* FIXME: The read/write/putc/getc function cannot directly
53 return an error code. It is stored in an error variable
54 inside the string. Right now there is no code to
55 return the error code or to reset it. */
59 * @file: The file to open
60 * @ret_s: The new STREAM object
62 * Creates a new stream based on an existing file. The stream is
63 * opened in read-only mode.
66 cdk_stream_open (const char *file
, cdk_stream_t
* ret_s
)
68 return _cdk_stream_open_mode (file
, "rb", ret_s
);
72 /* Helper function to allow to open a stream in different modes. */
74 _cdk_stream_open_mode (const char *file
, const char *mode
,
85 _gnutls_read_log ("open stream `%s'\n", file
);
87 s
= cdk_calloc (1, sizeof *s
);
91 return CDK_Out_Of_Core
;
93 s
->fname
= cdk_strdup (file
);
98 return CDK_Out_Of_Core
;
100 s
->fp
= fopen (file
, mode
);
106 return CDK_File_Error
;
108 _gnutls_read_log ("open stream fd=%d\n", fileno (s
->fp
));
116 * cdk_stream_new_from_cbs:
117 * @cbs: the callback context with all user callback functions
118 * @opa: uint8_t handle which is passed to all callbacks.
119 * @ret_s: the allocated stream
121 * This function creates a stream which uses user callback
122 * for the core operations (open, close, read, write, seek).
125 cdk_stream_new_from_cbs (cdk_stream_cbs_t cbs
, void *opa
,
126 cdk_stream_t
* ret_s
)
130 if (!cbs
|| !opa
|| !ret_s
)
133 return CDK_Inv_Value
;
137 s
= cdk_calloc (1, sizeof *s
);
141 return CDK_Out_Of_Core
;
144 s
->cbs
.read
= cbs
->read
;
145 s
->cbs
.write
= cbs
->write
;
146 s
->cbs
.seek
= cbs
->seek
;
147 s
->cbs
.release
= cbs
->release
;
148 s
->cbs
.open
= cbs
->open
;
152 /* If there is a user callback for open, we need to call it
153 here because read/write expects an open stream. */
155 return s
->cbs
.open (s
->cbs_hd
);
162 * @file: The name of the new file
163 * @ret_s: The new STREAM object
165 * Create a new stream into the given file.
168 cdk_stream_new (const char *file
, cdk_stream_t
* ret_s
)
175 return CDK_Inv_Value
;
178 _gnutls_read_log ("new stream `%s'\n", file
? file
: "[temp]");
180 s
= cdk_calloc (1, sizeof *s
);
184 return CDK_Out_Of_Core
;
191 s
->fname
= cdk_strdup (file
);
196 return CDK_Out_Of_Core
;
199 s
->fp
= _cdk_tmpfile ();
205 return CDK_File_Error
;
207 _gnutls_read_log ("new stream fd=%d\n", fileno (s
->fp
));
214 * @file: the filename
217 * Creates a new stream.
218 * The difference to cdk_stream_new is, that no filtering can be used with
219 * this kind of stream and everything is written directly to the stream.
222 cdk_stream_create (const char *file
, cdk_stream_t
* ret_s
)
229 return CDK_Inv_Value
;
232 _gnutls_read_log ("create stream `%s'\n", file
);
234 s
= cdk_calloc (1, sizeof *s
);
238 return CDK_Out_Of_Core
;
241 s
->flags
.filtrated
= 1;
242 s
->fname
= cdk_strdup (file
);
247 return CDK_Out_Of_Core
;
249 s
->fp
= fopen (file
, "w+b");
255 return CDK_File_Error
;
257 _gnutls_read_log ("stream create fd=%d\n", fileno (s
->fp
));
264 * cdk_stream_tmp_new:
265 * @r_out: the new temp stream.
267 * Allocates a new tempory stream which is not associated with a file.
270 cdk_stream_tmp_new (cdk_stream_t
* r_out
)
272 return cdk_stream_new (NULL
, r_out
);
278 * cdk_stream_tmp_from_mem:
279 * @buf: the buffer which shall be written to the temp stream.
280 * @buflen: how large the buffer is
281 * @r_out: the new stream with the given contents.
283 * Creates a new tempory stream with the given contests.
286 cdk_stream_tmp_from_mem (const void *buf
, size_t buflen
, cdk_stream_t
* r_out
)
293 rc
= cdk_stream_tmp_new (&s
);
300 nwritten
= cdk_stream_write (s
, buf
, buflen
);
303 cdk_stream_close (s
);
307 cdk_stream_seek (s
, 0);
314 _cdk_stream_fpopen (FILE * fp
, unsigned write_mode
, cdk_stream_t
* ret_out
)
319 s
= cdk_calloc (1, sizeof *s
);
323 return CDK_Out_Of_Core
;
326 _gnutls_read_log ("stream ref fd=%d\n", fileno (fp
));
329 s
->flags
.filtrated
= 1;
330 s
->flags
.write
= write_mode
;
338 _cdk_stream_append (const char *file
, cdk_stream_t
* ret_s
)
346 return CDK_Inv_Value
;
350 rc
= _cdk_stream_open_mode (file
, "a+b", &s
);
357 /* In the append mode, we need to write to the flag. */
364 * cdk_stream_is_compressed:
367 * Check whether stream is compressed.
369 * Returns: 0 if the stream is uncompressed, otherwise the compression
373 cdk_stream_is_compressed (cdk_stream_t s
)
377 return s
->flags
.compressed
;
381 _cdk_stream_set_compress_algo (cdk_stream_t s
, int algo
)
385 s
->flags
.compressed
= algo
;
390 cdk_stream_flush (cdk_stream_t s
)
397 return CDK_Inv_Value
;
400 /* The user callback does not support flush */
404 /* For read-only streams, no flush is needed. */
408 if (!s
->flags
.filtrated
)
410 if (!cdk_stream_get_length (s
))
412 rc
= cdk_stream_seek (s
, 0);
414 rc
= stream_flush (s
);
416 rc
= stream_filter_write (s
);
417 s
->flags
.filtrated
= 1;
430 cdk_stream_tmp_set_mode (cdk_stream_t s
, int val
)
432 if (s
&& s
->flags
.temp
)
439 * @s: The STREAM object.
441 * Close a stream and flush all buffers. This function work different
442 * for read or write streams. When the stream is for reading, the
443 * filtering is already done and we can simply close the file and all
444 * buffers. But for the case it's a write stream, we need to apply
445 * all registered filters now. The file is closed in the filter
446 * function and not here.
449 cdk_stream_close (cdk_stream_t s
)
451 struct stream_filter_s
*f
, *f2
;
457 return CDK_Inv_Value
;
460 _gnutls_read_log ("close stream ref=%d `%s'\n",
461 s
->fp_ref
, s
->fname
? s
->fname
: "[temp]");
463 /* In the user callback mode, we call the release cb if possible
464 and just free the stream. */
468 rc
= s
->cbs
.release (s
->cbs_hd
);
478 if (!s
->flags
.filtrated
&& !s
->error
)
479 rc
= cdk_stream_flush (s
);
480 if (!s
->fp_ref
&& (s
->fname
|| s
->flags
.temp
))
484 _gnutls_read_log ("close stream fd=%d\n", fileno (s
->fp
));
485 err
= fclose (s
->fp
);
491 /* Iterate over the filter list and use the cleanup flag to
492 free the allocated internal structures. */
498 f
->fnct (f
->uint8_t, STREAMCTL_FREE
, NULL
, NULL
);
509 cdk_free (s
->cache
.buf
);
510 s
->cache
.alloced
= 0;
520 * @s: The STREAM object.
522 * Return if the associated file handle was set to EOF. This
523 * function will only work with read streams.
526 cdk_stream_eof (cdk_stream_t s
)
528 return s
? s
->flags
.eof
: -1;
533 _cdk_stream_get_fname (cdk_stream_t s
)
539 return s
->fname
? s
->fname
: NULL
;
543 /* Return the underlying FP of the stream.
544 WARNING: This handle should not be closed. */
546 _cdk_stream_get_fp (cdk_stream_t s
)
548 return s
? s
->fp
: NULL
;
553 _cdk_stream_get_errno (cdk_stream_t s
)
555 return s
? s
->error
: CDK_Inv_Value
;
560 * cdk_stream_get_length:
561 * @s: The STREAM object.
563 * Return the length of the associated file handle. This function
564 * should work for both read and write streams. For write streams an
565 * additional flush is used to write possible pending data.
568 cdk_stream_get_length (cdk_stream_t s
)
579 /* The user callback does not support stat. */
583 rc
= stream_flush (s
);
591 if (fstat (fileno (s
->fp
), &statbuf
))
593 s
->error
= CDK_File_Error
;
598 return statbuf
.st_size
;
602 static struct stream_filter_s
*
603 filter_add2 (cdk_stream_t s
)
605 struct stream_filter_s
*f
;
609 f
= cdk_calloc (1, sizeof *f
);
612 f
->next
= s
->filters
;
618 static struct stream_filter_s
*
619 filter_search (cdk_stream_t s
, filter_fnct_t fnc
)
621 struct stream_filter_s
*f
;
625 for (f
= s
->filters
; f
; f
= f
->next
)
635 set_uint8_t (struct stream_filter_s
*f
)
640 f
->uint8_t = &f
->u
.afx
;
643 f
->uint8_t = &f
->u
.cfx
;
646 f
->uint8_t = &f
->u
.pfx
;
649 f
->uint8_t = &f
->u
.zfx
;
652 f
->uint8_t = &f
->u
.mfx
;
655 f
->uint8_t = &f
->u
.tfx
;
663 struct stream_filter_s
*
664 filter_add (cdk_stream_t s
, filter_fnct_t fnc
, int type
)
666 struct stream_filter_s
*f
;
670 s
->flags
.filtrated
= 0;
671 f
= filter_search (s
, fnc
);
678 f
->flags
.enabled
= 1;
688 stream_get_mode (cdk_stream_t s
)
694 return s
->flags
.write
;
699 stream_id_to_filter (int type
)
704 return _cdk_filter_armor
;
706 return _cdk_filter_literal
;
708 return _cdk_filter_text
;
709 /* case fCIPHER : return _cdk_filter_cipher; */
710 /* case fCOMPRESS: return _cdk_filter_compress; */
718 * cdk_stream_filter_disable:
719 * @s: The STREAM object
720 * @type: The numberic filter ID.
722 * Disables the filter with the type 'type'.
725 cdk_stream_filter_disable (cdk_stream_t s
, int type
)
727 struct stream_filter_s
*f
;
733 return CDK_Inv_Value
;
736 fnc
= stream_id_to_filter (type
);
740 return CDK_Inv_Value
;
742 f
= filter_search (s
, fnc
);
744 f
->flags
.enabled
= 0;
749 /* WARNING: tmp should not be closed by the caller. */
751 stream_fp_replace (cdk_stream_t s
, FILE ** tmp
)
757 _gnutls_read_log ("replace stream fd=%d with fd=%d\n",
758 fileno (s
->fp
), fileno (*tmp
));
764 return CDK_File_Error
;
772 /* This function is exactly like filter_read, except the fact that we can't
773 use tmpfile () all the time. That's why we open the real file when there
774 is no last filter. */
776 stream_filter_write (cdk_stream_t s
)
778 struct stream_filter_s
*f
;
783 if (s
->flags
.filtrated
)
786 return CDK_Inv_Value
;
789 for (f
= s
->filters
; f
; f
= f
->next
)
791 if (!f
->flags
.enabled
)
793 /* if there is no next filter, create the final output file */
794 _gnutls_read_log ("filter [write]: last filter=%d fname=%s\n",
795 f
->next
? 1 : 0, s
->fname
);
796 if (!f
->next
&& s
->fname
)
797 f
->tmp
= fopen (s
->fname
, "w+b");
799 f
->tmp
= _cdk_tmpfile ();
805 /* If there is no next filter, flush the cache. We also do this
806 when the next filter is the armor filter because this filter
807 is special and before it starts, all data should be written. */
808 if ((!f
->next
|| f
->next
->type
== fARMOR
) && s
->cache
.size
)
810 rc
= stream_cache_flush (s
, f
->tmp
);
814 rc
= f
->fnct (f
->uint8_t, f
->ctl
, s
->fp
, f
->tmp
);
815 _gnutls_read_log ("filter [write]: type=%d rc=%d\n", f
->type
, rc
);
817 rc
= stream_fp_replace (s
, &f
->tmp
);
819 rc
= cdk_stream_seek (s
, 0);
822 _gnutls_read_log ("filter [close]: fd=%d\n", fileno (f
->tmp
));
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. */
838 stream_filter_read (cdk_stream_t s
)
840 struct stream_filter_s
*f
;
845 if (s
->flags
.filtrated
)
848 for (f
= s
->filters
; f
; f
= f
->next
)
850 if (!f
->flags
.enabled
)
854 _gnutls_read_log ("filter %s [read]: has the error flag; skipped\n",
855 s
->fname
? s
->fname
: "[temp]");
859 f
->tmp
= _cdk_tmpfile ();
865 rc
= f
->fnct (f
->uint8_t, 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
);
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
);
888 rc
= cdk_stream_seek (s
, 0);
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;
904 _cdk_stream_get_uint8_t (cdk_stream_t s
, int fid
)
906 struct stream_filter_s
*f
;
911 for (f
= s
->filters
; f
; f
= f
->next
)
913 if ((int) f
->type
== fid
)
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
)
946 return s
->cbs
.read (s
->cbs_hd
, buf
, buflen
);
950 if (s
->flags
.write
&& !s
->flags
.temp
)
952 s
->error
= CDK_Inv_Mode
;
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
);
963 if (s
->fp
&& feof (s
->fp
))
968 s
->flags
.filtrated
= 1;
974 nread
= fread (buf
, 1, buflen
, s
->fp
);
988 cdk_stream_getc (cdk_stream_t s
)
990 unsigned char buf
[2];
998 nread
= cdk_stream_read (s
, buf
, 1);
1001 s
->error
= CDK_File_Error
;
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
1021 cdk_stream_write (cdk_stream_t s
, const void *buf
, size_t count
)
1034 return s
->cbs
.write (s
->cbs_hd
, buf
, count
);
1038 if (!s
->flags
.write
)
1040 s
->error
= CDK_Inv_Mode
; /* this is a read stream */
1046 return stream_flush (s
);
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
;
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
);
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
;
1070 nwritten
= fwrite (buf
, 1, count
, s
->fp
);
1078 cdk_stream_putc (cdk_stream_t s
, int c
)
1089 nwritten
= cdk_stream_write (s
, buf
, 1);
1090 if (nwritten
== EOF
)
1097 cdk_stream_tell (cdk_stream_t s
)
1099 return s
? ftell (s
->fp
) : (off_t
) - 1;
1104 cdk_stream_seek (cdk_stream_t s
, off_t offset
)
1111 return CDK_Inv_Value
;
1117 return s
->cbs
.seek (s
->cbs_hd
, offset
);
1121 /* Set or reset the EOF flag. */
1122 len
= cdk_stream_get_length (s
);
1128 if (fseek (s
->fp
, offset
, SEEK_SET
))
1131 return CDK_File_Error
;
1138 stream_flush (cdk_stream_t 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
);
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).
1161 cdk_stream_set_armor_flag (cdk_stream_t s
, int armor_type
)
1163 struct stream_filter_s
*f
;
1168 return CDK_Inv_Value
;
1170 f
= filter_add (s
, _cdk_filter_armor
, fARMOR
);
1174 return CDK_Out_Of_Core
;
1176 f
->u
.afx
.idx
= f
->u
.afx
.idx2
= armor_type
;
1177 f
->ctl
= stream_get_mode (s
);
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.
1194 cdk_stream_set_literal_flag (cdk_stream_t s
, cdk_lit_format_t mode
,
1197 struct stream_filter_s
*f
;
1198 const char *orig_fname
;
1200 _gnutls_read_log ("stream: enable literal mode.\n");
1205 return CDK_Inv_Value
;
1208 orig_fname
= _cdk_stream_get_fname (s
);
1209 f
= filter_add (s
, _cdk_filter_literal
, fLITERAL
);
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
);
1221 f
->u
.pfx
.blkmode
.on
= 1;
1222 f
->u
.pfx
.blkmode
.size
= s
->blkmode
;
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.
1240 cdk_stream_set_compress_flag (cdk_stream_t s
, int algo
, int level
)
1244 return CDK_Not_Implemented
;
1247 struct stream_filter_s
*f
;
1250 return CDK_Inv_Value
;
1251 f
= filter_add (s
, _cdk_filter_compress
, fCOMPRESS
);
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
;
1263 * cdk_stream_set_text_flag:
1264 * @s: the stream object
1267 * Pushes the text filter to store the stream data in cannoncial format.
1270 cdk_stream_set_text_flag (cdk_stream_t s
, const char *lf
)
1272 struct stream_filter_s
*f
;
1277 return CDK_Inv_Value
;
1279 f
= filter_add (s
, _cdk_filter_text
, fTEXT
);
1283 return CDK_Out_Of_Core
;
1285 f
->ctl
= stream_get_mode (s
);
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.
1300 cdk_stream_set_hash_flag (cdk_stream_t s
, int digest_algo
)
1302 struct stream_filter_s
*f
;
1307 return CDK_Inv_Value
;
1309 if (stream_get_mode (s
))
1312 return CDK_Inv_Mode
;
1314 f
= filter_add (s
, _cdk_filter_hash
, fHASH
);
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;
1328 * cdk_stream_enable_cache:
1329 * @s: the stream object
1332 * Enables or disable the cache section of a stream object.
1335 cdk_stream_enable_cache (cdk_stream_t s
, int val
)
1340 return CDK_Inv_Value
;
1342 if (!s
->flags
.write
)
1345 return CDK_Inv_Mode
;
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",
1360 stream_cache_flush (cdk_stream_t s
, FILE * fp
)
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
);
1373 return CDK_File_Error
;
1377 memset (s
->cache
.buf
, 0, s
->cache
.alloced
);
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.
1392 cdk_stream_kick_off (cdk_stream_t inp
, cdk_stream_t out
)
1395 int nread
, nwritten
;
1401 return CDK_Inv_Value
;
1404 while (!cdk_stream_eof (inp
))
1406 nread
= cdk_stream_read (inp
, buf
, DIM (buf
));
1407 if (!nread
|| nread
== EOF
)
1409 nwritten
= cdk_stream_write (out
, buf
, nread
);
1410 if (!nwritten
|| nwritten
== EOF
)
1411 { /* In case of errors, we leave the loop. */
1417 memset (buf
, 0, sizeof (buf
));
1423 * cdk_stream_mmap_part:
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.
1434 cdk_stream_mmap_part (cdk_stream_t s
, off_t off
, size_t len
,
1435 byte
** ret_buf
, size_t * ret_buflen
)
1441 if (!ret_buf
|| !ret_buflen
)
1444 return CDK_Inv_Value
;
1452 return CDK_Inv_Value
;
1455 /* Memory mapping is not supported on custom I/O objects. */
1458 _gnutls_read_log ("cdk_stream_mmap_part: not supported on callbacks\n");
1460 return CDK_Inv_Mode
;
1463 oldpos
= cdk_stream_tell (s
);
1464 rc
= cdk_stream_flush (s
);
1470 rc
= cdk_stream_seek (s
, off
);
1477 len
= cdk_stream_get_length (s
);
1480 _gnutls_read_log ("cdk_stream_mmap_part: invalid file size %lu\n", (unsigned long)len
);
1484 if (len
> MAX_MAP_SIZE
)
1487 return CDK_Too_Short
;
1490 *ret_buf
= cdk_calloc (1, len
+ 1);
1492 n
= cdk_stream_read (s
, *ret_buf
, len
);
1495 rc
= cdk_stream_seek (s
, oldpos
);
1503 cdk_stream_mmap (cdk_stream_t inp
, byte
** buf
, size_t * buflen
)
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
);
1516 * @inp: the input stream handle
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
)
1534 off
= cdk_stream_tell (inp
);
1535 nbytes
= cdk_stream_read (inp
, buf
, buflen
);
1538 if (cdk_stream_seek (inp
, off
))
1544 /* Try to read a line from the given stream. */
1546 _cdk_stream_gets (cdk_stream_t s
, char *buf
, size_t count
)
1553 while (!cdk_stream_eof (s
) && count
> 0)
1555 c
= cdk_stream_getc (s
);
1556 if (c
== EOF
|| c
== '\r' || c
== '\n')
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. */
1578 _cdk_stream_set_blockmode (cdk_stream_t s
, size_t nbytes
)
1582 _gnutls_read_log ("stream: activate block mode with blocksize %d\n",
1584 s
->blkmode
= nbytes
;
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;