1 /* iobuf.c - File Handling for OpenPGP.
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2006,
3 * 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
32 #ifdef HAVE_W32_SYSTEM
38 #endif /* __riscos__ */
44 /*-- Begin configurable part. --*/
46 /* The size of the internal buffers.
47 NOTE: If you change this value you MUST also adjust the regression
48 test "armored_key_8192" in armor.test! */
49 #define IOBUF_BUFFER_SIZE 8192
51 /* We don't want to use the STDIO based backend. */
52 #undef FILE_FILTER_USES_STDIO
54 /*-- End configurable part. --*/
57 /* Under W32 the default is to use the setmode call. Define a macro
58 which allows us to enable this call. */
59 #ifdef HAVE_W32_SYSTEM
60 # define USE_SETMODE 1
61 #endif /*HAVE_W32_SYSTEM*/
64 /* Definition of constants and macros used by our file filter
65 implementation. What we define here are 3 macros to make the
69 Is expanded to fileno(a) if using a stdion backend and to a if we
70 are using the low-level backend.
73 Is defined to fopen for the stdio backend and to direct_open if
74 we are using the low-evel backend.
77 Is defined to fopen for the stdio backend and to fd_cache_open if
78 we are using the low-evel backend.
81 Is the type we use for the backend stream or file descriptor.
83 INVALID_FP, FILEP_OR_FD_FOR_STDIN, FILEP_OR_FD_FOR_STDOUT
84 Are macros defined depending on the used backend.
87 #ifdef FILE_FILTER_USES_STDIO
88 # define my_fileno(a) fileno ((a))
89 # define my_fopen_ro(a,b) fopen ((a),(b))
90 # define my_fopen(a,b) fopen ((a),(b))
91 typedef FILE *fp_or_fd_t
;
92 # define INVALID_FP NULL
93 # define FILEP_OR_FD_FOR_STDIN (stdin)
94 # define FILEP_OR_FD_FOR_STDOUT (stdout)
95 #else /*!FILE_FILTER_USES_STDIO*/
96 # define my_fopen_ro(a,b) fd_cache_open ((a),(b))
97 # define my_fopen(a,b) direct_open ((a),(b))
98 # ifdef HAVE_W32_SYSTEM
99 /* (We assume that a HANDLE first into an int.) */
100 # define my_fileno(a) ((int)(a))
101 typedef HANDLE fp_or_fd_t
;
102 # define INVALID_FP ((HANDLE)-1)
103 # define FILEP_OR_FD_FOR_STDIN (GetStdHandle (STD_INPUT_HANDLE))
104 # define FILEP_OR_FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
106 # else /*!HAVE_W32_SYSTEM*/
107 # define my_fileno(a) (a)
108 typedef int fp_or_fd_t
;
109 # define INVALID_FP (-1)
110 # define FILEP_OR_FD_FOR_STDIN (0)
111 # define FILEP_OR_FD_FOR_STDOUT (1)
112 # endif /*!HAVE_W32_SYSTEM*/
113 #endif /*!FILE_FILTER_USES_STDIO*/
115 /* The context used by the file filter. */
118 fp_or_fd_t fp
; /* Open file pointer or handle. */
122 int print_only_name
; /* Flags indicating that fname is not a real file. */
123 char fname
[1]; /* Name of the file. */
128 /* If we are not using stdio as the backend we make use of a "close
130 #ifndef FILE_FILTER_USES_STDIO
133 struct close_cache_s
*next
;
137 typedef struct close_cache_s
*close_cache_t
;
138 static close_cache_t close_cache
;
139 #endif /*!FILE_FILTER_USES_STDIO*/
143 #ifdef HAVE_W32_SYSTEM
150 int print_only_name
; /* Flag indicating that fname is not a real file. */
151 char fname
[1]; /* Name of the file */
154 #endif /*HAVE_W32_SYSTEM*/
156 /* The first partial length header block must be of size 512
157 * to make it easier (and efficienter) we use a min. block size of 512
158 * for all chunks (but the last one) */
159 #define OP_MIN_PARTIAL_CHUNK 512
160 #define OP_MIN_PARTIAL_CHUNK_2POW 9
162 /* The context we use for the block filter (used to handle OpenPGP
163 length information header). */
169 int partial
; /* 1 = partial header, 2 in last partial packet. */
170 char *buffer
; /* Used for partial header. */
171 size_t buflen
; /* Used size of buffer. */
172 int first_c
; /* First character of a partial header (which is > 0). */
178 /* Global flag to tell whether special file names are enabled. See
179 gpg.c for an explanation of these file names. FIXME: it does not
180 belong into the iobuf subsystem. */
181 static int special_names_enabled
;
183 /* Local prototypes. */
184 static int underflow (iobuf_t a
);
185 static int translate_file_handle (int fd
, int for_write
);
189 #ifndef FILE_FILTER_USES_STDIO
191 * Invalidate (i.e. close) a cached iobuf
194 fd_cache_invalidate (const char *fname
)
200 log_debug ("fd_cache_invalidate (%s)\n", fname
);
202 for (cc
= close_cache
; cc
; cc
= cc
->next
)
204 if (cc
->fp
!= INVALID_FP
&& !strcmp (cc
->fname
, fname
))
207 log_debug (" did (%s)\n", cc
->fname
);
208 #ifdef HAVE_W32_SYSTEM
209 CloseHandle (cc
->fp
);
220 direct_open (const char *fname
, const char *mode
)
222 #ifdef HAVE_W32_SYSTEM
223 unsigned long da
, cd
, sm
;
226 /* Note, that we do not handle all mode combinations */
228 /* According to the ReactOS source it seems that open() of the
229 * standard MSW32 crt does open the file in share mode which is
230 * something new for MS applications ;-)
232 if (strchr (mode
, '+'))
234 fd_cache_invalidate (fname
);
235 da
= GENERIC_READ
| GENERIC_WRITE
;
237 sm
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
239 else if (strchr (mode
, 'w'))
241 fd_cache_invalidate (fname
);
244 sm
= FILE_SHARE_WRITE
;
250 sm
= FILE_SHARE_READ
;
253 hfile
= CreateFile (fname
, da
, sm
, NULL
, cd
, FILE_ATTRIBUTE_NORMAL
, NULL
);
255 #else /*!HAVE_W32_SYSTEM*/
257 int cflag
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
259 /* Note, that we do not handle all mode combinations */
260 if (strchr (mode
, '+'))
262 fd_cache_invalidate (fname
);
265 else if (strchr (mode
, 'w'))
267 fd_cache_invalidate (fname
);
268 oflag
= O_WRONLY
| O_CREAT
| O_TRUNC
;
275 if (strchr (mode
, 'b'))
278 /* No we need to distinguish between POSIX and RISC OS. */
280 return open (fname
, oflag
, cflag
);
284 int rc
= stat (fname
, &buf
);
286 /* Don't allow iobufs on directories */
287 if (!rc
&& S_ISDIR (buf
.st_mode
) && !S_ISREG (buf
.st_mode
))
288 return __set_errno (EISDIR
);
290 return open (fname
, oflag
, cflag
);
293 #endif /*!HAVE_W32_SYSTEM*/
298 * Instead of closing an FD we keep it open and cache it for later reuse
299 * Note that this caching strategy only works if the process does not chdir.
302 fd_cache_close (const char *fname
, fp_or_fd_t fp
)
307 if (!fname
|| !*fname
)
309 #ifdef HAVE_W32_SYSTEM
315 log_debug ("fd_cache_close (%d) real\n", (int)fp
);
318 /* try to reuse a slot */
319 for (cc
= close_cache
; cc
; cc
= cc
->next
)
321 if (cc
->fp
== INVALID_FP
&& !strcmp (cc
->fname
, fname
))
325 log_debug ("fd_cache_close (%s) used existing slot\n", fname
);
331 log_debug ("fd_cache_close (%s) new slot created\n", fname
);
332 cc
= xcalloc (1, sizeof *cc
+ strlen (fname
));
333 strcpy (cc
->fname
, fname
);
335 cc
->next
= close_cache
;
340 * Do an direct_open on FNAME but first try to reuse one from the fd_cache
343 fd_cache_open (const char *fname
, const char *mode
)
348 for (cc
= close_cache
; cc
; cc
= cc
->next
)
350 if (cc
->fp
!= INVALID_FP
&& !strcmp (cc
->fname
, fname
))
352 fp_or_fd_t fp
= cc
->fp
;
355 log_debug ("fd_cache_open (%s) using cached fp\n", fname
);
356 #ifdef HAVE_W32_SYSTEM
357 if (SetFilePointer (fp
, 0, NULL
, FILE_BEGIN
) == 0xffffffff)
359 log_error ("rewind file failed on handle %p: ec=%d\n",
360 fp
, (int) GetLastError ());
364 if (lseek (fp
, 0, SEEK_SET
) == (off_t
) - 1)
366 log_error ("can't rewind fd %d: %s\n", fp
, strerror (errno
));
374 log_debug ("fd_cache_open (%s) not cached\n", fname
);
375 return direct_open (fname
, mode
);
378 #endif /*FILE_FILTER_USES_STDIO */
382 * Read data from a file into buf which has an allocated length of *LEN.
383 * return the number of read bytes in *LEN. OPAQUE is the FILE * of
384 * the stream. A is not used.
386 * IOBUFCTRL_INIT: called just before the function is linked into the
387 * list of function. This can be used to prepare internal
388 * data structures of the function.
389 * IOBUFCTRL_FREE: called just before the function is removed from the
390 * list of functions and can be used to release internal
391 * data structures or close a file etc.
392 * IOBUFCTRL_UNDERFLOW: called by iobuf_underflow to fill the buffer
393 * with new stuff. *RET_LEN is the available size of the
394 * buffer, and should be set to the number of bytes
395 * which were put into the buffer. The function
396 * returns 0 to indicate success, -1 on EOF and
397 * GPG_ERR_xxxxx for other errors.
399 * IOBUFCTRL_FLUSH: called by iobuf_flush() to write out the collected stuff.
400 * *RET_LAN is the number of bytes in BUF.
402 * IOBUFCTRL_CANCEL: send to all filters on behalf of iobuf_cancel. The
403 * filter may take appropriate action on this message.
406 file_filter (void *opaque
, int control
, iobuf_t chain
, byte
* buf
,
409 file_filter_ctx_t
*a
= opaque
;
410 fp_or_fd_t f
= a
->fp
;
411 size_t size
= *ret_len
;
415 #ifdef FILE_FILTER_USES_STDIO
416 if (control
== IOBUFCTRL_UNDERFLOW
)
418 assert (size
); /* need a buffer */
420 { /* On terminals you could easiely read as many EOFs as you call */
421 rc
= -1; /* fread() or fgetc() repeatly. Every call will block until you press */
422 *ret_len
= 0; /* CTRL-D. So we catch this case before we call fread() again. */
427 nbytes
= fread (buf
, 1, size
, f
);
428 if (feof (f
) && !nbytes
)
430 rc
= -1; /* okay: we can return EOF now. */
432 else if (ferror (f
) && errno
!= EPIPE
)
434 rc
= gpg_error_from_syserror ();
435 log_error ("%s: read error: %s\n", a
->fname
, strerror (errno
));
440 else if (control
== IOBUFCTRL_FLUSH
)
445 nbytes
= fwrite (buf
, 1, size
, f
);
448 rc
= gpg_error_from_syserror ();
449 log_error ("%s: write error: %s\n", a
->fname
, strerror (errno
));
454 else if (control
== IOBUFCTRL_INIT
)
456 a
->keep_open
= a
->no_cache
= 0;
458 else if (control
== IOBUFCTRL_DESC
)
460 *(char **) buf
= "file_filter";
462 else if (control
== IOBUFCTRL_FREE
)
464 if (f
!= stdin
&& f
!= stdout
)
467 log_debug ("%s: close fd %d\n", a
->fname
, fileno (f
));
472 xfree (a
); /* we can free our context now */
474 #else /* !stdio implementation */
476 if (control
== IOBUFCTRL_UNDERFLOW
)
478 assert (size
); /* need a buffer */
486 #ifdef HAVE_W32_SYSTEM
490 if (!ReadFile (f
, buf
, size
, &nread
, NULL
))
492 int ec
= (int) GetLastError ();
493 if (ec
!= ERROR_BROKEN_PIPE
)
495 rc
= gpg_error_from_errno (ec
);
496 log_error ("%s: read error: ec=%d\n", a
->fname
, ec
);
516 n
= read (f
, buf
, size
);
518 while (n
== -1 && errno
== EINTR
);
523 rc
= gpg_error_from_syserror ();
524 log_error ("%s: read error: %s\n",
525 a
->fname
, strerror (errno
));
541 else if (control
== IOBUFCTRL_FLUSH
)
545 #ifdef HAVE_W32_SYSTEM
552 if (size
&& !WriteFile (f
, p
, nbytes
, &n
, NULL
))
554 int ec
= (int) GetLastError ();
555 rc
= gpg_error_from_errno (ec
);
556 log_error ("%s: write error: ec=%d\n", a
->fname
, ec
);
573 n
= write (f
, p
, nbytes
);
575 while (n
== -1 && errno
== EINTR
);
582 while (n
!= -1 && nbytes
);
585 rc
= gpg_error_from_syserror ();
586 log_error ("%s: write error: %s\n", a
->fname
, strerror (errno
));
593 else if (control
== IOBUFCTRL_INIT
)
599 else if (control
== IOBUFCTRL_DESC
)
601 *(char **) buf
= "file_filter(fd)";
603 else if (control
== IOBUFCTRL_FREE
)
605 #ifdef HAVE_W32_SYSTEM
606 if (f
!= FILEP_OR_FD_FOR_STDIN
&& f
!= FILEP_OR_FD_FOR_STDOUT
)
609 log_debug ("%s: close handle %p\n", a
->fname
, f
);
611 fd_cache_close (a
->no_cache
? NULL
: a
->fname
, f
);
614 if ((int) f
!= 0 && (int) f
!= 1)
617 log_debug ("%s: close fd %d\n", a
->fname
, f
);
619 fd_cache_close (a
->no_cache
? NULL
: a
->fname
, f
);
623 xfree (a
); /* we can free our context now */
625 #endif /* !stdio implementation */
630 #ifdef HAVE_W32_SYSTEM
631 /* Because network sockets are special objects under Lose32 we have to
632 use a dedicated filter for them. */
634 sock_filter (void *opaque
, int control
, iobuf_t chain
, byte
* buf
,
637 sock_filter_ctx_t
*a
= opaque
;
638 size_t size
= *ret_len
;
642 if (control
== IOBUFCTRL_UNDERFLOW
)
644 assert (size
); /* need a buffer */
654 nread
= recv (a
->sock
, buf
, size
, 0);
655 if (nread
== SOCKET_ERROR
)
657 int ec
= (int) WSAGetLastError ();
658 rc
= gpg_error_from_errno (ec
);
659 log_error ("socket read error: ec=%d\n", ec
);
673 else if (control
== IOBUFCTRL_FLUSH
)
683 n
= send (a
->sock
, p
, nbytes
, 0);
684 if (n
== SOCKET_ERROR
)
686 int ec
= (int) WSAGetLastError ();
687 rc
= gpg_error_from_errno (ec
);
688 log_error ("socket write error: ec=%d\n", ec
);
699 else if (control
== IOBUFCTRL_INIT
)
705 else if (control
== IOBUFCTRL_DESC
)
707 *(char **) buf
= "sock_filter";
709 else if (control
== IOBUFCTRL_FREE
)
712 closesocket (a
->sock
);
713 xfree (a
); /* we can free our context now */
717 #endif /*HAVE_W32_SYSTEM*/
720 * This is used to implement the block write mode.
721 * Block reading is done on a byte by byte basis in readbyte(),
725 block_filter (void *opaque
, int control
, iobuf_t chain
, byte
* buffer
,
728 block_filter_ctx_t
*a
= opaque
;
729 char *buf
= (char *)buffer
;
730 size_t size
= *ret_len
;
731 int c
, needed
, rc
= 0;
734 if (control
== IOBUFCTRL_UNDERFLOW
)
739 assert (size
); /* need a buffer */
740 if (a
->eof
) /* don't read any further */
745 { /* get the length bytes */
755 /* These OpenPGP introduced huffman like encoded length
756 * bytes are really a mess :-( */
762 else if ((c
= iobuf_get (chain
)) == -1)
764 log_error ("block_filter: 1st length byte missing\n");
765 rc
= GPG_ERR_BAD_DATA
;
782 a
->size
= (c
- 192) * 256;
783 if ((c
= iobuf_get (chain
)) == -1)
786 ("block_filter: 2nd length byte missing\n");
787 rc
= GPG_ERR_BAD_DATA
;
802 a
->size
= iobuf_get (chain
) << 24;
803 a
->size
|= iobuf_get (chain
) << 16;
804 a
->size
|= iobuf_get (chain
) << 8;
805 if ((c
= iobuf_get (chain
)) == -1)
807 log_error ("block_filter: invalid 4 byte length\n");
808 rc
= GPG_ERR_BAD_DATA
;
822 { /* Next partial body length. */
823 a
->size
= 1 << (c
& 0x1f);
825 /* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
831 while (!rc
&& size
&& a
->size
)
833 needed
= size
< a
->size
? size
: a
->size
;
834 c
= iobuf_read (chain
, p
, needed
);
840 ("block_filter %p: read error (size=%lu,a->size=%lu)\n",
841 a
, (ulong
) size
+ c
, (ulong
) a
->size
+ c
);
842 rc
= GPG_ERR_BAD_DATA
;
855 else if (control
== IOBUFCTRL_FLUSH
)
858 { /* the complicated openpgp scheme */
859 size_t blen
, n
, nbytes
= size
+ a
->buflen
;
861 assert (a
->buflen
<= OP_MIN_PARTIAL_CHUNK
);
862 if (nbytes
< OP_MIN_PARTIAL_CHUNK
)
864 /* not enough to write a partial block out; so we store it */
866 a
->buffer
= xmalloc (OP_MIN_PARTIAL_CHUNK
);
867 memcpy (a
->buffer
+ a
->buflen
, buf
, size
);
871 { /* okay, we can write out something */
872 /* do this in a loop to use the most efficient block lengths */
876 /* find the best matching block length - this is limited
877 * by the size of the internal buffering */
878 for (blen
= OP_MIN_PARTIAL_CHUNK
* 2,
879 c
= OP_MIN_PARTIAL_CHUNK_2POW
+ 1; blen
<= nbytes
;
884 /* write the partial length header */
885 assert (c
<= 0x1f); /*;-) */
887 iobuf_put (chain
, c
);
889 { /* write stuff from the buffer */
890 assert (n
== OP_MIN_PARTIAL_CHUNK
);
891 if (iobuf_write (chain
, a
->buffer
, n
))
892 rc
= gpg_error_from_syserror ();
896 if ((n
= nbytes
) > blen
)
898 if (n
&& iobuf_write (chain
, p
, n
))
899 rc
= gpg_error_from_syserror ();
903 while (!rc
&& nbytes
>= OP_MIN_PARTIAL_CHUNK
);
904 /* store the rest in the buffer */
908 assert (nbytes
< OP_MIN_PARTIAL_CHUNK
);
910 a
->buffer
= xmalloc (OP_MIN_PARTIAL_CHUNK
);
911 memcpy (a
->buffer
, p
, nbytes
);
919 else if (control
== IOBUFCTRL_INIT
)
922 log_debug ("init block_filter %p\n", a
);
925 else if (a
->use
== 1)
926 a
->count
= a
->size
= 0;
928 a
->count
= a
->size
; /* force first length bytes */
933 else if (control
== IOBUFCTRL_DESC
)
935 *(char **) buf
= "block_filter";
937 else if (control
== IOBUFCTRL_FREE
)
940 { /* write the end markers */
944 /* write out the remaining bytes without a partial header
945 * the length of this header may be 0 - but if it is
946 * the first block we are not allowed to use a partial header
947 * and frankly we can't do so, because this length must be
948 * a power of 2. This is _really_ complicated because we
949 * have to check the possible length of a packet prior
950 * to it's creation: a chain of filters becomes complicated
951 * and we need a lot of code to handle compressed packets etc.
954 /* construct header */
956 /*log_debug("partial: remaining length=%u\n", len ); */
958 rc
= iobuf_put (chain
, len
);
961 if (!(rc
= iobuf_put (chain
, ((len
- 192) / 256) + 192)))
962 rc
= iobuf_put (chain
, ((len
- 192) % 256));
965 { /* use a 4 byte header */
966 if (!(rc
= iobuf_put (chain
, 0xff)))
967 if (!(rc
= iobuf_put (chain
, (len
>> 24) & 0xff)))
968 if (!(rc
= iobuf_put (chain
, (len
>> 16) & 0xff)))
969 if (!(rc
= iobuf_put (chain
, (len
>> 8) & 0xff)))
970 rc
= iobuf_put (chain
, len
& 0xff);
973 rc
= iobuf_write (chain
, a
->buffer
, len
);
976 log_error ("block_filter: write error: %s\n",
978 rc
= gpg_error_from_syserror ();
989 log_error ("block_filter: pending bytes!\n");
992 log_debug ("free block_filter %p\n", a
);
993 xfree (a
); /* we can free our context now */
1001 print_chain (iobuf_t a
)
1005 for (; a
; a
= a
->chain
)
1007 size_t dummy_len
= 0;
1008 const char *desc
= "[none]";
1011 a
->filter (a
->filter_ov
, IOBUFCTRL_DESC
, NULL
,
1012 (byte
*) & desc
, &dummy_len
);
1014 log_debug ("iobuf chain: %d.%d `%s' filter_eof=%d start=%d len=%d\n",
1015 a
->no
, a
->subno
, desc
?desc
:"?", a
->filter_eof
,
1016 (int) a
->d
.start
, (int) a
->d
.len
);
1021 iobuf_print_chain (iobuf_t a
)
1028 * Allocate a new io buffer, with no function assigned.
1029 * Use is the desired usage: 1 for input, 2 for output, 3 for temp buffer
1030 * BUFSIZE is a suggested buffer size.
1033 iobuf_alloc (int use
, size_t bufsize
)
1036 static int number
= 0;
1038 a
= xcalloc (1, sizeof *a
);
1040 a
->d
.buf
= xmalloc (bufsize
);
1041 a
->d
.size
= bufsize
;
1045 a
->real_fname
= NULL
;
1050 iobuf_close (iobuf_t a
)
1053 size_t dummy_len
= 0;
1056 if (a
&& a
->directfp
)
1058 fclose (a
->directfp
);
1059 xfree (a
->real_fname
);
1061 log_debug ("iobuf_close -> %p\n", a
->directfp
);
1065 for (; a
&& !rc
; a
= a2
)
1068 if (a
->use
== 2 && (rc
= iobuf_flush (a
)))
1069 log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc
));
1072 log_debug ("iobuf-%d.%d: close `%s'\n", a
->no
, a
->subno
,
1073 a
->desc
?a
->desc
:"?");
1074 if (a
->filter
&& (rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_FREE
,
1075 a
->chain
, NULL
, &dummy_len
)))
1076 log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc
));
1077 xfree (a
->real_fname
);
1080 memset (a
->d
.buf
, 0, a
->d
.size
); /* erase the buffer */
1089 iobuf_cancel (iobuf_t a
)
1094 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1095 char *remove_name
= NULL
;
1098 if (a
&& a
->use
== 2)
1100 s
= iobuf_get_real_fname (a
);
1103 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1104 remove_name
= xstrdup (s
);
1111 /* send a cancel message to all filters */
1112 for (a2
= a
; a2
; a2
= a2
->chain
)
1116 a2
->filter (a2
->filter_ov
, IOBUFCTRL_CANCEL
, a2
->chain
, NULL
, &dummy
);
1119 rc
= iobuf_close (a
);
1120 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1123 /* Argg, MSDOS does not allow to remove open files. So
1124 * we have to do it here */
1125 remove (remove_name
);
1126 xfree (remove_name
);
1134 * create a temporary iobuf, which can be used to collect stuff
1135 * in an iobuf and later be written by iobuf_write_temp() to another
1143 a
= iobuf_alloc (3, IOBUF_BUFFER_SIZE
);
1149 iobuf_temp_with_content (const char *buffer
, size_t length
)
1153 a
= iobuf_alloc (3, length
);
1154 memcpy (a
->d
.buf
, buffer
, length
);
1161 iobuf_enable_special_filenames (int yes
)
1163 special_names_enabled
= yes
;
1167 /* See whether the filename has the form "-&nnnn", where n is a
1168 non-zero number. Returns this number or -1 if it is not the
1171 check_special_filename (const char *fname
)
1173 if (special_names_enabled
&& fname
&& *fname
== '-' && fname
[1] == '&')
1178 for (i
= 0; digitp (fname
+i
); i
++)
1181 return atoi (fname
);
1187 /* This fucntion returns true if FNAME indicates a PIPE (stdout or
1188 stderr) or a special file name if those are enabled. */
1190 iobuf_is_pipe_filename (const char *fname
)
1192 if (!fname
|| (*fname
=='-' && !fname
[1]) )
1194 return check_special_filename (fname
) != -1;
1198 * Create a head iobuf for reading from a file
1199 * returns: NULL if an error occures and sets errno
1202 iobuf_open (const char *fname
)
1206 file_filter_ctx_t
*fcx
;
1211 if (!fname
|| (*fname
== '-' && !fname
[1]))
1213 fp
= FILEP_OR_FD_FOR_STDIN
;
1215 setmode (my_fileno (fp
), O_BINARY
);
1220 else if ((fd
= check_special_filename (fname
)) != -1)
1221 return iobuf_fdopen (translate_file_handle (fd
, 0), "rb");
1222 else if ((fp
= my_fopen_ro (fname
, "rb")) == INVALID_FP
)
1224 a
= iobuf_alloc (1, IOBUF_BUFFER_SIZE
);
1225 fcx
= xmalloc (sizeof *fcx
+ strlen (fname
));
1227 fcx
->print_only_name
= print_only
;
1228 strcpy (fcx
->fname
, fname
);
1230 a
->real_fname
= xstrdup (fname
);
1231 a
->filter
= file_filter
;
1233 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1234 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1236 log_debug ("iobuf-%d.%d: open `%s' fd=%d\n",
1237 a
->no
, a
->subno
, fname
, (int) my_fileno (fcx
->fp
));
1243 * Create a head iobuf for reading from a file
1244 * returns: NULL if an error occures and sets errno
1247 iobuf_fdopen (int fd
, const char *mode
)
1251 file_filter_ctx_t
*fcx
;
1254 #ifdef FILE_FILTER_USES_STDIO
1255 if (!(fp
= fdopen (fd
, mode
)))
1258 fp
= (fp_or_fd_t
) fd
;
1260 a
= iobuf_alloc (strchr (mode
, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE
);
1261 fcx
= xmalloc (sizeof *fcx
+ 20);
1263 fcx
->print_only_name
= 1;
1264 sprintf (fcx
->fname
, "[fd %d]", fd
);
1265 a
->filter
= file_filter
;
1267 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1268 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1270 log_debug ("iobuf-%d.%d: fdopen `%s'\n", a
->no
, a
->subno
, fcx
->fname
);
1271 iobuf_ioctl (a
, 3, 1, NULL
); /* disable fd caching */
1277 iobuf_sockopen (int fd
, const char *mode
)
1280 #ifdef HAVE_W32_SYSTEM
1281 sock_filter_ctx_t
*scx
;
1284 a
= iobuf_alloc (strchr (mode
, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE
);
1285 scx
= xmalloc (sizeof *scx
+ 25);
1287 scx
->print_only_name
= 1;
1288 sprintf (scx
->fname
, "[sock %d]", fd
);
1289 a
->filter
= sock_filter
;
1291 sock_filter (scx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1292 sock_filter (scx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1294 log_debug ("iobuf-%d.%d: sockopen `%s'\n", a
->no
, a
->subno
, scx
->fname
);
1295 iobuf_ioctl (a
, 3, 1, NULL
); /* disable fd caching */
1297 a
= iobuf_fdopen (fd
, mode
);
1303 * create an iobuf for writing to a file; the file will be created.
1306 iobuf_create (const char *fname
)
1310 file_filter_ctx_t
*fcx
;
1315 if (!fname
|| (*fname
== '-' && !fname
[1]))
1317 fp
= FILEP_OR_FD_FOR_STDOUT
;
1319 setmode (my_fileno (fp
), O_BINARY
);
1324 else if ((fd
= check_special_filename (fname
)) != -1)
1325 return iobuf_fdopen (translate_file_handle (fd
, 1), "wb");
1326 else if ((fp
= my_fopen (fname
, "wb")) == INVALID_FP
)
1328 a
= iobuf_alloc (2, IOBUF_BUFFER_SIZE
);
1329 fcx
= xmalloc (sizeof *fcx
+ strlen (fname
));
1331 fcx
->print_only_name
= print_only
;
1332 strcpy (fcx
->fname
, fname
);
1334 a
->real_fname
= xstrdup (fname
);
1335 a
->filter
= file_filter
;
1337 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1338 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1340 log_debug ("iobuf-%d.%d: create `%s'\n", a
->no
, a
->subno
,
1341 a
->desc
?a
->desc
:"?");
1347 * append to an iobuf; if the file does not exist, create it.
1348 * cannot be used for stdout.
1349 * Note: This is not used.
1351 #if 0 /* not used */
1353 iobuf_append (const char *fname
)
1357 file_filter_ctx_t
*fcx
;
1362 else if (!(fp
= my_fopen (fname
, "ab")))
1364 a
= iobuf_alloc (2, IOBUF_BUFFER_SIZE
);
1365 fcx
= m_alloc (sizeof *fcx
+ strlen (fname
));
1367 strcpy (fcx
->fname
, fname
);
1368 a
->real_fname
= m_strdup (fname
);
1369 a
->filter
= file_filter
;
1371 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1372 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1374 log_debug ("iobuf-%d.%d: append `%s'\n", a
->no
, a
->subno
,
1375 a
->desc
?a
->desc
:"?");
1382 iobuf_openrw (const char *fname
)
1386 file_filter_ctx_t
*fcx
;
1391 else if ((fp
= my_fopen (fname
, "r+b")) == INVALID_FP
)
1393 a
= iobuf_alloc (2, IOBUF_BUFFER_SIZE
);
1394 fcx
= xmalloc (sizeof *fcx
+ strlen (fname
));
1396 strcpy (fcx
->fname
, fname
);
1397 a
->real_fname
= xstrdup (fname
);
1398 a
->filter
= file_filter
;
1400 file_filter (fcx
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &len
);
1401 file_filter (fcx
, IOBUFCTRL_INIT
, NULL
, NULL
, &len
);
1403 log_debug ("iobuf-%d.%d: openrw `%s'\n", a
->no
, a
->subno
,
1404 a
->desc
?a
->desc
:"?");
1411 iobuf_ioctl (iobuf_t a
, int cmd
, int intval
, void *ptrval
)
1414 { /* keep system filepointer/descriptor open */
1416 log_debug ("iobuf-%d.%d: ioctl `%s' keep=%d\n",
1417 a
? a
->no
: -1, a
? a
->subno
: -1,
1418 a
&& a
->desc
? a
->desc
: "?",
1420 for (; a
; a
= a
->chain
)
1421 if (!a
->chain
&& a
->filter
== file_filter
)
1423 file_filter_ctx_t
*b
= a
->filter_ov
;
1424 b
->keep_open
= intval
;
1427 #ifdef HAVE_W32_SYSTEM
1428 else if (!a
->chain
&& a
->filter
== sock_filter
)
1430 sock_filter_ctx_t
*b
= a
->filter_ov
;
1431 b
->keep_open
= intval
;
1437 { /* invalidate cache */
1439 log_debug ("iobuf-*.*: ioctl `%s' invalidate\n",
1440 ptrval
? (char *) ptrval
: "?");
1441 if (!a
&& !intval
&& ptrval
)
1443 #ifndef FILE_FILTER_USES_STDIO
1444 fd_cache_invalidate (ptrval
);
1450 { /* disallow/allow caching */
1452 log_debug ("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
1453 a
? a
->no
: -1, a
? a
->subno
: -1,
1454 a
&& a
->desc
? a
->desc
: "?",
1456 for (; a
; a
= a
->chain
)
1457 if (!a
->chain
&& a
->filter
== file_filter
)
1459 file_filter_ctx_t
*b
= a
->filter_ov
;
1460 b
->no_cache
= intval
;
1463 #ifdef HAVE_W32_SYSTEM
1464 else if (!a
->chain
&& a
->filter
== sock_filter
)
1466 sock_filter_ctx_t
*b
= a
->filter_ov
;
1467 b
->no_cache
= intval
;
1478 * Register an i/o filter.
1481 iobuf_push_filter (iobuf_t a
,
1482 int (*f
) (void *opaque
, int control
,
1483 iobuf_t chain
, byte
* buf
, size_t * len
),
1486 return iobuf_push_filter2 (a
, f
, ov
, 0);
1490 iobuf_push_filter2 (iobuf_t a
,
1491 int (*f
) (void *opaque
, int control
,
1492 iobuf_t chain
, byte
* buf
, size_t * len
),
1493 void *ov
, int rel_ov
)
1496 size_t dummy_len
= 0;
1502 if (a
->use
== 2 && (rc
= iobuf_flush (a
)))
1504 /* make a copy of the current stream, so that
1505 * A is the new stream and B the original one.
1506 * The contents of the buffers are transferred to the
1509 b
= xmalloc (sizeof *b
);
1510 memcpy (b
, a
, sizeof *b
);
1511 /* fixme: it is stupid to keep a copy of the name at every level
1512 * but we need the name somewhere because the name known by file_filter
1513 * may have been released when we need the name of the file */
1514 b
->real_fname
= a
->real_fname
? xstrdup (a
->real_fname
) : NULL
;
1515 /* remove the filter stuff from the new stream */
1517 a
->filter_ov
= NULL
;
1518 a
->filter_ov_owner
= 0;
1521 a
->use
= 2; /* make a write stream from a temp stream */
1524 { /* allocate a fresh buffer for the
1526 b
->d
.buf
= xmalloc (a
->d
.size
);
1531 { /* allocate a fresh buffer for the new
1533 a
->d
.buf
= xmalloc (a
->d
.size
);
1537 /* disable nlimit for the new stream */
1538 a
->ntotal
= b
->ntotal
+ b
->nbytes
;
1539 a
->nlimit
= a
->nbytes
= 0;
1541 /* make a link from the new stream to the original stream */
1543 a
->opaque
= b
->opaque
;
1545 /* setup the function on the new stream */
1548 a
->filter_ov_owner
= rel_ov
;
1550 a
->subno
= b
->subno
+ 1;
1551 f (ov
, IOBUFCTRL_DESC
, NULL
, (byte
*) & a
->desc
, &dummy_len
);
1555 log_debug ("iobuf-%d.%d: push `%s'\n", a
->no
, a
->subno
,
1556 a
->desc
?a
->desc
:"?");
1560 /* now we can initialize the new function if we have one */
1561 if (a
->filter
&& (rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_INIT
, a
->chain
,
1563 log_error ("IOBUFCTRL_INIT failed: %s\n", gpg_strerror (rc
));
1568 * Remove an i/o filter.
1571 pop_filter (iobuf_t a
, int (*f
) (void *opaque
, int control
,
1572 iobuf_t chain
, byte
* buf
, size_t * len
),
1576 size_t dummy_len
= 0;
1583 log_debug ("iobuf-%d.%d: pop `%s'\n", a
->no
, a
->subno
,
1584 a
->desc
?a
->desc
:"?");
1586 { /* this is simple */
1590 xfree (a
->real_fname
);
1591 memcpy (a
, b
, sizeof *a
);
1595 for (b
= a
; b
; b
= b
->chain
)
1596 if (b
->filter
== f
&& (!ov
|| b
->filter_ov
== ov
))
1599 log_bug ("pop_filter(): filter function not found\n");
1601 /* flush this stream if it is an output stream */
1602 if (a
->use
== 2 && (rc
= iobuf_flush (b
)))
1604 log_error ("iobuf_flush failed in pop_filter: %s\n", gpg_strerror (rc
));
1607 /* and tell the filter to free it self */
1608 if (b
->filter
&& (rc
= b
->filter (b
->filter_ov
, IOBUFCTRL_FREE
, b
->chain
,
1611 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc
));
1614 if (b
->filter_ov
&& b
->filter_ov_owner
)
1616 xfree (b
->filter_ov
);
1617 b
->filter_ov
= NULL
;
1621 /* and see how to remove it */
1622 if (a
== b
&& !b
->chain
)
1623 log_bug ("can't remove the last filter from the chain\n");
1625 { /* remove the first iobuf from the chain */
1626 /* everything from b is copied to a. This is save because
1627 * a flush has been done on the to be removed entry
1631 xfree (a
->real_fname
);
1632 memcpy (a
, b
, sizeof *a
);
1635 log_debug ("iobuf-%d.%d: popped filter\n", a
->no
, a
->subno
);
1638 { /* remove the last iobuf from the chain */
1639 log_bug ("Ohh jeee, trying to remove a head filter\n");
1642 { /* remove an intermediate iobuf from the chain */
1643 log_bug ("Ohh jeee, trying to remove an intermediate filter\n");
1651 * read underflow: read more bytes into the buffer and return
1652 * the first byte or -1 on EOF.
1655 underflow (iobuf_t a
)
1660 assert (a
->d
.start
== a
->d
.len
);
1662 return -1; /* EOF because a temp buffer can't do an underflow */
1668 iobuf_t b
= a
->chain
;
1670 log_debug ("iobuf-%d.%d: pop `%s' in underflow\n",
1671 a
->no
, a
->subno
, a
->desc
?a
->desc
:"?");
1673 xfree (a
->real_fname
);
1674 memcpy (a
, b
, sizeof *a
);
1679 a
->filter_eof
= 0; /* for the top level filter */
1681 log_debug ("iobuf-%d.%d: underflow: eof (due to filter eof)\n",
1683 return -1; /* return one(!) EOF */
1688 log_debug ("iobuf-%d.%d: error\n", a
->no
, a
->subno
);
1694 FILE *fp
= a
->directfp
;
1696 len
= fread (a
->d
.buf
, 1, a
->d
.size
, fp
);
1697 if (len
< a
->d
.size
)
1700 a
->error
= gpg_error_from_syserror ();
1704 return len
? a
->d
.buf
[a
->d
.start
++] : -1;
1712 log_debug ("iobuf-%d.%d: underflow: req=%lu\n",
1713 a
->no
, a
->subno
, (ulong
) len
);
1714 rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_UNDERFLOW
, a
->chain
,
1718 log_debug ("iobuf-%d.%d: underflow: got=%lu rc=%d\n",
1719 a
->no
, a
->subno
, (ulong
) len
, rc
);
1720 /* if( a->no == 1 ) */
1721 /* log_hexdump (" data:", a->d.buf, len); */
1723 if (a
->use
== 1 && rc
== -1)
1724 { /* EOF: we can remove the filter */
1725 size_t dummy_len
= 0;
1727 /* and tell the filter to free itself */
1728 if ((rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_FREE
, a
->chain
,
1730 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc
));
1731 if (a
->filter_ov
&& a
->filter_ov_owner
)
1733 xfree (a
->filter_ov
);
1734 a
->filter_ov
= NULL
;
1738 a
->filter_ov
= NULL
;
1740 if (!len
&& a
->chain
)
1742 iobuf_t b
= a
->chain
;
1744 log_debug ("iobuf-%d.%d: pop in underflow (!len)\n",
1747 xfree (a
->real_fname
);
1748 memcpy (a
, b
, sizeof *a
);
1759 log_debug ("iobuf-%d.%d: underflow: eof\n", a
->no
, a
->subno
);
1764 return a
->d
.buf
[a
->d
.start
++];
1769 log_debug ("iobuf-%d.%d: underflow: eof (no filter)\n",
1771 return -1; /* no filter; return EOF */
1777 iobuf_flush (iobuf_t a
)
1786 { /* increase the temp buffer */
1787 unsigned char *newbuf
;
1788 size_t newsize
= a
->d
.size
+ IOBUF_BUFFER_SIZE
;
1791 log_debug ("increasing temp iobuf from %lu to %lu\n",
1792 (ulong
) a
->d
.size
, (ulong
) newsize
);
1793 newbuf
= xmalloc (newsize
);
1794 memcpy (newbuf
, a
->d
.buf
, a
->d
.len
);
1797 a
->d
.size
= newsize
;
1800 else if (a
->use
!= 2)
1801 log_bug ("flush on non-output iobuf\n");
1802 else if (!a
->filter
)
1803 log_bug ("iobuf_flush: no filter\n");
1805 rc
= a
->filter (a
->filter_ov
, IOBUFCTRL_FLUSH
, a
->chain
, a
->d
.buf
, &len
);
1806 if (!rc
&& len
!= a
->d
.len
)
1808 log_info ("iobuf_flush did not write all!\n");
1809 rc
= GPG_ERR_INTERNAL
;
1820 * Read a byte from the iobuf; returns -1 on EOF
1823 iobuf_readbyte (iobuf_t a
)
1827 if (a
->nlimit
&& a
->nbytes
>= a
->nlimit
)
1828 return -1; /* forced EOF */
1830 if (a
->d
.start
< a
->d
.len
)
1832 c
= a
->d
.buf
[a
->d
.start
++];
1834 else if ((c
= underflow (a
)) == -1)
1835 return -1; /* EOF */
1843 iobuf_read (iobuf_t a
, void *buffer
, unsigned int buflen
)
1845 unsigned char *buf
= (unsigned char *)buffer
;
1850 /* Handle special cases. */
1851 for (n
= 0; n
< buflen
; n
++)
1853 if ((c
= iobuf_readbyte (a
)) == -1)
1856 return -1; /* eof */
1870 if (n
< buflen
&& a
->d
.start
< a
->d
.len
)
1872 unsigned size
= a
->d
.len
- a
->d
.start
;
1873 if (size
> buflen
- n
)
1876 memcpy (buf
, a
->d
.buf
+ a
->d
.start
, size
);
1884 if ((c
= underflow (a
)) == -1)
1887 return n
? n
: -1 /*EOF*/;
1902 * Have a look at the iobuf.
1903 * NOTE: This only works in special cases.
1906 iobuf_peek (iobuf_t a
, byte
* buf
, unsigned buflen
)
1913 if (!(a
->d
.start
< a
->d
.len
))
1915 if (underflow (a
) == -1)
1917 /* And unget this character. */
1918 assert (a
->d
.start
== 1);
1922 for (n
= 0; n
< buflen
&& (a
->d
.start
+ n
) < a
->d
.len
; n
++, buf
++)
1931 iobuf_writebyte (iobuf_t a
, unsigned int c
)
1938 if (a
->d
.len
== a
->d
.size
)
1939 if ((rc
=iobuf_flush (a
)))
1942 assert (a
->d
.len
< a
->d
.size
);
1943 a
->d
.buf
[a
->d
.len
++] = c
;
1949 iobuf_write (iobuf_t a
, const void *buffer
, unsigned int buflen
)
1951 const unsigned char *buf
= (const unsigned char *)buffer
;
1959 if (buflen
&& a
->d
.len
< a
->d
.size
)
1961 unsigned size
= a
->d
.size
- a
->d
.len
;
1964 memcpy (a
->d
.buf
+ a
->d
.len
, buf
, size
);
1971 rc
= iobuf_flush (a
);
1982 iobuf_writestr (iobuf_t a
, const char *buf
)
1987 if ((rc
=iobuf_writebyte (a
, *buf
)))
1995 * copy the contents of TEMP to A.
1998 iobuf_write_temp (iobuf_t a
, iobuf_t temp
)
2001 pop_filter (temp
, temp
->filter
, NULL
);
2002 return iobuf_write (a
, temp
->d
.buf
, temp
->d
.len
);
2006 * copy the contents of the temp io stream to BUFFER.
2009 iobuf_temp_to_buffer (iobuf_t a
, byte
* buffer
, size_t buflen
)
2011 size_t n
= a
->d
.len
;
2015 memcpy (buffer
, a
->d
.buf
, n
);
2021 * Call this function to terminate processing of the temp stream
2022 * without closing it. This removes all filters from the stream
2023 * makes sure that iobuf_get_temp_{buffer,length}() returns correct
2027 iobuf_flush_temp (iobuf_t temp
)
2030 pop_filter (temp
, temp
->filter
, NULL
);
2035 * Set a limit on how many bytes may be read from the input stream A.
2036 * Setting the limit to 0 disables this feature.
2039 iobuf_set_limit (iobuf_t a
, off_t nlimit
)
2046 a
->ntotal
+= a
->nbytes
;
2052 /* Return the length of an open file A. IF OVERFLOW is not NULL it
2053 will be set to true if the file is larger than what off_t can cope
2054 with. The function return 0 on error or on overflow condition. */
2056 iobuf_get_filelength (iobuf_t a
, int *overflow
)
2065 FILE *fp
= a
->directfp
;
2067 if ( !fstat(fileno(fp
), &st
) )
2069 log_error("fstat() failed: %s\n", strerror(errno
) );
2073 /* Hmmm: file_filter may have already been removed */
2074 for ( ; a
; a
= a
->chain
)
2075 if ( !a
->chain
&& a
->filter
== file_filter
)
2077 file_filter_ctx_t
*b
= a
->filter_ov
;
2078 fp_or_fd_t fp
= b
->fp
;
2080 #if defined(HAVE_W32_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2082 static int (* __stdcall get_file_size_ex
) (void *handle
,
2083 LARGE_INTEGER
*r_size
);
2084 static int get_file_size_ex_initialized
;
2086 if (!get_file_size_ex_initialized
)
2090 handle
= dlopen ("kernel32.dll", RTLD_LAZY
);
2093 get_file_size_ex
= dlsym (handle
, "GetFileSizeEx");
2094 if (!get_file_size_ex
)
2097 get_file_size_ex_initialized
= 1;
2100 if (get_file_size_ex
)
2102 /* This is a newer system with GetFileSizeEx; we use this
2103 then because it seem that GetFileSize won't return a
2104 proper error in case a file is larger than 4GB. */
2105 LARGE_INTEGER exsize
;
2107 if (get_file_size_ex (fp
, &exsize
))
2109 if (!exsize
.u
.HighPart
)
2110 return exsize
.u
.LowPart
;
2118 if ((size
=GetFileSize (fp
, NULL
)) != 0xffffffff)
2121 log_error ("GetFileSize for handle %p failed: %s\n",
2122 fp
, w32_strerror (0));
2124 if ( !fstat(my_fileno(fp
), &st
) )
2126 log_error("fstat() failed: %s\n", strerror(errno
) );
2128 break/*the for loop*/;
2135 /* Return the file descriptor of the underlying file or -1 if it is
2138 iobuf_get_fd (iobuf_t a
)
2141 return fileno ( (FILE*)a
->directfp
);
2143 for ( ; a
; a
= a
->chain
)
2144 if (!a
->chain
&& a
->filter
== file_filter
)
2146 file_filter_ctx_t
*b
= a
->filter_ov
;
2147 fp_or_fd_t fp
= b
->fp
;
2149 return my_fileno (fp
);
2158 * Tell the file position, where the next read will take place
2161 iobuf_tell (iobuf_t a
)
2163 return a
->ntotal
+ a
->nbytes
;
2167 #if !defined(HAVE_FSEEKO) && !defined(fseeko)
2169 #ifdef HAVE_LIMITS_H
2170 # include <limits.h>
2173 # define LONG_MAX ((long) ((unsigned long) -1 >> 1))
2176 # define LONG_MIN (-1 - LONG_MAX)
2180 * A substitute for fseeko, for hosts that don't have it.
2183 fseeko (FILE * stream
, off_t newpos
, int whence
)
2185 while (newpos
!= (long) newpos
)
2187 long pos
= newpos
< 0 ? LONG_MIN
: LONG_MAX
;
2188 if (fseek (stream
, pos
, whence
) != 0)
2193 return fseek (stream
, (long) newpos
, whence
);
2198 * This is a very limited implementation. It simply discards all internal
2199 * buffering and removes all filters but the first one.
2202 iobuf_seek (iobuf_t a
, off_t newpos
)
2204 file_filter_ctx_t
*b
= NULL
;
2208 FILE *fp
= a
->directfp
;
2209 if (fseeko (fp
, newpos
, SEEK_SET
))
2211 log_error ("can't seek: %s\n", strerror (errno
));
2218 for (; a
; a
= a
->chain
)
2220 if (!a
->chain
&& a
->filter
== file_filter
)
2228 #ifdef FILE_FILTER_USES_STDIO
2229 if (fseeko (b
->fp
, newpos
, SEEK_SET
))
2231 log_error ("can't fseek: %s\n", strerror (errno
));
2235 #ifdef HAVE_W32_SYSTEM
2236 if (SetFilePointer (b
->fp
, newpos
, NULL
, FILE_BEGIN
) == 0xffffffff)
2238 log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2239 b
->fp
, (int) GetLastError ());
2243 if (lseek (b
->fp
, newpos
, SEEK_SET
) == (off_t
) - 1)
2245 log_error ("can't lseek: %s\n", strerror (errno
));
2251 a
->d
.len
= 0; /* discard buffer */
2258 /* remove filters, but the last */
2260 log_debug ("pop_filter called in iobuf_seek - please report\n");
2262 pop_filter (a
, a
->filter
, NULL
);
2273 * Retrieve the real filename
2276 iobuf_get_real_fname (iobuf_t a
)
2279 return a
->real_fname
;
2281 /* the old solution */
2282 for (; a
; a
= a
->chain
)
2283 if (!a
->chain
&& a
->filter
== file_filter
)
2285 file_filter_ctx_t
*b
= a
->filter_ov
;
2286 return b
->print_only_name
? NULL
: b
->fname
;
2294 * Retrieve the filename
2297 iobuf_get_fname (iobuf_t a
)
2299 for (; a
; a
= a
->chain
)
2300 if (!a
->chain
&& a
->filter
== file_filter
)
2302 file_filter_ctx_t
*b
= a
->filter_ov
;
2311 * enable partial block mode as described in the OpenPGP draft.
2312 * LEN is the first length byte on read, but ignored on writes.
2315 iobuf_set_partial_block_mode (iobuf_t a
, size_t len
)
2317 block_filter_ctx_t
*ctx
= xcalloc (1, sizeof *ctx
);
2319 assert (a
->use
== 1 || a
->use
== 2);
2324 log_debug ("pop_filter called in set_partial_block_mode"
2325 " - please report\n");
2326 pop_filter (a
, block_filter
, NULL
);
2333 iobuf_push_filter (a
, block_filter
, ctx
);
2340 * Same as fgets() but if the buffer is too short a larger one will
2341 * be allocated up to some limit *max_length.
2342 * A line is considered a byte stream ending in a LF.
2343 * Returns the length of the line. EOF is indicated by a line of
2344 * length zero. The last LF may be missing due to an EOF.
2345 * is max_length is zero on return, the line has been truncated.
2347 * Note: The buffer is allocated with enough space to append a CR,LF,EOL
2350 iobuf_read_line (iobuf_t a
, byte
** addr_of_buffer
,
2351 unsigned *length_of_buffer
, unsigned *max_length
)
2354 char *buffer
= (char *)*addr_of_buffer
;
2355 unsigned length
= *length_of_buffer
;
2356 unsigned nbytes
= 0;
2357 unsigned maxlen
= *max_length
;
2361 { /* must allocate a new buffer */
2363 buffer
= xmalloc (length
);
2364 *addr_of_buffer
= (unsigned char *)buffer
;
2365 *length_of_buffer
= length
;
2368 length
-= 3; /* reserve 3 bytes (cr,lf,eol) */
2370 while ((c
= iobuf_get (a
)) != -1)
2372 if (nbytes
== length
)
2373 { /* increase the buffer */
2374 if (length
> maxlen
)
2375 { /* this is out limit */
2376 /* skip the rest of the line */
2377 while (c
!= '\n' && (c
= iobuf_get (a
)) != -1)
2379 *p
++ = '\n'; /* always append a LF (we have reserved space) */
2381 *max_length
= 0; /* indicate truncation */
2384 length
+= 3; /* correct for the reserved byte */
2385 length
+= length
< 1024 ? 256 : 1024;
2386 buffer
= xrealloc (buffer
, length
);
2387 *addr_of_buffer
= (unsigned char *)buffer
;
2388 *length_of_buffer
= length
;
2389 length
-= 3; /* and reserve again */
2390 p
= buffer
+ nbytes
;
2397 *p
= 0; /* make sure the line is a string */
2403 translate_file_handle (int fd
, int for_write
)
2405 #ifdef HAVE_W32_SYSTEM
2406 # ifdef FILE_FILTER_USES_STDIO
2407 fd
= translate_sys2libc_fd (fd
, for_write
);
2413 x
= (int) GetStdHandle (STD_INPUT_HANDLE
);
2415 x
= (int) GetStdHandle (STD_OUTPUT_HANDLE
);
2417 x
= (int) GetStdHandle (STD_ERROR_HANDLE
);
2422 log_debug ("GetStdHandle(%d) failed: ec=%d\n",
2423 fd
, (int) GetLastError ());
2434 iobuf_skip_rest (iobuf_t a
, unsigned long n
, int partial
)
2440 if (a
->nofast
|| a
->d
.start
>= a
->d
.len
)
2442 if (iobuf_readbyte (a
) == -1)
2449 unsigned long count
= a
->d
.len
- a
->d
.start
;
2451 a
->d
.start
= a
->d
.len
;
2457 unsigned long remaining
= n
;
2458 while (remaining
> 0)
2460 if (a
->nofast
|| a
->d
.start
>= a
->d
.len
)
2462 if (iobuf_readbyte (a
) == -1)
2470 unsigned long count
= a
->d
.len
- a
->d
.start
;
2471 if (count
> remaining
)
2476 a
->d
.start
+= count
;