1
/* iobuf.c - File Handling for OpenPGP.
2
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2006,
3
* 2007, 2008, 2009 Free Software Foundation, Inc.
5
* This file is part of GnuPG.
7
* GnuPG is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* GnuPG is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, see <http://www.gnu.org/licenses/>.
28
#include <sys/types.h>
32
#ifdef HAVE_W32_SYSTEM
33
# ifdef HAVE_WINSOCK2_H
34
# include <winsock2.h>
41
#endif /* __riscos__ */
47
/*-- Begin configurable part. --*/
49
/* The size of the internal buffers.
50
NOTE: If you change this value you MUST also adjust the regression
51
test "armored_key_8192" in armor.test! */
52
#define IOBUF_BUFFER_SIZE 8192
54
/* We don't want to use the STDIO based backend. If you change this
55
be aware that there is no fsync support for the stdio backend. */
56
#undef FILE_FILTER_USES_STDIO
58
/*-- End configurable part. --*/
61
/* Under W32 the default is to use the setmode call. Define a macro
62
which allows us to enable this call. */
63
#ifdef HAVE_W32_SYSTEM
64
# define USE_SETMODE 1
65
#endif /*HAVE_W32_SYSTEM*/
68
/* Definition of constants and macros used by our file filter
69
implementation. What we define here are 3 macros to make the
73
Is expanded to fileno(a) if using a stdion backend and to a if we
74
are using the low-level backend.
77
Is defined to fopen for the stdio backend and to direct_open if
78
we are using the low-evel backend.
81
Is defined to fopen for the stdio backend and to fd_cache_open if
82
we are using the low-evel backend.
85
Is the type we use for the backend stream or file descriptor.
87
INVALID_FP, FILEP_OR_FD_FOR_STDIN, FILEP_OR_FD_FOR_STDOUT
88
Are macros defined depending on the used backend.
91
#ifdef FILE_FILTER_USES_STDIO
92
# define my_fileno(a) fileno ((a))
93
# define my_fopen_ro(a,b) fopen ((a),(b))
94
# define my_fopen(a,b) fopen ((a),(b))
95
typedef FILE *fp_or_fd_t;
96
# define INVALID_FP NULL
97
# define FILEP_OR_FD_FOR_STDIN (stdin)
98
# define FILEP_OR_FD_FOR_STDOUT (stdout)
99
#else /*!FILE_FILTER_USES_STDIO*/
100
# define my_fopen_ro(a,b) fd_cache_open ((a),(b))
101
# define my_fopen(a,b) direct_open ((a),(b))
102
# ifdef HAVE_W32_SYSTEM
103
/* (We assume that a HANDLE first into an int.) */
104
# define my_fileno(a) ((int)(a))
105
typedef HANDLE fp_or_fd_t;
106
# define INVALID_FP ((HANDLE)-1)
107
# define FILEP_OR_FD_FOR_STDIN (GetStdHandle (STD_INPUT_HANDLE))
108
# define FILEP_OR_FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
110
# else /*!HAVE_W32_SYSTEM*/
111
# define my_fileno(a) (a)
112
typedef int fp_or_fd_t;
113
# define INVALID_FP (-1)
114
# define FILEP_OR_FD_FOR_STDIN (0)
115
# define FILEP_OR_FD_FOR_STDOUT (1)
116
# endif /*!HAVE_W32_SYSTEM*/
117
#endif /*!FILE_FILTER_USES_STDIO*/
119
/* The context used by the file filter. */
122
fp_or_fd_t fp; /* Open file pointer or handle. */
126
int print_only_name; /* Flags indicating that fname is not a real file. */
127
char fname[1]; /* Name of the file. */
132
/* If we are not using stdio as the backend we make use of a "close
134
#ifndef FILE_FILTER_USES_STDIO
137
struct close_cache_s *next;
141
typedef struct close_cache_s *close_cache_t;
142
static close_cache_t close_cache;
143
#endif /*!FILE_FILTER_USES_STDIO*/
147
#ifdef HAVE_W32_SYSTEM
154
int print_only_name; /* Flag indicating that fname is not a real file. */
155
char fname[1]; /* Name of the file */
158
#endif /*HAVE_W32_SYSTEM*/
160
/* The first partial length header block must be of size 512
161
* to make it easier (and efficienter) we use a min. block size of 512
162
* for all chunks (but the last one) */
163
#define OP_MIN_PARTIAL_CHUNK 512
164
#define OP_MIN_PARTIAL_CHUNK_2POW 9
166
/* The context we use for the block filter (used to handle OpenPGP
167
length information header). */
173
int partial; /* 1 = partial header, 2 in last partial packet. */
174
char *buffer; /* Used for partial header. */
175
size_t buflen; /* Used size of buffer. */
176
int first_c; /* First character of a partial header (which is > 0). */
182
/* Global flag to tell whether special file names are enabled. See
183
gpg.c for an explanation of these file names. FIXME: it does not
184
belong into the iobuf subsystem. */
185
static int special_names_enabled;
187
/* Local prototypes. */
188
static int underflow (iobuf_t a);
189
static int translate_file_handle (int fd, int for_write);
193
#ifndef FILE_FILTER_USES_STDIO
194
/* This is a replacement for strcmp. Under W32 it does not
195
distinguish between backslash and slash. */
197
fd_cache_strcmp (const char *a, const char *b)
199
#ifdef HAVE_DOSISH_SYSTEM
200
for (; *a && *b; a++, b++)
202
if (*a != *b && !((*a == '/' && *b == '\\')
203
|| (*a == '\\' && *b == '/')) )
206
return *(const unsigned char *)a - *(const unsigned char *)b;
208
return strcmp (a, b);
213
* Invalidate (i.e. close) a cached iobuf
216
fd_cache_invalidate (const char *fname)
223
log_debug ("fd_cache_invalidate (%s)\n", fname);
225
for (cc = close_cache; cc; cc = cc->next)
227
if (cc->fp != INVALID_FP && !fd_cache_strcmp (cc->fname, fname))
230
log_debug (" did (%s)\n", cc->fname);
231
#ifdef HAVE_W32_SYSTEM
232
if (!CloseHandle (cc->fp))
244
/* Try to sync changes to the disk. This is to avoid data loss during
245
a system crash in write/close/rename cycle on some file
248
fd_cache_synchronize (const char *fname)
256
log_debug ("fd_cache_synchronize (%s)\n", fname);
258
for (cc=close_cache; cc; cc = cc->next )
260
if (cc->fp != INVALID_FP && !fd_cache_strcmp (cc->fname, fname))
263
log_debug (" did (%s)\n", cc->fname);
265
err = fsync (cc->fp);
270
#endif /*HAVE_FSYNC*/
277
direct_open (const char *fname, const char *mode)
279
#ifdef HAVE_W32_SYSTEM
280
unsigned long da, cd, sm;
283
/* Note, that we do not handle all mode combinations */
285
/* According to the ReactOS source it seems that open() of the
286
* standard MSW32 crt does open the file in shared mode which is
287
* something new for MS applications ;-)
289
if (strchr (mode, '+'))
291
if (fd_cache_invalidate (fname))
293
da = GENERIC_READ | GENERIC_WRITE;
295
sm = FILE_SHARE_READ | FILE_SHARE_WRITE;
297
else if (strchr (mode, 'w'))
299
if (fd_cache_invalidate (fname))
303
sm = FILE_SHARE_WRITE;
309
sm = FILE_SHARE_READ;
312
hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL);
314
#else /*!HAVE_W32_SYSTEM*/
316
int cflag = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
318
/* Note, that we do not handle all mode combinations */
319
if (strchr (mode, '+'))
321
if (fd_cache_invalidate (fname))
325
else if (strchr (mode, 'w'))
327
if (fd_cache_invalidate (fname))
329
oflag = O_WRONLY | O_CREAT | O_TRUNC;
336
if (strchr (mode, 'b'))
339
/* No we need to distinguish between POSIX and RISC OS. */
341
return open (fname, oflag, cflag);
345
int rc = stat (fname, &buf);
347
/* Don't allow iobufs on directories */
348
if (!rc && S_ISDIR (buf.st_mode) && !S_ISREG (buf.st_mode))
349
return __set_errno (EISDIR);
351
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, fp_or_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 == INVALID_FP && !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 an 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 != INVALID_FP && !fd_cache_strcmp (cc->fname, fname))
413
fp_or_fd_t fp = cc->fp;
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 ());
425
if (lseek (fp, 0, SEEK_SET) == (off_t) - 1)
427
log_error ("can't rewind fd %d: %s\n", fp, strerror (errno));
435
log_debug ("fd_cache_open (%s) not cached\n", fname);
436
return direct_open (fname, mode);
439
#endif /*FILE_FILTER_USES_STDIO */
443
* Read data from a file into buf which has an allocated length of *LEN.
444
* return the number of read bytes in *LEN. OPAQUE is the FILE * of
445
* the stream. A is not used.
447
* IOBUFCTRL_INIT: called just before the function is linked into the
448
* list of function. This can be used to prepare internal
449
* data structures of the function.
450
* IOBUFCTRL_FREE: called just before the function is removed from the
451
* list of functions and can be used to release internal
452
* data structures or close a file etc.
453
* IOBUFCTRL_UNDERFLOW: called by iobuf_underflow to fill the buffer
454
* with new stuff. *RET_LEN is the available size of the
455
* buffer, and should be set to the number of bytes
456
* which were put into the buffer. The function
457
* returns 0 to indicate success, -1 on EOF and
458
* GPG_ERR_xxxxx for other errors.
460
* IOBUFCTRL_FLUSH: called by iobuf_flush() to write out the collected stuff.
461
* *RET_LAN is the number of bytes in BUF.
463
* IOBUFCTRL_CANCEL: send to all filters on behalf of iobuf_cancel. The
464
* filter may take appropriate action on this message.
467
file_filter (void *opaque, int control, iobuf_t chain, byte * buf,
470
file_filter_ctx_t *a = opaque;
471
fp_or_fd_t f = a->fp;
472
size_t size = *ret_len;
476
(void)chain; /* Not used. */
478
#ifdef FILE_FILTER_USES_STDIO
479
if (control == IOBUFCTRL_UNDERFLOW)
481
assert (size); /* We need a buffer. */
484
/* On terminals you could easily read as many EOFs as you
485
call fread() or fgetc() repeatly. Every call will block
486
until you press CTRL-D. So we catch this case before we
487
call fread() again. */
494
nbytes = fread (buf, 1, size, f);
495
if (feof (f) && !nbytes)
497
rc = -1; /* Okay: we can return EOF now. */
499
else if (ferror (f) && errno != EPIPE)
501
rc = gpg_error_from_syserror ();
502
log_error ("%s: read error: %s\n", a->fname, strerror (errno));
507
else if (control == IOBUFCTRL_FLUSH)
512
nbytes = fwrite (buf, 1, size, f);
515
rc = gpg_error_from_syserror ();
516
log_error ("%s: write error: %s\n", a->fname, strerror (errno));
521
else if (control == IOBUFCTRL_INIT)
523
a->keep_open = a->no_cache = 0;
525
else if (control == IOBUFCTRL_DESC)
527
*(char **) buf = "file_filter";
529
else if (control == IOBUFCTRL_FREE)
531
if (f != stdin && f != stdout)
534
log_debug ("%s: close fd %d\n", a->fname, fileno (f));
539
xfree (a); /* We can free our context now. */
541
#else /* !stdio implementation */
543
if (control == IOBUFCTRL_UNDERFLOW)
545
assert (size); /* We need a buffer. */
553
#ifdef HAVE_W32_SYSTEM
557
if (!ReadFile (f, buf, size, &nread, NULL))
559
int ec = (int) GetLastError ();
560
if (ec != ERROR_BROKEN_PIPE)
562
rc = gpg_error_from_errno (ec);
563
log_error ("%s: read error: ec=%d\n", a->fname, ec);
583
n = read (f, buf, size);
585
while (n == -1 && errno == EINTR);
590
rc = gpg_error_from_syserror ();
591
log_error ("%s: read error: %s\n",
592
a->fname, strerror (errno));
608
else if (control == IOBUFCTRL_FLUSH)
612
#ifdef HAVE_W32_SYSTEM
619
if (size && !WriteFile (f, p, nbytes, &n, NULL))
621
int ec = (int) GetLastError ();
622
rc = gpg_error_from_errno (ec);
623
log_error ("%s: write error: ec=%d\n", a->fname, ec);
640
n = write (f, p, nbytes);
642
while (n == -1 && errno == EINTR);
649
while (n != -1 && nbytes);
652
rc = gpg_error_from_syserror ();
653
log_error ("%s: write error: %s\n", a->fname, strerror (errno));
660
else if (control == IOBUFCTRL_INIT)
666
else if (control == IOBUFCTRL_DESC)
668
*(char **) buf = "file_filter(fd)";
670
else if (control == IOBUFCTRL_FREE)
672
#ifdef HAVE_W32_SYSTEM
673
if (f != FILEP_OR_FD_FOR_STDIN && f != FILEP_OR_FD_FOR_STDOUT)
676
log_debug ("%s: close handle %p\n", a->fname, f);
678
fd_cache_close (a->no_cache ? NULL : a->fname, f);
681
if ((int) f != 0 && (int) f != 1)
684
log_debug ("%s: close fd %d\n", a->fname, f);
686
fd_cache_close (a->no_cache ? NULL : a->fname, f);
690
xfree (a); /* We can free our context now. */
692
#endif /* !stdio implementation. */
697
#ifdef HAVE_W32_SYSTEM
698
/* Because network sockets are special objects under Lose32 we have to
699
use a dedicated filter for them. */
701
sock_filter (void *opaque, int control, iobuf_t chain, byte * buf,
704
sock_filter_ctx_t *a = opaque;
705
size_t size = *ret_len;
711
if (control == IOBUFCTRL_UNDERFLOW)
713
assert (size); /* need a buffer */
723
nread = recv (a->sock, buf, size, 0);
724
if (nread == SOCKET_ERROR)
726
int ec = (int) WSAGetLastError ();
727
rc = gpg_error_from_errno (ec);
728
log_error ("socket read error: ec=%d\n", ec);
742
else if (control == IOBUFCTRL_FLUSH)
752
n = send (a->sock, p, nbytes, 0);
753
if (n == SOCKET_ERROR)
755
int ec = (int) WSAGetLastError ();
756
rc = gpg_error_from_errno (ec);
757
log_error ("socket write error: ec=%d\n", ec);
768
else if (control == IOBUFCTRL_INIT)
774
else if (control == IOBUFCTRL_DESC)
776
*(char **) buf = "sock_filter";
778
else if (control == IOBUFCTRL_FREE)
781
closesocket (a->sock);
782
xfree (a); /* we can free our context now */
786
#endif /*HAVE_W32_SYSTEM*/
789
* This is used to implement the block write mode.
790
* Block reading is done on a byte by byte basis in readbyte(),
794
block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
797
block_filter_ctx_t *a = opaque;
798
char *buf = (char *)buffer;
799
size_t size = *ret_len;
800
int c, needed, rc = 0;
803
if (control == IOBUFCTRL_UNDERFLOW)
808
assert (size); /* need a buffer */
809
if (a->eof) /* don't read any further */
814
{ /* get the length bytes */
824
/* These OpenPGP introduced huffman like encoded length
825
* bytes are really a mess :-( */
831
else if ((c = iobuf_get (chain)) == -1)
833
log_error ("block_filter: 1st length byte missing\n");
834
rc = GPG_ERR_BAD_DATA;
851
a->size = (c - 192) * 256;
852
if ((c = iobuf_get (chain)) == -1)
855
("block_filter: 2nd length byte missing\n");
856
rc = GPG_ERR_BAD_DATA;
871
a->size = iobuf_get (chain) << 24;
872
a->size |= iobuf_get (chain) << 16;
873
a->size |= iobuf_get (chain) << 8;
874
if ((c = iobuf_get (chain)) == -1)
876
log_error ("block_filter: invalid 4 byte length\n");
877
rc = GPG_ERR_BAD_DATA;
891
{ /* Next partial body length. */
892
a->size = 1 << (c & 0x1f);
894
/* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
900
while (!rc && size && a->size)
902
needed = size < a->size ? size : a->size;
903
c = iobuf_read (chain, p, needed);
909
("block_filter %p: read error (size=%lu,a->size=%lu)\n",
910
a, (ulong) size + c, (ulong) a->size + c);
911
rc = GPG_ERR_BAD_DATA;
924
else if (control == IOBUFCTRL_FLUSH)
927
{ /* the complicated openpgp scheme */
928
size_t blen, n, nbytes = size + a->buflen;
930
assert (a->buflen <= OP_MIN_PARTIAL_CHUNK);
931
if (nbytes < OP_MIN_PARTIAL_CHUNK)
933
/* not enough to write a partial block out; so we store it */
935
a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
936
memcpy (a->buffer + a->buflen, buf, size);
940
{ /* okay, we can write out something */
941
/* do this in a loop to use the most efficient block lengths */
945
/* find the best matching block length - this is limited
946
* by the size of the internal buffering */
947
for (blen = OP_MIN_PARTIAL_CHUNK * 2,
948
c = OP_MIN_PARTIAL_CHUNK_2POW + 1; blen <= nbytes;
953
/* write the partial length header */
954
assert (c <= 0x1f); /*;-) */
956
iobuf_put (chain, c);
958
{ /* write stuff from the buffer */
959
assert (n == OP_MIN_PARTIAL_CHUNK);
960
if (iobuf_write (chain, a->buffer, n))
961
rc = gpg_error_from_syserror ();
965
if ((n = nbytes) > blen)
967
if (n && iobuf_write (chain, p, n))
968
rc = gpg_error_from_syserror ();
972
while (!rc && nbytes >= OP_MIN_PARTIAL_CHUNK);
973
/* store the rest in the buffer */
977
assert (nbytes < OP_MIN_PARTIAL_CHUNK);
979
a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
980
memcpy (a->buffer, p, nbytes);
988
else if (control == IOBUFCTRL_INIT)
991
log_debug ("init block_filter %p\n", a);
994
else if (a->use == 1)
995
a->count = a->size = 0;
997
a->count = a->size; /* force first length bytes */
1002
else if (control == IOBUFCTRL_DESC)
1004
*(char **) buf = "block_filter";
1006
else if (control == IOBUFCTRL_FREE)
1009
{ /* write the end markers */
1013
/* write out the remaining bytes without a partial header
1014
* the length of this header may be 0 - but if it is
1015
* the first block we are not allowed to use a partial header
1016
* and frankly we can't do so, because this length must be
1017
* a power of 2. This is _really_ complicated because we
1018
* have to check the possible length of a packet prior
1019
* to it's creation: a chain of filters becomes complicated
1020
* and we need a lot of code to handle compressed packets etc.
1023
/* construct header */
1025
/*log_debug("partial: remaining length=%u\n", len ); */
1027
rc = iobuf_put (chain, len);
1028
else if (len < 8384)
1030
if (!(rc = iobuf_put (chain, ((len - 192) / 256) + 192)))
1031
rc = iobuf_put (chain, ((len - 192) % 256));
1034
{ /* use a 4 byte header */
1035
if (!(rc = iobuf_put (chain, 0xff)))
1036
if (!(rc = iobuf_put (chain, (len >> 24) & 0xff)))
1037
if (!(rc = iobuf_put (chain, (len >> 16) & 0xff)))
1038
if (!(rc = iobuf_put (chain, (len >> 8) & 0xff)))
1039
rc = iobuf_put (chain, len & 0xff);
1042
rc = iobuf_write (chain, a->buffer, len);
1045
log_error ("block_filter: write error: %s\n",
1047
rc = gpg_error_from_syserror ();
1058
log_error ("block_filter: pending bytes!\n");
1061
log_debug ("free block_filter %p\n", a);
1062
xfree (a); /* we can free our context now */
1070
print_chain (iobuf_t a)
1074
for (; a; a = a->chain)
1076
size_t dummy_len = 0;
1077
const char *desc = "[none]";
1080
a->filter (a->filter_ov, IOBUFCTRL_DESC, NULL,
1081
(byte *) & desc, &dummy_len);
1083
log_debug ("iobuf chain: %d.%d `%s' filter_eof=%d start=%d len=%d\n",
1084
a->no, a->subno, desc?desc:"?", a->filter_eof,
1085
(int) a->d.start, (int) a->d.len);
1090
iobuf_print_chain (iobuf_t a)
1097
* Allocate a new io buffer, with no function assigned.
1098
* Use is the desired usage: 1 for input, 2 for output, 3 for temp buffer
1099
* BUFSIZE is a suggested buffer size.
1102
iobuf_alloc (int use, size_t bufsize)
1105
static int number = 0;
1107
a = xcalloc (1, sizeof *a);
1109
a->d.buf = xmalloc (bufsize);
1110
a->d.size = bufsize;
1114
a->real_fname = NULL;
1119
iobuf_close (iobuf_t a)
1122
size_t dummy_len = 0;
1125
if (a && a->directfp)
1127
fclose (a->directfp);
1128
xfree (a->real_fname);
1130
log_debug ("iobuf_close -> %p\n", a->directfp);
1134
for (; a && !rc; a = a2)
1137
if (a->use == 2 && (rc = iobuf_flush (a)))
1138
log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc));
1141
log_debug ("iobuf-%d.%d: close `%s'\n", a->no, a->subno,
1142
a->desc?a->desc:"?");
1143
if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_FREE,
1144
a->chain, NULL, &dummy_len)))
1145
log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
1146
xfree (a->real_fname);
1149
memset (a->d.buf, 0, a->d.size); /* erase the buffer */
1158
iobuf_cancel (iobuf_t a)
1163
#if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1164
char *remove_name = NULL;
1167
if (a && a->use == 2)
1169
s = iobuf_get_real_fname (a);
1172
#if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1173
remove_name = xstrdup (s);
1180
/* send a cancel message to all filters */
1181
for (a2 = a; a2; a2 = a2->chain)
1185
a2->filter (a2->filter_ov, IOBUFCTRL_CANCEL, a2->chain, NULL, &dummy);
1188
rc = iobuf_close (a);
1189
#if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1192
/* Argg, MSDOS does not allow to remove open files. So
1193
* we have to do it here */
1194
remove (remove_name);
1195
xfree (remove_name);
1203
* create a temporary iobuf, which can be used to collect stuff
1204
* in an iobuf and later be written by iobuf_write_temp() to another
1212
a = iobuf_alloc (3, IOBUF_BUFFER_SIZE);
1218
iobuf_temp_with_content (const char *buffer, size_t length)
1222
a = iobuf_alloc (3, length);
1223
memcpy (a->d.buf, buffer, length);
1230
iobuf_enable_special_filenames (int yes)
1232
special_names_enabled = yes;
1236
/* See whether the filename has the form "-&nnnn", where n is a
1237
non-zero number. Returns this number or -1 if it is not the
1240
check_special_filename (const char *fname)
1242
if (special_names_enabled && fname && *fname == '-' && fname[1] == '&')
1247
for (i = 0; digitp (fname+i); i++)
1250
return atoi (fname);
1256
/* This fucntion returns true if FNAME indicates a PIPE (stdout or
1257
stderr) or a special file name if those are enabled. */
1259
iobuf_is_pipe_filename (const char *fname)
1261
if (!fname || (*fname=='-' && !fname[1]) )
1263
return check_special_filename (fname) != -1;
1267
/* Either open the file specified by the file descriptor FD or - if FD
1268
is -1, the file with name FNAME. As of now MODE is assumed to be
1269
"rb" if FNAME is used. In contrast to iobuf_fdopen the file
1270
descriptor FD will not be closed during an iobuf_close. */
1272
iobuf_open_fd_or_name (gnupg_fd_t fd, const char *fname, const char *mode)
1277
a = iobuf_open (fname);
1286
a = iobuf_fdopen (fd2, mode);
1293
* Create a head iobuf for reading from a file
1294
* returns: NULL if an error occures and sets errno
1297
iobuf_open (const char *fname)
1301
file_filter_ctx_t *fcx;
1306
if (!fname || (*fname == '-' && !fname[1]))
1308
fp = FILEP_OR_FD_FOR_STDIN;
1310
setmode (my_fileno (fp), O_BINARY);
1315
else if ((fd = check_special_filename (fname)) != -1)
1316
return iobuf_fdopen (translate_file_handle (fd, 0), "rb");
1317
else if ((fp = my_fopen_ro (fname, "rb")) == INVALID_FP)
1319
a = iobuf_alloc (1, 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_DESC, NULL, (byte *) & a->desc, &len);
1329
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1331
log_debug ("iobuf-%d.%d: open `%s' fd=%d\n",
1332
a->no, a->subno, fname, (int) my_fileno (fcx->fp));
1338
* Create a head iobuf for reading from a file
1339
* returns: NULL if an error occures and sets errno
1342
iobuf_fdopen (int fd, const char *mode)
1346
file_filter_ctx_t *fcx;
1349
#ifdef FILE_FILTER_USES_STDIO
1350
if (!(fp = fdopen (fd, mode)))
1353
fp = (fp_or_fd_t) fd;
1355
a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE);
1356
fcx = xmalloc (sizeof *fcx + 20);
1358
fcx->print_only_name = 1;
1359
sprintf (fcx->fname, "[fd %d]", fd);
1360
a->filter = file_filter;
1362
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1363
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1365
log_debug ("iobuf-%d.%d: fdopen `%s'\n", a->no, a->subno, fcx->fname);
1366
iobuf_ioctl (a, 3, 1, NULL); /* disable fd caching */
1372
iobuf_sockopen (int fd, const char *mode)
1375
#ifdef HAVE_W32_SYSTEM
1376
sock_filter_ctx_t *scx;
1379
a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE);
1380
scx = xmalloc (sizeof *scx + 25);
1382
scx->print_only_name = 1;
1383
sprintf (scx->fname, "[sock %d]", fd);
1384
a->filter = sock_filter;
1386
sock_filter (scx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1387
sock_filter (scx, IOBUFCTRL_INIT, NULL, NULL, &len);
1389
log_debug ("iobuf-%d.%d: sockopen `%s'\n", a->no, a->subno, scx->fname);
1390
iobuf_ioctl (a, 3, 1, NULL); /* disable fd caching */
1392
a = iobuf_fdopen (fd, mode);
1398
* create an iobuf for writing to a file; the file will be created.
1401
iobuf_create (const char *fname)
1405
file_filter_ctx_t *fcx;
1410
if (!fname || (*fname == '-' && !fname[1]))
1412
fp = FILEP_OR_FD_FOR_STDOUT;
1414
setmode (my_fileno (fp), O_BINARY);
1419
else if ((fd = check_special_filename (fname)) != -1)
1420
return iobuf_fdopen (translate_file_handle (fd, 1), "wb");
1421
else if ((fp = my_fopen (fname, "wb")) == INVALID_FP)
1423
a = iobuf_alloc (2, IOBUF_BUFFER_SIZE);
1424
fcx = xmalloc (sizeof *fcx + strlen (fname));
1426
fcx->print_only_name = print_only;
1427
strcpy (fcx->fname, fname);
1429
a->real_fname = xstrdup (fname);
1430
a->filter = file_filter;
1432
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1433
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1435
log_debug ("iobuf-%d.%d: create `%s'\n", a->no, a->subno,
1436
a->desc?a->desc:"?");
1442
* append to an iobuf; if the file does not exist, create it.
1443
* cannot be used for stdout.
1444
* Note: This is not used.
1446
#if 0 /* not used */
1448
iobuf_append (const char *fname)
1452
file_filter_ctx_t *fcx;
1457
else if (!(fp = my_fopen (fname, "ab")))
1459
a = iobuf_alloc (2, IOBUF_BUFFER_SIZE);
1460
fcx = m_alloc (sizeof *fcx + strlen (fname));
1462
strcpy (fcx->fname, fname);
1463
a->real_fname = m_strdup (fname);
1464
a->filter = file_filter;
1466
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1467
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1469
log_debug ("iobuf-%d.%d: append `%s'\n", a->no, a->subno,
1470
a->desc?a->desc:"?");
1477
iobuf_openrw (const char *fname)
1481
file_filter_ctx_t *fcx;
1486
else if ((fp = my_fopen (fname, "r+b")) == INVALID_FP)
1488
a = iobuf_alloc (2, IOBUF_BUFFER_SIZE);
1489
fcx = xmalloc (sizeof *fcx + strlen (fname));
1491
strcpy (fcx->fname, fname);
1492
a->real_fname = xstrdup (fname);
1493
a->filter = file_filter;
1495
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1496
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1498
log_debug ("iobuf-%d.%d: openrw `%s'\n", a->no, a->subno,
1499
a->desc?a->desc:"?");
1506
iobuf_ioctl (iobuf_t a, int cmd, int intval, void *ptrval)
1509
{ /* keep system filepointer/descriptor open */
1511
log_debug ("iobuf-%d.%d: ioctl `%s' keep=%d\n",
1512
a ? a->no : -1, a ? a->subno : -1,
1513
a && a->desc ? a->desc : "?",
1515
for (; a; a = a->chain)
1516
if (!a->chain && a->filter == file_filter)
1518
file_filter_ctx_t *b = a->filter_ov;
1519
b->keep_open = intval;
1522
#ifdef HAVE_W32_SYSTEM
1523
else if (!a->chain && a->filter == sock_filter)
1525
sock_filter_ctx_t *b = a->filter_ov;
1526
b->keep_open = intval;
1532
{ /* invalidate cache */
1534
log_debug ("iobuf-*.*: ioctl `%s' invalidate\n",
1535
ptrval ? (char *) ptrval : "?");
1536
if (!a && !intval && ptrval)
1538
#ifndef FILE_FILTER_USES_STDIO
1539
if (fd_cache_invalidate (ptrval))
1546
{ /* disallow/allow caching */
1548
log_debug ("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
1549
a ? a->no : -1, a ? a->subno : -1,
1550
a && a->desc? a->desc : "?",
1552
for (; a; a = a->chain)
1553
if (!a->chain && a->filter == file_filter)
1555
file_filter_ctx_t *b = a->filter_ov;
1556
b->no_cache = intval;
1559
#ifdef HAVE_W32_SYSTEM
1560
else if (!a->chain && a->filter == sock_filter)
1562
sock_filter_ctx_t *b = a->filter_ov;
1563
b->no_cache = intval;
1570
/* Do a fsync on the open fd and return any errors to the caller
1571
of iobuf_ioctl. Note that we work on a file name here. */
1573
log_debug ("iobuf-*.*: ioctl `%s' fsync\n",
1574
ptrval? (const char*)ptrval:"<null>");
1576
if (!a && !intval && ptrval)
1578
#ifndef FILE_FILTER_USES_STDIO
1579
return fd_cache_synchronize (ptrval);
1592
* Register an i/o filter.
1595
iobuf_push_filter (iobuf_t a,
1596
int (*f) (void *opaque, int control,
1597
iobuf_t chain, byte * buf, size_t * len),
1600
return iobuf_push_filter2 (a, f, ov, 0);
1604
iobuf_push_filter2 (iobuf_t a,
1605
int (*f) (void *opaque, int control,
1606
iobuf_t chain, byte * buf, size_t * len),
1607
void *ov, int rel_ov)
1610
size_t dummy_len = 0;
1616
if (a->use == 2 && (rc = iobuf_flush (a)))
1618
/* make a copy of the current stream, so that
1619
* A is the new stream and B the original one.
1620
* The contents of the buffers are transferred to the
1623
b = xmalloc (sizeof *b);
1624
memcpy (b, a, sizeof *b);
1625
/* fixme: it is stupid to keep a copy of the name at every level
1626
* but we need the name somewhere because the name known by file_filter
1627
* may have been released when we need the name of the file */
1628
b->real_fname = a->real_fname ? xstrdup (a->real_fname) : NULL;
1629
/* remove the filter stuff from the new stream */
1631
a->filter_ov = NULL;
1632
a->filter_ov_owner = 0;
1635
a->use = 2; /* make a write stream from a temp stream */
1638
{ /* allocate a fresh buffer for the
1640
b->d.buf = xmalloc (a->d.size);
1645
{ /* allocate a fresh buffer for the new
1647
a->d.buf = xmalloc (a->d.size);
1651
/* disable nlimit for the new stream */
1652
a->ntotal = b->ntotal + b->nbytes;
1653
a->nlimit = a->nbytes = 0;
1655
/* make a link from the new stream to the original stream */
1657
a->opaque = b->opaque;
1659
/* setup the function on the new stream */
1662
a->filter_ov_owner = rel_ov;
1664
a->subno = b->subno + 1;
1665
f (ov, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &dummy_len);
1669
log_debug ("iobuf-%d.%d: push `%s'\n", a->no, a->subno,
1670
a->desc?a->desc:"?");
1674
/* now we can initialize the new function if we have one */
1675
if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_INIT, a->chain,
1677
log_error ("IOBUFCTRL_INIT failed: %s\n", gpg_strerror (rc));
1682
* Remove an i/o filter.
1685
pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
1686
iobuf_t chain, byte * buf, size_t * len),
1690
size_t dummy_len = 0;
1697
log_debug ("iobuf-%d.%d: pop `%s'\n", a->no, a->subno,
1698
a->desc?a->desc:"?");
1700
{ /* this is simple */
1704
xfree (a->real_fname);
1705
memcpy (a, b, sizeof *a);
1709
for (b = a; b; b = b->chain)
1710
if (b->filter == f && (!ov || b->filter_ov == ov))
1713
log_bug ("pop_filter(): filter function not found\n");
1715
/* flush this stream if it is an output stream */
1716
if (a->use == 2 && (rc = iobuf_flush (b)))
1718
log_error ("iobuf_flush failed in pop_filter: %s\n", gpg_strerror (rc));
1721
/* and tell the filter to free it self */
1722
if (b->filter && (rc = b->filter (b->filter_ov, IOBUFCTRL_FREE, b->chain,
1725
log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1728
if (b->filter_ov && b->filter_ov_owner)
1730
xfree (b->filter_ov);
1731
b->filter_ov = NULL;
1735
/* and see how to remove it */
1736
if (a == b && !b->chain)
1737
log_bug ("can't remove the last filter from the chain\n");
1739
{ /* remove the first iobuf from the chain */
1740
/* everything from b is copied to a. This is save because
1741
* a flush has been done on the to be removed entry
1745
xfree (a->real_fname);
1746
memcpy (a, b, sizeof *a);
1749
log_debug ("iobuf-%d.%d: popped filter\n", a->no, a->subno);
1752
{ /* remove the last iobuf from the chain */
1753
log_bug ("Ohh jeee, trying to remove a head filter\n");
1756
{ /* remove an intermediate iobuf from the chain */
1757
log_bug ("Ohh jeee, trying to remove an intermediate filter\n");
1765
* read underflow: read more bytes into the buffer and return
1766
* the first byte or -1 on EOF.
1769
underflow (iobuf_t a)
1774
assert (a->d.start == a->d.len);
1776
return -1; /* EOF because a temp buffer can't do an underflow */
1782
iobuf_t b = a->chain;
1784
log_debug ("iobuf-%d.%d: pop `%s' in underflow\n",
1785
a->no, a->subno, a->desc?a->desc:"?");
1787
xfree (a->real_fname);
1788
memcpy (a, b, sizeof *a);
1793
a->filter_eof = 0; /* for the top level filter */
1795
log_debug ("iobuf-%d.%d: underflow: eof (due to filter eof)\n",
1797
return -1; /* return one(!) EOF */
1802
log_debug ("iobuf-%d.%d: error\n", a->no, a->subno);
1808
FILE *fp = a->directfp;
1810
len = fread (a->d.buf, 1, a->d.size, fp);
1811
if (len < a->d.size)
1814
a->error = gpg_error_from_syserror ();
1818
return len ? a->d.buf[a->d.start++] : -1;
1826
log_debug ("iobuf-%d.%d: underflow: req=%lu\n",
1827
a->no, a->subno, (ulong) len);
1828
rc = a->filter (a->filter_ov, IOBUFCTRL_UNDERFLOW, a->chain,
1832
log_debug ("iobuf-%d.%d: underflow: got=%lu rc=%d\n",
1833
a->no, a->subno, (ulong) len, rc);
1834
/* if( a->no == 1 ) */
1835
/* log_hexdump (" data:", a->d.buf, len); */
1837
if (a->use == 1 && rc == -1)
1838
{ /* EOF: we can remove the filter */
1839
size_t dummy_len = 0;
1841
/* and tell the filter to free itself */
1842
if ((rc = a->filter (a->filter_ov, IOBUFCTRL_FREE, a->chain,
1844
log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1845
if (a->filter_ov && a->filter_ov_owner)
1847
xfree (a->filter_ov);
1848
a->filter_ov = NULL;
1852
a->filter_ov = NULL;
1854
if (!len && a->chain)
1856
iobuf_t b = a->chain;
1858
log_debug ("iobuf-%d.%d: pop in underflow (!len)\n",
1861
xfree (a->real_fname);
1862
memcpy (a, b, sizeof *a);
1873
log_debug ("iobuf-%d.%d: underflow: eof\n", a->no, a->subno);
1878
return a->d.buf[a->d.start++];
1883
log_debug ("iobuf-%d.%d: underflow: eof (no filter)\n",
1885
return -1; /* no filter; return EOF */
1891
iobuf_flush (iobuf_t a)
1900
{ /* increase the temp buffer */
1901
unsigned char *newbuf;
1902
size_t newsize = a->d.size + IOBUF_BUFFER_SIZE;
1905
log_debug ("increasing temp iobuf from %lu to %lu\n",
1906
(ulong) a->d.size, (ulong) newsize);
1907
newbuf = xmalloc (newsize);
1908
memcpy (newbuf, a->d.buf, a->d.len);
1911
a->d.size = newsize;
1914
else if (a->use != 2)
1915
log_bug ("flush on non-output iobuf\n");
1916
else if (!a->filter)
1917
log_bug ("iobuf_flush: no filter\n");
1919
rc = a->filter (a->filter_ov, IOBUFCTRL_FLUSH, a->chain, a->d.buf, &len);
1920
if (!rc && len != a->d.len)
1922
log_info ("iobuf_flush did not write all!\n");
1923
rc = GPG_ERR_INTERNAL;
1934
* Read a byte from the iobuf; returns -1 on EOF
1937
iobuf_readbyte (iobuf_t a)
1941
if (a->nlimit && a->nbytes >= a->nlimit)
1942
return -1; /* forced EOF */
1944
if (a->d.start < a->d.len)
1946
c = a->d.buf[a->d.start++];
1948
else if ((c = underflow (a)) == -1)
1949
return -1; /* EOF */
1957
iobuf_read (iobuf_t a, void *buffer, unsigned int buflen)
1959
unsigned char *buf = (unsigned char *)buffer;
1964
/* Handle special cases. */
1965
for (n = 0; n < buflen; n++)
1967
if ((c = iobuf_readbyte (a)) == -1)
1970
return -1; /* eof */
1984
if (n < buflen && a->d.start < a->d.len)
1986
unsigned size = a->d.len - a->d.start;
1987
if (size > buflen - n)
1990
memcpy (buf, a->d.buf + a->d.start, size);
1998
if ((c = underflow (a)) == -1)
2001
return n ? n : -1 /*EOF*/;
2016
* Have a look at the iobuf.
2017
* NOTE: This only works in special cases.
2020
iobuf_peek (iobuf_t a, byte * buf, unsigned buflen)
2027
if (!(a->d.start < a->d.len))
2029
if (underflow (a) == -1)
2031
/* And unget this character. */
2032
assert (a->d.start == 1);
2036
for (n = 0; n < buflen && (a->d.start + n) < a->d.len; n++, buf++)
2045
iobuf_writebyte (iobuf_t a, unsigned int c)
2052
if (a->d.len == a->d.size)
2053
if ((rc=iobuf_flush (a)))
2056
assert (a->d.len < a->d.size);
2057
a->d.buf[a->d.len++] = c;
2063
iobuf_write (iobuf_t a, const void *buffer, unsigned int buflen)
2065
const unsigned char *buf = (const unsigned char *)buffer;
2073
if (buflen && a->d.len < a->d.size)
2075
unsigned size = a->d.size - a->d.len;
2078
memcpy (a->d.buf + a->d.len, buf, size);
2085
rc = iobuf_flush (a);
2096
iobuf_writestr (iobuf_t a, const char *buf)
2101
if ((rc=iobuf_writebyte (a, *buf)))
2109
* copy the contents of TEMP to A.
2112
iobuf_write_temp (iobuf_t a, iobuf_t temp)
2115
pop_filter (temp, temp->filter, NULL);
2116
return iobuf_write (a, temp->d.buf, temp->d.len);
2120
* copy the contents of the temp io stream to BUFFER.
2123
iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen)
2125
size_t n = a->d.len;
2129
memcpy (buffer, a->d.buf, n);
2135
* Call this function to terminate processing of the temp stream
2136
* without closing it. This removes all filters from the stream
2137
* makes sure that iobuf_get_temp_{buffer,length}() returns correct
2141
iobuf_flush_temp (iobuf_t temp)
2144
pop_filter (temp, temp->filter, NULL);
2149
* Set a limit on how many bytes may be read from the input stream A.
2150
* Setting the limit to 0 disables this feature.
2153
iobuf_set_limit (iobuf_t a, off_t nlimit)
2160
a->ntotal += a->nbytes;
2166
/* Return the length of an open file A. IF OVERFLOW is not NULL it
2167
will be set to true if the file is larger than what off_t can cope
2168
with. The function return 0 on error or on overflow condition. */
2170
iobuf_get_filelength (iobuf_t a, int *overflow)
2179
FILE *fp = a->directfp;
2181
if ( !fstat(fileno(fp), &st) )
2183
log_error("fstat() failed: %s\n", strerror(errno) );
2187
/* Hmmm: file_filter may have already been removed */
2188
for ( ; a; a = a->chain )
2189
if ( !a->chain && a->filter == file_filter )
2191
file_filter_ctx_t *b = a->filter_ov;
2192
fp_or_fd_t fp = b->fp;
2194
#if defined(HAVE_W32_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2196
static int (* __stdcall get_file_size_ex) (void *handle,
2197
LARGE_INTEGER *r_size);
2198
static int get_file_size_ex_initialized;
2200
if (!get_file_size_ex_initialized)
2204
handle = dlopen ("kernel32.dll", RTLD_LAZY);
2207
get_file_size_ex = dlsym (handle, "GetFileSizeEx");
2208
if (!get_file_size_ex)
2211
get_file_size_ex_initialized = 1;
2214
if (get_file_size_ex)
2216
/* This is a newer system with GetFileSizeEx; we use this
2217
then because it seem that GetFileSize won't return a
2218
proper error in case a file is larger than 4GB. */
2219
LARGE_INTEGER exsize;
2221
if (get_file_size_ex (fp, &exsize))
2223
if (!exsize.u.HighPart)
2224
return exsize.u.LowPart;
2232
if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
2235
log_error ("GetFileSize for handle %p failed: %s\n",
2236
fp, w32_strerror (0));
2238
if ( !fstat(my_fileno(fp), &st) )
2240
log_error("fstat() failed: %s\n", strerror(errno) );
2242
break/*the for loop*/;
2249
/* Return the file descriptor of the underlying file or -1 if it is
2252
iobuf_get_fd (iobuf_t a)
2255
return fileno ( (FILE*)a->directfp );
2257
for ( ; a; a = a->chain )
2258
if (!a->chain && a->filter == file_filter)
2260
file_filter_ctx_t *b = a->filter_ov;
2261
fp_or_fd_t fp = b->fp;
2263
return my_fileno (fp);
2272
* Tell the file position, where the next read will take place
2275
iobuf_tell (iobuf_t a)
2277
return a->ntotal + a->nbytes;
2281
#if !defined(HAVE_FSEEKO) && !defined(fseeko)
2283
#ifdef HAVE_LIMITS_H
2284
# include <limits.h>
2287
# define LONG_MAX ((long) ((unsigned long) -1 >> 1))
2290
# define LONG_MIN (-1 - LONG_MAX)
2294
* A substitute for fseeko, for hosts that don't have it.
2297
fseeko (FILE * stream, off_t newpos, int whence)
2299
while (newpos != (long) newpos)
2301
long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
2302
if (fseek (stream, pos, whence) != 0)
2307
return fseek (stream, (long) newpos, whence);
2312
* This is a very limited implementation. It simply discards all internal
2313
* buffering and removes all filters but the first one.
2316
iobuf_seek (iobuf_t a, off_t newpos)
2318
file_filter_ctx_t *b = NULL;
2322
FILE *fp = a->directfp;
2323
if (fseeko (fp, newpos, SEEK_SET))
2325
log_error ("can't seek: %s\n", strerror (errno));
2332
for (; a; a = a->chain)
2334
if (!a->chain && a->filter == file_filter)
2342
#ifdef FILE_FILTER_USES_STDIO
2343
if (fseeko (b->fp, newpos, SEEK_SET))
2345
log_error ("can't fseek: %s\n", strerror (errno));
2349
#ifdef HAVE_W32_SYSTEM
2350
if (SetFilePointer (b->fp, newpos, NULL, FILE_BEGIN) == 0xffffffff)
2352
log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2353
b->fp, (int) GetLastError ());
2357
if (lseek (b->fp, newpos, SEEK_SET) == (off_t) - 1)
2359
log_error ("can't lseek: %s\n", strerror (errno));
2365
a->d.len = 0; /* discard buffer */
2372
/* remove filters, but the last */
2374
log_debug ("pop_filter called in iobuf_seek - please report\n");
2376
pop_filter (a, a->filter, NULL);
2387
* Retrieve the real filename
2390
iobuf_get_real_fname (iobuf_t a)
2393
return a->real_fname;
2395
/* the old solution */
2396
for (; a; a = a->chain)
2397
if (!a->chain && a->filter == file_filter)
2399
file_filter_ctx_t *b = a->filter_ov;
2400
return b->print_only_name ? NULL : b->fname;
2408
* Retrieve the filename
2411
iobuf_get_fname (iobuf_t a)
2413
for (; a; a = a->chain)
2414
if (!a->chain && a->filter == file_filter)
2416
file_filter_ctx_t *b = a->filter_ov;
2424
* enable partial block mode as described in the OpenPGP draft.
2425
* LEN is the first length byte on read, but ignored on writes.
2428
iobuf_set_partial_block_mode (iobuf_t a, size_t len)
2430
block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
2432
assert (a->use == 1 || a->use == 2);
2437
log_debug ("pop_filter called in set_partial_block_mode"
2438
" - please report\n");
2439
pop_filter (a, block_filter, NULL);
2446
iobuf_push_filter (a, block_filter, ctx);
2453
* Same as fgets() but if the buffer is too short a larger one will
2454
* be allocated up to some limit *max_length.
2455
* A line is considered a byte stream ending in a LF.
2456
* Returns the length of the line. EOF is indicated by a line of
2457
* length zero. The last LF may be missing due to an EOF.
2458
* is max_length is zero on return, the line has been truncated.
2460
* Note: The buffer is allocated with enough space to append a CR,LF,EOL
2463
iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
2464
unsigned *length_of_buffer, unsigned *max_length)
2467
char *buffer = (char *)*addr_of_buffer;
2468
unsigned length = *length_of_buffer;
2469
unsigned nbytes = 0;
2470
unsigned maxlen = *max_length;
2474
{ /* must allocate a new buffer */
2476
buffer = xmalloc (length);
2477
*addr_of_buffer = (unsigned char *)buffer;
2478
*length_of_buffer = length;
2481
length -= 3; /* reserve 3 bytes (cr,lf,eol) */
2483
while ((c = iobuf_get (a)) != -1)
2485
if (nbytes == length)
2486
{ /* increase the buffer */
2487
if (length > maxlen)
2488
{ /* this is out limit */
2489
/* skip the rest of the line */
2490
while (c != '\n' && (c = iobuf_get (a)) != -1)
2492
*p++ = '\n'; /* always append a LF (we have reserved space) */
2494
*max_length = 0; /* indicate truncation */
2497
length += 3; /* correct for the reserved byte */
2498
length += length < 1024 ? 256 : 1024;
2499
buffer = xrealloc (buffer, length);
2500
*addr_of_buffer = (unsigned char *)buffer;
2501
*length_of_buffer = length;
2502
length -= 3; /* and reserve again */
2503
p = buffer + nbytes;
2510
*p = 0; /* make sure the line is a string */
2516
translate_file_handle (int fd, int for_write)
2518
#ifdef HAVE_W32_SYSTEM
2519
# ifdef FILE_FILTER_USES_STDIO
2520
fd = translate_sys2libc_fd (fd, for_write);
2528
x = (int) GetStdHandle (STD_INPUT_HANDLE);
2530
x = (int) GetStdHandle (STD_OUTPUT_HANDLE);
2532
x = (int) GetStdHandle (STD_ERROR_HANDLE);
2537
log_debug ("GetStdHandle(%d) failed: ec=%d\n",
2538
fd, (int) GetLastError ());
2551
iobuf_skip_rest (iobuf_t a, unsigned long n, int partial)
2557
if (a->nofast || a->d.start >= a->d.len)
2559
if (iobuf_readbyte (a) == -1)
2566
unsigned long count = a->d.len - a->d.start;
2568
a->d.start = a->d.len;
2574
unsigned long remaining = n;
2575
while (remaining > 0)
2577
if (a->nofast || a->d.start >= a->d.len)
2579
if (iobuf_readbyte (a) == -1)
2587
unsigned long count = a->d.len - a->d.start;
2588
if (count > remaining)
2593
a->d.start += count;