~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to common/iobuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* iobuf.c  -  file handling
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
 
1
/* iobuf.c  -  File Handling for OpenPGP.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2006,
 
3
 *               2007  Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * 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
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * 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
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
29
29
#include <sys/stat.h>
30
30
#include <fcntl.h>
31
31
#include <unistd.h>
32
 
#ifdef HAVE_DOSISH_SYSTEM
33
 
#include <windows.h>
 
32
#ifdef HAVE_W32_SYSTEM
 
33
# include <windows.h>
34
34
#endif
35
35
#ifdef __riscos__
36
 
#include <kernel.h>
37
 
#include <swis.h>
 
36
# include <kernel.h>
 
37
# include <swis.h>
38
38
#endif /* __riscos__ */
39
39
 
40
 
#include "memory.h"
41
40
#include "util.h"
 
41
#include "sysutils.h"
42
42
#include "iobuf.h"
43
43
 
 
44
/*-- Begin configurable part.  --*/
 
45
 
 
46
/* The size of the internal buffers. 
 
47
   NOTE: If you change this value you MUST also adjust the regression
 
48
   test "armored_key_8192" in armor.test! */
 
49
#define IOBUF_BUFFER_SIZE  8192
 
50
 
 
51
/* We don't want to use the STDIO based backend.  */
44
52
#undef FILE_FILTER_USES_STDIO
45
53
 
46
 
#ifdef HAVE_DOSISH_SYSTEM
47
 
#define USE_SETMODE 1
48
 
#endif
49
 
 
 
54
/*-- End configurable part.  --*/
 
55
 
 
56
 
 
57
/* Under W32 the default is to use the setmode call.  Define a macro
 
58
   which allows us to enable this call.  */
 
59
#ifdef HAVE_W32_SYSTEM
 
60
# define USE_SETMODE 1
 
61
#endif /*HAVE_W32_SYSTEM*/
 
62
 
 
63
 
 
64
/* Definition of constants and macros used by our file filter
 
65
   implementation.  What we define here are 3 macros to make the
 
66
   appropriate calls:
 
67
 
 
68
   my_fileno 
 
69
     Is expanded to fileno(a) if using a stdion backend and to a if we
 
70
     are using the low-level backend.
 
71
 
 
72
   my_fopen 
 
73
     Is defined to fopen for the stdio backend and to direct_open if
 
74
     we are using the low-evel backend.
 
75
 
 
76
   my_fopen_ro 
 
77
     Is defined to fopen for the stdio backend and to fd_cache_open if
 
78
     we are using the low-evel backend.
 
79
 
 
80
   fp_or_fd_t
 
81
     Is the type we use for the backend stream or fiel descriptor.
 
82
 
 
83
   INVALID_FP, FILEP_OR_FD_FOR_STDIN, FILEP_OR_FD_FOR_STDOUT
 
84
     Are macros defined depending on the used backend.
 
85
 
 
86
*/
50
87
#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)
58
 
typedef struct
59
 
{
60
 
  FILE *fp;                     /* open file handle */
61
 
  int keep_open;
62
 
  int no_cache;
63
 
  int print_only_name;          /* flags indicating that fname is not a real file */
64
 
  char fname[1];                /* name of the file */
65
 
}
66
 
file_filter_ctx_t;
67
 
#else
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))
76
 
#undef USE_SETMODE
77
 
#else
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)
82
 
#endif
83
 
typedef struct
84
 
