1 /* iobuf.c - file handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 * 2004, 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
30 #include <sys/types.h>
34 #ifdef HAVE_DOSISH_SYSTEM
40 #endif /* __riscos__ */
45 /* The size of the internal buffers.
46 NOTE: If you change this value you MUST also adjust the regression
47 test "armored_key_8192" in armor.test! */
48 #define IOBUF_BUFFER_SIZE 8192
50 #undef FILE_FILTER_USES_STDIO
52 #ifdef HAVE_DOSISH_SYSTEM
56 #ifdef FILE_FILTER_USES_STDIO
57 #define my_fileno(a) fileno ((a))
58 #define my_fopen_ro(a,b) fopen ((a),(b))
59 #define my_fopen(a,b) fopen ((a),(b))
60 typedef FILE *FILEP_OR_FD
;
61 #define INVALID_FP NULL
62 #define FILEP_OR_FD_FOR_STDIN (stdin)
63 #define FILEP_OR_FD_FOR_STDOUT (stdout)
66 FILE *fp
; /* open file handle */
69 int print_only_name
; /* flags indicating that fname is not a real file */
70 char fname
[1]; /* name of the file */
74 #define my_fileno(a) (a)
75 #define my_fopen_ro(a,b) fd_cache_open ((a),(b))
76 #define my_fopen(a,b) direct_open ((a),(b))
77 #ifdef HAVE_DOSISH_SYSTEM
78 typedef HANDLE FILEP_OR_FD
;
79 #define INVALID_FP ((HANDLE)-1)
80 #define FILEP_OR_FD_FOR_STDIN (GetStdHandle (STD_INPUT_HANDLE))
81 #define FILEP_OR_FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
84 typedef int FILEP_OR_FD
;
85 #define INVALID_FP (-1)
86 #define FILEP_OR_FD_FOR_STDIN (0)
87 #define FILEP_OR_FD_FOR_STDOUT (1)
91 FILEP_OR_FD fp
; /* open file handle */
95 int print_only_name
; /* flags indicating that fname is not a real file */
96 char fname
[1]; /* name of the file */
102 struct close_cache_s
*next
;
106 typedef struct close_cache_s
*CLOSE_CACHE
;
107 static CLOSE_CACHE close_cache
;
117 int print_only_name
; /* flags indicating that fname is not a real file */
118 char fname
[1]; /* name of the file */
123 /* The first partial length header block must be of size 512
124 * to make it easier (and efficienter) we use a min. block size of 512
125 * for all chunks (but the last one) */
126 #define OP_MIN_PARTIAL_CHUNK 512
127 #define OP_MIN_PARTIAL_CHUNK_2POW 9
134 int partial
; /* 1 = partial header, 2 in last partial packet */
135 char *buffer
; /* used for partial header */
136 size_t buflen
; /* used size of buffer */
137 int first_c
; /* of partial header (which is > 0) */
142 static int special_names_enabled
;
144 static int underflow (iobuf_t a
);
145 static int translate_file_handle (int fd
, int for_write
);
147 #ifndef FILE_FILTER_USES_STDIO
150 * Invalidate (i.e. close) a cached iobuf
153 fd_cache_invalidate (const char *fname
)
159 log_debug ("fd_cache_invalidate (%s)\n", fname
);
161 for (cc
= close_cache
; cc
; cc
= cc
->next
)
163 if (cc
->fp
!= INVALID_FP
&& !strcmp (cc
->fname
, fname
))
166 log_debug (" did (%s)\n", cc
->fname
);
167 #ifdef HAVE_DOSISH_SYSTEM
168 CloseHandle (cc
->fp
);
180 direct_open (const char *fname
, const char *mode
)
182 #ifdef HAVE_DOSISH_SYSTEM
183 unsigned long da
, cd
, sm
;
186 /* Note, that we do not handle all mode combinations */
188 /* According to the ReactOS source it seems that open() of the
189 * standard MSW32 crt does open the file in share mode which is
190 * something new for MS applications ;-)
192 if (strchr (mode
, '+'))
194 fd_cache_invalidate (fname
);
195 da
= GENERIC_READ
| GENERIC_WRITE
;
197 sm
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
199 else if (strchr (mode
, 'w'))
201 fd_cache_invalidate (fname
);
204 sm
= FILE_SHARE_WRITE
;
210 sm
= FILE_SHARE_READ
;
213 hfile
= CreateFile (fname
, da
, sm
, NULL
, cd
, FILE_ATTRIBUTE_NORMAL
, NULL
);
217 int cflag
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
219 /* Note, that we do not handle all mode combinations */
220 if (strchr (mode
, '+'))
222 fd_cache_invalidate (fname
);
225 else if (strchr (mode
, 'w'))
227 fd_cache_invalidate (fname
);
228 oflag
= O_WRONLY
| O_CREAT
| O_TRUNC
;
235 if (strchr (mode
, 'b'))
239 return open (fname
, oflag
, cflag
);
243 int rc
= stat (fname
, &buf
);
245 /* Don't allow iobufs on directories */
246 if (!rc
&& S_ISDIR (buf
.st_mode
) && !S_ISREG (buf
.st_mode
))
247 return __set_errno (EISDIR
);
249 return open (fname
, oflag
, cflag
);
257 * Instead of closing an FD we keep it open and cache it for later reuse
258 * Note that this caching strategy only works if the process does not chdir.
261 fd_cache_close (const char *fname
, FILEP_OR_FD fp
)
266 if (!fname
|| !*fname
)
268 #ifdef HAVE_DOSISH_SYSTEM
274 log_debug ("fd_cache_close (%p) real\n", (void *) fp
);
277 /* try to reuse a slot */
278 for (cc
= close_cache
; cc
; cc
= cc
->next
)
280 if (cc
->fp
== INVALID_FP
&& !strcmp (cc
->fname
, fname
))
284 log_debug ("fd_cache_close (%s) used existing slot\n", fname
);
290 log_debug ("fd_cache_close (%s) new slot created\n", fname
);
291 cc
= xcalloc (1, sizeof *cc
+ strlen (fname
));
292 strcpy (cc
->fname
, fname
);
294 cc
->next
= close_cache
;
299 * Do an direct_open on FNAME but first try to reuse one from the fd_cache
302 fd_cache_open (const char *fname
, const char *mode
)
307 for (cc
= close_cache
; cc
; cc
= cc
->next
)
309 if (cc
->fp
!= INVALID_FP
&& !strcmp (cc
->fname
, fname
))
311 FILEP_OR_FD fp
= cc
->fp
;
314 log_debug ("fd_cache_open (%s) using cached fp\n", fname
);
315 #ifdef HAVE_DOSISH_SYSTEM
316 if (SetFilePointer (fp
, 0, NULL
, FILE_BEGIN
) == 0xffffffff)
318 log_error ("rewind file failed on handle %p: ec=%d\n",
319 fp
, (int) GetLastError ());
323 if (lseek (fp
, 0, SEEK_SET
) == (off_t
) - 1)
325 log_error ("can't rewind fd %d: %s\n", fp
, strerror (errno
));
333 log_debug ("fd_cache_open (%s) not cached\n", fname
);
334 return direct_open (fname
, mode
);
338 #endif /*FILE_FILTER_USES_STDIO */
342 * Read data from a file into buf which has an allocated length of *LEN.
343 * return the number of read bytes in *LEN. OPAQUE is the FILE * of
344 * the stream. A is not used.
346 * IOBUFCTRL_INIT: called just before the function is linked into the
347 * list of function. This can be used to prepare internal
348 * data structures of the function.
349 * IOBUFCTRL_FREE: called just before the function is removed from the
350 * list of functions and can be used to release internal
351 * data structures or close a file etc.
352 * IOBUFCTRL_UNDERFLOW: called by iobuf_underflow to fill the buffer
353 * with new stuff. *RET_LEN is the available size of the
354 * buffer, and should be set to the number of bytes
355 * which were put into the buffer. The function
356 * returns 0 to indicate success, -1 on EOF and
357 * GPG_ERR_xxxxx for other errors.
359 * IOBUFCTRL_FLUSH: called by iobuf_flush() to write out the collected stuff.
360 * *RET_LAN is the number of bytes in BUF.
362 * IOBUFCTRL_CANCEL: send to all filters on behalf of iobuf_cancel. The
363 * filter may take appropriate action on this message.
366 file_filter (void *opaque
, int control
, iobuf_t chain
, byte
* buf
,
369 file_filter_ctx_t
*a
= opaque
;
370 FILEP_OR_FD f
= a
->fp
;
371 size_t size
= *ret_len
;
375 #ifdef FILE_FILTER_USES_STDIO
376 if (control
== IOBUFCTRL_UNDERFLOW
)
378 assert (size
); /* need a buffer */
380 { /* On terminals you could easiely read as many EOFs as you call */
381 rc
= -1; /* fread() or fgetc() repeatly. Every call will block until you press */
382 *ret_len
= 0; /* CTRL-D. So we catch this case before we call fread() again. */
387 nbytes
= fread (buf
, 1, size
, f
);
388 if (feof (f
) && !nbytes
)
390 rc
= -1; /* okay: we can return EOF now. */
392 else if (ferror (f
) && errno
!= EPIPE
)
394 rc
= gpg_error_from_syserror ();
395 log_error ("%s: read error: %s\n", a
->fname
, strerror (errno
));
400 else if (control
== IOBUFCTRL_FLUSH
)
405 nbytes
= fwrite (buf
, 1, size
, f
);
408 rc
= gpg_error_from_syserror ();
409 log_error ("%s: write error: %s\n", a
->fname
, strerror (errno
));
414 else if (control
== IOBUFCTRL_INIT
)
416 a
->keep_open
= a
->no_cache
= 0;
418 else if (control
== IOBUFCTRL_DESC
)
420 *(char **) buf
= "file_filter";
422 else if (control
== IOBUFCTRL_FREE
)
424 if (f
!= stdin
&& f
!= stdout
)
427 log_debug ("%s: close fd %d\n", a
->fname
, fileno (f
));
432 xfree (a
); /* we can free our context now */
434 #else /* !stdio implementation */
436 if (control
== IOBUFCTRL_UNDERFLOW
)
438 assert (size
); /* need a buffer */
446 #ifdef HAVE_DOSISH_SYSTEM
450 if (!ReadFile (f
, buf
, size
, &nread
, NULL
))
452 int ec
= (int) GetLastError ();
453 if (ec
!= ERROR_BROKEN_PIPE
)
455 rc
= gpg_error_from_errno (ec
);
456 log_error ("%s: read error: ec=%d\n", a
->fname
, ec
);
476 n
= read (f
, buf
, size
);
478 while (n
== -1 && errno
== EINTR
);
483 rc
= gpg_error_from_syserror ();
484 log_error ("%s: read error: %s\n",
485 a
->fname
, strerror (errno
));
501 else if (control
== IOBUFCTRL_FLUSH
)
505 #ifdef HAVE_DOSISH_SYSTEM
512 if (size
&& !WriteFile (f
, p
, nbytes
, &n
, NULL
))
514 int ec
= (int) GetLastError ();
515 rc
= gpg_error_from_errno (ec
);
516 log_error ("%s: write error: ec=%d\n", a
->fname
, ec
);
533 n
= write (f
, p
, nbytes
);
535 while (n
== -1 && errno
== EINTR
);
542 while (n
!= -1 && nbytes
);
545 rc
= gpg_error_from_syserror ();
546 log_error ("%s: write error: %s\n", a
->fname
, strerror (errno
));
553 else if (control
== IOBUFCTRL_INIT
)
559 else if (control
== IOBUFCTRL_DESC
)
561 *(char **) buf
= "file_filter(fd)";
563 else if (control
== IOBUFCTRL_FREE
)
565 #ifdef HAVE_DOSISH_SYSTEM
566 if (f
!= FILEP_OR_FD_FOR_STDIN
&& f
!= FILEP_OR_FD_FOR_STDOUT
)
569 log_debug ("%s: close handle %p\n", a
->fname
, f
);
571 fd_cache_close (a
->no_cache
? NULL
: a
->fname
, f
);
574 if ((int) f
!= 0 && (int) f
!= 1)
577 log_debug ("%s: close fd %d\n", a
->fname
, f
);
579 fd_cache_close (a
->no_cache
? NULL
: a
->fname
, f
);
583 xfree (a
); /* we can free our context now */
585 #endif /* !stdio implementation */
590 /* Becuase sockets are an special object under Lose32 we have to
591 * use a special filter */
593 sock_filter (void *opaque
, int control
, iobuf_t chain
, byte
* buf
,
596 sock_filter_ctx_t
*a
= opaque
;
597 size_t size
= *ret_len
;
601 if (control
== IOBUFCTRL_UNDERFLOW
)
603 assert (size
); /* need a buffer */
613 nread
= recv (a
->sock
, buf
, size
, 0);
614 if (nread
== SOCKET_ERROR
)
616 int ec
= (int) WSAGetLastError ();
617 rc
= gpg_error_from_errno (ec
);
618 log_error ("socket read error: ec=%d\n", ec
);
632 else if (control
== IOBUFCTRL_FLUSH
)
642 n
= send (a
->sock
, p
, nbytes
, 0);
643 if (n
== SOCKET_ERROR
)
645 int ec
= (int) WSAGetLastError ();
646 rc
= gpg_error_from_errno (ec
);
647 log_error ("socket write error: ec=%d\n", ec
);
658 else if (control
== IOBUFCTRL_INIT
)
664 else if (control
== IOBUFCTRL_DESC
)
666 *(char **) buf
= "sock_filter";
668 else if (control
== IOBUFCTRL_FREE
)
671 closesocket (a
->sock
);
672 xfree (a
); /* we can free our context now */
679 * This is used to implement the block write mode.
680 * Block reading is done on a byte by byte basis in readbyte(),
684 block_filter (void *opaque
, int control
, iobuf_t chain
, byte
* buffer
,
687 block_filter_ctx_t
*a
= opaque
;
688 char *buf
= (char *)buffer
;
689 size_t size
= *ret_len
;
690 int c
, needed
, rc
= 0;
693 if (control
== IOBUFCTRL_UNDERFLOW
)
698 assert (size
); /* need a buffer */
699 if (a
->eof
) /* don't read any further */
704 { /* get the length bytes */
714 /* These OpenPGP introduced huffman like encoded length
715 * bytes are really a mess :-( */
721 else if ((c
= iobuf_get (chain
)) == -1)
723 log_error ("block_filter: 1st length byte missing\n");
724 rc
= GPG_ERR_BAD_DATA
;
741 a
->size
= (c
- 192) * 256;
742 if ((c
= iobuf_get (chain
)) == -1)
745 ("block_filter: 2nd length byte missing\n");
746 rc
= GPG_ERR_BAD_DATA
;
761 a
->size
= iobuf_get (chain
) << 24;
762 a
->size
|= iobuf_get (chain
) << 16;
763 a
->size
|= iobuf_get (chain
) << 8;
764 if ((c
= iobuf_get (chain
)) == -1)
766 log_error ("block_filter: invalid 4 byte length\n");
767 rc
= GPG_ERR_BAD_DATA
;
781 { /* Next partial body length. */
782 a
->size
= 1 << (c
& 0x1f);
784 /* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
790 while (!rc
&& size
&& a
->size
)
792 needed
= size
< a
->size
? size
: a
->size
;
793 c
= iobuf_read (chain
, p
, needed
);
799 ("block_filter %p: read error (size=%lu,a->size=%lu)\n",
800 a
, (ulong
) size
+ c
, (ulong
) a
->size
+ c
);
801 rc
= GPG_ERR_BAD_DATA
;
814 else if (control
== IOBUFCTRL_FLUSH
)
817 { /* the complicated openpgp scheme */
818 size_t blen
, n
, nbytes
= size
+ a
->buflen
;
820 assert (a
->buflen
<= OP_MIN_PARTIAL_CHUNK
);
821 if (nbytes
< OP_MIN_PARTIAL_CHUNK
)
823 /* not enough to write a partial block out; so we store it */
825 a
->buffer
= xmalloc (OP_MIN_PARTIAL_CHUNK
);
826 memcpy (a
->buffer
+ a
->buflen
, buf
, size
);
830 { /* okay, we can write out something */
831 /* do this in a loop to use the most efficient block lengths */
835 /* find the best matching block length - this is limited
836 * by the size of the internal buffering */
837 for (blen
= OP_MIN_PARTIAL_CHUNK
* 2,
838 c
= OP_MIN_PARTIAL_CHUNK_2POW
+ 1; blen
<= nbytes
;
843 /* write the partial length header */
844 assert (c
<= 0x1f); /*;-) */
846 iobuf_put (chain
, c
);
848 { /* write stuff from the buffer */
849 assert (n
== OP_MIN_PARTIAL_CHUNK
);
850 if (iobuf_write (chain
, a
->buffer
, n
))
851 rc
= gpg_error_from_syserror ();
855 if ((n
= nbytes
) > blen
)
857 if (n
&& iobuf_write (chain
, p
, n
))
858 rc
= gpg_error_from_syserror ();
862 while (!rc
&& nbytes
>= OP_MIN_PARTIAL_CHUNK
);
863 /* store the rest in the buffer */
867 assert (nbytes
< OP_MIN_PARTIAL_CHUNK
);
869 a
->buffer
= xmalloc (OP_MIN_PARTIAL_CHUNK
);
870 memcpy (a
->buffer
, p
, nbytes
);
878 else if (control
== IOBUFCTRL_INIT
)
881 log_debug ("init block_filter %p\n", a
);
884 else if (a
->use
== 1)
885 a
->count
= a
->size
= 0;
887 a
->count
= a
->size
; /* force first length bytes */
892 else if (control
== IOBUFCTRL_DESC
)
894 *(char **) buf
= "block_filter";
896 else if (control
== IOBUFCTRL_FREE
)
899 { /* write the end markers */
903 /* write out the remaining bytes without a partial header
904 * the length of this header may be 0 - but if it is
905 * the first block we are not allowed to use a partial header
906 * and frankly we can't do so, because this length must be
907 * a power of 2. This is _really_ complicated because we
908 * have to check the possible length of a packet prior
909 * to it's creation: a chain of filters becomes complicated
910 * and we need a lot of code to handle compressed packets etc.
913 /* construct header */
915 /*log_debug("partial: remaining length=%u\n", len ); */
917 rc
= iobuf_put (chain
, len
);
920 if (!(rc
= iobuf_put (chain
, ((len
- 192) / 256) + 192)))
921 rc
= iobuf_put (chain
, ((len
- 192) % 256));
924 { /* use a 4 byte header */
925 if (!(rc
= iobuf_put (chain
, 0xff)))
926 if (!(rc
= iobuf_put (chain
, (len
>> 24) & 0xff)))
927 if (!(rc
= iobuf_put (chain
, (len
>> 16) & 0xff)))
928 if (!(rc
= iobuf_put (chain
, (len
>> 8) & 0xff)))
929 rc
= iobuf_put (chain
, len
& 0xff);
932 rc
= iobuf_write (chain
, a
->buffer
, len
);
935 log_error ("block_filter: write error: %s\n",
937 rc
= gpg_error_from_syserror ();
948 log_error ("block_filter: pending bytes!\n");
951 log_debug ("free block_filter %p\n", a
);
952 xfree (a
); /* we can free our context now */
960 print_chain (iobuf_t a
)
964 for (; a
; a
= a
->chain
)
966 size_t dummy_len
= 0;
967 const char *desc
= "[none]";
970 a
->filter (a
->filter_ov
, IOBUFCTRL_DESC
, NULL
,
971 (byte
*) & desc
, &dummy_len
);
973 log_debug ("iobuf chain: %d.%d `%s' filter_eof=%d start=%d len=%d\n",
974 a
->no
, a
->subno
, desc
, a
->filter_eof
,
975 (int) a
->d
.start
, (int) a
->d
.len
);
980 iobuf_print_chain (iobuf_t a
)
987 * Allocate a new io buffer, with no function assigned.
988 * Use is the desired usage: 1 for input, 2 for output, 3 for temp buffer
989 * BUFSIZE is a suggested buffer size.
992 iobuf_alloc (int use
, size_t bufsize
)
995 static int number
= 0;
997 a
= xcalloc (1, sizeof *a
);
999 a
->d
.buf
= xmalloc (bufsize
);
1000 a
->d
.size
= bufsize
;
1004 a
->real_fname
= NULL
;
1009 iobuf_close (iobuf_t a
)
1012 size_t dummy_len
= 0;
1015 if (a
&& a
->directfp
)
1017 fclose (a
->directfp
);
1018 xfree (a
->real_fname
);
1020 log_debug ("iobuf_close -> %p\n", a
->directfp
);
1024 for (; a
&& !rc
; a
= a2
)
1027 if (a
->use
== 2 && (rc
= iobuf_flush (a
)))
1028 log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc
));
1031 log_debug ("iobuf-%d.%d: close `%s'\n", a
->no
, a
->subno
, a
->desc
);
1032 if (a
->filter
&& (rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_FREE
,
1033 a
->chain
, NULL
, &dummy_len
)))
1034 log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc
));
1035 xfree (a
->real_fname
);
1038 memset (a
->d
.buf
, 0, a
->d
.size
); /* erase the buffer */
1047 iobuf_cancel (iobuf_t a
)
1052 #if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1053 char *remove_name
= NULL
;
1056 if (a
&& a
->use
== 2)
1058 s
= iobuf_get_real_fname (a
);
1061 #if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1062 remove_name
= xstrdup (s
);
1069 /* send a cancel message to all filters */
1070 for (a2
= a
; a2
; a2
= a2
->chain
)
1074 a2
->filter (a2
->filter_ov
, IOBUFCTRL_CANCEL
, a2
->chain
, NULL
, &dummy
);
1077 rc
= iobuf_close (a
);
1078 #if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1081 /* Argg, MSDOS does not allow to remove open files. So
1082 * we have to do it here */
1083 remove (remove_name
);
1084 xfree (remove_name
);
1092 * create a temporary iobuf, which can be used to collect stuff
1093 * in an iobuf and later be written by iobuf_write_temp() to another
1101 a
= iobuf_alloc (3, 8192);
1107 iobuf_temp_with_content (const char *buffer
, size_t length
)
1111 a
= iobuf_alloc (3, length
);
1112 memcpy (a
->d
.buf
, buffer
, length
);
1119 iobuf_enable_special_filenames (int yes
)
1121 special_names_enabled
= yes
;
1125 /* See whether the filename has the form "-&nnnn", where n is a
1126 non-zero number. Returns this number or -1 if it is not the
1129 check_special_filename (const char *fname
)
1131 if (special_names_enabled
&& fname
&& *fname
== '-' && fname
[1] == '&')
1136 for (i
= 0; digitp (fname
+i
); i
++)
1139 return atoi (fname
);
1145 /* This fucntion returns true if FNAME indicates a PIPE (stdout or
1146 stderr) or a special file name if those are enabled. */
1148 iobuf_is_pipe_filename (const char *fname
)
1150 if (!fname
|| (*fname
=='-' && !fname
[1]) )
1152 return check_special_filename (fname
) != -1;
1156 * Create a head iobuf for reading from a file
1157 * returns: NULL if an error occures and sets errno
1160 iobuf_open (const char *fname
)
1164 file_filter_ctx_t
*fcx
;
1169 if (!fname
|| (*fname
== '-' && !fname
[1]))
1171 fp
= FILEP_OR_FD_FOR_STDIN
;
1173 setmode (my_fileno (fp
), O_BINARY
);
1178 else if ((fd
= check_special_filename (fname
)) != -1)
1179 return iobuf_fdopen (translate_file_handle (fd
, 0), "rb");
1180 else if ((fp
= my_fopen_ro (fname
, "rb")) == INVALID_FP
)
1182 a
= iobuf_alloc (1, 8192);
1183 fcx
= xmalloc (sizeof *fcx
+ strlen (fname
));
1185 fcx
->print_only_name
= print_only
;
1186 strcpy (fcx
->fname
, fname
);
1188 a
->real_fname
= xstrdup (fname
);
1189 a
->filter
= file_filter
;
1191 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1192 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1194 log_debug ("iobuf-%d.%d: open `%s' fd=%d\n",
1195 a
->no
, a
->subno
, fname
, (int) my_fileno (fcx
->fp
));
1201 * Create a head iobuf for reading from a file
1202 * returns: NULL if an error occures and sets errno
1205 iobuf_fdopen (int fd
, const char *mode
)
1209 file_filter_ctx_t
*fcx
;
1212 #ifdef FILE_FILTER_USES_STDIO
1213 if (!(fp
= fdopen (fd
, mode
)))
1216 fp
= (FILEP_OR_FD
) fd
;
1218 a
= iobuf_alloc (strchr (mode
, 'w') ? 2 : 1, 8192);
1219 fcx
= xmalloc (sizeof *fcx
+ 20);
1221 fcx
->print_only_name
= 1;
1222 sprintf (fcx
->fname
, "[fd %d]", fd
);
1223 a
->filter
= file_filter
;
1225 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1226 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1228 log_debug ("iobuf-%d.%d: fdopen `%s'\n", a
->no
, a
->subno
, fcx
->fname
);
1229 iobuf_ioctl (a
, 3, 1, NULL
); /* disable fd caching */
1235 iobuf_sockopen (int fd
, const char *mode
)
1239 sock_filter_ctx_t
*scx
;
1242 a
= iobuf_alloc (strchr (mode
, 'w') ? 2 : 1, 8192);
1243 scx
= xmalloc (sizeof *scx
+ 25);
1245 scx
->print_only_name
= 1;
1246 sprintf (scx
->fname
, "[sock %d]", fd
);
1247 a
->filter
= sock_filter
;
1249 sock_filter (scx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1250 sock_filter (scx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1252 log_debug ("iobuf-%d.%d: sockopen `%s'\n", a
->no
, a
->subno
, scx
->fname
);
1253 iobuf_ioctl (a
, 3, 1, NULL
); /* disable fd caching */
1255 a
= iobuf_fdopen (fd
, mode
);
1261 * create an iobuf for writing to a file; the file will be created.
1264 iobuf_create (const char *fname
)
1268 file_filter_ctx_t
*fcx
;
1273 if (!fname
|| (*fname
== '-' && !fname
[1]))
1275 fp
= FILEP_OR_FD_FOR_STDOUT
;
1277 setmode (my_fileno (fp
), O_BINARY
);
1282 else if ((fd
= check_special_filename (fname
)) != -1)
1283 return iobuf_fdopen (translate_file_handle (fd
, 1), "wb");
1284 else if ((fp
= my_fopen (fname
, "wb")) == INVALID_FP
)
1286 a
= iobuf_alloc (2, 8192);
1287 fcx
= xmalloc (sizeof *fcx
+ strlen (fname
));
1289 fcx
->print_only_name
= print_only
;
1290 strcpy (fcx
->fname
, fname
);
1292 a
->real_fname
= xstrdup (fname
);
1293 a
->filter
= file_filter
;
1295 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1296 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1298 log_debug ("iobuf-%d.%d: create `%s'\n", a
->no
, a
->subno
, a
->desc
);
1304 * append to an iobuf; if the file does not exist, create it.
1305 * cannot be used for stdout.
1306 * Note: This is not used.
1308 #if 0 /* not used */
1310 iobuf_append (const char *fname
)
1314 file_filter_ctx_t
*fcx
;
1319 else if (!(fp
= my_fopen (fname
, "ab")))
1321 a
= iobuf_alloc (2, 8192);
1322 fcx
= m_alloc (sizeof *fcx
+ strlen (fname
));
1324 strcpy (fcx
->fname
, fname
);
1325 a
->real_fname
= m_strdup (fname
);
1326 a
->filter
= file_filter
;
1328 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1329 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1331 log_debug ("iobuf-%d.%d: append `%s'\n", a
->no
, a
->subno
, a
->desc
);
1338 iobuf_openrw (const char *fname
)
1342 file_filter_ctx_t
*fcx
;
1347 else if ((fp
= my_fopen (fname
, "r+b")) == INVALID_FP
)
1349 a
= iobuf_alloc (2, 8192);
1350 fcx
= xmalloc (sizeof *fcx
+ strlen (fname
));
1352 strcpy (fcx
->fname
, fname
);
1353 a
->real_fname
= xstrdup (fname
);
1354 a
->filter
= file_filter
;
1356 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1357 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1359 log_debug ("iobuf-%d.%d: openrw `%s'\n", a
->no
, a
->subno
, a
->desc
);
1366 iobuf_ioctl (iobuf_t a
, int cmd
, int intval
, void *ptrval
)
1369 { /* keep system filepointer/descriptor open */
1371 log_debug ("iobuf-%d.%d: ioctl `%s' keep=%d\n",
1372 a
? a
->no
: -1, a
? a
->subno
: -1, a
? a
->desc
: "?",
1374 for (; a
; a
= a
->chain
)
1375 if (!a
->chain
&& a
->filter
== file_filter
)
1377 file_filter_ctx_t
*b
= a
->filter_ov
;
1378 b
->keep_open
= intval
;
1382 else if (!a
->chain
&& a
->filter
== sock_filter
)
1384 sock_filter_ctx_t
*b
= a
->filter_ov
;
1385 b
->keep_open
= intval
;
1391 { /* invalidate cache */
1393 log_debug ("iobuf-*.*: ioctl `%s' invalidate\n",
1394 ptrval
? (char *) ptrval
: "?");
1395 if (!a
&& !intval
&& ptrval
)
1397 #ifndef FILE_FILTER_USES_STDIO
1398 fd_cache_invalidate (ptrval
);
1404 { /* disallow/allow caching */
1406 log_debug ("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
1407 a
? a
->no
: -1, a
? a
->subno
: -1, a
? a
->desc
: "?",
1409 for (; a
; a
= a
->chain
)
1410 if (!a
->chain
&& a
->filter
== file_filter
)
1412 file_filter_ctx_t
*b
= a
->filter_ov
;
1413 b
->no_cache
= intval
;
1417 else if (!a
->chain
&& a
->filter
== sock_filter
)
1419 sock_filter_ctx_t
*b
= a
->filter_ov
;
1420 b
->no_cache
= intval
;
1431 * Register an i/o filter.
1434 iobuf_push_filter (iobuf_t a
,
1435 int (*f
) (void *opaque
, int control
,
1436 iobuf_t chain
, byte
* buf
, size_t * len
),
1439 return iobuf_push_filter2 (a
, f
, ov
, 0);
1443 iobuf_push_filter2 (iobuf_t a
,
1444 int (*f
) (void *opaque
, int control
,
1445 iobuf_t chain
, byte
* buf
, size_t * len
),
1446 void *ov
, int rel_ov
)
1449 size_t dummy_len
= 0;
1455 if (a
->use
== 2 && (rc
= iobuf_flush (a
)))
1457 /* make a copy of the current stream, so that
1458 * A is the new stream and B the original one.
1459 * The contents of the buffers are transferred to the
1462 b
= xmalloc (sizeof *b
);
1463 memcpy (b
, a
, sizeof *b
);
1464 /* fixme: it is stupid to keep a copy of the name at every level
1465 * but we need the name somewhere because the name known by file_filter
1466 * may have been released when we need the name of the file */
1467 b
->real_fname
= a
->real_fname
? xstrdup (a
->real_fname
) : NULL
;
1468 /* remove the filter stuff from the new stream */
1470 a
->filter_ov
= NULL
;
1471 a
->filter_ov_owner
= 0;
1474 a
->use
= 2; /* make a write stream from a temp stream */
1477 { /* allocate a fresh buffer for the
1479 b
->d
.buf
= xmalloc (a
->d
.size
);
1484 { /* allocate a fresh buffer for the new
1486 a
->d
.buf
= xmalloc (a
->d
.size
);
1490 /* disable nlimit for the new stream */
1491 a
->ntotal
= b
->ntotal
+ b
->nbytes
;
1492 a
->nlimit
= a
->nbytes
= 0;
1494 /* make a link from the new stream to the original stream */
1496 a
->opaque
= b
->opaque
;
1498 /* setup the function on the new stream */
1501 a
->filter_ov_owner
= rel_ov
;
1503 a
->subno
= b
->subno
+ 1;
1504 f (ov
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &dummy_len
);
1508 log_debug ("iobuf-%d.%d: push `%s'\n", a
->no
, a
->subno
, a
->desc
);
1512 /* now we can initialize the new function if we have one */
1513 if (a
->filter
&& (rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_INIT
, a
->chain
,
1515 log_error ("IOBUFCTRL_INIT failed: %s\n", gpg_strerror (rc
));
1520 * Remove an i/o filter.
1523 pop_filter (iobuf_t a
, int (*f
) (void *opaque
, int control
,
1524 iobuf_t chain
, byte
* buf
, size_t * len
),
1528 size_t dummy_len
= 0;
1535 log_debug ("iobuf-%d.%d: pop `%s'\n", a
->no
, a
->subno
, a
->desc
);
1537 { /* this is simple */
1541 xfree (a
->real_fname
);
1542 memcpy (a
, b
, sizeof *a
);
1546 for (b
= a
; b
; b
= b
->chain
)
1547 if (b
->filter
== f
&& (!ov
|| b
->filter_ov
== ov
))
1550 log_bug ("pop_filter(): filter function not found\n");
1552 /* flush this stream if it is an output stream */
1553 if (a
->use
== 2 && (rc
= iobuf_flush (b
)))
1555 log_error ("iobuf_flush failed in pop_filter: %s\n", gpg_strerror (rc
));
1558 /* and tell the filter to free it self */
1559 if (b
->filter
&& (rc
= b
->filter (b
->filter_ov
, IOBUFCTRL_FREE
, b
->chain
,
1562 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc
));
1565 if (b
->filter_ov
&& b
->filter_ov_owner
)
1567 xfree (b
->filter_ov
);
1568 b
->filter_ov
= NULL
;
1572 /* and see how to remove it */
1573 if (a
== b
&& !b
->chain
)
1574 log_bug ("can't remove the last filter from the chain\n");
1576 { /* remove the first iobuf from the chain */
1577 /* everything from b is copied to a. This is save because
1578 * a flush has been done on the to be removed entry
1582 xfree (a
->real_fname
);
1583 memcpy (a
, b
, sizeof *a
);
1586 log_debug ("iobuf-%d.%d: popped filter\n", a
->no
, a
->subno
);
1589 { /* remove the last iobuf from the chain */
1590 log_bug ("Ohh jeee, trying to remove a head filter\n");
1593 { /* remove an intermediate iobuf from the chain */
1594 log_bug ("Ohh jeee, trying to remove an intermediate filter\n");
1602 * read underflow: read more bytes into the buffer and return
1603 * the first byte or -1 on EOF.
1606 underflow (iobuf_t a
)
1611 assert (a
->d
.start
== a
->d
.len
);
1613 return -1; /* EOF because a temp buffer can't do an underflow */
1619 iobuf_t b
= a
->chain
;
1621 log_debug ("iobuf-%d.%d: pop `%s' in underflow\n",
1622 a
->no
, a
->subno
, a
->desc
);
1624 xfree (a
->real_fname
);
1625 memcpy (a
, b
, sizeof *a
);
1630 a
->filter_eof
= 0; /* for the top level filter */
1632 log_debug ("iobuf-%d.%d: underflow: eof (due to filter eof)\n",
1634 return -1; /* return one(!) EOF */
1639 log_debug ("iobuf-%d.%d: error\n", a
->no
, a
->subno
);
1645 FILE *fp
= a
->directfp
;
1647 len
= fread (a
->d
.buf
, 1, a
->d
.size
, fp
);
1648 if (len
< a
->d
.size
)
1651 a
->error
= gpg_error_from_syserror ();
1655 return len
? a
->d
.buf
[a
->d
.start
++] : -1;
1663 log_debug ("iobuf-%d.%d: underflow: req=%lu\n",
1664 a
->no
, a
->subno
, (ulong
) len
);
1665 rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_UNDERFLOW
, a
->chain
,
1669 log_debug ("iobuf-%d.%d: underflow: got=%lu rc=%d\n",
1670 a
->no
, a
->subno
, (ulong
) len
, rc
);
1671 /* if( a->no == 1 ) */
1672 /* log_hexdump (" data:", a->d.buf, len); */
1674 if (a
->use
== 1 && rc
== -1)
1675 { /* EOF: we can remove the filter */
1676 size_t dummy_len
= 0;
1678 /* and tell the filter to free itself */
1679 if ((rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_FREE
, a
->chain
,
1681 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc
));
1682 if (a
->filter_ov
&& a
->filter_ov_owner
)
1684 xfree (a
->filter_ov
);
1685 a
->filter_ov
= NULL
;
1689 a
->filter_ov
= NULL
;
1691 if (!len
&& a
->chain
)
1693 iobuf_t b
= a
->chain
;
1695 log_debug ("iobuf-%d.%d: pop `%s' in underflow (!len)\n",
1696 a
->no
, a
->subno
, a
->desc
);
1698 xfree (a
->real_fname
);
1699 memcpy (a
, b
, sizeof *a
);
1710 log_debug ("iobuf-%d.%d: underflow: eof\n", a
->no
, a
->subno
);
1715 return a
->d
.buf
[a
->d
.start
++];
1720 log_debug ("iobuf-%d.%d: underflow: eof (no filter)\n",
1722 return -1; /* no filter; return EOF */
1728 iobuf_flush (iobuf_t a
)
1737 { /* increase the temp buffer */
1738 unsigned char *newbuf
;
1739 size_t newsize
= a
->d
.size
+ 8192;
1742 log_debug ("increasing temp iobuf from %lu to %lu\n",
1743 (ulong
) a
->d
.size
, (ulong
) newsize
);
1744 newbuf
= xmalloc (newsize
);
1745 memcpy (newbuf
, a
->d
.buf
, a
->d
.len
);
1748 a
->d
.size
= newsize
;
1751 else if (a
->use
!= 2)
1752 log_bug ("flush on non-output iobuf\n");
1753 else if (!a
->filter
)
1754 log_bug ("iobuf_flush: no filter\n");
1756 rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_FLUSH
, a
->chain
, a
->d
.buf
, &len
);
1757 if (!rc
&& len
!= a
->d
.len
)
1759 log_info ("iobuf_flush did not write all!\n");
1760 rc
= GPG_ERR_INTERNAL
;
1771 * Read a byte from the iobuf; returns -1 on EOF
1774 iobuf_readbyte (iobuf_t a
)
1778 /* nlimit does not work together with unget */
1779 /* nbytes is also not valid! */
1782 if (a
->unget
.start
< a
->unget
.len
)
1783 return a
->unget
.buf
[a
->unget
.start
++];
1784 xfree (a
->unget
.buf
);
1785 a
->unget
.buf
= NULL
;
1789 if (a
->nlimit
&& a
->nbytes
>= a
->nlimit
)
1790 return -1; /* forced EOF */
1792 if (a
->d
.start
< a
->d
.len
)
1794 c
= a
->d
.buf
[a
->d
.start
++];
1796 else if ((c
= underflow (a
)) == -1)
1797 return -1; /* EOF */
1805 iobuf_read (iobuf_t a
, void *buffer
, unsigned int buflen
)
1807 unsigned char *buf
= (unsigned char *)buffer
;
1810 if (a
->unget
.buf
|| a
->nlimit
)
1812 /* handle special cases */
1813 for (n
= 0; n
< buflen
; n
++)
1815 if ((c
= iobuf_readbyte (a
)) == -1)
1818 return -1; /* eof */
1832 if (n
< buflen
&& a
->d
.start
< a
->d
.len
)
1834 unsigned size
= a
->d
.len
- a
->d
.start
;
1835 if (size
> buflen
- n
)
1838 memcpy (buf
, a
->d
.buf
+ a
->d
.start
, size
);
1846 if ((c
= underflow (a
)) == -1)
1849 return n
? n
: -1 /*EOF*/;
1863 * Have a look at the iobuf.
1864 * NOTE: This only works in special cases.
1867 iobuf_peek (iobuf_t a
, byte
* buf
, unsigned buflen
)
1874 if (!(a
->d
.start
< a
->d
.len
))
1876 if (underflow (a
) == -1)
1878 /* and unget this character */
1879 assert (a
->d
.start
== 1);
1883 for (n
= 0; n
< buflen
&& (a
->d
.start
+ n
) < a
->d
.len
; n
++, buf
++)
1892 iobuf_writebyte (iobuf_t a
, unsigned int c
)
1899 if (a
->d
.len
== a
->d
.size
)
1900 if ((rc
=iobuf_flush (a
)))
1903 assert (a
->d
.len
< a
->d
.size
);
1904 a
->d
.buf
[a
->d
.len
++] = c
;
1910 iobuf_write (iobuf_t a
, const void *buffer
, unsigned int buflen
)
1912 const unsigned char *buf
= (const unsigned char *)buffer
;
1920 if (buflen
&& a
->d
.len
< a
->d
.size
)
1922 unsigned size
= a
->d
.size
- a
->d
.len
;
1925 memcpy (a
->d
.buf
+ a
->d
.len
, buf
, size
);
1932 rc
= iobuf_flush (a
);
1943 iobuf_writestr (iobuf_t a
, const char *buf
)
1948 if ((rc
=iobuf_writebyte (a
, *buf
)))
1956 * copy the contents of TEMP to A.
1959 iobuf_write_temp (iobuf_t a
, iobuf_t temp
)
1962 pop_filter (temp
, temp
->filter
, NULL
);
1963 return iobuf_write (a
, temp
->d
.buf
, temp
->d
.len
);
1967 * copy the contents of the temp io stream to BUFFER.
1970 iobuf_temp_to_buffer (iobuf_t a
, byte
* buffer
, size_t buflen
)
1972 size_t n
= a
->d
.len
;
1976 memcpy (buffer
, a
->d
.buf
, n
);
1982 * Call this function to terminate processing of the temp stream
1983 * without closing it. This removes all filters from the stream
1984 * makes sure that iobuf_get_temp_{buffer,length}() returns correct
1988 iobuf_flush_temp (iobuf_t temp
)
1991 pop_filter (temp
, temp
->filter
, NULL
);
1996 * Set a limit on how many bytes may be read from the input stream A.
1997 * Setting the limit to 0 disables this feature.
2000 iobuf_set_limit (iobuf_t a
, off_t nlimit
)
2007 a
->ntotal
+= a
->nbytes
;
2013 /* Return the length of an open file A. IF OVERFLOW is not NULL it
2014 will be set to true if the file is larger than what off_t can cope
2015 with. The function return 0 on error or on overflow condition. */
2017 iobuf_get_filelength (iobuf_t a
, int *overflow
)
2025 FILE *fp
= a
->directfp
;
2027 if( !fstat(fileno(fp
), &st
) )
2029 log_error("fstat() failed: %s\n", strerror(errno
) );
2033 /* Hmmm: file_filter may have already been removed */
2034 for( ; a
; a
= a
->chain
)
2035 if( !a
->chain
&& a
->filter
== file_filter
) {
2036 file_filter_ctx_t
*b
= a
->filter_ov
;
2037 FILEP_OR_FD fp
= b
->fp
;
2039 #if defined(HAVE_DOSISH_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2041 static int (* __stdcall get_file_size_ex
)
2042 (void *handle
, LARGE_INTEGER
*size
);
2043 static int get_file_size_ex_initialized
;
2045 if (!get_file_size_ex_initialized
)
2049 handle
= dlopen ("kernel32.dll", RTLD_LAZY
);
2052 get_file_size_ex
= dlsym (handle
, "GetFileSizeEx");
2053 if (!get_file_size_ex
)
2056 get_file_size_ex_initialized
= 1;
2059 if (get_file_size_ex
)
2061 /* This is a newer system with GetFileSizeEx; we use
2062 this then becuase it seem that GetFileSize won't
2063 return a proper error in case a file is larger than
2067 if (get_file_size_ex (fp
, &size
))
2069 if (!size
.u
.HighPart
)
2070 return size
.u
.LowPart
;
2078 if ((size
=GetFileSize (fp
, NULL
)) != 0xffffffff)
2081 log_error ("GetFileSize for handle %p failed: %s\n",
2082 fp
, w32_strerror (0));
2084 if( !fstat(my_fileno(fp
), &st
) )
2086 log_error("fstat() failed: %s\n", strerror(errno
) );
2095 /* Return the file descriptor of the underlying file or -1 if it is
2098 iobuf_get_fd (iobuf_t a
)
2101 return fileno ( (FILE*)a
->directfp
);
2103 for ( ; a
; a
= a
->chain
)
2104 if (!a
->chain
&& a
->filter
== file_filter
)
2106 file_filter_ctx_t
*b
= a
->filter_ov
;
2107 FILEP_OR_FD fp
= b
->fp
;
2109 return my_fileno (fp
);
2118 * Tell the file position, where the next read will take place
2121 iobuf_tell (iobuf_t a
)
2123 return a
->ntotal
+ a
->nbytes
;
2127 #if !defined(HAVE_FSEEKO) && !defined(fseeko)
2129 #ifdef HAVE_LIMITS_H
2130 # include <limits.h>
2133 # define LONG_MAX ((long) ((unsigned long) -1 >> 1))
2136 # define LONG_MIN (-1 - LONG_MAX)
2140 * A substitute for fseeko, for hosts that don't have it.
2143 fseeko (FILE * stream
, off_t newpos
, int whence
)
2145 while (newpos
!= (long) newpos
)
2147 long pos
= newpos
< 0 ? LONG_MIN
: LONG_MAX
;
2148 if (fseek (stream
, pos
, whence
) != 0)
2153 return fseek (stream
, (long) newpos
, whence
);
2158 * This is a very limited implementation. It simply discards all internal
2159 * buffering and removes all filters but the first one.
2162 iobuf_seek (iobuf_t a
, off_t newpos
)
2164 file_filter_ctx_t
*b
= NULL
;
2168 FILE *fp
= a
->directfp
;
2169 if (fseeko (fp
, newpos
, SEEK_SET
))
2171 log_error ("can't seek: %s\n", strerror (errno
));
2178 for (; a
; a
= a
->chain
)
2180 if (!a
->chain
&& a
->filter
== file_filter
)
2188 #ifdef FILE_FILTER_USES_STDIO
2189 if (fseeko (b
->fp
, newpos
, SEEK_SET
))
2191 log_error ("can't fseek: %s\n", strerror (errno
));
2195 #ifdef HAVE_DOSISH_SYSTEM
2196 if (SetFilePointer (b
->fp
, newpos
, NULL
, FILE_BEGIN
) == 0xffffffff)
2198 log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2199 b
->fp
, (int) GetLastError ());
2203 if (lseek (b
->fp
, newpos
, SEEK_SET
) == (off_t
) - 1)
2205 log_error ("can't lseek: %s\n", strerror (errno
));
2211 a
->d
.len
= 0; /* discard buffer */
2218 /* remove filters, but the last */
2220 log_debug ("pop_filter called in iobuf_seek - please report\n");
2222 pop_filter (a
, a
->filter
, NULL
);
2233 * Retrieve the real filename
2236 iobuf_get_real_fname (iobuf_t a
)
2239 return a
->real_fname
;
2241 /* the old solution */
2242 for (; a
; a
= a
->chain
)
2243 if (!a
->chain
&& a
->filter
== file_filter
)
2245 file_filter_ctx_t
*b
= a
->filter_ov
;
2246 return b
->print_only_name
? NULL
: b
->fname
;
2254 * Retrieve the filename
2257 iobuf_get_fname (iobuf_t a
)
2259 for (; a
; a
= a
->chain
)
2260 if (!a
->chain
&& a
->filter
== file_filter
)
2262 file_filter_ctx_t
*b
= a
->filter_ov
;
2271 * enable partial block mode as described in the OpenPGP draft.
2272 * LEN is the first length byte on read, but ignored on writes.
2275 iobuf_set_partial_block_mode (iobuf_t a
, size_t len
)
2277 block_filter_ctx_t
*ctx
= xcalloc (1, sizeof *ctx
);
2279 assert (a
->use
== 1 || a
->use
== 2);
2284 log_debug ("pop_filter called in set_partial_block_mode"
2285 " - please report\n");
2286 pop_filter (a
, block_filter
, NULL
);
2293 iobuf_push_filter (a
, block_filter
, ctx
);
2300 * Same as fgets() but if the buffer is too short a larger one will
2301 * be allocated up to some limit *max_length.
2302 * A line is considered a byte stream ending in a LF.
2303 * Returns the length of the line. EOF is indicated by a line of
2304 * length zero. The last LF may be missing due to an EOF.
2305 * is max_length is zero on return, the line has been truncated.
2307 * Note: The buffer is allocated with enough space to append a CR,LF,EOL
2310 iobuf_read_line (iobuf_t a
, byte
** addr_of_buffer
,
2311 unsigned *length_of_buffer
, unsigned *max_length
)
2314 char *buffer
= (char *)*addr_of_buffer
;
2315 unsigned length
= *length_of_buffer
;
2316 unsigned nbytes
= 0;
2317 unsigned maxlen
= *max_length
;
2321 { /* must allocate a new buffer */
2323 buffer
= xmalloc (length
);
2324 *addr_of_buffer
= (unsigned char *)buffer
;
2325 *length_of_buffer
= length
;
2328 length
-= 3; /* reserve 3 bytes (cr,lf,eol) */
2330 while ((c
= iobuf_get (a
)) != -1)
2332 if (nbytes
== length
)
2333 { /* increase the buffer */
2334 if (length
> maxlen
)
2335 { /* this is out limit */
2336 /* skip the rest of the line */
2337 while (c
!= '\n' && (c
= iobuf_get (a
)) != -1)
2339 *p
++ = '\n'; /* always append a LF (we have reserved space) */
2341 *max_length
= 0; /* indicate truncation */
2344 length
+= 3; /* correct for the reserved byte */
2345 length
+= length
< 1024 ? 256 : 1024;
2346 buffer
= xrealloc (buffer
, length
);
2347 *addr_of_buffer
= (unsigned char *)buffer
;
2348 *length_of_buffer
= length
;
2349 length
-= 3; /* and reserve again */
2350 p
= buffer
+ nbytes
;
2357 *p
= 0; /* make sure the line is a string */
2362 /* This is the non iobuf specific function */
2364 iobuf_translate_file_handle (int fd
, int for_write
)
2371 return fd
; /* do not do this for error, stdin, stdout, stderr */
2373 x
= _open_osfhandle (fd
, for_write
? 1 : 0);
2375 log_error ("failed to translate osfhandle %p\n", (void *) fd
);
2378 /*log_info ("_open_osfhandle %p yields %d%s\n",
2379 (void*)fd, x, for_write? " for writing":"" ); */
2388 translate_file_handle (int fd
, int for_write
)
2391 #ifdef FILE_FILTER_USES_STDIO
2392 fd
= iobuf_translate_file_handle (fd
, for_write
);
2398 x
= (int) GetStdHandle (STD_INPUT_HANDLE
);
2400 x
= (int) GetStdHandle (STD_OUTPUT_HANDLE
);
2402 x
= (int) GetStdHandle (STD_ERROR_HANDLE
);
2407 log_debug ("GetStdHandle(%d) failed: ec=%d\n",
2408 fd
, (int) GetLastError ());
2419 iobuf_skip_rest (iobuf_t a
, unsigned long n
, int partial
)
2425 if (a
->nofast
|| a
->d
.start
>= a
->d
.len
)
2427 if (iobuf_readbyte (a
) == -1)
2434 unsigned long count
= a
->d
.len
- a
->d
.start
;
2436 a
->d
.start
= a
->d
.len
;
2442 unsigned long remaining
= n
;
2443 while (remaining
> 0)
2445 if (a
->nofast
|| a
->d
.start
>= a
->d
.len
)
2447 if (iobuf_readbyte (a
) == -1)
2455 unsigned long count
= a
->d
.len
- a
->d
.start
;
2456 if (count
> remaining
)
2461 a
->d
.start
+= count
;