1 /* iobuf.c - File Handling for OpenPGP.
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2006, 2007, 2008,
3 * 2009, 2010, 2011 Free Software Foundation, Inc.
4 * Copyright (C) 2015 g10 Code GmbH
6 * This file is part of GnuPG.
8 * This file is free software; you can redistribute it and/or modify
9 * it under the terms of either
11 * - the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at
13 * your option) any later version.
17 * - the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * or both in parallel, as here.
23 * This file is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, see <https://www.gnu.org/licenses/>.
39 #include <sys/types.h>
43 #ifdef HAVE_W32_SYSTEM
44 # ifdef HAVE_WINSOCK2_H
45 # include <winsock2.h>
52 #endif /* __riscos__ */
60 /*-- Begin configurable part. --*/
62 /* The size of the internal buffers.
63 NOTE: If you change this value you MUST also adjust the regression
64 test "armored_key_8192" in armor.test! */
65 #define IOBUF_BUFFER_SIZE 8192
67 /* To avoid a potential DoS with compression packets we better limit
68 the number of filters in a chain. */
69 #define MAX_NESTING_FILTER 64
71 /*-- End configurable part. --*/
74 #ifdef HAVE_W32_SYSTEM
75 # ifdef HAVE_W32CE_SYSTEM
76 # define FD_FOR_STDIN (es_fileno (es_stdin))
77 # define FD_FOR_STDOUT (es_fileno (es_stdout))
79 # define FD_FOR_STDIN (GetStdHandle (STD_INPUT_HANDLE))
80 # define FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
82 #else /*!HAVE_W32_SYSTEM*/
83 # define FD_FOR_STDIN (0)
84 # define FD_FOR_STDOUT (1)
85 #endif /*!HAVE_W32_SYSTEM*/
88 /* The context used by the file filter. */
91 gnupg_fd_t fp; /* Open file pointer or handle. */
95 int print_only_name; /* Flags indicating that fname is not a real file. */
96 char fname[1]; /* Name of the file. */
99 /* The context used by the estream filter. */
102 estream_t fp; /* Open estream handle. */
106 int print_only_name; /* Flags indicating that fname is not a real file. */
107 char fname[1]; /* Name of the file. */
108 } file_es_filter_ctx_t;
111 /* Object to control the "close cache". */
114 struct close_cache_s *next;
118 typedef struct close_cache_s *close_cache_t;
119 static close_cache_t close_cache;
123 #ifdef HAVE_W32_SYSTEM
130 int print_only_name; /* Flag indicating that fname is not a real file. */
131 char fname[1]; /* Name of the file */
134 #endif /*HAVE_W32_SYSTEM*/
136 /* The first partial length header block must be of size 512 to make
137 * it easier (and more efficient) we use a min. block size of 512 for
138 * all chunks (but the last one) */
139 #define OP_MIN_PARTIAL_CHUNK 512
140 #define OP_MIN_PARTIAL_CHUNK_2POW 9
142 /* The context we use for the block filter (used to handle OpenPGP
143 length information header). */
149 int partial; /* 1 = partial header, 2 in last partial packet. */
150 char *buffer; /* Used for partial header. */
151 size_t buflen; /* Used size of buffer. */
152 int first_c; /* First character of a partial header (which is > 0). */
158 /* Local prototypes. */
159 static int underflow (iobuf_t a, int clear_pending_eof);
160 static int underflow_target (iobuf_t a, int clear_pending_eof, size_t target);
161 static int translate_file_handle (int fd, int for_write);
163 /* Sends any pending data to the filter's FILTER function. Note: this
164 works on the filter and not on the whole pipeline. That is,
165 iobuf_flush doesn't necessarily cause data to be written to any
166 underlying file; it just causes any data buffered at the filter A
167 to be sent to A's filter function.
169 If A is a IOBUF_OUTPUT_TEMP filter, then this also enlarges the
170 buffer by IOBUF_BUFFER_SIZE.
172 May only be called on an IOBUF_OUTPUT or IOBUF_OUTPUT_TEMP filters. */
173 static int filter_flush (iobuf_t a);
177 /* This is a replacement for strcmp. Under W32 it does not
178 distinguish between backslash and slash. */
180 fd_cache_strcmp (const char *a, const char *b)
182 #ifdef HAVE_DOSISH_SYSTEM
183 for (; *a && *b; a++, b++)
185 if (*a != *b && !((*a == '/' && *b == '\\')
186 || (*a == '\\' && *b == '/')) )
189 return *(const unsigned char *)a - *(const unsigned char *)b;
191 return strcmp (a, b);
196 * Invalidate (i.e. close) a cached iobuf
199 fd_cache_invalidate (const char *fname)
206 log_debug ("fd_cache_invalidate (%s)\n", fname);
208 for (cc = close_cache; cc; cc = cc->next)
210 if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
213 log_debug (" did (%s)\n", cc->fname);
214 #ifdef HAVE_W32_SYSTEM
215 if (!CloseHandle (cc->fp))
220 cc->fp = GNUPG_INVALID_FD;
227 /* Try to sync changes to the disk. This is to avoid data loss during
228 a system crash in write/close/rename cycle on some file
231 fd_cache_synchronize (const char *fname)
239 log_debug ("fd_cache_synchronize (%s)\n", fname);
241 for (cc=close_cache; cc; cc = cc->next )
243 if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
246 log_debug (" did (%s)\n", cc->fname);
248 err = fsync (cc->fp);
253 #endif /*HAVE_FSYNC*/
260 direct_open (const char *fname, const char *mode, int mode700)
262 #ifdef HAVE_W32_SYSTEM
263 unsigned long da, cd, sm;
267 /* Note, that we do not handle all mode combinations */
269 /* According to the ReactOS source it seems that open() of the
270 * standard MSW32 crt does open the file in shared mode which is
271 * something new for MS applications ;-)
273 if (strchr (mode, '+'))
275 if (fd_cache_invalidate (fname))
276 return GNUPG_INVALID_FD;
277 da = GENERIC_READ | GENERIC_WRITE;
279 sm = FILE_SHARE_READ | FILE_SHARE_WRITE;
281 else if (strchr (mode, 'w'))
283 if (fd_cache_invalidate (fname))
284 return GNUPG_INVALID_FD;
287 sm = FILE_SHARE_WRITE;
293 sm = FILE_SHARE_READ;
296 #ifdef HAVE_W32CE_SYSTEM
298 wchar_t *wfname = utf8_to_wchar (fname);
301 hfile = CreateFile (wfname, da, sm, NULL, cd,
302 FILE_ATTRIBUTE_NORMAL, NULL);
306 hfile = INVALID_HANDLE_VALUE;
309 hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL);
313 #else /*!HAVE_W32_SYSTEM*/
316 int cflag = S_IRUSR | S_IWUSR;
319 cflag |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
321 /* Note, that we do not handle all mode combinations */
322 if (strchr (mode, '+'))
324 if (fd_cache_invalidate (fname))
325 return GNUPG_INVALID_FD;
328 else if (strchr (mode, 'w'))
330 if (fd_cache_invalidate (fname))
331 return GNUPG_INVALID_FD;
332 oflag = O_WRONLY | O_CREAT | O_TRUNC;
339 if (strchr (mode, 'b'))
347 /* Don't allow iobufs on directories */
348 if (!stat (fname, &buf) && S_ISDIR (buf.st_mode) && !S_ISREG (buf.st_mode))
349 return __set_errno (EISDIR);
352 return open (fname, oflag, cflag);
354 #endif /*!HAVE_W32_SYSTEM*/
359 * Instead of closing an FD we keep it open and cache it for later reuse
360 * Note that this caching strategy only works if the process does not chdir.
363 fd_cache_close (const char *fname, gnupg_fd_t fp)
368 if (!fname || !*fname)
370 #ifdef HAVE_W32_SYSTEM
376 log_debug ("fd_cache_close (%d) real\n", (int)fp);
379 /* try to reuse a slot */
380 for (cc = close_cache; cc; cc = cc->next)
382 if (cc->fp == GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
386 log_debug ("fd_cache_close (%s) used existing slot\n", fname);
392 log_debug ("fd_cache_close (%s) new slot created\n", fname);
393 cc = xcalloc (1, sizeof *cc + strlen (fname));
394 strcpy (cc->fname, fname);
396 cc->next = close_cache;
401 * Do a direct_open on FNAME but first try to reuse one from the fd_cache
404 fd_cache_open (const char *fname, const char *mode)
409 for (cc = close_cache; cc; cc = cc->next)
411 if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
413 gnupg_fd_t fp = cc->fp;
414 cc->fp = GNUPG_INVALID_FD;
416 log_debug ("fd_cache_open (%s) using cached fp\n", fname);
417 #ifdef HAVE_W32_SYSTEM
418 if (SetFilePointer (fp, 0, NULL, FILE_BEGIN) == 0xffffffff)
420 log_error ("rewind file failed on handle %p: ec=%d\n",
421 fp, (int) GetLastError ());
422 fp = GNUPG_INVALID_FD;
425 if (lseek (fp, 0, SEEK_SET) == (off_t) - 1)
427 log_error ("can't rewind fd %d: %s\n", fp, strerror (errno));
428 fp = GNUPG_INVALID_FD;
435 log_debug ("fd_cache_open (%s) not cached\n", fname);
436 return direct_open (fname, mode, 0);
441 file_filter (void *opaque, int control, iobuf_t chain, byte * buf,
444 file_filter_ctx_t *a = opaque;
445 gnupg_fd_t f = a->fp;
446 size_t size = *ret_len;
450 (void)chain; /* Not used. */
452 if (control == IOBUFCTRL_UNDERFLOW)
454 assert (size); /* We need a buffer. */
462 #ifdef HAVE_W32_SYSTEM
466 if (!ReadFile (f, buf, size, &nread, NULL))
468 int ec = (int) GetLastError ();
469 if (ec != ERROR_BROKEN_PIPE)
471 rc = gpg_error_from_errno (ec);
472 log_error ("%s: read error: ec=%d\n", a->fname, ec);
492 n = read (f, buf, size);
494 while (n == -1 && errno == EINTR);
499 rc = gpg_error_from_syserror ();
500 log_error ("%s: read error: %s\n",
501 a->fname, strerror (errno));
517 else if (control == IOBUFCTRL_FLUSH)
521 #ifdef HAVE_W32_SYSTEM
528 if (size && !WriteFile (f, p, nbytes, &n, NULL))
530 int ec = (int) GetLastError ();
531 rc = gpg_error_from_errno (ec);
532 log_error ("%s: write error: ec=%d\n", a->fname, ec);
549 n = write (f, p, nbytes);
551 while (n == -1 && errno == EINTR);
558 while (n != -1 && nbytes);
561 rc = gpg_error_from_syserror ();
562 log_error ("%s: write error: %s\n", a->fname, strerror (errno));
569 else if (control == IOBUFCTRL_INIT)
575 else if (control == IOBUFCTRL_DESC)
577 mem2str (buf, "file_filter(fd)", *ret_len);
579 else if (control == IOBUFCTRL_FREE)
581 if (f != FD_FOR_STDIN && f != FD_FOR_STDOUT)
584 log_debug ("%s: close fd/handle %d\n", a->fname, FD2INT (f));
586 fd_cache_close (a->no_cache ? NULL : a->fname, f);
588 xfree (a); /* We can free our context now. */
595 /* Similar to file_filter but using the estream system. */
597 file_es_filter (void *opaque, int control, iobuf_t chain, byte * buf,
600 file_es_filter_ctx_t *a = opaque;
602 size_t size = *ret_len;
606 (void)chain; /* Not used. */
608 if (control == IOBUFCTRL_UNDERFLOW)
610 assert (size); /* We need a buffer. */
619 rc = es_read (f, buf, size, &nbytes);
622 rc = gpg_error_from_syserror ();
623 log_error ("%s: read error: %s\n", a->fname, strerror (errno));
633 else if (control == IOBUFCTRL_FLUSH)
644 if (es_write (f, p, nbytes, &nwritten))
646 rc = gpg_error_from_syserror ();
647 log_error ("%s: write error: %s\n",
648 a->fname, strerror (errno));
659 else if (control == IOBUFCTRL_INIT)
664 else if (control == IOBUFCTRL_DESC)
666 mem2str (buf, "estream_filter", *ret_len);
668 else if (control == IOBUFCTRL_FREE)
670 if (f != es_stdin && f != es_stdout)
673 log_debug ("%s: es_fclose %p\n", a->fname, f);
678 xfree (a); /* We can free our context now. */
685 #ifdef HAVE_W32_SYSTEM
686 /* Because network sockets are special objects under Lose32 we have to
687 use a dedicated filter for them. */
689 sock_filter (void *opaque, int control, iobuf_t chain, byte * buf,
692 sock_filter_ctx_t *a = opaque;
693 size_t size = *ret_len;
699 if (control == IOBUFCTRL_UNDERFLOW)
701 assert (size); /* need a buffer */
711 nread = recv (a->sock, buf, size, 0);
712 if (nread == SOCKET_ERROR)
714 int ec = (int) WSAGetLastError ();
715 rc = gpg_error_from_errno (ec);
716 log_error ("socket read error: ec=%d\n", ec);
730 else if (control == IOBUFCTRL_FLUSH)
740 n = send (a->sock, p, nbytes, 0);
741 if (n == SOCKET_ERROR)
743 int ec = (int) WSAGetLastError ();
744 rc = gpg_error_from_errno (ec);
745 log_error ("socket write error: ec=%d\n", ec);
756 else if (control == IOBUFCTRL_INIT)
762 else if (control == IOBUFCTRL_DESC)
764 mem2str (buf, "sock_filter", *ret_len);
766 else if (control == IOBUFCTRL_FREE)
769 closesocket (a->sock);
770 xfree (a); /* we can free our context now */
774 #endif /*HAVE_W32_SYSTEM*/
777 * This is used to implement the block write mode.
778 * Block reading is done on a byte by byte basis in readbyte(),
782 block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
785 block_filter_ctx_t *a = opaque;
786 char *buf = (char *)buffer;
787 size_t size = *ret_len;
788 int c, needed, rc = 0;
791 if (control == IOBUFCTRL_UNDERFLOW)
796 assert (size); /* need a buffer */
797 if (a->eof) /* don't read any further */
802 { /* get the length bytes */
812 /* These OpenPGP introduced huffman like encoded length
813 * bytes are really a mess :-( */
819 else if ((c = iobuf_get (chain)) == -1)
821 log_error ("block_filter: 1st length byte missing\n");
822 rc = GPG_ERR_BAD_DATA;
839 a->size = (c - 192) * 256;
840 if ((c = iobuf_get (chain)) == -1)
843 ("block_filter: 2nd length byte missing\n");
844 rc = GPG_ERR_BAD_DATA;
859 a->size = (size_t)iobuf_get (chain) << 24;
860 a->size |= iobuf_get (chain) << 16;
861 a->size |= iobuf_get (chain) << 8;
862 if ((c = iobuf_get (chain)) == -1)
864 log_error ("block_filter: invalid 4 byte length\n");
865 rc = GPG_ERR_BAD_DATA;
879 { /* Next partial body length. */
880 a->size = 1 << (c & 0x1f);
882 /* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
888 while (!rc && size && a->size)
890 needed = size < a->size ? size : a->size;
891 c = iobuf_read (chain, p, needed);
897 ("block_filter %p: read error (size=%lu,a->size=%lu)\n",
898 a, (ulong) size + c, (ulong) a->size + c);
899 rc = GPG_ERR_BAD_DATA;
912 else if (control == IOBUFCTRL_FLUSH)
915 { /* the complicated openpgp scheme */
916 size_t blen, n, nbytes = size + a->buflen;
918 assert (a->buflen <= OP_MIN_PARTIAL_CHUNK);
919 if (nbytes < OP_MIN_PARTIAL_CHUNK)
921 /* not enough to write a partial block out; so we store it */
923 a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
924 memcpy (a->buffer + a->buflen, buf, size);
928 { /* okay, we can write out something */
929 /* do this in a loop to use the most efficient block lengths */
933 /* find the best matching block length - this is limited
934 * by the size of the internal buffering */
935 for (blen = OP_MIN_PARTIAL_CHUNK * 2,
936 c = OP_MIN_PARTIAL_CHUNK_2POW + 1; blen <= nbytes;
941 /* write the partial length header */
942 assert (c <= 0x1f); /*;-) */
944 iobuf_put (chain, c);
946 { /* write stuff from the buffer */
947 assert (n == OP_MIN_PARTIAL_CHUNK);
948 if (iobuf_write (chain, a->buffer, n))
949 rc = gpg_error_from_syserror ();
953 if ((n = nbytes) > blen)
955 if (n && iobuf_write (chain, p, n))
956 rc = gpg_error_from_syserror ();
960 while (!rc && nbytes >= OP_MIN_PARTIAL_CHUNK);
961 /* store the rest in the buffer */
965 assert (nbytes < OP_MIN_PARTIAL_CHUNK);
967 a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
968 memcpy (a->buffer, p, nbytes);
976 else if (control == IOBUFCTRL_INIT)
979 log_debug ("init block_filter %p\n", a);
982 else if (a->use == IOBUF_INPUT)
983 a->count = a->size = 0;
985 a->count = a->size; /* force first length bytes */
990 else if (control == IOBUFCTRL_DESC)
992 mem2str (buf, "block_filter", *ret_len);
994 else if (control == IOBUFCTRL_FREE)
996 if (a->use == IOBUF_OUTPUT)
997 { /* write the end markers */
1001 /* write out the remaining bytes without a partial header
1002 * the length of this header may be 0 - but if it is
1003 * the first block we are not allowed to use a partial header
1004 * and frankly we can't do so, because this length must be
1005 * a power of 2. This is _really_ complicated because we
1006 * have to check the possible length of a packet prior
1007 * to it's creation: a chain of filters becomes complicated
1008 * and we need a lot of code to handle compressed packets etc.
1011 /* construct header */
1013 /*log_debug("partial: remaining length=%u\n", len ); */
1015 rc = iobuf_put (chain, len);
1016 else if (len < 8384)
1018 if (!(rc = iobuf_put (chain, ((len - 192) / 256) + 192)))
1019 rc = iobuf_put (chain, ((len - 192) % 256));
1022 { /* use a 4 byte header */
1023 if (!(rc = iobuf_put (chain, 0xff)))
1024 if (!(rc = iobuf_put (chain, (len >> 24) & 0xff)))
1025 if (!(rc = iobuf_put (chain, (len >> 16) & 0xff)))
1026 if (!(rc = iobuf_put (chain, (len >> 8) & 0xff)))
1027 rc = iobuf_put (chain, len & 0xff);
1030 rc = iobuf_write (chain, a->buffer, len);
1033 log_error ("block_filter: write error: %s\n",
1035 rc = gpg_error_from_syserror ();
1046 log_error ("block_filter: pending bytes!\n");
1049 log_debug ("free block_filter %p\n", a);
1050 xfree (a); /* we can free our context now */
1056 #define MAX_IOBUF_DESC 32
1058 * Fill the buffer by the description of iobuf A.
1059 * The buffer size should be MAX_IOBUF_DESC (or larger).
1060 * Returns BUF as (const char *).
1063 iobuf_desc (iobuf_t a, byte *buf)
1065 size_t len = MAX_IOBUF_DESC;
1067 if (! a || ! a->filter)
1068 memcpy (buf, "?", 2);
1070 a->filter (a->filter_ov, IOBUFCTRL_DESC, NULL, buf, &len);
1076 print_chain (iobuf_t a)
1080 for (; a; a = a->chain)
1082 byte desc[MAX_IOBUF_DESC];
1084 log_debug ("iobuf chain: %d.%d '%s' filter_eof=%d start=%d len=%d\n",
1085 a->no, a->subno, iobuf_desc (a, desc), a->filter_eof,
1086 (int) a->d.start, (int) a->d.len);
1091 iobuf_print_chain (iobuf_t a)
1098 iobuf_alloc (int use, size_t bufsize)
1101 static int number = 0;
1103 assert (use == IOBUF_INPUT || use == IOBUF_INPUT_TEMP
1104 || use == IOBUF_OUTPUT || use == IOBUF_OUTPUT_TEMP);
1107 log_bug ("iobuf_alloc() passed a bufsize of 0!\n");
1108 bufsize = IOBUF_BUFFER_SIZE;
1111 a = xcalloc (1, sizeof *a);
1113 a->d.buf = xmalloc (bufsize);
1114 a->d.size = bufsize;
1117 a->real_fname = NULL;
1122 iobuf_close (iobuf_t a)
1125 size_t dummy_len = 0;
1128 for (; a; a = a_chain)
1130 byte desc[MAX_IOBUF_DESC];
1135 if (a->use == IOBUF_OUTPUT && (rc = filter_flush (a)))
1136 log_error ("filter_flush failed on close: %s\n", gpg_strerror (rc));
1139 log_debug ("iobuf-%d.%d: close '%s'\n",
1140 a->no, a->subno, iobuf_desc (a, desc));
1142 if (a->filter && (rc2 = a->filter (a->filter_ov, IOBUFCTRL_FREE,
1143 a->chain, NULL, &dummy_len)))
1144 log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
1146 /* Whoops! An error occurred. Save it in RC if we haven't
1147 already recorded an error. */
1150 xfree (a->real_fname);
1153 memset (a->d.buf, 0, a->d.size); /* erase the buffer */
1162 iobuf_cancel (iobuf_t a)
1167 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1168 char *remove_name = NULL;
1171 if (a && a->use == IOBUF_OUTPUT)
1173 s = iobuf_get_real_fname (a);
1176 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1177 remove_name = xstrdup (s);
1184 /* send a cancel message to all filters */
1185 for (a2 = a; a2; a2 = a2->chain)
1189 a2->filter (a2->filter_ov, IOBUFCTRL_CANCEL, a2->chain, NULL, &dummy);
1192 rc = iobuf_close (a);
1193 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1196 /* Argg, MSDOS does not allow removing open files. So
1197 * we have to do it here */
1198 #ifdef HAVE_W32CE_SYSTEM
1199 wchar_t *wtmp = utf8_to_wchar (remove_name);
1204 remove (remove_name);
1206 xfree (remove_name);
1216 return iobuf_alloc (IOBUF_OUTPUT_TEMP, IOBUF_BUFFER_SIZE);
1220 iobuf_temp_with_content (const char *buffer, size_t length)
1225 a = iobuf_alloc (IOBUF_INPUT_TEMP, length);
1226 assert (length == a->d.size);
1227 /* memcpy (a->d.buf, buffer, length); */
1228 for (i=0; i < length; i++)
1229 a->d.buf[i] = buffer[i];
1237 iobuf_is_pipe_filename (const char *fname)
1239 if (!fname || (*fname=='-' && !fname[1]) )
1241 return check_special_filename (fname, 0, 1) != -1;
1246 do_open (const char *fname, int special_filenames,
1247 int use, const char *opentype, int mode700)
1251 file_filter_ctx_t *fcx;
1255 byte desc[MAX_IOBUF_DESC];
1257 assert (use == IOBUF_INPUT || use == IOBUF_OUTPUT);
1259 if (special_filenames
1261 && (!fname || (*fname == '-' && !fname[1])))
1263 if (use == IOBUF_INPUT)
1277 else if (special_filenames
1278 && (fd = check_special_filename (fname, 0, 1)) != -1)
1279 return iobuf_fdopen (translate_file_handle (fd, use == IOBUF_INPUT ? 0 : 1),
1283 if (use == IOBUF_INPUT)
1284 fp = fd_cache_open (fname, opentype);
1286 fp = direct_open (fname, opentype, mode700);
1287 if (fp == GNUPG_INVALID_FD)
1291 a = iobuf_alloc (use, IOBUF_BUFFER_SIZE);
1292 fcx = xmalloc (sizeof *fcx + strlen (fname));
1294 fcx->print_only_name = print_only;
1295 strcpy (fcx->fname, fname);
1297 a->real_fname = xstrdup (fname);
1298 a->filter = file_filter;
1300 file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1302 log_debug ("iobuf-%d.%d: open '%s' desc=%s fd=%d\n",
1303 a->no, a->subno, fname, iobuf_desc (a, desc), FD2INT (fcx->fp));
1309 iobuf_open (const char *fname)
1311 return do_open (fname, 1, IOBUF_INPUT, "rb", 0);
1315 iobuf_create (const char *fname, int mode700)
1317 return do_open (fname, 1, IOBUF_OUTPUT, "wb", mode700);
1321 iobuf_openrw (const char *fname)
1323 return do_open (fname, 0, IOBUF_OUTPUT, "r+b", 0);
1328 do_iobuf_fdopen (int fd, const char *mode, int keep_open)
1332 file_filter_ctx_t *fcx;
1337 a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT,
1339 fcx = xmalloc (sizeof *fcx + 20);
1341 fcx->print_only_name = 1;
1342 fcx->keep_open = keep_open;
1343 sprintf (fcx->fname, "[fd %d]", fd);
1344 a->filter = file_filter;
1346 file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1348 log_debug ("iobuf-%d.%d: fdopen%s '%s'\n",
1349 a->no, a->subno, keep_open? "_nc":"", fcx->fname);
1350 iobuf_ioctl (a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
1356 iobuf_fdopen (int fd, const char *mode)
1358 return do_iobuf_fdopen (fd, mode, 0);
1362 iobuf_fdopen_nc (int fd, const char *mode)
1364 return do_iobuf_fdopen (fd, mode, 1);
1369 iobuf_esopen (estream_t estream, const char *mode, int keep_open)
1372 file_es_filter_ctx_t *fcx;
1375 a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT,
1377 fcx = xtrymalloc (sizeof *fcx + 30);
1379 fcx->print_only_name = 1;
1380 fcx->keep_open = keep_open;
1381 sprintf (fcx->fname, "[fd %p]", estream);
1382 a->filter = file_es_filter;
1384 file_es_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1386 log_debug ("iobuf-%d.%d: esopen%s '%s'\n",
1387 a->no, a->subno, keep_open? "_nc":"", fcx->fname);
1393 iobuf_sockopen (int fd, const char *mode)
1396 #ifdef HAVE_W32_SYSTEM
1397 sock_filter_ctx_t *scx;
1400 a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT,
1402 scx = xmalloc (sizeof *scx + 25);
1404 scx->print_only_name = 1;
1405 sprintf (scx->fname, "[sock %d]", fd);
1406 a->filter = sock_filter;
1408 sock_filter (scx, IOBUFCTRL_INIT, NULL, NULL, &len);
1410 log_debug ("iobuf-%d.%d: sockopen '%s'\n", a->no, a->subno, scx->fname);
1411 iobuf_ioctl (a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
1413 a = iobuf_fdopen (fd, mode);
1419 iobuf_ioctl (iobuf_t a, iobuf_ioctl_t cmd, int intval, void *ptrval)
1421 byte desc[MAX_IOBUF_DESC];
1423 if (cmd == IOBUF_IOCTL_KEEP_OPEN)
1425 /* Keep system filepointer/descriptor open. This was used in
1426 the past by http.c; this ioctl is not directly used
1429 log_debug ("iobuf-%d.%d: ioctl '%s' keep_open=%d\n",
1430 a ? a->no : -1, a ? a->subno : -1, iobuf_desc (a, desc),
1432 for (; a; a = a->chain)
1433 if (!a->chain && a->filter == file_filter)
1435 file_filter_ctx_t *b = a->filter_ov;
1436 b->keep_open = intval;
1439 #ifdef HAVE_W32_SYSTEM
1440 else if (!a->chain && a->filter == sock_filter)
1442 sock_filter_ctx_t *b = a->filter_ov;
1443 b->keep_open = intval;
1448 else if (cmd == IOBUF_IOCTL_INVALIDATE_CACHE)
1451 log_debug ("iobuf-*.*: ioctl '%s' invalidate\n",
1452 ptrval ? (char *) ptrval : "?");
1453 if (!a && !intval && ptrval)
1455 if (fd_cache_invalidate (ptrval))
1460 else if (cmd == IOBUF_IOCTL_NO_CACHE)
1463 log_debug ("iobuf-%d.%d: ioctl '%s' no_cache=%d\n",
1464 a ? a->no : -1, a ? a->subno : -1, iobuf_desc (a, desc),
1466 for (; a; a = a->chain)
1467 if (!a->chain && a->filter == file_filter)
1469 file_filter_ctx_t *b = a->filter_ov;
1470 b->no_cache = intval;
1473 #ifdef HAVE_W32_SYSTEM
1474 else if (!a->chain && a->filter == sock_filter)
1476 sock_filter_ctx_t *b = a->filter_ov;
1477 b->no_cache = intval;
1482 else if (cmd == IOBUF_IOCTL_FSYNC)
1484 /* Do a fsync on the open fd and return any errors to the caller
1485 of iobuf_ioctl. Note that we work on a file name here. */
1487 log_debug ("iobuf-*.*: ioctl '%s' fsync\n",
1488 ptrval? (const char*)ptrval:"<null>");
1490 if (!a && !intval && ptrval)
1492 return fd_cache_synchronize (ptrval);
1502 * Register an i/o filter.
1505 iobuf_push_filter (iobuf_t a,
1506 int (*f) (void *opaque, int control,
1507 iobuf_t chain, byte * buf, size_t * len),
1510 return iobuf_push_filter2 (a, f, ov, 0);
1514 iobuf_push_filter2 (iobuf_t a,
1515 int (*f) (void *opaque, int control,
1516 iobuf_t chain, byte * buf, size_t * len),
1517 void *ov, int rel_ov)
1520 size_t dummy_len = 0;
1523 if (a->use == IOBUF_OUTPUT && (rc = filter_flush (a)))
1526 if (a->subno >= MAX_NESTING_FILTER)
1528 log_error ("i/o filter too deeply nested - corrupted data?\n");
1529 return GPG_ERR_BAD_DATA;
1532 /* We want to create a new filter and put it in front of A. A
1533 simple implementation would do:
1535 b = iobuf_alloc (...);
1539 This is a bit problematic: A is the head of the pipeline and
1540 there are potentially many pointers to it. Requiring the caller
1541 to update all of these pointers is a burden.
1543 An alternative implementation would add a level of indirection.
1544 For instance, we could use a pipeline object, which contains a
1545 pointer to the first filter in the pipeline. This is not what we
1548 Instead, we allocate a new buffer (B) and copy the first filter's
1549 state into that and use the initial buffer (A) for the new
1550 filter. One limitation of this approach is that it is not
1551 practical to maintain a pointer to a specific filter's state.
1558 +----------+ +----------+
1559 | filter x |--------->| filter y |---->....
1560 +----------+ +----------+
1569 +----------+ +----------+
1570 | filter w | | filter y |---->....
1571 +----------+ +----------+
1573 Note: filter x's address changed from 0x100 to 0x300, but A still
1574 points to the head of the pipeline.
1577 b = xmalloc (sizeof *b);
1578 memcpy (b, a, sizeof *b);
1579 /* fixme: it is stupid to keep a copy of the name at every level
1580 * but we need the name somewhere because the name known by file_filter
1581 * may have been released when we need the name of the file */
1582 b->real_fname = a->real_fname ? xstrdup (a->real_fname) : NULL;
1583 /* remove the filter stuff from the new stream */
1585 a->filter_ov = NULL;
1586 a->filter_ov_owner = 0;
1588 if (a->use == IOBUF_OUTPUT_TEMP)
1589 /* A TEMP filter buffers any data sent to it; it does not forward
1590 any data down the pipeline. If we add a new filter to the
1591 pipeline, it shouldn't also buffer data. It should send it
1592 downstream to be buffered. Thus, the correct type for a filter
1593 added in front of an IOBUF_OUTPUT_TEMP filter is IOBUF_OUPUT, not
1594 IOBUF_OUTPUT_TEMP. */
1596 a->use = IOBUF_OUTPUT;
1598 /* When pipeline is written to, the temp buffer's size is
1599 increased accordingly. We don't need to allocate a 10 MB
1600 buffer for a non-terminal filter. Just use the default
1602 a->d.size = IOBUF_BUFFER_SIZE;
1604 else if (a->use == IOBUF_INPUT_TEMP)
1605 /* Same idea as above. */
1607 a->use = IOBUF_INPUT;
1608 a->d.size = IOBUF_BUFFER_SIZE;
1611 /* The new filter (A) gets a new buffer.
1613 If the pipeline is an output or temp pipeline, then giving the
1614 buffer to the new filter means that data that was written before
1615 the filter was pushed gets sent to the filter. That's clearly
1618 If the pipeline is an input pipeline, then giving the buffer to
1619 the new filter (A) means that data that has read from (B), but
1620 not yet read from the pipeline won't be processed by the new
1621 filter (A)! That's certainly not what we want. */
1622 a->d.buf = xmalloc (a->d.size);
1626 /* disable nlimit for the new stream */
1627 a->ntotal = b->ntotal + b->nbytes;
1628 a->nlimit = a->nbytes = 0;
1630 /* make a link from the new stream to the original stream */
1633 /* setup the function on the new stream */
1636 a->filter_ov_owner = rel_ov;
1638 a->subno = b->subno + 1;
1642 byte desc[MAX_IOBUF_DESC];
1643 log_debug ("iobuf-%d.%d: push '%s'\n",
1644 a->no, a->subno, iobuf_desc (a, desc));
1648 /* now we can initialize the new function if we have one */
1649 if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_INIT, a->chain,
1651 log_error ("IOBUFCTRL_INIT failed: %s\n", gpg_strerror (rc));
1656 * Remove an i/o filter.
1659 iobuf_pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
1660 iobuf_t chain, byte * buf, size_t * len),
1664 size_t dummy_len = 0;
1666 byte desc[MAX_IOBUF_DESC];
1669 log_debug ("iobuf-%d.%d: pop '%s'\n",
1670 a->no, a->subno, iobuf_desc (a, desc));
1671 if (a->use == IOBUF_INPUT_TEMP || a->use == IOBUF_OUTPUT_TEMP)
1673 /* This should be the last filter in the pipeline. */
1674 assert (! a->chain);
1678 { /* this is simple */
1682 xfree (a->real_fname);
1683 memcpy (a, b, sizeof *a);
1687 for (b = a; b; b = b->chain)
1688 if (b->filter == f && (!ov || b->filter_ov == ov))
1691 log_bug ("iobuf_pop_filter(): filter function not found\n");
1693 /* flush this stream if it is an output stream */
1694 if (a->use == IOBUF_OUTPUT && (rc = filter_flush (b)))
1696 log_error ("filter_flush failed in iobuf_pop_filter: %s\n",
1700 /* and tell the filter to free it self */
1701 if (b->filter && (rc = b->filter (b->filter_ov, IOBUFCTRL_FREE, b->chain,
1704 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1707 if (b->filter_ov && b->filter_ov_owner)
1709 xfree (b->filter_ov);
1710 b->filter_ov = NULL;
1714 /* and see how to remove it */
1715 if (a == b && !b->chain)
1716 log_bug ("can't remove the last filter from the chain\n");
1718 { /* remove the first iobuf from the chain */
1719 /* everything from b is copied to a. This is save because
1720 * a flush has been done on the to be removed entry
1724 xfree (a->real_fname);
1725 memcpy (a, b, sizeof *a);
1728 log_debug ("iobuf-%d.%d: popped filter\n", a->no, a->subno);
1731 { /* remove the last iobuf from the chain */
1732 log_bug ("Ohh jeee, trying to remove a head filter\n");
1735 { /* remove an intermediate iobuf from the chain */
1736 log_bug ("Ohh jeee, trying to remove an intermediate filter\n");
1744 * read underflow: read at least one byte into the buffer and return
1745 * the first byte or -1 on EOF.
1748 underflow (iobuf_t a, int clear_pending_eof)
1750 return underflow_target (a, clear_pending_eof, 1);
1755 * read underflow: read TARGET bytes into the buffer and return
1756 * the first byte or -1 on EOF.
1759 underflow_target (iobuf_t a, int clear_pending_eof, size_t target)
1765 log_debug ("iobuf-%d.%d: underflow: buffer size: %d; still buffered: %d => space for %d bytes\n",
1767 (int) a->d.size, (int) (a->d.len - a->d.start),
1768 (int) (a->d.size - (a->d.len - a->d.start)));
1770 if (a->use == IOBUF_INPUT_TEMP)
1771 /* By definition, there isn't more data to read into the
1775 assert (a->use == IOBUF_INPUT);
1777 /* If there is still some buffered data, then move it to the start
1778 of the buffer and try to fill the end of the buffer. (This is
1779 useful if we are called from iobuf_peek().) */
1780 assert (a->d.start <= a->d.len);
1781 a->d.len -= a->d.start;
1782 memmove (a->d.buf, &a->d.buf[a->d.start], a->d.len);
1785 if (a->d.len < target && a->filter_eof)
1786 /* The last time we tried to read from this filter, we got an EOF.
1787 We couldn't return the EOF, because there was buffered data.
1788 Since there is no longer any buffered data, return the
1792 log_debug ("iobuf-%d.%d: underflow: eof (pending eof)\n",
1794 if (! clear_pending_eof)
1798 /* A filter follows this one. Free this filter. */
1800 iobuf_t b = a->chain;
1802 log_debug ("iobuf-%d.%d: filter popped (pending EOF returned)\n",
1805 xfree (a->real_fname);
1806 memcpy (a, b, sizeof *a);
1811 a->filter_eof = 0; /* for the top level filter */
1812 return -1; /* return one(!) EOF */
1815 if (a->d.len == 0 && a->error)
1816 /* The last time we tried to read from this filter, we got an
1817 error. We couldn't return the error, because there was
1818 buffered data. Since there is no longer any buffered data,
1819 return the error. */
1822 log_debug ("iobuf-%d.%d: pending error (%s) returned\n",
1823 a->no, a->subno, gpg_strerror (a->error));
1827 if (a->filter && ! a->filter_eof && ! a->error)
1828 /* We have a filter function and the last time we tried to read we
1829 didn't get an EOF or an error. Try to fill the buffer. */
1831 /* Be careful to account for any buffered data. */
1832 len = a->d.size - a->d.len;
1834 log_debug ("iobuf-%d.%d: underflow: A->FILTER (%lu bytes)\n",
1835 a->no, a->subno, (ulong) len);
1837 /* There is no space for more data. Don't bother calling
1841 rc = a->filter (a->filter_ov, IOBUFCTRL_UNDERFLOW, a->chain,
1842 &a->d.buf[a->d.len], &len);
1846 log_debug ("iobuf-%d.%d: A->FILTER() returned rc=%d (%s), read %lu bytes\n",
1848 rc, rc == 0 ? "ok" : rc == -1 ? "EOF" : gpg_strerror (rc),
1850 /* if( a->no == 1 ) */
1851 /* log_hexdump (" data:", a->d.buf, len); */
1856 size_t dummy_len = 0;
1858 /* Tell the filter to free itself */
1859 if ((rc = a->filter (a->filter_ov, IOBUFCTRL_FREE, a->chain,
1861 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1863 /* Free everything except for the internal buffer. */
1864 if (a->filter_ov && a->filter_ov_owner)
1865 xfree (a->filter_ov);
1866 a->filter_ov = NULL;
1870 if (clear_pending_eof && a->d.len == 0 && a->chain)
1871 /* We don't need to keep this filter around at all:
1874 - we have no buffered data
1875 - a filter follows this one.
1877 Unlink this filter. */
1879 iobuf_t b = a->chain;
1881 log_debug ("iobuf-%d.%d: pop in underflow (nothing buffered, got EOF)\n",
1884 xfree (a->real_fname);
1885 memcpy (a, b, sizeof *a);
1892 else if (a->d.len == 0)
1893 /* We can't unlink this filter (it is the only one in the
1894 pipeline), but we can immediately return EOF. */
1898 /* Record the error. */
1903 /* There is no buffered data. Immediately return EOF. */
1908 assert (a->d.start <= a->d.len);
1909 if (a->d.start < a->d.len)
1910 return a->d.buf[a->d.start++];
1918 filter_flush (iobuf_t a)
1923 if (a->use == IOBUF_OUTPUT_TEMP)
1924 { /* increase the temp buffer */
1925 size_t newsize = a->d.size + IOBUF_BUFFER_SIZE;
1928 log_debug ("increasing temp iobuf from %lu to %lu\n",
1929 (ulong) a->d.size, (ulong) newsize);
1931 a->d.buf = xrealloc (a->d.buf, newsize);
1932 a->d.size = newsize;
1935 else if (a->use != IOBUF_OUTPUT)
1936 log_bug ("flush on non-output iobuf\n");
1937 else if (!a->filter)
1938 log_bug ("filter_flush: no filter\n");
1940 rc = a->filter (a->filter_ov, IOBUFCTRL_FLUSH, a->chain, a->d.buf, &len);
1941 if (!rc && len != a->d.len)
1943 log_info ("filter_flush did not write all!\n");
1944 rc = GPG_ERR_INTERNAL;
1955 iobuf_readbyte (iobuf_t a)
1959 if (a->use == IOBUF_OUTPUT || a->use == IOBUF_OUTPUT_TEMP)
1961 log_bug ("iobuf_readbyte called on a non-INPUT pipeline!\n");
1965 assert (a->d.start <= a->d.len);
1967 if (a->nlimit && a->nbytes >= a->nlimit)
1968 return -1; /* forced EOF */
1970 if (a->d.start < a->d.len)
1972 c = a->d.buf[a->d.start++];
1974 else if ((c = underflow (a, 1)) == -1)
1975 return -1; /* EOF */
1977 assert (a->d.start <= a->d.len);
1979 /* Note: if underflow doesn't return EOF, then it returns the first
1980 byte that was read and advances a->d.start appropriately. */
1988 iobuf_read (iobuf_t a, void *buffer, unsigned int buflen)
1990 unsigned char *buf = (unsigned char *)buffer;
1993 if (a->use == IOBUF_OUTPUT || a->use == IOBUF_OUTPUT_TEMP)
1995 log_bug ("iobuf_read called on a non-INPUT pipeline!\n");
2001 /* Handle special cases. */
2002 for (n = 0; n < buflen; n++)
2004 if ((c = iobuf_readbyte (a)) == -1)
2007 return -1; /* eof */
2023 if (n < buflen && a->d.start < a->d.len)
2024 /* Drain the buffer. */
2026 unsigned size = a->d.len - a->d.start;
2027 if (size > buflen - n)
2030 memcpy (buf, a->d.buf + a->d.start, size);
2037 /* Draining the internal buffer didn't fill BUFFER. Call
2038 underflow to read more data into the filter's internal
2041 if ((c = underflow (a, 1)) == -1)
2042 /* EOF. If we managed to read something, don't return EOF
2046 return n ? n : -1 /*EOF*/;
2061 iobuf_peek (iobuf_t a, byte * buf, unsigned buflen)
2065 assert (buflen > 0);
2066 assert (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP);
2068 if (buflen > a->d.size)
2069 /* We can't peek more than we can buffer. */
2072 /* Try to fill the internal buffer with enough data to satisfy the
2074 while (buflen > a->d.len - a->d.start)
2076 if (underflow_target (a, 0, buflen) == -1)
2077 /* EOF. We can't read any more. */
2080 /* Underflow consumes the first character (it's the return
2081 value). unget() it by resetting the "file position". */
2082 assert (a->d.start == 1);
2086 n = a->d.len - a->d.start;
2094 memcpy (buf, &a->d.buf[a->d.start], n);
2103 iobuf_writebyte (iobuf_t a, unsigned int c)
2107 if (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP)
2109 log_bug ("iobuf_writebyte called on an input pipeline!\n");
2113 if (a->d.len == a->d.size)
2114 if ((rc=filter_flush (a)))
2117 assert (a->d.len < a->d.size);
2118 a->d.buf[a->d.len++] = c;
2124 iobuf_write (iobuf_t a, const void *buffer, unsigned int buflen)
2126 const unsigned char *buf = (const unsigned char *)buffer;
2129 if (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP)
2131 log_bug ("iobuf_write called on an input pipeline!\n");
2137 if (buflen && a->d.len < a->d.size)
2139 unsigned size = a->d.size - a->d.len;
2142 memcpy (a->d.buf + a->d.len, buf, size);
2149 rc = filter_flush (a);
2160 iobuf_writestr (iobuf_t a, const char *buf)
2162 if (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP)
2164 log_bug ("iobuf_writestr called on an input pipeline!\n");
2168 return iobuf_write (a, buf, strlen (buf));
2174 iobuf_write_temp (iobuf_t dest, iobuf_t source)
2176 assert (source->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP);
2177 assert (dest->use == IOBUF_OUTPUT || dest->use == IOBUF_OUTPUT_TEMP);
2179 iobuf_flush_temp (source);
2180 return iobuf_write (dest, source->d.buf, source->d.len);
2184 iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen)
2186 byte desc[MAX_IOBUF_DESC];
2191 int rc = filter_flush (a);
2193 log_bug ("Flushing iobuf %d.%d (%s) from iobuf_temp_to_buffer failed. Ignoring.\n",
2194 a->no, a->subno, iobuf_desc (a, desc));
2203 memcpy (buffer, a->d.buf, n);
2207 /* Copies the data from the input iobuf SOURCE to the output iobuf
2208 DEST until either an error is encountered or EOF is reached.
2209 Returns the number of bytes copies. */
2211 iobuf_copy (iobuf_t dest, iobuf_t source)
2214 /* Use a 32 KB buffer. */
2215 const size_t temp_size = 32 * 1024;
2221 assert (source->use == IOBUF_INPUT || source->use == IOBUF_INPUT_TEMP);
2222 assert (dest->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP);
2224 if (iobuf_error (dest))
2227 temp = xmalloc (temp_size);
2230 nread = iobuf_read (source, temp, temp_size);
2235 err = iobuf_write (dest, temp, nread);
2241 /* Burn the buffer. */
2242 wipememory (temp, sizeof (temp));
2250 iobuf_flush_temp (iobuf_t temp)
2252 if (temp->use == IOBUF_INPUT || temp->use == IOBUF_INPUT_TEMP)
2253 log_bug ("iobuf_flush_temp called on an input pipeline!\n");
2255 iobuf_pop_filter (temp, temp->filter, NULL);
2260 iobuf_set_limit (iobuf_t a, off_t nlimit)
2267 a->ntotal += a->nbytes;
2274 iobuf_get_filelength (iobuf_t a, int *overflow)
2279 /* Hmmm: file_filter may have already been removed */
2280 for ( ; a->chain; a = a->chain )
2283 if (a->filter != file_filter)
2287 file_filter_ctx_t *b = a->filter_ov;
2288 gnupg_fd_t fp = b->fp;
2290 #if defined(HAVE_W32_SYSTEM)
2292 static int (* __stdcall get_file_size_ex) (void *handle,
2293 LARGE_INTEGER *r_size);
2294 static int get_file_size_ex_initialized;
2296 if (!get_file_size_ex_initialized)
2300 handle = dlopen ("kernel32.dll", RTLD_LAZY);
2303 get_file_size_ex = dlsym (handle, "GetFileSizeEx");
2304 if (!get_file_size_ex)
2307 get_file_size_ex_initialized = 1;
2310 if (get_file_size_ex)
2312 /* This is a newer system with GetFileSizeEx; we use this
2313 then because it seem that GetFileSize won't return a
2314 proper error in case a file is larger than 4GB. */
2315 LARGE_INTEGER exsize;
2317 if (get_file_size_ex (fp, &exsize))
2319 if (!exsize.u.HighPart)
2320 return exsize.u.LowPart;
2328 if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
2331 log_error ("GetFileSize for handle %p failed: %s\n",
2332 fp, w32_strerror (0));
2333 #else /*!HAVE_W32_SYSTEM*/
2337 if ( !fstat (FD2INT (fp), &st) )
2339 log_error("fstat() failed: %s\n", strerror(errno) );
2341 #endif /*!HAVE_W32_SYSTEM*/
2349 iobuf_get_fd (iobuf_t a)
2351 for (; a->chain; a = a->chain)
2354 if (a->filter != file_filter)
2358 file_filter_ctx_t *b = a->filter_ov;
2359 gnupg_fd_t fp = b->fp;
2367 iobuf_tell (iobuf_t a)
2369 return a->ntotal + a->nbytes;
2373 #if !defined(HAVE_FSEEKO) && !defined(fseeko)
2375 #ifdef HAVE_LIMITS_H
2376 # include <limits.h>
2379 # define LONG_MAX ((long) ((unsigned long) -1 >> 1))
2382 # define LONG_MIN (-1 - LONG_MAX)
2386 * A substitute for fseeko, for hosts that don't have it.
2389 fseeko (FILE * stream, off_t newpos, int whence)
2391 while (newpos != (long) newpos)
2393 long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
2394 if (fseek (stream, pos, whence) != 0)
2399 return fseek (stream, (long) newpos, whence);
2404 iobuf_seek (iobuf_t a, off_t newpos)
2406 file_filter_ctx_t *b = NULL;
2408 if (a->use == IOBUF_OUTPUT || a->use == IOBUF_INPUT)
2410 /* Find the last filter in the pipeline. */
2411 for (; a->chain; a = a->chain)
2414 if (a->filter != file_filter)
2419 #ifdef HAVE_W32_SYSTEM
2420 if (SetFilePointer (b->fp, newpos, NULL, FILE_BEGIN) == 0xffffffff)
2422 log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2423 b->fp, (int) GetLastError ());
2427 if (lseek (b->fp, newpos, SEEK_SET) == (off_t) - 1)
2429 log_error ("can't lseek: %s\n", strerror (errno));
2433 /* Discard the buffer it is not a temp stream. */
2443 /* It is impossible for A->CHAIN to be non-NULL. If A is an INPUT
2444 or OUTPUT buffer, then we find the last filter, which is defined
2445 as A->CHAIN being NULL. If A is a TEMP filter, then A must be
2446 the only filter in the pipe: when iobuf_push_filter adds a filter
2447 to the front of a pipeline, it sets the new filter to be an
2448 OUTPUT filter if the pipeline is an OUTPUT or TEMP pipeline and
2449 to be an INPUT filter if the pipeline is an INPUT pipeline.
2450 Thus, only the last filter in a TEMP pipeline can be a */
2452 /* remove filters, but the last */
2454 log_debug ("iobuf_pop_filter called in iobuf_seek - please report\n");
2456 iobuf_pop_filter (a, a->filter, NULL);
2463 iobuf_get_real_fname (iobuf_t a)
2466 return a->real_fname;
2468 /* the old solution */
2469 for (; a; a = a->chain)
2470 if (!a->chain && a->filter == file_filter)
2472 file_filter_ctx_t *b = a->filter_ov;
2473 return b->print_only_name ? NULL : b->fname;
2480 iobuf_get_fname (iobuf_t a)
2482 for (; a; a = a->chain)
2483 if (!a->chain && a->filter == file_filter)
2485 file_filter_ctx_t *b = a->filter_ov;
2492 iobuf_get_fname_nonnull (iobuf_t a)
2496 fname = iobuf_get_fname (a);
2497 return fname? fname : "[?]";
2502 * Enable or disable partial body length mode (RFC 4880 4.2.2.4).
2504 * If LEN is 0, this disables partial block mode by popping the
2505 * partial body length filter, which which must be the most recently
2508 * If LEN is non-zero, it pushes a partial body length filter. If
2509 * this is a read filter, LEN must be the length byte from the first
2510 * chunk and A should be position just after this first partial body
2514 iobuf_set_partial_body_length_mode (iobuf_t a, size_t len)
2517 /* Disable partial body length mode. */
2519 if (a->use == IOBUF_INPUT)
2520 log_debug ("iobuf_pop_filter called in set_partial_block_mode"
2521 " - please report\n");
2523 log_assert (a->filter == block_filter);
2524 iobuf_pop_filter (a, block_filter, NULL);
2527 /* Enabled partial body length mode. */
2529 block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
2534 iobuf_push_filter (a, block_filter, ctx);
2541 iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
2542 unsigned *length_of_buffer, unsigned *max_length)
2545 char *buffer = (char *)*addr_of_buffer;
2546 unsigned length = *length_of_buffer;
2547 unsigned nbytes = 0;
2548 unsigned maxlen = *max_length;
2551 /* The code assumes that we have space for at least a newline and a
2552 NUL character in the buffer. This requires at least 2 bytes. We
2553 don't complicate the code by handling the stupid corner case, but
2554 simply assert that it can't happen. */
2555 assert (length >= 2 || maxlen >= 2);
2557 if (!buffer || length <= 1)
2558 /* must allocate a new buffer */
2560 length = 256 <= maxlen ? 256 : maxlen;
2561 buffer = xrealloc (buffer, length);
2562 *addr_of_buffer = (unsigned char *)buffer;
2563 *length_of_buffer = length;
2567 while ((c = iobuf_get (a)) != -1)
2574 if (nbytes == length - 1)
2575 /* We don't have enough space to add a \n and a \0. Increase
2578 if (length == maxlen)
2579 /* We reached the buffer's size limit! */
2581 /* Skip the rest of the line. */
2582 while (c != '\n' && (c = iobuf_get (a)) != -1)
2585 /* p is pointing at the last byte in the buffer. We
2586 always terminate the line with "\n\0" so overwrite
2587 the previous byte with a \n. */
2588 assert (p > buffer);
2591 /* Indicate truncation. */
2596 length += length < 1024 ? 256 : 1024;
2597 if (length > maxlen)
2600 buffer = xrealloc (buffer, length);
2601 *addr_of_buffer = (unsigned char *)buffer;
2602 *length_of_buffer = length;
2603 p = buffer + nbytes;
2606 /* Add the terminating NUL. */
2609 /* Return the number of characters written to the buffer including
2610 the newline, but not including the terminating NUL. */
2615 translate_file_handle (int fd, int for_write)
2617 #if defined(HAVE_W32CE_SYSTEM)
2618 /* This is called only with one of the special filenames. Under
2619 W32CE the FD here is not a file descriptor but a rendezvous id,
2620 thus we need to finish the pipe first. */
2621 fd = _assuan_w32ce_finish_pipe (fd, for_write);
2622 #elif defined(HAVE_W32_SYSTEM)
2629 x = (int) GetStdHandle (STD_INPUT_HANDLE);
2631 x = (int) GetStdHandle (STD_OUTPUT_HANDLE);
2633 x = (int) GetStdHandle (STD_ERROR_HANDLE);
2638 log_debug ("GetStdHandle(%d) failed: ec=%d\n",
2639 fd, (int) GetLastError ());
2651 iobuf_skip_rest (iobuf_t a, unsigned long n, int partial)
2657 if (a->nofast || a->d.start >= a->d.len)
2659 if (iobuf_readbyte (a) == -1)
2666 unsigned long count = a->d.len - a->d.start;
2668 a->d.start = a->d.len;
2674 unsigned long remaining = n;
2675 while (remaining > 0)
2677 if (a->nofast || a->d.start >= a->d.len)
2679 if (iobuf_readbyte (a) == -1)
2687 unsigned long count = a->d.len - a->d.start;
2688 if (count > remaining)
2693 a->d.start += count;