{
85
 
  FILEP_OR_FD fp;               /* open file handle */
86
 
  int keep_open;
 
88
# define my_fileno(a)     fileno ((a))
 
89
# define my_fopen_ro(a,b) fopen ((a),(b))
 
90
# define my_fopen(a,b)    fopen ((a),(b))
 
91
  typedef FILE *fp_or_fd_t;
 
92
# define INVALID_FP              NULL
 
93
# define FILEP_OR_FD_FOR_STDIN   (stdin)
 
94
# define FILEP_OR_FD_FOR_STDOUT  (stdout)
 
95
#else /*!FILE_FILTER_USES_STDIO*/
 
96
# define my_fopen_ro(a,b) fd_cache_open ((a),(b))
 
97
# define my_fopen(a,b)    direct_open ((a),(b))
 
98
# ifdef HAVE_W32_SYSTEM
 
99
   /* (We assume that a HANDLE first into an int.)  */
 
100
#  define my_fileno(a)  ((int)(a))
 
101
   typedef HANDLE fp_or_fd_t;
 
102
#  define INVALID_FP             ((HANDLE)-1)
 
103
#  define FILEP_OR_FD_FOR_STDIN  (GetStdHandle (STD_INPUT_HANDLE))
 
104
#  define FILEP_OR_FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
 
105
#  undef USE_SETMODE
 
106
# else /*!HAVE_W32_SYSTEM*/
 
107
#  define my_fileno(a)  (a)
 
108
   typedef int fp_or_fd_t;
 
109
#  define INVALID_FP             (-1)
 
110
#  define FILEP_OR_FD_FOR_STDIN  (0)
 
111
#  define FILEP_OR_FD_FOR_STDOUT (1)
 
112
# endif /*!HAVE_W32_SYSTEM*/
 
113
#endif /*!FILE_FILTER_USES_STDIO*/
 
114
 
 
115
/* The context used by the file filter.  */
 
116
typedef struct
 
117
{
 
118
  fp_or_fd_t fp;       /* Open file pointer or handle.  */
 
119
  int keep_open; 
87
120
  int no_cache;
88
121
  int eof_seen;
89
 
  int print_only_name;          /* flags indicating that fname is not a real file */
90
 
  char fname[1];                /* name of the file */
 
122
  int print_only_name; /* Flags indicating that fname is not a real file.  */
 
123
  char fname[1];       /* Name of the file.  */
91
124
}
92
125
file_filter_ctx_t;
93
126
 
 
127
 
 
128
/* If we are not using stdio as the backend we make use of a "close
 
129
   cache".  */
 
130
#ifndef FILE_FILTER_USES_STDIO
94
131
struct close_cache_s
95
132
{
96
133
  struct close_cache_s *next;
97
 
  FILEP_OR_FD fp;
 
134
  fp_or_fd_t fp;
98
135
  char fname[1];
99
136
};
100
 
typedef struct close_cache_s *CLOSE_CACHE;
101
 
static CLOSE_CACHE close_cache;
102
 
#endif
103
 
 
104
 
#ifdef _WIN32
 
137
typedef struct close_cache_s *close_cache_t;
 
138
static close_cache_t close_cache;
 
139
#endif /*!FILE_FILTER_USES_STDIO*/
 
140
 
 
141
 
 
142
 
 
143
#ifdef HAVE_W32_SYSTEM
105
144
typedef struct
106
145
{
107
146
  int sock;
108
147
  int keep_open;
109
148
  int no_cache;
110
149
  int eof_seen;
111
 
  int print_only_name;          /* flags indicating that fname is not a real file */
112
 
  char fname[1];                /* name of the file */
 
150
  int print_only_name;  /* Flag indicating that fname is not a real file.  */
 
151
  char fname[1];        /* Name of the file */
113
152
}
114
153
sock_filter_ctx_t;
115
 
#endif /*_WIN32*/
 
154
#endif /*HAVE_W32_SYSTEM*/
116
155
 
117
156
/* The first partial length header block must be of size 512
118
157
 * to make it easier (and efficienter) we use a min. block size of 512
120
159
#define OP_MIN_PARTIAL_CHUNK      512
121
160
#define OP_MIN_PARTIAL_CHUNK_2POW 9
122
161
 
 
162
/* The context we use for the block filter (used to handle OpenPGP
 
163
   length information header).  */
123
164
typedef struct
124
165
{
125
166
  int use;
126
167
  size_t size;
127
168
  size_t count;
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) */
 
169
  int partial;     /* 1 = partial header, 2 in last partial packet.  */
 
170
  char *buffer;    /* Used for partial header.  */
 
171
  size_t buflen;   /* Used size of buffer.  */
 
172
  int first_c;     /* First character of a partial header (which is > 0).  */
132
173
  int eof;
133
174
}
134
175
block_filter_ctx_t;
135
176
 
 
177
 
 
178
/* Global flag to tell whether special file names are enabled.  See
 
179
   gpg.c for an explanation of these file names.  FIXME: it does not
 
180
   belong into the iobuf subsystem. */
136
181
static int special_names_enabled;
137
182
 
 
183
/* Local prototypes.  */
138
184
static int underflow (iobuf_t a);
139
185
static int translate_file_handle (int fd, int for_write);
140
186
 
 
187
 
 
188
 
141
189
#ifndef FILE_FILTER_USES_STDIO
142
 
 
143
190
/*
144
191
 * Invalidate (i.e. close) a cached iobuf
145
192
 */
146
193
static void
147
194
fd_cache_invalidate (const char *fname)
148
195
{
149
 
  CLOSE_CACHE cc;
 
196
  close_cache_t cc;
150
197
 
151
198
  assert (fname);
152
199
  if (DBG_IOBUF)
158
205
        {
159
206
          if (DBG_IOBUF)
160
207
            log_debug ("                did (%s)\n", cc->fname);
161
 
#ifdef HAVE_DOSISH_SYSTEM
 
208
#ifdef HAVE_W32_SYSTEM
162
209
          CloseHandle (cc->fp);
163
210
#else
164
211
          close (cc->fp);
169
216
}
170
217
 
171
218
 
172
 
 
173
 
static FILEP_OR_FD
 
219
static fp_or_fd_t
174
220
direct_open (const char *fname, const char *mode)
175
221
{
176
 
#ifdef HAVE_DOSISH_SYSTEM
 
222
#ifdef HAVE_W32_SYSTEM
177
223
  unsigned long da, cd, sm;
178
224
  HANDLE hfile;
179
225
 
206
252
 
207
253
  hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL);
208
254
  return hfile;
209
 
#else
 
255
#else /*!HAVE_W32_SYSTEM*/
210
256
  int oflag;
211
257
  int cflag = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
212
258
 
229
275
  if (strchr (mode, 'b'))
230
276
    oflag |= O_BINARY;
231
277
#endif
 
278
  /* No we need to distinguish between POSIX and RISC OS.  */
232
279
#ifndef __riscos__
233
280
  return open (fname, oflag, cflag);
234
281
#else
243
290
      return open (fname, oflag, cflag);
244
291
  }
245
292
#endif
246
 
#endif
 
