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 <http://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 /* Global flag to tell whether special file names are enabled. See
159 gpg.c for an explanation of these file names. FIXME: This does not
160 belong in the iobuf subsystem. */
161 static int special_names_enabled;
163 /* Local prototypes. */
164 static int underflow (iobuf_t a, int clear_pending_eof);
165 static int translate_file_handle (int fd, int for_write);
167 /* Sends any pending data to the filter's FILTER function. Note: this
168 works on the filter and not on the whole pipeline. That is,
169 iobuf_flush doesn't necessarily cause data to be written to any
170 underlying file; it just causes any data buffered at the filter A
171 to be sent to A's filter function.
173 If A is a IOBUF_OUTPUT_TEMP filter, then this also enlarges the
174 buffer by IOBUF_BUFFER_SIZE.
176 May only be called on an IOBUF_OUTPUT or IOBUF_OUTPUT_TEMP filters. */
177 static int filter_flush (iobuf_t a);
181 /* This is a replacement for strcmp. Under W32 it does not
182 distinguish between backslash and slash. */
184 fd_cache_strcmp (const char *a, const char *b)
186 #ifdef HAVE_DOSISH_SYSTEM
187 for (; *a && *b; a++, b++)
189 if (*a != *b && !((*a == '/' && *b == '\\')
190 || (*a == '\\' && *b == '/')) )
193 return *(const unsigned char *)a - *(const unsigned char *)b;
195 return strcmp (a, b);
200 * Invalidate (i.e. close) a cached iobuf
203 fd_cache_invalidate (const char *fname)
210 log_debug ("fd_cache_invalidate (%s)\n", fname);
212 for (cc = close_cache; cc; cc = cc->next)
214 if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
217 log_debug (" did (%s)\n", cc->fname);
218 #ifdef HAVE_W32_SYSTEM
219 if (!CloseHandle (cc->fp))
224 cc->fp = GNUPG_INVALID_FD;
231 /* Try to sync changes to the disk. This is to avoid data loss during
232 a system crash in write/close/rename cycle on some file
235 fd_cache_synchronize (const char *fname)
243 log_debug ("fd_cache_synchronize (%s)\n", fname);
245 for (cc=close_cache; cc; cc = cc->next )
247 if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
250 log_debug (" did (%s)\n", cc->fname);
252 err = fsync (cc->fp);
257 #endif /*HAVE_FSYNC*/
264 direct_open (const char *fname, const char *mode, int mode700)
266 #ifdef HAVE_W32_SYSTEM
267 unsigned long da, cd, sm;
271 /* Note, that we do not handle all mode combinations */
273 /* According to the ReactOS source it seems that open() of the
274 * standard MSW32 crt does open the file in shared mode which is
275 * something new for MS applications ;-)
277 if (strchr (mode, '+'))
279 if (fd_cache_invalidate (fname))
280 return GNUPG_INVALID_FD;
281 da = GENERIC_READ | GENERIC_WRITE;
283 sm = FILE_SHARE_READ | FILE_SHARE_WRITE;
285 else if (strchr (mode, 'w'))
287 if (fd_cache_invalidate (fname))
288 return GNUPG_INVALID_FD;
291 sm = FILE_SHARE_WRITE;
297 sm = FILE_SHARE_READ;
300 #ifdef HAVE_W32CE_SYSTEM
302 wchar_t *wfname = utf8_to_wchar (fname);
305 hfile = CreateFile (wfname, da, sm, NULL, cd,
306 FILE_ATTRIBUTE_NORMAL, NULL);
310 hfile = INVALID_HANDLE_VALUE;
313 hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL);
317 #else /*!HAVE_W32_SYSTEM*/
320 int cflag = S_IRUSR | S_IWUSR;
323 cflag |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
325 /* Note, that we do not handle all mode combinations */
326 if (strchr (mode, '+'))
328 if (fd_cache_invalidate (fname))
329 return GNUPG_INVALID_FD;
332 else if (strchr (mode, 'w'))
334 if (fd_cache_invalidate (fname))
335 return GNUPG_INVALID_FD;
336 oflag = O_WRONLY | O_CREAT | O_TRUNC;
343 if (strchr (mode, 'b'))
351 /* Don't allow iobufs on directories */
352 if (!stat (fname, &buf) && S_ISDIR (buf.st_mode) && !S_ISREG (buf.st_mode))
353 return __set_errno (EISDIR);
356 return open (fname, oflag, cflag);
358 #endif /*!HAVE_W32_SYSTEM*/
363 * Instead of closing an FD we keep it open and cache it for later reuse
364 * Note that this caching strategy only works if the process does not chdir.
367 fd_cache_close (const char *fname, gnupg_fd_t fp)
372 if (!fname || !*fname)
374 #ifdef HAVE_W32_SYSTEM
380 log_debug ("fd_cache_close (%d) real\n", (int)fp);
383 /* try to reuse a slot */
384 for (cc = close_cache; cc; cc = cc->next)
386 if (cc->fp == GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
390 log_debug ("fd_cache_close (%s) used existing slot\n", fname);
396 log_debug ("fd_cache_close (%s) new slot created\n", fname);
397 cc = xcalloc (1, sizeof *cc + strlen (fname));
398 strcpy (cc->fname, fname);
400 cc->next = close_cache;
405 * Do a direct_open on FNAME but first try to reuse one from the fd_cache
408 fd_cache_open (const char *fname, const char *mode)
413 for (cc = close_cache; cc; cc = cc->next)
415 if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
417 gnupg_fd_t fp = cc->fp;
418 cc->fp = GNUPG_INVALID_FD;
420 log_debug ("fd_cache_open (%s) using cached fp\n", fname);
421 #ifdef HAVE_W32_SYSTEM
422 if (SetFilePointer (fp, 0, NULL, FILE_BEGIN) == 0xffffffff)
424 log_error ("rewind file failed on handle %p: ec=%d\n",
425 fp, (int) GetLastError ());
426 fp = GNUPG_INVALID_FD;
429 if (lseek (fp, 0, SEEK_SET) == (off_t) - 1)
431 log_error ("can't rewind fd %d: %s\n", fp, strerror (errno));
432 fp = GNUPG_INVALID_FD;
439 log_debug ("fd_cache_open (%s) not cached\n", fname);
440 return direct_open (fname, mode, 0);
445 file_filter (void *opaque, int control, iobuf_t chain, byte * buf,
448 file_filter_ctx_t *a = opaque;
449 gnupg_fd_t f = a->fp;
450 size_t size = *ret_len;
454 (void)chain; /* Not used. */
456 if (control == IOBUFCTRL_UNDERFLOW)
458 assert (size); /* We need a buffer. */
466 #ifdef HAVE_W32_SYSTEM
470 if (!ReadFile (f, buf, size, &nread, NULL))
472 int ec = (int) GetLastError ();
473 if (ec != ERROR_BROKEN_PIPE)
475 rc = gpg_error_from_errno (ec);
476 log_error ("%s: read error: ec=%d\n", a->fname, ec);
496 n = read (f, buf, size);
498 while (n == -1 && errno == EINTR);
503 rc = gpg_error_from_syserror ();
504 log_error ("%s: read error: %s\n",
505 a->fname, strerror (errno));
521 else if (control == IOBUFCTRL_FLUSH)
525 #ifdef HAVE_W32_SYSTEM
532 if (size && !WriteFile (f, p, nbytes, &n, NULL))
534 int ec = (int) GetLastError ();
535 rc = gpg_error_from_errno (ec);
536 log_error ("%s: write error: ec=%d\n", a->fname, ec);
553 n = write (f, p, nbytes);
555 while (n == -1 && errno == EINTR);
562 while (n != -1 && nbytes);
565 rc = gpg_error_from_syserror ();
566 log_error ("%s: write error: %s\n", a->fname, strerror (errno));
573 else if (control == IOBUFCTRL_INIT)
579 else if (control == IOBUFCTRL_DESC)
581 mem2str (buf, "file_filter(fd)", *ret_len);
583 else if (control == IOBUFCTRL_FREE)
585 if (f != FD_FOR_STDIN && f != FD_FOR_STDOUT)
588 log_debug ("%s: close fd/handle %d\n", a->fname, FD2INT (f));
590 fd_cache_close (a->no_cache ? NULL : a->fname, f);
592 xfree (a); /* We can free our context now. */
599 /* Similar to file_filter but using the estream system. */
601 file_es_filter (void *opaque, int control, iobuf_t chain, byte * buf,
604 file_es_filter_ctx_t *a = opaque;
606 size_t size = *ret_len;
610 (void)chain; /* Not used. */
612 if (control == IOBUFCTRL_UNDERFLOW)
614 assert (size); /* We need a buffer. */
623 rc = es_read (f, buf, size, &nbytes);
626 rc = gpg_error_from_syserror ();
627 log_error ("%s: read error: %s\n", a->fname, strerror (errno));
637 else if (control == IOBUFCTRL_FLUSH)
648 if (es_write (f, p, nbytes, &nwritten))
650 rc = gpg_error_from_syserror ();
651 log_error ("%s: write error: %s\n",
652 a->fname, strerror (errno));
663 else if (control == IOBUFCTRL_INIT)
668 else if (control == IOBUFCTRL_DESC)
670 mem2str (buf, "estream_filter", *ret_len);
672 else if (control == IOBUFCTRL_FREE)
674 if (f != es_stdin && f != es_stdout)
677 log_debug ("%s: es_fclose %p\n", a->fname, f);
682 xfree (a); /* We can free our context now. */
689 #ifdef HAVE_W32_SYSTEM
690 /* Because network sockets are special objects under Lose32 we have to
691 use a dedicated filter for them. */
693 sock_filter (void *opaque, int control, iobuf_t chain, byte * buf,
696 sock_filter_ctx_t *a = opaque;
697 size_t size = *ret_len;
703 if (control == IOBUFCTRL_UNDERFLOW)
705 assert (size); /* need a buffer */
715 nread = recv (a->sock, buf, size, 0);
716 if (nread == SOCKET_ERROR)
718 int ec = (int) WSAGetLastError ();
719 rc = gpg_error_from_errno (ec);
720 log_error ("socket read error: ec=%d\n", ec);
734 else if (control == IOBUFCTRL_FLUSH)
744 n = send (a->sock, p, nbytes, 0);
745 if (n == SOCKET_ERROR)
747 int ec = (int) WSAGetLastError ();
748 rc = gpg_error_from_errno (ec);
749 log_error ("socket write error: ec=%d\n", ec);
760 else if (control == IOBUFCTRL_INIT)
766 else if (control == IOBUFCTRL_DESC)
768 mem2str (buf, "sock_filter", *ret_len);
770 else if (control == IOBUFCTRL_FREE)
773 closesocket (a->sock);
774 xfree (a); /* we can free our context now */
778 #endif /*HAVE_W32_SYSTEM*/
781 * This is used to implement the block write mode.
782 * Block reading is done on a byte by byte basis in readbyte(),
786 block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
789 block_filter_ctx_t *a = opaque;
790 char *buf = (char *)buffer;
791 size_t size = *ret_len;
792 int c, needed, rc = 0;
795 if (control == IOBUFCTRL_UNDERFLOW)
800 assert (size); /* need a buffer */
801 if (a->eof) /* don't read any further */
806 { /* get the length bytes */
816 /* These OpenPGP introduced huffman like encoded length
817 * bytes are really a mess :-( */
823 else if ((c = iobuf_get (chain)) == -1)
825 log_error ("block_filter: 1st length byte missing\n");
826 rc = GPG_ERR_BAD_DATA;
843 a->size = (c - 192) * 256;
844 if ((c = iobuf_get (chain)) == -1)
847 ("block_filter: 2nd length byte missing\n");
848 rc = GPG_ERR_BAD_DATA;
863 a->size = (size_t)iobuf_get (chain) << 24;
864 a->size |= iobuf_get (chain) << 16;
865 a->size |= iobuf_get (chain) << 8;
866 if ((c = iobuf_get (chain)) == -1)
868 log_error ("block_filter: invalid 4 byte length\n");
869 rc = GPG_ERR_BAD_DATA;
883 { /* Next partial body length. */
884 a->size = 1 << (c & 0x1f);
886 /* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
892 while (!rc && size && a->size)
894 needed = size < a->size ? size : a->size;
895 c = iobuf_read (chain, p, needed);
901 ("block_filter %p: read error (size=%lu,a->size=%lu)\n",
902 a, (ulong) size + c, (ulong) a->size + c);
903 rc = GPG_ERR_BAD_DATA;
916 else if (control == IOBUFCTRL_FLUSH)
919 { /* the complicated openpgp scheme */
920 size_t blen, n, nbytes = size + a->buflen;
922 assert (a->buflen <= OP_MIN_PARTIAL_CHUNK);
923 if (nbytes < OP_MIN_PARTIAL_CHUNK)
925 /* not enough to write a partial block out; so we store it */
927 a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
928 memcpy (a->buffer + a->buflen, buf, size);
932 { /* okay, we can write out something */
933 /* do this in a loop to use the most efficient block lengths */
937 /* find the best matching block length - this is limited
938 * by the size of the internal buffering */
939 for (blen = OP_MIN_PARTIAL_CHUNK * 2,
940 c = OP_MIN_PARTIAL_CHUNK_2POW + 1; blen <= nbytes;
945 /* write the partial length header */
946 assert (c <= 0x1f); /*;-) */
948 iobuf_put (chain, c);
950 { /* write stuff from the buffer */
951 assert (n == OP_MIN_PARTIAL_CHUNK);
952 if (iobuf_write (chain, a->buffer, n))
953 rc = gpg_error_from_syserror ();
957 if ((n = nbytes) > blen)
959 if (n && iobuf_write (chain, p, n))
960 rc = gpg_error_from_syserror ();
964 while (!rc && nbytes >= OP_MIN_PARTIAL_CHUNK);
965 /* store the rest in the buffer */
969 assert (nbytes < OP_MIN_PARTIAL_CHUNK);
971 a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
972 memcpy (a->buffer, p, nbytes);
980 else if (control == IOBUFCTRL_INIT)
983 log_debug ("init block_filter %p\n", a);
986 else if (a->use == IOBUF_INPUT)
987 a->count = a->size = 0;
989 a->count = a->size; /* force first length bytes */
994 else if (control == IOBUFCTRL_DESC)
996 mem2str (buf, "block_filter", *ret_len);
998 else if (control == IOBUFCTRL_FREE)
1000 if (a->use == IOBUF_OUTPUT)
1001 { /* write the end markers */
1005 /* write out the remaining bytes without a partial header
1006 * the length of this header may be 0 - but if it is
1007 * the first block we are not allowed to use a partial header
1008 * and frankly we can't do so, because this length must be
1009 * a power of 2. This is _really_ complicated because we
1010 * have to check the possible length of a packet prior
1011 * to it's creation: a chain of filters becomes complicated
1012 * and we need a lot of code to handle compressed packets etc.
1015 /* construct header */
1017 /*log_debug("partial: remaining length=%u\n", len ); */
1019 rc = iobuf_put (chain, len);
1020 else if (len < 8384)
1022 if (!(rc = iobuf_put (chain, ((len - 192) / 256) + 192)))
1023 rc = iobuf_put (chain, ((len - 192) % 256));
1026 { /* use a 4 byte header */
1027 if (!(rc = iobuf_put (chain, 0xff)))
1028 if (!(rc = iobuf_put (chain, (len >> 24) & 0xff)))
1029 if (!(rc = iobuf_put (chain, (len >> 16) & 0xff)))
1030 if (!(rc = iobuf_put (chain, (len >> 8) & 0xff)))
1031 rc = iobuf_put (chain, len & 0xff);
1034 rc = iobuf_write (chain, a->buffer, len);
1037 log_error ("block_filter: write error: %s\n",
1039 rc = gpg_error_from_syserror ();
1050 log_error ("block_filter: pending bytes!\n");
1053 log_debug ("free block_filter %p\n", a);
1054 xfree (a); /* we can free our context now */
1060 #define MAX_IOBUF_DESC 32
1062 * Fill the buffer by the description of iobuf A.
1063 * The buffer size should be MAX_IOBUF_DESC (or larger).
1064 * Returns BUF as (const char *).
1067 iobuf_desc (iobuf_t a, byte *buf)
1069 size_t len = MAX_IOBUF_DESC;
1071 if (! a || ! a->filter)
1072 memcpy (buf, "?", 2);
1074 a->filter (a->filter_ov, IOBUFCTRL_DESC, NULL, buf, &len);
1080 print_chain (iobuf_t a)
1084 for (; a; a = a->chain)
1086 byte desc[MAX_IOBUF_DESC];
1088 log_debug ("iobuf chain: %d.%d '%s' filter_eof=%d start=%d len=%d\n",
1089 a->no, a->subno, iobuf_desc (a, desc), a->filter_eof,
1090 (int) a->d.start, (int) a->d.len);
1095 iobuf_print_chain (iobuf_t a)
1102 iobuf_alloc (int use, size_t bufsize)
1105 static int number = 0;
1107 assert (use == IOBUF_INPUT || use == IOBUF_INPUT_TEMP
1108 || use == IOBUF_OUTPUT || use == IOBUF_OUTPUT_TEMP);
1111 log_bug ("iobuf_alloc() passed a bufsize of 0!\n");
1112 bufsize = IOBUF_BUFFER_SIZE;
1115 a = xcalloc (1, sizeof *a);
1117 a->d.buf = xmalloc (bufsize);
1118 a->d.size = bufsize;
1121 a->real_fname = NULL;
1126 iobuf_close (iobuf_t a)
1129 size_t dummy_len = 0;
1132 for (; a; a = a_chain)
1134 byte desc[MAX_IOBUF_DESC];
1139 if (a->use == IOBUF_OUTPUT && (rc = filter_flush (a)))
1140 log_error ("filter_flush failed on close: %s\n", gpg_strerror (rc));
1143 log_debug ("iobuf-%d.%d: close '%s'\n",
1144 a->no, a->subno, iobuf_desc (a, desc));
1146 if (a->filter && (rc2 = a->filter (a->filter_ov, IOBUFCTRL_FREE,
1147 a->chain, NULL, &dummy_len)))
1148 log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
1150 /* Whoops! An error occurred. Save it in RC if we haven't
1151 already recorded an error. */
1154 xfree (a->real_fname);
1157 memset (a->d.buf, 0, a->d.size); /* erase the buffer */
1166 iobuf_cancel (iobuf_t a)
1171 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1172 char *remove_name = NULL;
1175 if (a && a->use == IOBUF_OUTPUT)
1177 s = iobuf_get_real_fname (a);
1180 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1181 remove_name = xstrdup (s);
1188 /* send a cancel message to all filters */
1189 for (a2 = a; a2; a2 = a2->chain)
1193 a2->filter (a2->filter_ov, IOBUFCTRL_CANCEL, a2->chain, NULL, &dummy);
1196 rc = iobuf_close (a);
1197 #if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1200 /* Argg, MSDOS does not allow to remove open files. So
1201 * we have to do it here */
1202 #ifdef HAVE_W32CE_SYSTEM
1203 wchar_t *wtmp = utf8_to_wchar (remove_name);
1208 remove (remove_name);
1210 xfree (remove_name);
1220 return iobuf_alloc (IOBUF_OUTPUT_TEMP, IOBUF_BUFFER_SIZE);
1224 iobuf_temp_with_content (const char *buffer, size_t length)
1229 a = iobuf_alloc (IOBUF_INPUT_TEMP, length);
1230 assert (length == a->d.size);
1231 /* memcpy (a->d.buf, buffer, length); */
1232 for (i=0; i < length; i++)
1233 a->d.buf[i] = buffer[i];
1240 iobuf_enable_special_filenames (int yes)
1242 special_names_enabled = yes;
1246 /* See whether the filename has the form "-&nnnn", where n is a
1247 non-zero number. Returns this number or -1 if it is not the
1250 check_special_filename (const char *fname)
1252 if (special_names_enabled && fname && *fname == '-' && fname[1] == '&')
1257 for (i = 0; digitp (fname+i); i++)
1260 return atoi (fname);
1267 iobuf_is_pipe_filename (const char *fname)
1269 if (!fname || (*fname=='-' && !fname[1]) )
1271 return check_special_filename (fname) != -1;
1275 do_open (const char *fname, int special_filenames,
1276 int use, const char *opentype, int mode700)
1280 file_filter_ctx_t *fcx;
1284 byte desc[MAX_IOBUF_DESC];
1286 assert (use == IOBUF_INPUT || use == IOBUF_OUTPUT);
1288 if (special_filenames
1290 && (!fname || (*fname == '-' && !fname[1])))
1292 if (use == IOBUF_INPUT)
1306 else if (special_filenames && (fd = check_special_filename (fname)) != -1)
1307 return iobuf_fdopen (translate_file_handle (fd, use == IOBUF_INPUT ? 0 : 1),
1311 if (use == IOBUF_INPUT)
1312 fp = fd_cache_open (fname, opentype);
1314 fp = direct_open (fname, opentype, mode700);
1315 if (fp == GNUPG_INVALID_FD)
1319 a = iobuf_alloc (use, IOBUF_BUFFER_SIZE);
1320 fcx = xmalloc (sizeof *fcx + strlen (fname));
1322 fcx->print_only_name = print_only;
1323 strcpy (fcx->fname, fname);
1325 a->real_fname = xstrdup (fname);
1326 a->filter = file_filter;
1328 file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1330 log_debug ("iobuf-%d.%d: open '%s' desc=%s fd=%d\n",
1331 a->no, a->subno, fname, iobuf_desc (a, desc), FD2INT (fcx->fp));
1337 iobuf_open (const char *fname)
1339 return do_open (fname, 1, IOBUF_INPUT, "rb", 0);
1343 iobuf_create (const char *fname, int mode700)
1345 return do_open (fname, 1, IOBUF_OUTPUT, "wb", mode700);
1349 iobuf_openrw (const char *fname)
1351 return do_open (fname, 0, IOBUF_OUTPUT, "r+b", 0);
1356 do_iobuf_fdopen (int fd, const char *mode, int keep_open)
1360 file_filter_ctx_t *fcx;
1365 a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT,
1367 fcx = xmalloc (sizeof *fcx + 20);
1369 fcx->print_only_name = 1;
1370 fcx->keep_open = keep_open;
1371 sprintf (fcx->fname, "[fd %d]", fd);
1372 a->filter = file_filter;
1374 file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1376 log_debug ("iobuf-%d.%d: fdopen%s '%s'\n",
1377 a->no, a->subno, keep_open? "_nc":"", fcx->fname);
1378 iobuf_ioctl (a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
1384 iobuf_fdopen (int fd, const char *mode)
1386 return do_iobuf_fdopen (fd, mode, 0);
1390 iobuf_fdopen_nc (int fd, const char *mode)
1392 return do_iobuf_fdopen (fd, mode, 1);
1397 iobuf_esopen (estream_t estream, const char *mode, int keep_open)
1400 file_es_filter_ctx_t *fcx;
1403 a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT,
1405 fcx = xtrymalloc (sizeof *fcx + 30);
1407 fcx->print_only_name = 1;
1408 fcx->keep_open = keep_open;
1409 sprintf (fcx->fname, "[fd %p]", estream);
1410 a->filter = file_es_filter;
1412 file_es_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1414 log_debug ("iobuf-%d.%d: esopen%s '%s'\n",
1415 a->no, a->subno, keep_open? "_nc":"", fcx->fname);
1421 iobuf_sockopen (int fd, const char *mode)
1424 #ifdef HAVE_W32_SYSTEM
1425 sock_filter_ctx_t *scx;
1428 a = iobuf_alloc (strchr (mode, 'w') ? IOBUF_OUTPUT : IOBUF_INPUT,
1430 scx = xmalloc (sizeof *scx + 25);
1432 scx->print_only_name = 1;
1433 sprintf (scx->fname, "[sock %d]", fd);
1434 a->filter = sock_filter;
1436 sock_filter (scx, IOBUFCTRL_INIT, NULL, NULL, &len);
1438 log_debug ("iobuf-%d.%d: sockopen '%s'\n", a->no, a->subno, scx->fname);
1439 iobuf_ioctl (a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
1441 a = iobuf_fdopen (fd, mode);
1447 iobuf_ioctl (iobuf_t a, iobuf_ioctl_t cmd, int intval, void *ptrval)
1449 byte desc[MAX_IOBUF_DESC];
1451 if (cmd == IOBUF_IOCTL_KEEP_OPEN)
1453 /* Keep system filepointer/descriptor open. This was used in
1454 the past by http.c; this ioctl is not directly used
1457 log_debug ("iobuf-%d.%d: ioctl '%s' keep_open=%d\n",
1458 a ? a->no : -1, a ? a->subno : -1, iobuf_desc (a, desc),
1460 for (; a; a = a->chain)
1461 if (!a->chain && a->filter == file_filter)
1463 file_filter_ctx_t *b = a->filter_ov;
1464 b->keep_open = intval;
1467 #ifdef HAVE_W32_SYSTEM
1468 else if (!a->chain && a->filter == sock_filter)
1470 sock_filter_ctx_t *b = a->filter_ov;
1471 b->keep_open = intval;
1476 else if (cmd == IOBUF_IOCTL_INVALIDATE_CACHE)
1479 log_debug ("iobuf-*.*: ioctl '%s' invalidate\n",
1480 ptrval ? (char *) ptrval : "?");
1481 if (!a && !intval && ptrval)
1483 if (fd_cache_invalidate (ptrval))
1488 else if (cmd == IOBUF_IOCTL_NO_CACHE)
1491 log_debug ("iobuf-%d.%d: ioctl '%s' no_cache=%d\n",
1492 a ? a->no : -1, a ? a->subno : -1, iobuf_desc (a, desc),
1494 for (; a; a = a->chain)
1495 if (!a->chain && a->filter == file_filter)
1497 file_filter_ctx_t *b = a->filter_ov;
1498 b->no_cache = intval;
1501 #ifdef HAVE_W32_SYSTEM
1502 else if (!a->chain && a->filter == sock_filter)
1504 sock_filter_ctx_t *b = a->filter_ov;
1505 b->no_cache = intval;
1510 else if (cmd == IOBUF_IOCTL_FSYNC)
1512 /* Do a fsync on the open fd and return any errors to the caller
1513 of iobuf_ioctl. Note that we work on a file name here. */
1515 log_debug ("iobuf-*.*: ioctl '%s' fsync\n",
1516 ptrval? (const char*)ptrval:"<null>");
1518 if (!a && !intval && ptrval)
1520 return fd_cache_synchronize (ptrval);
1530 * Register an i/o filter.
1533 iobuf_push_filter (iobuf_t a,
1534 int (*f) (void *opaque, int control,
1535 iobuf_t chain, byte * buf, size_t * len),
1538 return iobuf_push_filter2 (a, f, ov, 0);
1542 iobuf_push_filter2 (iobuf_t a,
1543 int (*f) (void *opaque, int control,
1544 iobuf_t chain, byte * buf, size_t * len),
1545 void *ov, int rel_ov)
1548 size_t dummy_len = 0;
1551 if (a->use == IOBUF_OUTPUT && (rc = filter_flush (a)))
1554 if (a->subno >= MAX_NESTING_FILTER)
1556 log_error ("i/o filter too deeply nested - corrupted data?\n");
1557 return GPG_ERR_BAD_DATA;
1560 /* We want to create a new filter and put it in front of A. A
1561 simple implementation would do:
1563 b = iobuf_alloc (...);
1567 This is a bit problematic: A is the head of the pipeline and
1568 there are potentially many pointers to it. Requiring the caller
1569 to update all of these pointers is a burden.
1571 An alternative implementation would add a level of indirection.
1572 For instance, we could use a pipeline object, which contains a
1573 pointer to the first filter in the pipeline. This is not what we
1576 Instead, we allocate a new buffer (B) and copy the first filter's
1577 state into that and use the initial buffer (A) for the new
1578 filter. One limitation of this approach is that it is not
1579 practical to maintain a pointer to a specific filter's state.
1586 +----------+ +----------+
1587 | filter x |--------->| filter y |---->....
1588 +----------+ +----------+
1597 +----------+ +----------+
1598 | filter w | | filter y |---->....
1599 +----------+ +----------+
1601 Note: filter x's address changed from 0x100 to 0x300, but A still
1602 points to the head of the pipeline.
1605 b = xmalloc (sizeof *b);
1606 memcpy (b, a, sizeof *b);
1607 /* fixme: it is stupid to keep a copy of the name at every level
1608 * but we need the name somewhere because the name known by file_filter
1609 * may have been released when we need the name of the file */
1610 b->real_fname = a->real_fname ? xstrdup (a->real_fname) : NULL;
1611 /* remove the filter stuff from the new stream */
1613 a->filter_ov = NULL;
1614 a->filter_ov_owner = 0;
1616 if (a->use == IOBUF_OUTPUT_TEMP)
1617 /* A TEMP filter buffers any data sent to it; it does not forward
1618 any data down the pipeline. If we add a new filter to the
1619 pipeline, it shouldn't also buffer data. It should send it
1620 downstream to be buffered. Thus, the correct type for a filter
1621 added in front of an IOBUF_OUTPUT_TEMP filter is IOBUF_OUPUT, not
1622 IOBUF_OUTPUT_TEMP. */
1624 a->use = IOBUF_OUTPUT;
1626 /* When pipeline is written to, the temp buffer's size is
1627 increased accordingly. We don't need to allocate a 10 MB
1628 buffer for a non-terminal filter. Just use the default
1630 a->d.size = IOBUF_BUFFER_SIZE;
1632 else if (a->use == IOBUF_INPUT_TEMP)
1633 /* Same idea as above. */
1635 a->use = IOBUF_INPUT;
1636 a->d.size = IOBUF_BUFFER_SIZE;
1639 /* The new filter (A) gets a new buffer.
1641 If the pipeline is an output or temp pipeline, then giving the
1642 buffer to the new filter means that data that was written before
1643 the filter was pushed gets sent to the filter. That's clearly
1646 If the pipeline is an input pipeline, then giving the buffer to
1647 the new filter (A) means that data that has read from (B), but
1648 not yet read from the pipeline won't be processed by the new
1649 filter (A)! That's certainly not what we want. */
1650 a->d.buf = xmalloc (a->d.size);
1654 /* disable nlimit for the new stream */
1655 a->ntotal = b->ntotal + b->nbytes;
1656 a->nlimit = a->nbytes = 0;
1658 /* make a link from the new stream to the original stream */
1661 /* setup the function on the new stream */
1664 a->filter_ov_owner = rel_ov;
1666 a->subno = b->subno + 1;
1670 byte desc[MAX_IOBUF_DESC];
1671 log_debug ("iobuf-%d.%d: push '%s'\n",
1672 a->no, a->subno, iobuf_desc (a, desc));
1676 /* now we can initialize the new function if we have one */
1677 if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_INIT, a->chain,
1679 log_error ("IOBUFCTRL_INIT failed: %s\n", gpg_strerror (rc));
1684 * Remove an i/o filter.
1687 iobuf_pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
1688 iobuf_t chain, byte * buf, size_t * len),
1692 size_t dummy_len = 0;
1694 byte desc[MAX_IOBUF_DESC];
1697 log_debug ("iobuf-%d.%d: pop '%s'\n",
1698 a->no, a->subno, iobuf_desc (a, desc));
1699 if (a->use == IOBUF_INPUT_TEMP || a->use == IOBUF_OUTPUT_TEMP)
1701 /* This should be the last filter in the pipeline. */
1702 assert (! a->chain);
1706 { /* this is simple */
1710 xfree (a->real_fname);
1711 memcpy (a, b, sizeof *a);
1715 for (b = a; b; b = b->chain)
1716 if (b->filter == f && (!ov || b->filter_ov == ov))
1719 log_bug ("iobuf_pop_filter(): filter function not found\n");
1721 /* flush this stream if it is an output stream */
1722 if (a->use == IOBUF_OUTPUT && (rc = filter_flush (b)))
1724 log_error ("filter_flush failed in iobuf_pop_filter: %s\n",
1728 /* and tell the filter to free it self */
1729 if (b->filter && (rc = b->filter (b->filter_ov, IOBUFCTRL_FREE, b->chain,
1732 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1735 if (b->filter_ov && b->filter_ov_owner)
1737 xfree (b->filter_ov);
1738 b->filter_ov = NULL;
1742 /* and see how to remove it */
1743 if (a == b && !b->chain)
1744 log_bug ("can't remove the last filter from the chain\n");
1746 { /* remove the first iobuf from the chain */
1747 /* everything from b is copied to a. This is save because
1748 * a flush has been done on the to be removed entry
1752 xfree (a->real_fname);
1753 memcpy (a, b, sizeof *a);
1756 log_debug ("iobuf-%d.%d: popped filter\n", a->no, a->subno);
1759 { /* remove the last iobuf from the chain */
1760 log_bug ("Ohh jeee, trying to remove a head filter\n");
1763 { /* remove an intermediate iobuf from the chain */
1764 log_bug ("Ohh jeee, trying to remove an intermediate filter\n");
1772 * read underflow: read more bytes into the buffer and return
1773 * the first byte or -1 on EOF.
1776 underflow (iobuf_t a, int clear_pending_eof)
1782 log_debug ("iobuf-%d.%d: underflow: buffer size: %d; still buffered: %d => space for %d bytes\n",
1784 (int) a->d.size, (int) (a->d.len - a->d.start),
1785 (int) (a->d.size - (a->d.len - a->d.start)));
1787 if (a->use == IOBUF_INPUT_TEMP)
1788 /* By definition, there isn't more data to read into the
1792 assert (a->use == IOBUF_INPUT);
1794 /* If there is still some buffered data, then move it to the start
1795 of the buffer and try to fill the end of the buffer. (This is
1796 useful if we are called from iobuf_peek().) */
1797 assert (a->d.start <= a->d.len);
1798 a->d.len -= a->d.start;
1799 memmove (a->d.buf, &a->d.buf[a->d.start], a->d.len);
1802 if (a->d.len == 0 && a->filter_eof)
1803 /* The last time we tried to read from this filter, we got an EOF.
1804 We couldn't return the EOF, because there was buffered data.
1805 Since there is no longer any buffered data, return the
1809 log_debug ("iobuf-%d.%d: underflow: eof (pending eof)\n",
1811 if (! clear_pending_eof)
1815 /* A filter follows this one. Free this filter. */
1817 iobuf_t b = a->chain;
1819 log_debug ("iobuf-%d.%d: filter popped (pending EOF returned)\n",
1822 xfree (a->real_fname);
1823 memcpy (a, b, sizeof *a);
1828 a->filter_eof = 0; /* for the top level filter */
1829 return -1; /* return one(!) EOF */
1832 if (a->d.len == 0 && a->error)
1833 /* The last time we tried to read from this filter, we got an
1834 error. We couldn't return the error, because there was
1835 buffered data. Since there is no longer any buffered data,
1836 return the error. */
1839 log_debug ("iobuf-%d.%d: pending error (%s) returned\n",
1840 a->no, a->subno, gpg_strerror (a->error));
1844 if (a->filter && ! a->filter_eof && ! a->error)
1845 /* We have a filter function and the last time we tried to read we
1846 didn't get an EOF or an error. Try to fill the buffer. */
1848 /* Be careful to account for any buffered data. */
1849 len = a->d.size - a->d.len;
1851 log_debug ("iobuf-%d.%d: underflow: A->FILTER (%lu bytes)\n",
1852 a->no, a->subno, (ulong) len);
1854 /* There is no space for more data. Don't bother calling
1858 rc = a->filter (a->filter_ov, IOBUFCTRL_UNDERFLOW, a->chain,
1859 &a->d.buf[a->d.len], &len);
1863 log_debug ("iobuf-%d.%d: A->FILTER() returned rc=%d (%s), read %lu bytes\n",
1865 rc, rc == 0 ? "ok" : rc == -1 ? "EOF" : gpg_strerror (rc),
1867 /* if( a->no == 1 ) */
1868 /* log_hexdump (" data:", a->d.buf, len); */
1873 size_t dummy_len = 0;
1875 /* Tell the filter to free itself */
1876 if ((rc = a->filter (a->filter_ov, IOBUFCTRL_FREE, a->chain,
1878 log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1880 /* Free everything except for the internal buffer. */
1881 if (a->filter_ov && a->filter_ov_owner)
1882 xfree (a->filter_ov);
1883 a->filter_ov = NULL;
1887 if (clear_pending_eof && a->d.len == 0 && a->chain)
1888 /* We don't need to keep this filter around at all:
1891 - we have no buffered data
1892 - a filter follows this one.
1894 Unlink this filter. */
1896 iobuf_t b = a->chain;
1898 log_debug ("iobuf-%d.%d: pop in underflow (nothing buffered, got EOF)\n",
1901 xfree (a->real_fname);
1902 memcpy (a, b, sizeof *a);
1909 else if (a->d.len == 0)
1910 /* We can't unlink this filter (it is the only one in the
1911 pipeline), but we can immediately return EOF. */
1915 /* Record the error. */
1920 /* There is no buffered data. Immediately return EOF. */
1925 assert (a->d.start <= a->d.len);
1926 if (a->d.start < a->d.len)
1927 return a->d.buf[a->d.start++];
1935 filter_flush (iobuf_t a)
1940 if (a->use == IOBUF_OUTPUT_TEMP)
1941 { /* increase the temp buffer */
1942 size_t newsize = a->d.size + IOBUF_BUFFER_SIZE;
1945 log_debug ("increasing temp iobuf from %lu to %lu\n",
1946 (ulong) a->d.size, (ulong) newsize);
1948 a->d.buf = xrealloc (a->d.buf, newsize);
1949 a->d.size = newsize;
1952 else if (a->use != IOBUF_OUTPUT)
1953 log_bug ("flush on non-output iobuf\n");
1954 else if (!a->filter)
1955 log_bug ("filter_flush: no filter\n");
1957 rc = a->filter (a->filter_ov, IOBUFCTRL_FLUSH, a->chain, a->d.buf, &len);
1958 if (!rc && len != a->d.len)
1960 log_info ("filter_flush did not write all!\n");
1961 rc = GPG_ERR_INTERNAL;
1972 iobuf_readbyte (iobuf_t a)
1976 if (a->use == IOBUF_OUTPUT || a->use == IOBUF_OUTPUT_TEMP)
1978 log_bug ("iobuf_readbyte called on a non-INPUT pipeline!\n");
1982 assert (a->d.start <= a->d.len);
1984 if (a->nlimit && a->nbytes >= a->nlimit)
1985 return -1; /* forced EOF */
1987 if (a->d.start < a->d.len)
1989 c = a->d.buf[a->d.start++];
1991 else if ((c = underflow (a, 1)) == -1)
1992 return -1; /* EOF */
1994 assert (a->d.start <= a->d.len);
1996 /* Note: if underflow doesn't return EOF, then it returns the first
1997 byte that was read and advances a->d.start appropriately. */
2005 iobuf_read (iobuf_t a, void *buffer, unsigned int buflen)
2007 unsigned char *buf = (unsigned char *)buffer;
2010 if (a->use == IOBUF_OUTPUT || a->use == IOBUF_OUTPUT_TEMP)
2012 log_bug ("iobuf_read called on a non-INPUT pipeline!\n");
2018 /* Handle special cases. */
2019 for (n = 0; n < buflen; n++)
2021 if ((c = iobuf_readbyte (a)) == -1)
2024 return -1; /* eof */
2040 if (n < buflen && a->d.start < a->d.len)
2041 /* Drain the buffer. */
2043 unsigned size = a->d.len - a->d.start;
2044 if (size > buflen - n)
2047 memcpy (buf, a->d.buf + a->d.start, size);
2054 /* Draining the internal buffer didn't fill BUFFER. Call
2055 underflow to read more data into the filter's internal
2058 if ((c = underflow (a, 1)) == -1)
2059 /* EOF. If we managed to read something, don't return EOF
2063 return n ? n : -1 /*EOF*/;
2078 iobuf_peek (iobuf_t a, byte * buf, unsigned buflen)
2082 assert (buflen > 0);
2083 assert (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP);
2085 if (buflen > a->d.size)
2086 /* We can't peek more than we can buffer. */
2089 /* Try to fill the internal buffer with enough data to satisfy the
2091 while (buflen > a->d.len - a->d.start)
2093 if (underflow (a, 0) == -1)
2094 /* EOF. We can't read any more. */
2097 /* Underflow consumes the first character (it's the return
2098 value). unget() it by resetting the "file position". */
2099 assert (a->d.start == 1);
2103 n = a->d.len - a->d.start;
2111 memcpy (buf, &a->d.buf[a->d.start], n);
2120 iobuf_writebyte (iobuf_t a, unsigned int c)
2124 if (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP)
2126 log_bug ("iobuf_writebyte called on an input pipeline!\n");
2130 if (a->d.len == a->d.size)
2131 if ((rc=filter_flush (a)))
2134 assert (a->d.len < a->d.size);
2135 a->d.buf[a->d.len++] = c;
2141 iobuf_write (iobuf_t a, const void *buffer, unsigned int buflen)
2143 const unsigned char *buf = (const unsigned char *)buffer;
2146 if (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP)
2148 log_bug ("iobuf_write called on an input pipeline!\n");
2154 if (buflen && a->d.len < a->d.size)
2156 unsigned size = a->d.size - a->d.len;
2159 memcpy (a->d.buf + a->d.len, buf, size);
2166 rc = filter_flush (a);
2177 iobuf_writestr (iobuf_t a, const char *buf)
2179 if (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP)
2181 log_bug ("iobuf_writestr called on an input pipeline!\n");
2185 return iobuf_write (a, buf, strlen (buf));
2191 iobuf_write_temp (iobuf_t dest, iobuf_t source)
2193 assert (source->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP);
2194 assert (dest->use == IOBUF_OUTPUT || dest->use == IOBUF_OUTPUT_TEMP);
2196 iobuf_flush_temp (source);
2197 return iobuf_write (dest, source->d.buf, source->d.len);
2201 iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen)
2203 byte desc[MAX_IOBUF_DESC];
2208 int rc = filter_flush (a);
2210 log_bug ("Flushing iobuf %d.%d (%s) from iobuf_temp_to_buffer failed. Ignoring.\n",
2211 a->no, a->subno, iobuf_desc (a, desc));
2220 memcpy (buffer, a->d.buf, n);
2224 /* Copies the data from the input iobuf SOURCE to the output iobuf
2225 DEST until either an error is encountered or EOF is reached.
2226 Returns the number of bytes copies. */
2228 iobuf_copy (iobuf_t dest, iobuf_t source)
2231 /* Use a 32 KB buffer. */
2232 const size_t temp_size = 32 * 1024;
2238 assert (source->use == IOBUF_INPUT || source->use == IOBUF_INPUT_TEMP);
2239 assert (dest->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP);
2241 if (iobuf_error (dest))
2244 temp = xmalloc (temp_size);
2247 nread = iobuf_read (source, temp, temp_size);
2252 err = iobuf_write (dest, temp, nread);
2258 /* Burn the buffer. */
2259 wipememory (temp, sizeof (temp));
2267 iobuf_flush_temp (iobuf_t temp)
2269 if (temp->use == IOBUF_INPUT || temp->use == IOBUF_INPUT_TEMP)
2270 log_bug ("iobuf_flush_temp called on an input pipeline!\n");
2272 iobuf_pop_filter (temp, temp->filter, NULL);
2277 iobuf_set_limit (iobuf_t a, off_t nlimit)
2284 a->ntotal += a->nbytes;
2291 iobuf_get_filelength (iobuf_t a, int *overflow)
2296 /* Hmmm: file_filter may have already been removed */
2297 for ( ; a->chain; a = a->chain )
2300 if (a->filter != file_filter)
2304 file_filter_ctx_t *b = a->filter_ov;
2305 gnupg_fd_t fp = b->fp;
2307 #if defined(HAVE_W32_SYSTEM)
2309 static int (* __stdcall get_file_size_ex) (void *handle,
2310 LARGE_INTEGER *r_size);
2311 static int get_file_size_ex_initialized;
2313 if (!get_file_size_ex_initialized)
2317 handle = dlopen ("kernel32.dll", RTLD_LAZY);
2320 get_file_size_ex = dlsym (handle, "GetFileSizeEx");
2321 if (!get_file_size_ex)
2324 get_file_size_ex_initialized = 1;
2327 if (get_file_size_ex)
2329 /* This is a newer system with GetFileSizeEx; we use this
2330 then because it seem that GetFileSize won't return a
2331 proper error in case a file is larger than 4GB. */
2332 LARGE_INTEGER exsize;
2334 if (get_file_size_ex (fp, &exsize))
2336 if (!exsize.u.HighPart)
2337 return exsize.u.LowPart;
2345 if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
2348 log_error ("GetFileSize for handle %p failed: %s\n",
2349 fp, w32_strerror (0));
2350 #else /*!HAVE_W32_SYSTEM*/
2354 if ( !fstat (FD2INT (fp), &st) )
2356 log_error("fstat() failed: %s\n", strerror(errno) );
2358 #endif /*!HAVE_W32_SYSTEM*/
2366 iobuf_get_fd (iobuf_t a)
2368 for (; a->chain; a = a->chain)
2371 if (a->filter != file_filter)
2375 file_filter_ctx_t *b = a->filter_ov;
2376 gnupg_fd_t fp = b->fp;
2384 iobuf_tell (iobuf_t a)
2386 return a->ntotal + a->nbytes;
2390 #if !defined(HAVE_FSEEKO) && !defined(fseeko)
2392 #ifdef HAVE_LIMITS_H
2393 # include <limits.h>
2396 # define LONG_MAX ((long) ((unsigned long) -1 >> 1))
2399 # define LONG_MIN (-1 - LONG_MAX)
2403 * A substitute for fseeko, for hosts that don't have it.
2406 fseeko (FILE * stream, off_t newpos, int whence)
2408 while (newpos != (long) newpos)
2410 long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
2411 if (fseek (stream, pos, whence) != 0)
2416 return fseek (stream, (long) newpos, whence);
2421 iobuf_seek (iobuf_t a, off_t newpos)
2423 file_filter_ctx_t *b = NULL;
2425 if (a->use == IOBUF_OUTPUT || a->use == IOBUF_INPUT)
2427 /* Find the last filter in the pipeline. */
2428 for (; a->chain; a = a->chain)
2431 if (a->filter != file_filter)
2436 #ifdef HAVE_W32_SYSTEM
2437 if (SetFilePointer (b->fp, newpos, NULL, FILE_BEGIN) == 0xffffffff)
2439 log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2440 b->fp, (int) GetLastError ());
2444 if (lseek (b->fp, newpos, SEEK_SET) == (off_t) - 1)
2446 log_error ("can't lseek: %s\n", strerror (errno));
2450 /* Discard the buffer it is not a temp stream. */
2460 /* It is impossible for A->CHAIN to be non-NULL. If A is an INPUT
2461 or OUTPUT buffer, then we find the last filter, which is defined
2462 as A->CHAIN being NULL. If A is a TEMP filter, then A must be
2463 the only filter in the pipe: when iobuf_push_filter adds a filter
2464 to the front of a pipeline, it sets the new filter to be an
2465 OUTPUT filter if the pipeline is an OUTPUT or TEMP pipeline and
2466 to be an INPUT filter if the pipeline is an INPUT pipeline.
2467 Thus, only the last filter in a TEMP pipeline can be a */
2469 /* remove filters, but the last */
2471 log_debug ("iobuf_pop_filter called in iobuf_seek - please report\n");
2473 iobuf_pop_filter (a, a->filter, NULL);
2480 iobuf_get_real_fname (iobuf_t a)
2483 return a->real_fname;
2485 /* the old solution */
2486 for (; a; a = a->chain)
2487 if (!a->chain && a->filter == file_filter)
2489 file_filter_ctx_t *b = a->filter_ov;
2490 return b->print_only_name ? NULL : b->fname;
2497 iobuf_get_fname (iobuf_t a)
2499 for (; a; a = a->chain)
2500 if (!a->chain && a->filter == file_filter)
2502 file_filter_ctx_t *b = a->filter_ov;
2509 iobuf_get_fname_nonnull (iobuf_t a)
2513 fname = iobuf_get_fname (a);
2514 return fname? fname : "[?]";
2519 * Enable or disable partial body length mode (RFC 4880 4.2.2.4).
2521 * If LEN is 0, this disables partial block mode by popping the
2522 * partial body length filter, which which must be the most recently
2525 * If LEN is non-zero, it pushes a partial body length filter. If
2526 * this is a read filter, LEN must be the length byte from the first
2527 * chunk and A should be position just after this first partial body
2531 iobuf_set_partial_body_length_mode (iobuf_t a, size_t len)
2533 block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
2537 /* Disable partial body length mode. */
2539 if (a->use == IOBUF_INPUT)
2540 log_debug ("iobuf_pop_filter called in set_partial_block_mode"
2541 " - please report\n");
2543 log_assert (a->filter == block_filter);
2544 iobuf_pop_filter (a, block_filter, NULL);
2547 /* Enabled partial body length mode. */
2552 iobuf_push_filter (a, block_filter, ctx);
2559 iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
2560 unsigned *length_of_buffer, unsigned *max_length)
2563 char *buffer = (char *)*addr_of_buffer;
2564 unsigned length = *length_of_buffer;
2565 unsigned nbytes = 0;
2566 unsigned maxlen = *max_length;
2569 /* The code assumes that we have space for at least a newline and a
2570 NUL character in the buffer. This requires at least 2 bytes. We
2571 don't complicate the code by handling the stupid corner case, but
2572 simply assert that it can't happen. */
2573 assert (length >= 2 || maxlen >= 2);
2575 if (!buffer || length <= 1)
2576 /* must allocate a new buffer */
2578 length = 256 <= maxlen ? 256 : maxlen;
2579 buffer = xrealloc (buffer, length);
2580 *addr_of_buffer = (unsigned char *)buffer;
2581 *length_of_buffer = length;
2585 while ((c = iobuf_get (a)) != -1)
2592 if (nbytes == length - 1)
2593 /* We don't have enough space to add a \n and a \0. Increase
2596 if (length == maxlen)
2597 /* We reached the buffer's size limit! */
2599 /* Skip the rest of the line. */
2600 while (c != '\n' && (c = iobuf_get (a)) != -1)
2603 /* p is pointing at the last byte in the buffer. We
2604 always terminate the line with "\n\0" so overwrite
2605 the previous byte with a \n. */
2606 assert (p > buffer);
2609 /* Indicate truncation. */
2614 length += length < 1024 ? 256 : 1024;
2615 if (length > maxlen)
2618 buffer = xrealloc (buffer, length);
2619 *addr_of_buffer = (unsigned char *)buffer;
2620 *length_of_buffer = length;
2621 p = buffer + nbytes;
2624 /* Add the terminating NUL. */
2627 /* Return the number of characters written to the buffer including
2628 the newline, but not including the terminating NUL. */
2633 translate_file_handle (int fd, int for_write)
2635 #if defined(HAVE_W32CE_SYSTEM)
2636 /* This is called only with one of the special filenames. Under
2637 W32CE the FD here is not a file descriptor but a rendezvous id,
2638 thus we need to finish the pipe first. */
2639 fd = _assuan_w32ce_finish_pipe (fd, for_write);
2640 #elif defined(HAVE_W32_SYSTEM)
2647 x = (int) GetStdHandle (STD_INPUT_HANDLE);
2649 x = (int) GetStdHandle (STD_OUTPUT_HANDLE);
2651 x = (int) GetStdHandle (STD_ERROR_HANDLE);
2656 log_debug ("GetStdHandle(%d) failed: ec=%d\n",
2657 fd, (int) GetLastError ());
2669 iobuf_skip_rest (iobuf_t a, unsigned long n, int partial)
2675 if (a->nofast || a->d.start >= a->d.len)
2677 if (iobuf_readbyte (a) == -1)
2684 unsigned long count = a->d.len - a->d.start;
2686 a->d.start = a->d.len;
2692 unsigned long remaining = n;
2693 while (remaining > 0)
2695 if (a->nofast || a->d.start >= a->d.len)
2697 if (iobuf_readbyte (a) == -1)
2705 unsigned long count = a->d.len - a->d.start;
2706 if (count > remaining)
2711 a->d.start += count;