1
/* iobuf.c - file handling
2
* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28
#include <sys/types.h>
32
#ifdef HAVE_DOSISH_SYSTEM
38
#endif /* __riscos__ */
44
#undef FILE_FILTER_USES_STDIO
46
#ifdef HAVE_DOSISH_SYSTEM
50
#ifdef FILE_FILTER_USES_STDIO
51
#define my_fileno(a) fileno ((a))
52
#define my_fopen_ro(a,b) fopen ((a),(b))
53
#define my_fopen(a,b) fopen ((a),(b))
54
typedef FILE *FILEP_OR_FD;
55
#define INVALID_FP NULL
56
#define FILEP_OR_FD_FOR_STDIN (stdin)
57
#define FILEP_OR_FD_FOR_STDOUT (stdout)
60
FILE *fp; /* open file handle */
63
int print_only_name; /* flags indicating that fname is not a real file */
64
char fname[1]; /* name of the file */
68
#define my_fileno(a) (a)
69
#define my_fopen_ro(a,b) fd_cache_open ((a),(b))
70
#define my_fopen(a,b) direct_open ((a),(b))
71
#ifdef HAVE_DOSISH_SYSTEM
72
typedef HANDLE FILEP_OR_FD;
73
#define INVALID_FP ((HANDLE)-1)
74
#define FILEP_OR_FD_FOR_STDIN (GetStdHandle (STD_INPUT_HANDLE))
75
#define FILEP_OR_FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
78
typedef int FILEP_OR_FD;
79
#define INVALID_FP (-1)
80
#define FILEP_OR_FD_FOR_STDIN (0)
81
#define FILEP_OR_FD_FOR_STDOUT (1)
85
FILEP_OR_FD fp; /* open file handle */
89
int print_only_name; /* flags indicating that fname is not a real file */
90
char fname[1]; /* name of the file */
96
struct close_cache_s *next;
100
typedef struct close_cache_s *CLOSE_CACHE;
101
static CLOSE_CACHE close_cache;
111
int print_only_name; /* flags indicating that fname is not a real file */
112
char fname[1]; /* name of the file */
117
/* The first partial length header block must be of size 512
118
* to make it easier (and efficienter) we use a min. block size of 512
119
* for all chunks (but the last one) */
120
#define OP_MIN_PARTIAL_CHUNK 512
121
#define OP_MIN_PARTIAL_CHUNK_2POW 9
128
int partial; /* 1 = partial header, 2 in last partial packet */
129
char *buffer; /* used for partial header */
130
size_t buflen; /* used size of buffer */
131
int first_c; /* of partial header (which is > 0) */
136
static int special_names_enabled;
138
static int underflow (iobuf_t a);
139
static int translate_file_handle (int fd, int for_write);
141
#ifndef FILE_FILTER_USES_STDIO
144
* Invalidate (i.e. close) a cached iobuf
147
fd_cache_invalidate (const char *fname)
153
log_debug ("fd_cache_invalidate (%s)\n", fname);
155
for (cc = close_cache; cc; cc = cc->next)
157
if (cc->fp != INVALID_FP && !strcmp (cc->fname, fname))
160
log_debug (" did (%s)\n", cc->fname);
161
#ifdef HAVE_DOSISH_SYSTEM
162
CloseHandle (cc->fp);
174
direct_open (const char *fname, const char *mode)
176
#ifdef HAVE_DOSISH_SYSTEM
177
unsigned long da, cd, sm;
180
/* Note, that we do not handle all mode combinations */
182
/* According to the ReactOS source it seems that open() of the
183
* standard MSW32 crt does open the file in share mode which is
184
* something new for MS applications ;-)
186
if (strchr (mode, '+'))
188
fd_cache_invalidate (fname);
189
da = GENERIC_READ | GENERIC_WRITE;
191
sm = FILE_SHARE_READ | FILE_SHARE_WRITE;
193
else if (strchr (mode, 'w'))
195
fd_cache_invalidate (fname);
198
sm = FILE_SHARE_WRITE;
204
sm = FILE_SHARE_READ;
207
hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL);
211
int cflag = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
213
/* Note, that we do not handle all mode combinations */
214
if (strchr (mode, '+'))
216
fd_cache_invalidate (fname);
219
else if (strchr (mode, 'w'))
221
fd_cache_invalidate (fname);
222
oflag = O_WRONLY | O_CREAT | O_TRUNC;
229
if (strchr (mode, 'b'))
233
return open (fname, oflag, cflag);
237
int rc = stat (fname, &buf);
239
/* Don't allow iobufs on directories */
240
if (!rc && S_ISDIR (buf.st_mode) && !S_ISREG (buf.st_mode))
241
return __set_errno (EISDIR);
243
return open (fname, oflag, cflag);
251
* Instead of closing an FD we keep it open and cache it for later reuse
252
* Note that this caching strategy only works if the process does not chdir.
255
fd_cache_close (const char *fname, FILEP_OR_FD fp)
260
if (!fname || !*fname)
262
#ifdef HAVE_DOSISH_SYSTEM
268
log_debug ("fd_cache_close (%p) real\n", (void *) fp);
271
/* try to reuse a slot */
272
for (cc = close_cache; cc; cc = cc->next)
274
if (cc->fp == INVALID_FP && !strcmp (cc->fname, fname))
278
log_debug ("fd_cache_close (%s) used existing slot\n", fname);
284
log_debug ("fd_cache_close (%s) new slot created\n", fname);
285
cc = xcalloc (1, sizeof *cc + strlen (fname));
286
strcpy (cc->fname, fname);
288
cc->next = close_cache;
293
* Do an direct_open on FNAME but first try to reuse one from the fd_cache
296
fd_cache_open (const char *fname, const char *mode)
301
for (cc = close_cache; cc; cc = cc->next)
303
if (cc->fp != INVALID_FP && !strcmp (cc->fname, fname))
305
FILEP_OR_FD fp = cc->fp;
308
log_debug ("fd_cache_open (%s) using cached fp\n", fname);
309
#ifdef HAVE_DOSISH_SYSTEM
310
if (SetFilePointer (fp, 0, NULL, FILE_BEGIN) == 0xffffffff)
312
log_error ("rewind file failed on handle %p: ec=%d\n",
313
fp, (int) GetLastError ());
317
if (lseek (fp, 0, SEEK_SET) == (off_t) - 1)
319
log_error ("can't rewind fd %d: %s\n", fp, strerror (errno));
327
log_debug ("fd_cache_open (%s) not cached\n", fname);
328
return direct_open (fname, mode);
332
#endif /*FILE_FILTER_USES_STDIO */
336
* Read data from a file into buf which has an allocated length of *LEN.
337
* return the number of read bytes in *LEN. OPAQUE is the FILE * of
338
* the stream. A is not used.
340
* IOBUFCTRL_INIT: called just before the function is linked into the
341
* list of function. This can be used to prepare internal
342
* data structures of the function.
343
* IOBUFCTRL_FREE: called just before the function is removed from the
344
* list of functions and can be used to release internal
345
* data structures or close a file etc.
346
* IOBUFCTRL_UNDERFLOW: called by iobuf_underflow to fill the buffer
347
* with new stuff. *RET_LEN is the available size of the
348
* buffer, and should be set to the number of bytes
349
* which were put into the buffer. The function
350
* returns 0 to indicate success, -1 on EOF and
351
* GPG_ERR_xxxxx for other errors.
353
* IOBUFCTRL_FLUSH: called by iobuf_flush() to write out the collected stuff.
354
* *RET_LAN is the number of bytes in BUF.
356
* IOBUFCTRL_CANCEL: send to all filters on behalf of iobuf_cancel. The
357
* filter may take appropriate action on this message.
360
file_filter (void *opaque, int control, iobuf_t chain, byte * buf,
363
file_filter_ctx_t *a = opaque;
364
FILEP_OR_FD f = a->fp;
365
size_t size = *ret_len;
369
#ifdef FILE_FILTER_USES_STDIO
370
if (control == IOBUFCTRL_UNDERFLOW)
372
assert (size); /* need a buffer */
374
{ /* On terminals you could easiely read as many EOFs as you call */
375
rc = -1; /* fread() or fgetc() repeatly. Every call will block until you press */
376
*ret_len = 0; /* CTRL-D. So we catch this case before we call fread() again. */
381
nbytes = fread (buf, 1, size, f);
382
if (feof (f) && !nbytes)
384
rc = -1; /* okay: we can return EOF now. */
386
else if (ferror (f) && errno != EPIPE)
388
rc = gpg_error_from_errno (errno);
389
log_error ("%s: read error: %s\n", a->fname, strerror (errno));
394
else if (control == IOBUFCTRL_FLUSH)
399
nbytes = fwrite (buf, 1, size, f);
402
rc = gpg_error_from_errno (errno);
403
log_error ("%s: write error: %s\n", a->fname, strerror (errno));
408
else if (control == IOBUFCTRL_INIT)
410
a->keep_open = a->no_cache = 0;
412
else if (control == IOBUFCTRL_DESC)
414
*(char **) buf = "file_filter";
416
else if (control == IOBUFCTRL_FREE)
418
if (f != stdin && f != stdout)
421
log_debug ("%s: close fd %d\n", a->fname, fileno (f));
426
xfree (a); /* we can free our context now */
428
#else /* !stdio implementation */
430
if (control == IOBUFCTRL_UNDERFLOW)
432
assert (size); /* need a buffer */
440
#ifdef HAVE_DOSISH_SYSTEM
444
if (!ReadFile (f, buf, size, &nread, NULL))
446
int ec = (int) GetLastError ();
447
if (ec != ERROR_BROKEN_PIPE)
449
rc = gpg_error_from_errno (ec);
450
log_error ("%s: read error: ec=%d\n", a->fname, ec);
470
n = read (f, buf, size);
472
while (n == -1 && errno == EINTR);
477
rc = gpg_error_from_errno (errno);
478
log_error ("%s: read error: %s\n",
479
a->fname, strerror (errno));
495
else if (control == IOBUFCTRL_FLUSH)
499
#ifdef HAVE_DOSISH_SYSTEM
506
if (size && !WriteFile (f, p, nbytes, &n, NULL))
508
int ec = (int) GetLastError ();
509
rc = gpg_error_from_errno (ec);
510
log_error ("%s: write error: ec=%d\n", a->fname, ec);
527
n = write (f, p, nbytes);
529
while (n == -1 && errno == EINTR);
536
while (n != -1 && nbytes);
539
rc = gpg_error_from_errno (errno);
540
log_error ("%s: write error: %s\n", a->fname, strerror (errno));
547
else if (control == IOBUFCTRL_INIT)
553
else if (control == IOBUFCTRL_DESC)
555
*(char **) buf = "file_filter(fd)";
557
else if (control == IOBUFCTRL_FREE)
559
#ifdef HAVE_DOSISH_SYSTEM
560
if (f != FILEP_OR_FD_FOR_STDIN && f != FILEP_OR_FD_FOR_STDOUT)
563
log_debug ("%s: close handle %p\n", a->fname, f);
565
fd_cache_close (a->no_cache ? NULL : a->fname, f);
568
if ((int) f != 0 && (int) f != 1)
571
log_debug ("%s: close fd %d\n", a->fname, f);
573
fd_cache_close (a->no_cache ? NULL : a->fname, f);
577
xfree (a); /* we can free our context now */
579
#endif /* !stdio implementation */
584
/* Becuase sockets are an special object under Lose32 we have to
585
* use a special filter */
587
sock_filter (void *opaque, int control, iobuf_t chain, byte * buf,
590
sock_filter_ctx_t *a = opaque;
591
size_t size = *ret_len;
595
if (control == IOBUFCTRL_UNDERFLOW)
597
assert (size); /* need a buffer */
607
nread = recv (a->sock, buf, size, 0);
608
if (nread == SOCKET_ERROR)
610
int ec = (int) WSAGetLastError ();
611
rc = gpg_error_from_errno (ec);
612
log_error ("socket read error: ec=%d\n", ec);
626
else if (control == IOBUFCTRL_FLUSH)
636
n = send (a->sock, p, nbytes, 0);
637
if (n == SOCKET_ERROR)
639
int ec = (int) WSAGetLastError ();
640
rc = gpg_error_from_errno (ec);
641
log_error ("socket write error: ec=%d\n", ec);
652
else if (control == IOBUFCTRL_INIT)
658
else if (control == IOBUFCTRL_DESC)
660
*(char **) buf = "sock_filter";
662
else if (control == IOBUFCTRL_FREE)
665
closesocket (a->sock);
666
xfree (a); /* we can free our context now */
673
* This is used to implement the block write mode.
674
* Block reading is done on a byte by byte basis in readbyte(),
678
block_filter (void *opaque, int control, iobuf_t chain, byte * buf,
681
block_filter_ctx_t *a = opaque;
682
size_t size = *ret_len;
683
int c, needed, rc = 0;
686
if (control == IOBUFCTRL_UNDERFLOW)
691
assert (size); /* need a buffer */
692
if (a->eof) /* don't read any further */
697
{ /* get the length bytes */
707
/* These OpenPGP introduced huffman like encoded length
708
* bytes are really a mess :-( */
714
else if ((c = iobuf_get (chain)) == -1)
716
log_error ("block_filter: 1st length byte missing\n");
717
rc = GPG_ERR_BAD_DATA;
734
a->size = (c - 192) * 256;
735
if ((c = iobuf_get (chain)) == -1)
738
("block_filter: 2nd length byte missing\n");
739
rc = GPG_ERR_BAD_DATA;
754
a->size = iobuf_get (chain) << 24;
755
a->size |= iobuf_get (chain) << 16;
756
a->size |= iobuf_get (chain) << 8;
757
if ((c = iobuf_get (chain)) == -1)
759
log_error ("block_filter: invalid 4 byte length\n");
760
rc = GPG_ERR_BAD_DATA;
766
{ /* next partial body length */
767
a->size = 1 << (c & 0x1f);
769
/* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
772
{ /* the gnupg partial length scheme - much better :-) */
773
c = iobuf_get (chain);
775
c = iobuf_get (chain);
779
log_error ("block_filter: error reading length info\n");
780
rc = GPG_ERR_BAD_DATA;
792
while (!rc && size && a->size)
794
needed = size < a->size ? size : a->size;
795
c = iobuf_read (chain, p, needed);
801
("block_filter %p: read error (size=%lu,a->size=%lu)\n",
802
a, (ulong) size + c, (ulong) a->size + c);
803
rc = GPG_ERR_BAD_DATA;
816
else if (control == IOBUFCTRL_FLUSH)
819
{ /* the complicated openpgp scheme */
820
size_t blen, n, nbytes = size + a->buflen;
822
assert (a->buflen <= OP_MIN_PARTIAL_CHUNK);
823
if (nbytes < OP_MIN_PARTIAL_CHUNK)
825
/* not enough to write a partial block out; so we store it */
827
a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
828
memcpy (a->buffer + a->buflen, buf, size);
832
{ /* okay, we can write out something */
833
/* do this in a loop to use the most efficient block lengths */
837
/* find the best matching block length - this is limited
838
* by the size of the internal buffering */
839
for (blen = OP_MIN_PARTIAL_CHUNK * 2,
840
c = OP_MIN_PARTIAL_CHUNK_2POW + 1; blen <= nbytes;
845
/* write the partial length header */
846
assert (c <= 0x1f); /*;-) */
848
iobuf_put (chain, c);
850
{ /* write stuff from the buffer */
851
assert (n == OP_MIN_PARTIAL_CHUNK);
852
if (iobuf_write (chain, a->buffer, n))
853
rc = gpg_error_from_errno (errno);
857
if ((n = nbytes) > blen)
859
if (n && iobuf_write (chain, p, n))
860
rc = gpg_error_from_errno (errno);
864
while (!rc && nbytes >= OP_MIN_PARTIAL_CHUNK);
865
/* store the rest in the buffer */
869
assert (nbytes < OP_MIN_PARTIAL_CHUNK);
871
a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
872
memcpy (a->buffer, p, nbytes);
878
{ /* the gnupg scheme (which is not openpgp compliant) */
881
for (p = buf; !rc && size;)
884
avail = a->size - a->count;
889
iobuf_put (chain, (a->size >> 8) & 0xff);
890
iobuf_put (chain, a->size & 0xff);
896
iobuf_put (chain, (n >> 8) & 0xff);
897
iobuf_put (chain, n & 0xff);
899
a->count = a->size - n;
904
if (iobuf_write (chain, p, n))
905
rc = gpg_error_from_errno (errno);
912
else if (control == IOBUFCTRL_INIT)
915
log_debug ("init block_filter %p\n", a);
918
else if (a->use == 1)
919
a->count = a->size = 0;
921
a->count = a->size; /* force first length bytes */
926
else if (control == IOBUFCTRL_DESC)
928
*(char **) buf = "block_filter";
930
else if (control == IOBUFCTRL_FREE)
933
{ /* write the end markers */
937
/* write out the remaining bytes without a partial header
938
* the length of this header may be 0 - but if it is
939
* the first block we are not allowed to use a partial header
940
* and frankly we can't do so, because this length must be
941
* a power of 2. This is _really_ complicated because we
942
* have to check the possible length of a packet prior
943
* to it's creation: a chain of filters becomes complicated
944
* and we need a lot of code to handle compressed packets etc.
947
/* construct header */
949
/*log_debug("partial: remaining length=%u\n", len ); */
951
rc = iobuf_put (chain, len);
954
if (!(rc = iobuf_put (chain, ((len - 192) / 256) + 192)))
955
rc = iobuf_put (chain, ((len - 192) % 256));
958
{ /* use a 4 byte header */
959
if (!(rc = iobuf_put (chain, 0xff)))
960
if (!(rc = iobuf_put (chain, (len >> 24) & 0xff)))
961
if (!(rc = iobuf_put (chain, (len >> 16) & 0xff)))
962
if (!(rc = iobuf_put (chain, (len >> 8) & 0xff)))
963
rc = iobuf_put (chain, len & 0xff);
966
rc = iobuf_write (chain, a->buffer, len);
969
log_error ("block_filter: write error: %s\n",
971
rc = gpg_error_from_errno (errno);
979
iobuf_writebyte (chain, 0);
980
iobuf_writebyte (chain, 0);
985
log_error ("block_filter: pending bytes!\n");
988
log_debug ("free block_filter %p\n", a);
989
xfree (a); /* we can free our context now */
997
print_chain (iobuf_t a)
1001
for (; a; a = a->chain)
1003
size_t dummy_len = 0;
1004
const char *desc = "[none]";
1007
a->filter (a->filter_ov, IOBUFCTRL_DESC, NULL,
1008
(byte *) & desc, &dummy_len);
1010
log_debug ("iobuf chain: %d.%d `%s' filter_eof=%d start=%d len=%d\n",
1011
a->no, a->subno, desc, a->filter_eof,
1012
(int) a->d.start, (int) a->d.len);
1017
iobuf_print_chain (iobuf_t a)
1024
* Allocate a new io buffer, with no function assigned.
1025
* Use is the desired usage: 1 for input, 2 for output, 3 for temp buffer
1026
* BUFSIZE is a suggested buffer size.
1029
iobuf_alloc (int use, size_t bufsize)
1032
static int number = 0;
1034
a = xcalloc (1, sizeof *a);
1036
a->d.buf = xmalloc (bufsize);
1037
a->d.size = bufsize;
1041
a->real_fname = NULL;
1046
iobuf_close (iobuf_t a)
1049
size_t dummy_len = 0;
1052
if (a && a->directfp)
1054
fclose (a->directfp);
1055
xfree (a->real_fname);
1057
log_debug ("iobuf_close -> %p\n", a->directfp);
1061
for (; a && !rc; a = a2)
1064
if (a->use == 2 && (rc = iobuf_flush (a)))
1065
log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc));
1068
log_debug ("iobuf-%d.%d: close `%s'\n", a->no, a->subno, a->desc);
1069
if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_FREE,
1070
a->chain, NULL, &dummy_len)))
1071
log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
1072
xfree (a->real_fname);
1075
memset (a->d.buf, 0, a->d.size); /* erase the buffer */
1084
iobuf_cancel (iobuf_t a)
1089
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1090
char *remove_name = NULL;
1093
if (a && a->use == 2)
1095
s = iobuf_get_real_fname (a);
1098
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1099
remove_name = xstrdup (s);
1106
/* send a cancel message to all filters */
1107
for (a2 = a; a2; a2 = a2->chain)
1111
a2->filter (a2->filter_ov, IOBUFCTRL_CANCEL, a2->chain, NULL, &dummy);
1114
rc = iobuf_close (a);
1115
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1118
/* Argg, MSDOS does not allow to remove open files. So
1119
* we have to do it here */
1120
remove (remove_name);
1121
xfree (remove_name);
1129
* create a temporary iobuf, which can be used to collect stuff
1130
* in an iobuf and later be written by iobuf_write_temp() to another
1138
a = iobuf_alloc (3, 8192);
1144
iobuf_temp_with_content (const char *buffer, size_t length)
1148
a = iobuf_alloc (3, length);
1149
memcpy (a->d.buf, buffer, length);
1156
iobuf_enable_special_filenames (int yes)
1158
special_names_enabled = yes;
1162
* see whether the filename has the for "-&nnnn", where n is a
1164
* Returns this number or -1 if it is not the case.
1167
check_special_filename (const char *fname)
1169
if (special_names_enabled && fname && *fname == '-' && fname[1] == '&')
1174
for (i = 0; digitp (fname+i); i++)
1177
return atoi (fname);
1183
* Create a head iobuf for reading from a file
1184
* returns: NULL if an error occures and sets errno
1187
iobuf_open (const char *fname)
1191
file_filter_ctx_t *fcx;
1196
if (!fname || (*fname == '-' && !fname[1]))
1198
fp = FILEP_OR_FD_FOR_STDIN;
1200
setmode (my_fileno (fp), O_BINARY);
1205
else if ((fd = check_special_filename (fname)) != -1)
1206
return iobuf_fdopen (translate_file_handle (fd, 0), "rb");
1207
else if ((fp = my_fopen_ro (fname, "rb")) == INVALID_FP)
1209
a = iobuf_alloc (1, 8192);
1210
fcx = xmalloc (sizeof *fcx + strlen (fname));
1212
fcx->print_only_name = print_only;
1213
strcpy (fcx->fname, fname);
1215
a->real_fname = xstrdup (fname);
1216
a->filter = file_filter;
1218
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1219
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1221
log_debug ("iobuf-%d.%d: open `%s' fd=%d\n",
1222
a->no, a->subno, fname, (int) my_fileno (fcx->fp));
1228
* Create a head iobuf for reading from a file
1229
* returns: NULL if an error occures and sets errno
1232
iobuf_fdopen (int fd, const char *mode)
1236
file_filter_ctx_t *fcx;
1239
#ifdef FILE_FILTER_USES_STDIO
1240
if (!(fp = fdopen (fd, mode)))
1243
fp = (FILEP_OR_FD) fd;
1245
a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, 8192);
1246
fcx = xmalloc (sizeof *fcx + 20);
1248
fcx->print_only_name = 1;
1249
sprintf (fcx->fname, "[fd %d]", fd);
1250
a->filter = file_filter;
1252
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1253
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1255
log_debug ("iobuf-%d.%d: fdopen `%s'\n", a->no, a->subno, fcx->fname);
1256
iobuf_ioctl (a, 3, 1, NULL); /* disable fd caching */
1262
iobuf_sockopen (int fd, const char *mode)
1266
sock_filter_ctx_t *scx;
1269
a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, 8192);
1270
scx = xmalloc (sizeof *scx + 25);
1272
scx->print_only_name = 1;
1273
sprintf (scx->fname, "[sock %d]", fd);
1274
a->filter = sock_filter;
1276
sock_filter (scx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1277
sock_filter (scx, IOBUFCTRL_INIT, NULL, NULL, &len);
1279
log_debug ("iobuf-%d.%d: sockopen `%s'\n", a->no, a->subno, scx->fname);
1280
iobuf_ioctl (a, 3, 1, NULL); /* disable fd caching */
1282
a = iobuf_fdopen (fd, mode);
1288
* create an iobuf for writing to a file; the file will be created.
1291
iobuf_create (const char *fname)
1295
file_filter_ctx_t *fcx;
1300
if (!fname || (*fname == '-' && !fname[1]))
1302
fp = FILEP_OR_FD_FOR_STDOUT;
1304
setmode (my_fileno (fp), O_BINARY);
1309
else if ((fd = check_special_filename (fname)) != -1)
1310
return iobuf_fdopen (translate_file_handle (fd, 1), "wb");
1311
else if ((fp = my_fopen (fname, "wb")) == INVALID_FP)
1313
a = iobuf_alloc (2, 8192);
1314
fcx = xmalloc (sizeof *fcx + strlen (fname));
1316
fcx->print_only_name = print_only;
1317
strcpy (fcx->fname, fname);
1319
a->real_fname = xstrdup (fname);
1320
a->filter = file_filter;
1322
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1323
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1325
log_debug ("iobuf-%d.%d: create `%s'\n", a->no, a->subno, a->desc);
1331
* append to an iobuf; if the file does not exist, create it.
1332
* cannot be used for stdout.
1333
* Note: This is not used.
1335
#if 0 /* not used */
1337
iobuf_append (const char *fname)
1341
file_filter_ctx_t *fcx;
1346
else if (!(fp = my_fopen (fname, "ab")))
1348
a = iobuf_alloc (2, 8192);
1349
fcx = m_alloc (sizeof *fcx + strlen (fname));
1351
strcpy (fcx->fname, fname);
1352
a->real_fname = m_strdup (fname);
1353
a->filter = file_filter;
1355
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1356
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1358
log_debug ("iobuf-%d.%d: append `%s'\n", a->no, a->subno, a->desc);
1365
iobuf_openrw (const char *fname)
1369
file_filter_ctx_t *fcx;
1374
else if ((fp = my_fopen (fname, "r+b")) == INVALID_FP)
1376
a = iobuf_alloc (2, 8192);
1377
fcx = xmalloc (sizeof *fcx + strlen (fname));
1379
strcpy (fcx->fname, fname);
1380
a->real_fname = xstrdup (fname);
1381
a->filter = file_filter;
1383
file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1384
file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1386
log_debug ("iobuf-%d.%d: openrw `%s'\n", a->no, a->subno, a->desc);
1393
iobuf_ioctl (iobuf_t a, int cmd, int intval, void *ptrval)
1396
{ /* keep system filepointer/descriptor open */
1398
log_debug ("iobuf-%d.%d: ioctl `%s' keep=%d\n",
1399
a ? a->no : -1, a ? a->subno : -1, a ? a->desc : "?",
1401
for (; a; a = a->chain)
1402
if (!a->chain && a->filter == file_filter)
1404
file_filter_ctx_t *b = a->filter_ov;
1405
b->keep_open = intval;
1409
else if (!a->chain && a->filter == sock_filter)
1411
sock_filter_ctx_t *b = a->filter_ov;
1412
b->keep_open = intval;
1418
{ /* invalidate cache */
1420
log_debug ("iobuf-*.*: ioctl `%s' invalidate\n",
1421
ptrval ? (char *) ptrval : "?");
1422
if (!a && !intval && ptrval)
1424
#ifndef FILE_FILTER_USES_STDIO
1425
fd_cache_invalidate (ptrval);
1431
{ /* disallow/allow caching */
1433
log_debug ("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
1434
a ? a->no : -1, a ? a->subno : -1, a ? a->desc : "?",
1436
for (; a; a = a->chain)
1437
if (!a->chain && a->filter == file_filter)
1439
file_filter_ctx_t *b = a->filter_ov;
1440
b->no_cache = intval;
1444
else if (!a->chain && a->filter == sock_filter)
1446
sock_filter_ctx_t *b = a->filter_ov;
1447
b->no_cache = intval;
1458
* Register an i/o filter.
1461
iobuf_push_filter (iobuf_t a,
1462
int (*f) (void *opaque, int control,
1463
iobuf_t chain, byte * buf, size_t * len),
1466
return iobuf_push_filter2 (a, f, ov, 0);
1470
iobuf_push_filter2 (iobuf_t a,
1471
int (*f) (void *opaque, int control,
1472
iobuf_t chain, byte * buf, size_t * len),
1473
void *ov, int rel_ov)
1476
size_t dummy_len = 0;
1482
if (a->use == 2 && (rc = iobuf_flush (a)))
1484
/* make a copy of the current stream, so that
1485
* A is the new stream and B the original one.
1486
* The contents of the buffers are transferred to the
1489
b = xmalloc (sizeof *b);
1490
memcpy (b, a, sizeof *b);
1491
/* fixme: it is stupid to keep a copy of the name at every level
1492
* but we need the name somewhere because the name known by file_filter
1493
* may have been released when we need the name of the file */
1494
b->real_fname = a->real_fname ? xstrdup (a->real_fname) : NULL;
1495
/* remove the filter stuff from the new stream */
1497
a->filter_ov = NULL;
1498
a->filter_ov_owner = 0;
1501
a->use = 2; /* make a write stream from a temp stream */
1504
{ /* allocate a fresh buffer for the
1506
b->d.buf = xmalloc (a->d.size);
1511
{ /* allocate a fresh buffer for the new
1513
a->d.buf = xmalloc (a->d.size);
1517
/* disable nlimit for the new stream */
1518
a->ntotal = b->ntotal + b->nbytes;
1519
a->nlimit = a->nbytes = 0;
1521
/* make a link from the new stream to the original stream */
1523
a->opaque = b->opaque;
1525
/* setup the function on the new stream */
1528
a->filter_ov_owner = rel_ov;
1530
a->subno = b->subno + 1;
1531
f (ov, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &dummy_len);
1535
log_debug ("iobuf-%d.%d: push `%s'\n", a->no, a->subno, a->desc);
1539
/* now we can initialize the new function if we have one */
1540
if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_INIT, a->chain,
1542
log_error ("IOBUFCTRL_INIT failed: %s\n", gpg_strerror (rc));
1547
* Remove an i/o filter.
1550
pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
1551
iobuf_t chain, byte * buf, size_t * len),
1555
size_t dummy_len = 0;
1562
log_debug ("iobuf-%d.%d: pop `%s'\n", a->no, a->subno, a->desc);
1564
{ /* this is simple */
1568
xfree (a->real_fname);
1569
memcpy (a, b, sizeof *a);
1573
for (b = a; b; b = b->chain)
1574
if (b->filter == f && (!ov || b->filter_ov == ov))
1577
log_bug ("pop_filter(): filter function not found\n");
1579
/* flush this stream if it is an output stream */
1580
if (a->use == 2 && (rc = iobuf_flush (b)))
1582
log_error ("iobuf_flush failed in pop_filter: %s\n", gpg_strerror (rc));
1585
/* and tell the filter to free it self */
1586
if (b->filter && (rc = b->filter (b->filter_ov, IOBUFCTRL_FREE, b->chain,
1589
log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1592
if (b->filter_ov && b->filter_ov_owner)
1594
xfree (b->filter_ov);
1595
b->filter_ov = NULL;
1599
/* and see how to remove it */
1600
if (a == b && !b->chain)
1601
log_bug ("can't remove the last filter from the chain\n");
1603
{ /* remove the first iobuf from the chain */
1604
/* everything from b is copied to a. This is save because
1605
* a flush has been done on the to be removed entry
1609
xfree (a->real_fname);
1610
memcpy (a, b, sizeof *a);
1613
log_debug ("iobuf-%d.%d: popped filter\n", a->no, a->subno);
1616
{ /* remove the last iobuf from the chain */
1617
log_bug ("Ohh jeee, trying to remove a head filter\n");
1620
{ /* remove an intermediate iobuf from the chain */
1621
log_bug ("Ohh jeee, trying to remove an intermediate filter\n");
1629
* read underflow: read more bytes into the buffer and return
1630
* the first byte or -1 on EOF.
1633
underflow (iobuf_t a)
1638
assert (a->d.start == a->d.len);
1640
return -1; /* EOF because a temp buffer can't do an underflow */
1646
iobuf_t b = a->chain;
1648
log_debug ("iobuf-%d.%d: pop `%s' in underflow\n",
1649
a->no, a->subno, a->desc);
1651
xfree (a->real_fname);
1652
memcpy (a, b, sizeof *a);
1657
a->filter_eof = 0; /* for the top level filter */
1659
log_debug ("iobuf-%d.%d: underflow: eof (due to filter eof)\n",
1661
return -1; /* return one(!) EOF */
1666
log_debug ("iobuf-%d.%d: error\n", a->no, a->subno);
1672
FILE *fp = a->directfp;
1674
len = fread (a->d.buf, 1, a->d.size, fp);
1675
if (len < a->d.size)
1678
a->error = gpg_error_from_errno (errno);
1682
return len ? a->d.buf[a->d.start++] : -1;
1690
log_debug ("iobuf-%d.%d: underflow: req=%lu\n",
1691
a->no, a->subno, (ulong) len);
1692
rc = a->filter (a->filter_ov, IOBUFCTRL_UNDERFLOW, a->chain,
1696
log_debug ("iobuf-%d.%d: underflow: got=%lu rc=%d\n",
1697
a->no, a->subno, (ulong) len, rc);
1698
/* if( a->no == 1 ) */
1699
/* log_hexdump (" data:", a->d.buf, len); */
1701
if (a->use == 1 && rc == -1)
1702
{ /* EOF: we can remove the filter */
1703
size_t dummy_len = 0;
1705
/* and tell the filter to free itself */
1706
if ((rc = a->filter (a->filter_ov, IOBUFCTRL_FREE, a->chain,
1708
log_error ("IOBUFCTRL_FREE failed: %s\n", gpg_strerror (rc));
1709
if (a->filter_ov && a->filter_ov_owner)
1711
xfree (a->filter_ov);
1712
a->filter_ov = NULL;
1716
a->filter_ov = NULL;
1718
if (!len && a->chain)
1720
iobuf_t b = a->chain;
1722
log_debug ("iobuf-%d.%d: pop `%s' in underflow (!len)\n",
1723
a->no, a->subno, a->desc);
1725
xfree (a->real_fname);
1726
memcpy (a, b, sizeof *a);
1737
log_debug ("iobuf-%d.%d: underflow: eof\n", a->no, a->subno);
1742
return a->d.buf[a->d.start++];
1747
log_debug ("iobuf-%d.%d: underflow: eof (no filter)\n",
1749
return -1; /* no filter; return EOF */
1755
iobuf_flush (iobuf_t a)
1764
{ /* increase the temp buffer */
1766
size_t newsize = a->d.size + 8192;
1769
log_debug ("increasing temp iobuf from %lu to %lu\n",
1770
(ulong) a->d.size, (ulong) newsize);
1771
newbuf = xmalloc (newsize);
1772
memcpy (newbuf, a->d.buf, a->d.len);
1775
a->d.size = newsize;
1778
else if (a->use != 2)
1779
log_bug ("flush on non-output iobuf\n");
1780
else if (!a->filter)
1781
log_bug ("iobuf_flush: no filter\n");
1783
rc = a->filter (a->filter_ov, IOBUFCTRL_FLUSH, a->chain, a->d.buf, &len);
1784
if (!rc && len != a->d.len)
1786
log_info ("iobuf_flush did not write all!\n");
1787
rc = GPG_ERR_INTERNAL;
1798
* Read a byte from the iobuf; returns -1 on EOF
1801
iobuf_readbyte (iobuf_t a)
1805
/* nlimit does not work together with unget */
1806
/* nbytes is also not valid! */
1809
if (a->unget.start < a->unget.len)
1810
return a->unget.buf[a->unget.start++];
1811
xfree (a->unget.buf);
1812
a->unget.buf = NULL;
1816
if (a->nlimit && a->nbytes >= a->nlimit)
1817
return -1; /* forced EOF */
1819
if (a->d.start < a->d.len)
1821
c = a->d.buf[a->d.start++];
1823
else if ((c = underflow (a)) == -1)
1824
return -1; /* EOF */
1832
iobuf_read (iobuf_t a, byte * buf, unsigned buflen)
1836
if (a->unget.buf || a->nlimit)
1838
/* handle special cases */
1839
for (n = 0; n < buflen; n++)
1841
if ((c = iobuf_readbyte (a)) == -1)
1844
return -1; /* eof */
1858
if (n < buflen && a->d.start < a->d.len)
1860
unsigned size = a->d.len - a->d.start;
1861
if (size > buflen - n)
1864
memcpy (buf, a->d.buf + a->d.start, size);
1872
if ((c = underflow (a)) == -1)
1875
return n ? n : -1 /*EOF*/;
1889
* Have a look at the iobuf.
1890
* NOTE: This only works in special cases.
1893
iobuf_peek (iobuf_t a, byte * buf, unsigned buflen)
1900
if (!(a->d.start < a->d.len))
1902
if (underflow (a) == -1)
1904
/* and unget this character */
1905
assert (a->d.start == 1);
1909
for (n = 0; n < buflen && (a->d.start + n) < a->d.len; n++, buf++)
1918
iobuf_writebyte (iobuf_t a, unsigned c)
1925
if (a->d.len == a->d.size)
1926
if ((rc=iobuf_flush (a)))
1929
assert (a->d.len < a->d.size);
1930
a->d.buf[a->d.len++] = c;
1936
iobuf_write (iobuf_t a, byte * buf, unsigned buflen)
1945
if (buflen && a->d.len < a->d.size)
1947
unsigned size = a->d.size - a->d.len;
1950
memcpy (a->d.buf + a->d.len, buf, size);
1957
rc = iobuf_flush (a);
1968
iobuf_writestr (iobuf_t a, const char *buf)
1973
if ((rc=iobuf_writebyte (a, *buf)))
1981
* copy the contents of TEMP to A.
1984
iobuf_write_temp (iobuf_t a, iobuf_t temp)
1987
pop_filter (temp, temp->filter, NULL);
1988
return iobuf_write (a, temp->d.buf, temp->d.len);
1992
* copy the contents of the temp io stream to BUFFER.
1995
iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen)
1997
size_t n = a->d.len;
2001
memcpy (buffer, a->d.buf, n);
2007
* Call this function to terminate processing of the temp stream
2008
* without closing it. This removes all filters from the stream
2009
* makes sure that iobuf_get_temp_{buffer,length}() returns correct
2013
iobuf_flush_temp (iobuf_t temp)
2016
pop_filter (temp, temp->filter, NULL);
2021
* Set a limit on how many bytes may be read from the input stream A.
2022
* Setting the limit to 0 disables this feature.
2025
iobuf_set_limit (iobuf_t a, off_t nlimit)
2032
a->ntotal += a->nbytes;
2039
* Return the length of an open file
2042
iobuf_get_filelength (iobuf_t a)
2048
FILE *fp = a->directfp;
2050
if (!fstat (fileno (fp), &st))
2052
log_error ("fstat() failed: %s\n", strerror (errno));
2056
/* Hmmm: file_filter may have already been removed */
2057
for (; a; a = a->chain)
2058
if (!a->chain && a->filter == file_filter)
2060
file_filter_ctx_t *b = a->filter_ov;
2061
FILEP_OR_FD fp = b->fp;
2063
#if defined(HAVE_DOSISH_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2066
if ((size = GetFileSize (fp, NULL)) != 0xffffffff)
2068
log_error ("GetFileSize for handle %p failed: ec=%d\n",
2069
fp, (int) GetLastError ());
2071
if (!fstat (my_fileno (fp), &st))
2073
log_error ("fstat() failed: %s\n", strerror (errno));
2082
* Tell the file position, where the next read will take place
2085
iobuf_tell (iobuf_t a)
2087
return a->ntotal + a->nbytes;
2091
#if !defined(HAVE_FSEEKO) && !defined(fseeko)
2093
#ifdef HAVE_LIMITS_H
2094
# include <limits.h>
2097
# define LONG_MAX ((long) ((unsigned long) -1 >> 1))
2100
# define LONG_MIN (-1 - LONG_MAX)
2104
* A substitute for fseeko, for hosts that don't have it.
2107
fseeko (FILE * stream, off_t newpos, int whence)
2109
while (newpos != (long) newpos)
2111
long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
2112
if (fseek (stream, pos, whence) != 0)
2117
return fseek (stream, (long) newpos, whence);
2122
* This is a very limited implementation. It simply discards all internal
2123
* buffering and removes all filters but the first one.
2126
iobuf_seek (iobuf_t a, off_t newpos)
2128
file_filter_ctx_t *b = NULL;
2132
FILE *fp = a->directfp;
2133
if (fseeko (fp, newpos, SEEK_SET))
2135
log_error ("can't seek: %s\n", strerror (errno));
2142
for (; a; a = a->chain)
2144
if (!a->chain && a->filter == file_filter)
2152
#ifdef FILE_FILTER_USES_STDIO
2153
if (fseeko (b->fp, newpos, SEEK_SET))
2155
log_error ("can't fseek: %s\n", strerror (errno));
2159
#ifdef HAVE_DOSISH_SYSTEM
2160
if (SetFilePointer (b->fp, newpos, NULL, FILE_BEGIN) == 0xffffffff)
2162
log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2163
b->fp, (int) GetLastError ());
2167
if (lseek (b->fp, newpos, SEEK_SET) == (off_t) - 1)
2169
log_error ("can't lseek: %s\n", strerror (errno));
2175
a->d.len = 0; /* discard buffer */
2182
/* remove filters, but the last */
2184
log_debug ("pop_filter called in iobuf_seek - please report\n");
2186
pop_filter (a, a->filter, NULL);
2197
* Retrieve the real filename
2200
iobuf_get_real_fname (iobuf_t a)
2203
return a->real_fname;
2205
/* the old solution */
2206
for (; a; a = a->chain)
2207
if (!a->chain && a->filter == file_filter)
2209
file_filter_ctx_t *b = a->filter_ov;
2210
return b->print_only_name ? NULL : b->fname;
2218
* Retrieve the filename
2221
iobuf_get_fname (iobuf_t a)
2223
for (; a; a = a->chain)
2224
if (!a->chain && a->filter == file_filter)
2226
file_filter_ctx_t *b = a->filter_ov;
2234
* Start the block write mode, see rfc1991.new for details.
2235
* A value of 0 for N stops this mode (flushes and writes
2239
iobuf_set_block_mode (iobuf_t a, size_t n)
2241
block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
2243
assert (a->use == 1 || a->use == 2);
2248
log_debug ("pop_filter called in set_block_mode - please report\n");
2249
pop_filter (a, block_filter, NULL);
2253
ctx->size = n; /* only needed for use 2 */
2254
iobuf_push_filter (a, block_filter, ctx);
2259
* enable partial block mode as described in the OpenPGP draft.
2260
* LEN is the first length byte on read, but ignored on writes.
2263
iobuf_set_partial_block_mode (iobuf_t a, size_t len)
2265
block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
2267
assert (a->use == 1 || a->use == 2);
2272
log_debug ("pop_filter called in set_partial_block_mode"
2273
" - please report\n");
2274
pop_filter (a, block_filter, NULL);
2281
iobuf_push_filter (a, block_filter, ctx);
2287
* Checks whether the stream is in block mode
2288
* Note: This does not work if other filters are pushed on the stream.
2291
iobuf_in_block_mode (iobuf_t a)
2293
if (a && a->filter == block_filter)
2300
* Same as fgets() but if the buffer is too short a larger one will
2301
* be allocated up to some limit *max_length.
2302
* A line is considered a byte stream ending in a LF.
2303
* Returns the length of the line. EOF is indicated by a line of
2304
* length zero. The last LF may be missing due to an EOF.
2305
* is max_length is zero on return, the line has been truncated.
2307
* Note: The buffer is allocated with enough space to append a CR,LF,EOL
2310
iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
2311
unsigned *length_of_buffer, unsigned *max_length)
2314
char *buffer = *addr_of_buffer;
2315
unsigned length = *length_of_buffer;
2316
unsigned nbytes = 0;
2317
unsigned maxlen = *max_length;
2321
{ /* must allocate a new buffer */
2323
buffer = xmalloc (length);
2324
*addr_of_buffer = buffer;
2325
*length_of_buffer = length;
2328
length -= 3; /* reserve 3 bytes (cr,lf,eol) */
2330
while ((c = iobuf_get (a)) != -1)
2332
if (nbytes == length)
2333
{ /* increase the buffer */
2334
if (length > maxlen)
2335
{ /* this is out limit */
2336
/* skip the rest of the line */
2337
while (c != '\n' && (c = iobuf_get (a)) != -1)
2339
*p++ = '\n'; /* always append a LF (we have reserved space) */
2341
*max_length = 0; /* indicate truncation */
2344
length += 3; /* correct for the reserved byte */
2345
length += length < 1024 ? 256 : 1024;
2346
buffer = xrealloc (buffer, length);
2347
*addr_of_buffer = buffer;
2348
*length_of_buffer = length;
2349
length -= 3; /* and reserve again */
2350
p = buffer + nbytes;
2357
*p = 0; /* make sure the line is a string */
2362
/* This is the non iobuf specific function */
2364
iobuf_translate_file_handle (int fd, int for_write)
2371
return fd; /* do not do this for error, stdin, stdout, stderr */
2373
x = _open_osfhandle (fd, for_write ? 1 : 0);
2375
log_error ("failed to translate osfhandle %p\n", (void *) fd);
2378
/*log_info ("_open_osfhandle %p yields %d%s\n",
2379
(void*)fd, x, for_write? " for writing":"" ); */
2388
translate_file_handle (int fd, int for_write)
2391
#ifdef FILE_FILTER_USES_STDIO
2392
fd = iobuf_translate_file_handle (fd, for_write);
2398
x = (int) GetStdHandle (STD_INPUT_HANDLE);
2400
x = (int) GetStdHandle (STD_OUTPUT_HANDLE);
2402
x = (int) GetStdHandle (STD_ERROR_HANDLE);
2407
log_debug ("GetStdHandle(%d) failed: ec=%d\n",
2408
fd, (int) GetLastError ());