293
#endif /*!HAVE_W32_SYSTEM*/
247
294
}
248
295
 
249
296
 
252
299
 * Note that this caching strategy only works if the process does not chdir.
253
300
 */
254
301
static void
255
 
fd_cache_close (const char *fname, FILEP_OR_FD fp)
 
302
fd_cache_close (const char *fname, fp_or_fd_t fp)
256
303
{
257
 
  CLOSE_CACHE cc;
 
304
  close_cache_t cc;
258
305
 
259
306
  assert (fp);
260
307
  if (!fname || !*fname)
261
308
    {
262
 
#ifdef HAVE_DOSISH_SYSTEM
 
309
#ifdef HAVE_W32_SYSTEM
263
310
      CloseHandle (fp);
264
311
#else
265
312
      close (fp);
266
313
#endif
267
314
      if (DBG_IOBUF)
268
 
        log_debug ("fd_cache_close (%p) real\n", (void *) fp);
 
315
        log_debug ("fd_cache_close (%d) real\n", (int)fp);
269
316
      return;
270
317
    }
271
318
  /* try to reuse a slot */
292
339
/*
293
340
 * Do an direct_open on FNAME but first try to reuse one from the fd_cache
294
341
 */
295
 
static FILEP_OR_FD
 
342
static fp_or_fd_t
296
343
fd_cache_open (const char *fname, const char *mode)
297
344
{
298
 
  CLOSE_CACHE cc;
 
345
  close_cache_t cc;
299
346
 
300
347
  assert (fname);
301
348
  for (cc = close_cache; cc; cc = cc->next)
302
349
    {
303
350
      if (cc->fp != INVALID_FP && !strcmp (cc->fname, fname))
304
351
        {
305
 
          FILEP_OR_FD fp = cc->fp;
 
352
          fp_or_fd_t fp = cc->fp;
306
353
          cc->fp = INVALID_FP;
307
354
          if (DBG_IOBUF)
308
355
            log_debug ("fd_cache_open (%s) using cached fp\n", fname);
309
 
#ifdef HAVE_DOSISH_SYSTEM
 
356
#ifdef HAVE_W32_SYSTEM
310
357
          if (SetFilePointer (fp, 0, NULL, FILE_BEGIN) == 0xffffffff)
311
358
            {
312
359
              log_error ("rewind file failed on handle %p: ec=%d\n",
328
375
  return direct_open (fname, mode);
329
376
}
330
377
 
331
 
 
332
378
#endif /*FILE_FILTER_USES_STDIO */
333
379
 
334
380
 
361
407
             size_t * ret_len)
362
408
{
363
409
  file_filter_ctx_t *a = opaque;
364
 
  FILEP_OR_FD f = a->fp;
 
410
  fp_or_fd_t f = a->fp;
365
411
  size_t size = *ret_len;
366
412
  size_t nbytes = 0;
367
413
  int rc = 0;
385
431
            }
386
432
          else if (ferror (f) && errno != EPIPE)
387
433
            {
388
 
              rc = gpg_error_from_errno (errno);
 
434
              rc = gpg_error_from_syserror ();
389
435
              log_error ("%s: read error: %s\n", a->fname, strerror (errno));
390
436
            }
391
437
          *ret_len = nbytes;
399
445
          nbytes = fwrite (buf, 1, size, f);
400
446
          if (ferror (f))
401
447
            {
402
 
              rc = gpg_error_from_errno (errno);
 
448
              rc = gpg_error_from_syserror ();
403
449
              log_error ("%s: write error: %s\n", a->fname, strerror (errno));
404
450
            }
405
451
        }
437
483
        }
438
484
      else
439
485
        {
440
 
#ifdef HAVE_DOSISH_SYSTEM
 
486
#ifdef HAVE_W32_SYSTEM
441
487
          unsigned long nread;
442
488
 
443
489
          nbytes = 0;
474
520
            {                   /* error */
475
521
              if (errno != EPIPE)
476
522
                {
477
 
                  rc = gpg_error_from_errno (errno);
 
523
                  rc = gpg_error_from_syserror ();
478
524
                  log_error ("%s: read error: %s\n",
479
525
                             a->fname, strerror (errno));
480
526
                }
496
542
    {
497
543
      if (size)
498
544
        {
499
 
#ifdef HAVE_DOSISH_SYSTEM
 
545
#ifdef HAVE_W32_SYSTEM
500
546
          byte *p = buf;
501
547
          unsigned long n;
502
548
 
536
582
          while (n != -1 && nbytes);
537
583
          if (n == -1)
538
584
            {
539
 
              rc = gpg_error_from_errno (errno);
 
585
              rc = gpg_error_from_syserror ();
540
586
              log_error ("%s: write error: %s\n", a->fname, strerror (errno));
541
587
            }
542
588
          nbytes = p - buf;
556
602
    }
557
603
  else if (control == IOBUFCTRL_FREE)
558
604
    {
559
 
#ifdef HAVE_DOSISH_SYSTEM
 
605
#ifdef HAVE_W32_SYSTEM
560
606
      if (f != FILEP_OR_FD_FOR_STDIN && f != FILEP_OR_FD_FOR_STDOUT)
561
607
        {
562
608
          if (DBG_IOBUF)
580
626
  return rc;
581
627
}
582
628
 
583
 
#ifdef _WIN32
584
 
/* Becuase sockets are an special object under Lose32 we have to
585
 
 * use a special filter */
 
629
 
 
630
#ifdef HAVE_W32_SYSTEM
 
631
/* Because network sockets are special objects under Lose32 we have to
 
632
   use a dedicated filter for them. */
586
633
static int
587
634
sock_filter (void *opaque, int control, iobuf_t chain, byte * buf,
588
635
             size_t * ret_len)
667
714
    }
668
715
  return rc;
669
716
}
670
 
#endif /*_WIN32*/
 
717
#endif /*HAVE_W32_SYSTEM*/
671
718
 
672
719
/****************
673
720
 * This is used to implement the block write mode.
762
809
                          break;
763
810
                        }
764
811
                      a->size |= c;
 
812
                      a->partial = 2;
 
813
                      if (!a->size)
 
814
                        {
 
815
                          a->eof = 1;
 
816
                          if (!n)
 
817
                            rc = -1;
 
818
                          break;
 
819
                        }
765
820
                    }
766
821
                  else
767
 
                    {           /* next partial body length */
 
822
                    { /* Next partial body length. */
768
823
                      a->size = 1 << (c & 0x1f);
769
824
                    }
770
825
                  /*  log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
771
826
                }
772
827
              else
773
 
                {               /* the gnupg partial length scheme - much better :-) */
774
 
                  c = iobuf_get (chain);
775
 
                  a->size = c << 8;
776
 
                  c = iobuf_get (chain);
777
 
                  a->size |= c;
778
 
                  if (c == -1)
779
 
                    {
780
 
                      log_error ("block_filter: error reading length info\n");
781
 
                      rc = GPG_ERR_BAD_DATA;
782
 
                    }
783
 
                  if (!a->size)
784
 
                    {
785
 
                      a->eof = 1;
786
 
                      if (!n)
787
 
                        rc = -1;
788
 
                      break;
789
 
                    }
790
 
                }
 
828
                BUG (); 
791
829
            }
792
830
 
793
831
          while (!rc && size && a->size)
851
889
                    {           /* write stuff from the buffer */
852
890
                      assert (n == OP_MIN_PARTIAL_CHUNK);
853
891
                      if (iobuf_write (chain, a->buffer, n))
854
 
                        rc = gpg_error_from_errno (errno);
 
892
                        rc = gpg_error_from_syserror ();
855
893
                      a->buflen = 0;
856
894
                      nbytes -= n;
857
895
                    }
858
896
                  if ((n = nbytes) > blen)
859
897
                    n = blen;
860
898
                  if (n && iobuf_write (chain, p, n))
861
 
                    rc = gpg_error_from_errno (errno);
 
899
                    rc = gpg_error_from_syserror ();
862
900
                  p += n;
863
901
                  nbytes -= n;
864
902
                }
876
914
            }
877
915
        }
878
916
      else
879
 
        {                       /* the gnupg scheme (which is not openpgp compliant) */
880
 
          size_t avail, n;
881
 
 
882
 
          for (p = buf; !rc && size;)
883
 
            {
884
 
              n = size;
885
 
              avail = a->size - a->count;
886
 
              if (!avail)
887
 
                {
888
 
                  if (n > a->size)
889
 
                    {
890
 
                      iobuf_put (chain, (a->size >> 8) & 0xff);
891
 
                      iobuf_put (chain, a->size & 0xff);
892
 
                      avail = a->size;
893
 
                      a->count = 0;
894
 
                    }
895
 
                  else
896
 
                    {
897
 
                      iobuf_put (chain, (n >> 8) & 0xff);
898
 
                      iobuf_put (chain, n & 0xff);
899
 
                      avail = n;
900
 
                      a->count = a->size - n;
901
 
                    }
902
 
                }
903
 
              if (n > avail)
904
 
                n = avail;
905
 
              if (iobuf_write (chain, p, n))
906
 
                rc = gpg_error_from_errno (errno);
907
 
              a->count += n;
908
 
              p += n;
909
 
              size -= n;
910
 
            }
911
 
        }
 
917
        BUG ();
912
918
    }
913
919
  else if (control == IOBUFCTRL_INIT)
914
920
    {
969
975
                {
970
976
                  log_error ("block_filter: write error: %s\n",
971
977
                             strerror (errno));
972
 
                  rc = gpg_error_from_errno (errno);
 
978
                  rc = gpg_error_from_syserror ();
973
979
                }
974
980
              xfree (a->buffer);
975
981
              a->buffer = NULL;
976
982
              a->buflen = 0;
977
983
            }
978
984
          else
979
 
            {
980
 
              iobuf_writebyte (chain, 0);
981
 
              iobuf_writebyte (chain, 0);
982
 
            }
 
985
            BUG ();
983
986
        }
984
987
      else if (a->size)
985
988
        {
1087
1090
  const char *s;
1088
1091
  iobuf_t a2;
1089
1092
  int rc;
1090
 
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
 
1093
#if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1091
1094
  char *remove_name = NULL;
1092
1095
#endif
1093
1096
 
1096
1099
      s = iobuf_get_real_fname (a);
1097
1100
      if (s && *s)
1098
1101
        {
1099
 
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
 
1102
#if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1100
1103
          remove_name = xstrdup (s);
1101
1104
#else
1102
1105
          remove (s);
1113
1116
    }
1114
1117
 
1115
1118
  rc = iobuf_close (a);
1116
 
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
 
1119
#if defined(HAVE_W32_SYSTEM) || defined(__riscos__)
1117
1120
  if (remove_name)
1118
1121
    {
1119
1122
      /* Argg, MSDOS does not allow to remove open files.  So
1136
1139
{
1137
1140
  iobuf_t a;
1138
1141
 
1139
 
  a = iobuf_alloc (3, 8192);
 
1142
  a = iobuf_alloc (3, IOBUF_BUFFER_SIZE);
1140
1143
 
1141
1144
  return a;
1142
1145
}
1159
1162
  special_names_enabled = yes;
1160
1163
}
1161
1164
 
1162
 
/*
1163
 
 * see whether the filename has the for "-&nnnn", where n is a
1164
 
 * non-zero number.
1165
 
 * Returns this number or -1 if it is not the case.
1166
 
 */
 
1165
 
 
1166
/* See whether the filename has the form "-&nnnn", where n is a
 
1167
   non-zero number.  Returns this number or -1 if it is not the
 
1168
   case.  */
1167
1169
static int
1168
1170
check_special_filename (const char *fname)
1169
1171
{
1180
1182
  return -1;
1181
1183
}
1182
1184
 
 
1185
 
 
1186
/* This fucntion returns true if FNAME indicates a PIPE (stdout or
 
1187
   stderr) or a special file name if those are enabled. */
 
1188
int
 
1189
iobuf_is_pipe_filename (const char *fname)
 
1190
{
 
1191
  if (!fname || (*fname=='-' && !fname[1]) )
 
1192
    return 1;
 
1193
  return check_special_filename (fname) != -1;
 
1194
}
 
1195
 
1183
1196
/****************
1184
1197
 * Create a head iobuf for reading from a file
1185
1198
 * returns: NULL if an error occures and sets errno
1188
1201
iobuf_open (const char *fname)
1189
1202
{
1190
1203
  iobuf_t a;
1191
 
  FILEP_OR_FD fp;
 
1204
  fp_or_fd_t fp;
1192
1205
  file_filter_ctx_t *fcx;
1193
1206
  size_t len;
1194
1207
  int print_only = 0;
1207
1220
    return iobuf_fdopen (translate_file_handle (fd, 0), "rb");
1208
1221
  else if ((fp = my_fopen_ro (fname, "rb")) == INVALID_FP)
1209
1222
    return NULL;
1210
 
  a = iobuf_alloc (1, 8192);
 
1223
  a = iobuf_alloc (1, IOBUF_BUFFER_SIZE);
1211
1224
  fcx = xmalloc (sizeof *fcx + strlen (fname));
1212
1225
  fcx->fp = fp;
1213
1226
  fcx->print_only_name = print_only;
1233
1246
iobuf_fdopen (int fd, const char *mode)
1234
1247
{
1235
1248
  iobuf_t a;
1236
 
  FILEP_OR_FD fp;
 
1249
  fp_or_fd_t fp;
1237
1250
  file_filter_ctx_t *fcx;
1238
1251
  size_t len;
1239
1252
 
1241
1254
  if (!(fp = fdopen (fd, mode)))
1242
1255
    return NULL;
1243
1256
#else
1244
 
  fp = (FILEP_OR_FD) fd;
 
1257
  fp = (fp_or_fd_t) fd;
1245
1258
#endif
1246
 
  a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, 8192);
 
1259
  a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE);
1247
1260
  fcx = xmalloc (sizeof *fcx + 20);
1248
1261
  fcx->fp = fp;
1249
1262
  fcx->print_only_name = 1;
1263
1276
iobuf_sockopen (int fd, const char *mode)
1264
1277
{
1265
1278
  iobuf_t a;
1266
 
#ifdef _WIN32
 
1279
#ifdef HAVE_W32_SYSTEM
1267
1280
  sock_filter_ctx_t *scx;
1268
1281
  size_t len;
1269
1282
 
1270
 
  a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, 8192);
 
1283
  a = iobuf_alloc (strchr (mode, 'w') ? 2 : 1, IOBUF_BUFFER_SIZE);
1271
1284
  scx = xmalloc (sizeof *scx + 25);
1272
1285
  scx->sock = fd;
1273
1286
  scx->print_only_name = 1;
1292
1305
iobuf_create (const char *fname)
1293
1306
{
1294
1307
  iobuf_t a;
1295
 
  FILEP_OR_FD fp;
 
1308
  fp_or_fd_t fp;
1296
1309
  file_filter_ctx_t *fcx;
1297
1310
  size_t len;
1298
1311
  int print_only = 0;
1311
1324
    return iobuf_fdopen (translate_file_handle (fd, 1), "wb");
1312
1325
  else if ((fp = my_fopen (fname, "wb")) == INVALID_FP)
1313
1326
    return NULL;
1314
 
  a = iobuf_alloc (2, 8192);
 
1327
  a = iobuf_alloc (2, IOBUF_BUFFER_SIZE);
1315
1328
  fcx = xmalloc (sizeof *fcx + strlen (fname));
1316
1329
  fcx->fp = fp;
1317
1330
  fcx->print_only_name = print_only;
1346
1359
    return NULL;
1347
1360
  else if (!(fp = my_fopen (fname, "ab")))
1348
1361
    return NULL;
1349
 
  a = iobuf_alloc (2, 8192);
 
1362
  a = iobuf_alloc (2, IOBUF_BUFFER_SIZE);
1350
1363
  fcx = m_alloc (sizeof *fcx + strlen (fname));
1351
1364
  fcx->fp = fp;
1352
1365
  strcpy (fcx->fname, fname);
1366
1379
iobuf_openrw (const char *fname)
1367
1380
{
1368
1381
  iobuf_t a;
1369
 
  FILEP_OR_FD fp;
 
1382
  fp_or_fd_t fp;
1370
1383
  file_filter_ctx_t *fcx;
1371
1384
  size_t len;
1372
1385
 
1374
1387
    return NULL;
1375
1388
  else if ((fp = my_fopen (fname, "r+b")) == INVALID_FP)
1376
1389
    return NULL;
1377
 
  a = iobuf_alloc (2, 8192);
 
1390
  a = iobuf_alloc (2, IOBUF_BUFFER_SIZE);
1378
1391
  fcx = xmalloc (sizeof *fcx + strlen (fname));
1379
1392
  fcx->fp = fp;
1380
1393
  strcpy (fcx->fname, fname);
1406
1419
            b->keep_open = intval;
1407
1420
            return 0;
1408
1421
          }
1409
 
#ifdef _WIN32
 
1422
#ifdef HAVE_W32_SYSTEM
1410
1423
        else if (!a->chain && a->filter == sock_filter)
1411
1424
          {
1412
1425
            sock_filter_ctx_t *b = a->filter_ov;
1441
1454
            b->no_cache = intval;
1442
1455
            return 0;
1443
1456
          }
1444
 
#ifdef _WIN32
 
1457
#ifdef HAVE_W32_SYSTEM
1445
1458
        else if (!a->chain && a->filter == sock_filter)
1446
1459
          {
1447
1460
            sock_filter_ctx_t *b = a->filter_ov;
1547
1560
/****************
1548
1561
 * Remove an i/o filter.
1549
1562
 */
1550
 
int
 
1563
static int
1551
1564
pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
1552
1565
                               iobuf_t chain, byte * buf, size_t * len),
1553
1566
            void *ov)
1676
1689
      if (len < a->d.size)
1677
1690
        {
1678
1691
          if (ferror (fp))
1679
 
            a->error = gpg_error_from_errno (errno);
 
1692
            a->error = gpg_error_from_syserror ();
1680
1693
        }
1681
1694
      a->d.len = len;
1682
1695
      a->d.start = 0;
1764
1777
  if (a->use == 3)
1765
1778
    {                           /* increase the temp buffer */
1766
1779
      unsigned char *newbuf;
1767
 
      size_t newsize = a->d.size + 8192;
 
1780
      size_t newsize = a->d.size + IOBUF_BUFFER_SIZE;
1768
1781
 
1769
1782
      if (DBG_IOBUF)
1770
1783
        log_debug ("increasing temp iobuf from %lu to %lu\n",
1803
1816
{
1804
1817
  int c;
1805
1818
 
1806
 
  /* nlimit does not work together with unget */
1807
 
  /* nbytes is also not valid! */
1808
 
  if (a->unget.buf)
1809
 
    {
1810
 
      if (a->unget.start < a->unget.len)
1811
 
        return a->unget.buf[a->unget.start++];
1812
 
      xfree (a->unget.buf);
1813
 
      a->unget.buf = NULL;
1814
 
      a->nofast &= ~2;
1815
 
    }
1816
 
 
1817
1819
  if (a->nlimit && a->nbytes >= a->nlimit)
1818
1820
    return -1;                  /* forced EOF */
1819
1821
 
1835
1837
  unsigned char *buf = (unsigned char *)buffer;
1836
1838
  int c, n;
1837
1839
 
1838
 
  if (a->unget.buf || a->nlimit)
 
1840
  if (a->nlimit)
1839
1841
    {
1840
 
      /* handle special cases */
 
1842
      /* Handle special cases. */
1841
1843
      for (n = 0; n < buflen; n++)
1842
1844
        {
1843
1845
          if ((c = iobuf_readbyte (a)) == -1)
1887
1889
}
1888
1890
 
1889
1891
 
 
1892
 
1890
1893
/****************
1891
1894
 * Have a look at the iobuf.
1892
1895
 * NOTE: This only works in special cases.
1903
1906
    {
1904
1907
      if (underflow (a) == -1)
1905
1908
        return -1;
1906
 
      /* and unget this character */
 
1909
      /* And unget this character. */
1907
1910
      assert (a->d.start == 1);
1908
1911
      a->d.start = 0;
1909
1912
    }
2038
2041
 
2039
2042
 
2040
2043
 
2041
 
/****************
2042
 
 * Return the length of an open file
2043
 
 */
 
2044
/* Return the length of an open file A.  IF OVERFLOW is not NULL it
 
2045
   will be set to true if the file is larger than what off_t can cope
 
2046
   with.  The function return 0 on error or on overflow condition.  */
2044
2047
off_t
2045
 
iobuf_get_filelength (iobuf_t a)
 
2048
iobuf_get_filelength (iobuf_t a, int *overflow)
2046
2049
{
2047
2050
  struct stat st;
2048
2051
 
2049
 
  if (a->directfp)
 
2052
  if (overflow)
 
2053
    *overflow = 0;
 
2054
  
 
2055
  if ( a->directfp )  
2050
2056
    {
2051
2057
      FILE *fp = a->directfp;
2052
 
 
2053
 
      if (!fstat (fileno (fp), &st))
2054
 
        return st.st_size;
2055
 
      log_error ("fstat() failed: %s\n", strerror (errno));
 
2058
      
 
2059
      if ( !fstat(fileno(fp), &st) )
 
2060
        return st.st_size;
 
2061
      log_error("fstat() failed: %s\n", strerror(errno) );
2056
2062
      return 0;
2057
2063
    }
2058
 
 
 
2064
  
2059
2065
  /* Hmmm: file_filter may have already been removed */
2060
 
  for (; a; a = a->chain)
2061
 
    if (!a->chain && a->filter == file_filter)
 
2066
  for ( ; a; a = a->chain )
 
2067
    if ( !a->chain && a->filter == file_filter )
2062
2068
      {
2063
 
        file_filter_ctx_t *b = a->filter_ov;
2064
 
        FILEP_OR_FD fp = b->fp;
2065
 
 
2066
 
#if defined(HAVE_DOSISH_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2067
 
        ulong size;
2068
 
 
2069
 
        if ((size = GetFileSize (fp, NULL)) != 0xffffffff)
2070
 
          return size;
2071
 
        log_error ("GetFileSize for handle %p failed: ec=%d\n",
2072
 
                   fp, (int) GetLastError ());
 
2069
        file_filter_ctx_t *b = a->filter_ov;
 
2070
        fp_or_fd_t fp = b->fp;
 
2071
        
 
2072
#if defined(HAVE_W32_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
 
2073
        ulong size;
 
2074
        static int (* __stdcall get_file_size_ex) (void *handle,
 
2075
                                                   LARGE_INTEGER *r_size);
 
2076
        static int get_file_size_ex_initialized;
 
2077
 
 
2078
        if (!get_file_size_ex_initialized)
 
2079
          {
 
2080
            void *handle;
 
2081
            
 
2082
            handle = dlopen ("kernel32.dll", RTLD_LAZY);
 
2083
            if (handle)
 
2084
              {
 
2085
                get_file_size_ex = dlsym (handle, "GetFileSizeEx");
 
2086
                if (!get_file_size_ex)
 
2087
                  dlclose (handle);
 
2088
              }
 
2089
            get_file_size_ex_initialized = 1;
 
2090
          }
 
2091
        
 
2092
        if (get_file_size_ex)
 
2093
          {
 
2094
            /* This is a newer system with GetFileSizeEx; we use this
 
2095
               then because it seem that GetFileSize won't return a
 
2096
               proper error in case a file is larger than 4GB. */
 
2097
            LARGE_INTEGER exsize;
 
2098
            
 
2099
            if (get_file_size_ex (fp, &exsize))
 
2100
              {
 
2101
                if (!exsize.u.HighPart)
 
2102
                  return exsize.u.LowPart;
 
2103
                if (overflow)
 
2104
                  *overflow = 1;
 
2105
                return 0; 
 
2106
              }
 
2107
          }
 
2108
        else
 
2109
          {
 
2110
            if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
 
2111
              return size;
 
2112
          }
 
2113
        log_error ("GetFileSize for handle %p failed: %s\n",
 
2114
                   fp, w32_strerror (0));
2073
2115
#else
2074
 
        if (!fstat (my_fileno (fp), &st))
2075
 
          return st.st_size;
2076
 
        log_error ("fstat() failed: %s\n", strerror (errno));
 
2116
        if ( !fstat(my_fileno(fp), &st) )
 
2117
          return st.st_size;
 
2118
        log_error("fstat() failed: %s\n", strerror(errno) );
2077
2119
#endif
2078
 
        break;
2079
 
      }
2080
 
 
2081
 
  return 0;
2082
 
}
 
2120
        break/*the for loop*/;
 
2121
      }
 
2122
  
 
2123
    return 0;
 
2124
}
 
2125
 
 
2126
 
 
2127
/* Return the file descriptor of the underlying file or -1 if it is
 
2128
   not available.  */
 
2129
int 
 
2130
iobuf_get_fd (iobuf_t a)
 
2131
{
 
2132
  if (a->directfp)
 
2133
    return fileno ( (FILE*)a->directfp );
 
2134
 
 
2135
  for ( ; a; a = a->chain )
 
2136
    if (!a->chain && a->filter == file_filter)
 
2137
      {
 
2138
        file_filter_ctx_t *b = a->filter_ov;
 
2139
        fp_or_fd_t fp = b->fp;
 
2140
 
 
2141
        return my_fileno (fp);
 
2142
      }
 
2143
 
 
2144
  return -1;
 
2145
}
 
2146
 
 
2147
 
2083
2148
 
2084
2149
/****************
2085
2150
 * Tell the file position, where the next read will take place
2159
2224
          return -1;
2160
2225
        }
2161
2226
#else
2162
 
#ifdef HAVE_DOSISH_SYSTEM
 
2227
#ifdef HAVE_W32_SYSTEM
2163
2228
      if (SetFilePointer (b->fp, newpos, NULL, FILE_BEGIN) == 0xffffffff)
2164
2229
        {
2165
2230
          log_error ("SetFilePointer failed on handle %p: ec=%d\n",
2233
2298
  return NULL;
2234
2299
}
2235
2300
 
2236
 
/****************
2237
 
 * Start the block write mode, see rfc1991.new for details.
2238
 
 * A value of 0 for N stops this mode (flushes and writes
2239
 
 * the end marker)
2240
 
 */
2241
 
void
2242
 
iobuf_set_block_mode (iobuf_t a, size_t n)
2243
 
{
2244
 
  block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
2245
 
 
2246
 
  assert (a->use == 1 || a->use == 2);
2247
 
  ctx->use = a->use;
2248
 
  if (!n)
2249
 
    {
2250
 
      if (a->use == 1)
2251
 
        log_debug ("pop_filter called in set_block_mode - please report\n");
2252
 
      pop_filter (a, block_filter, NULL);
2253
 
    }
2254
 
  else
2255
 
    {
2256
 
      ctx->size = n;            /* only needed for use 2 */
2257
 
      iobuf_push_filter (a, block_filter, ctx);
2258
 
    }
2259
 
}
2260
2301
 
2261
2302
/****************
2262
2303
 * enable partial block mode as described in the OpenPGP draft.
2286
2327
}
2287
2328
 
2288
2329
 
2289
 
/****************
2290
 
 * Checks whether the stream is in block mode
2291
 
 * Note: This does not work if other filters are pushed on the stream.
2292
 
 */
2293
 
int
2294
 
iobuf_in_block_mode (iobuf_t a)
2295
 
{
2296
 
  if (a && a->filter == block_filter)
2297
 
    return 1;                   /* yes */
2298
 
  return 0;                     /* no */
2299
 
}
2300
 
 
2301
2330
 
2302
2331
/****************
2303
2332
 * Same as fgets() but if the buffer is too short a larger one will
2362
2391
  return nbytes;
2363
2392
}
2364
2393
 
2365
 
/* This is the non iobuf specific function */
2366
 
int
2367
 
iobuf_translate_file_handle (int fd, int for_write)
2368
 
{
2369
 
#ifdef _WIN32
2370
 
  {
2371
 
    int x;
2372
 
 
2373
 
    if (fd <= 2)
2374
 
      return fd;                /* do not do this for error, stdin, stdout, stderr */
2375
 
 
2376
 
    x = _open_osfhandle (fd, for_write ? 1 : 0);
2377
 
    if (x == -1)
2378
 
      log_error ("failed to translate osfhandle %p\n", (void *) fd);
2379
 
    else
2380
 
      {
2381
 
        /*log_info ("_open_osfhandle %p yields %d%s\n",
2382
 
           (void*)fd, x, for_write? " for writing":"" ); */
2383
 
        fd = x;
2384
 
      }
2385
 
  }
2386
 
#endif
2387
 
  return fd;
2388
 
}
2389
 
 
2390
2394
static int
2391
2395
translate_file_handle (int fd, int for_write)
2392
2396
{
2393
 
#ifdef _WIN32
2394
 
#ifdef FILE_FILTER_USES_STDIO
2395
 
  fd = iobuf_translate_file_handle (fd, for_write);
2396
 
#else
 
2397
#ifdef HAVE_W32_SYSTEM
 
2398
# ifdef FILE_FILTER_USES_STDIO
 
2399
  fd = translate_sys2libc_fd (fd, for_write);
 
2400
# else
2397
2401
  {
2398
2402
    int x;
2399
2403
 
2412
2416
 
2413
2417
    fd = x;
2414
2418
  }
2415
 
#endif
 
2419
# endif
2416
2420
#endif
2417
2421
  return fd;
2418
2422
}
 
2423
 
 
2424
 
 
2425
void
 
2426
iobuf_skip_rest (iobuf_t a, unsigned long n, int partial)
 
2427
{
 
2428
  if ( partial )
 
2429
    {
 
2430
      for (;;)
 
2431
        {
 
2432
          if (a->nofast || a->d.start >= a->d.len) 
 
2433
            {
 
2434
              if (iobuf_readbyte (a) == -1)
 
2435
                {
 
2436
                  break;
 
2437
                }
 
2438
            } 
 
2439
          else
 
2440
            {
 
2441
              unsigned long count = a->d.len - a->d.start;
 
2442
              a->nbytes += count;
 
2443
              a->d.start = a->d.len;
 
2444
            }
 
2445
        }
 
2446
    } 
 
2447
  else
 
2448
    {
 
2449
      unsigned long remaining = n;
 
2450
      while (remaining > 0) 
 
2451
        {
 
2452
          if (a->nofast || a->d.start >= a->d.len)
 
2453
            {
 
2454
              if (iobuf_readbyte (a) == -1)
 
2455
                {
 
2456
                  break;
 
2457
                }
 
2458
              --remaining;
 
2459
            } 
 
2460
          else 
 
2461
            {
 
2462
              unsigned long count = a->d.len - a->d.start;
 
2463
              if (count > remaining) 
 
2464
                {
 
2465
                  count = remaining;
 
2466
                }
 
2467
              a->nbytes += count;
 
2468
              a->d.start += count;
 
2469
              remaining -= count;
 
2470
            }
 
2471
        }
 
2472
    }
 
2473
}