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.
4
5
* This file is part of GnuPG.
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.
11
12
* GnuPG is distributed in the hope that it will be useful,
29
29
#include <sys/stat.h>
31
31
#include <unistd.h>
32
#ifdef HAVE_DOSISH_SYSTEM
32
#ifdef HAVE_W32_SYSTEM
38
38
#endif /* __riscos__ */
44
/*-- Begin configurable part. --*/
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
51
/* We don't want to use the STDIO based backend. */
44
52
#undef FILE_FILTER_USES_STDIO
46
#ifdef HAVE_DOSISH_SYSTEM
54
/*-- End configurable part. --*/
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*/
64
/* Definition of constants and macros used by our file filter
65
implementation. What we define here are 3 macros to make the
69
Is expanded to fileno(a) if using a stdion backend and to a if we
70
are using the low-level backend.
73
Is defined to fopen for the stdio backend and to direct_open if
74
we are using the low-evel backend.
77
Is defined to fopen for the stdio backend and to fd_cache_open if
78
we are using the low-evel backend.
81
Is the type we use for the backend stream or fiel descriptor.
83
INVALID_FP, FILEP_OR_FD_FOR_STDIN, FILEP_OR_FD_FOR_STDOUT
84
Are macros defined depending on the used backend.
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)
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 */
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))
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*/
115
/* The context used by the file filter. */
118
fp_or_fd_t fp; /* Open file pointer or handle. */
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. */
92
125
file_filter_ctx_t;
128
/* If we are not using stdio as the backend we make use of a "close
130
#ifndef FILE_FILTER_USES_STDIO
94
131
struct close_cache_s
96
133
struct close_cache_s *next;
100
typedef struct close_cache_s *CLOSE_CACHE;
101
static CLOSE_CACHE close_cache;
137
typedef struct close_cache_s *close_cache_t;
138
static close_cache_t close_cache;
139
#endif /*!FILE_FILTER_USES_STDIO*/
143
#ifdef HAVE_W32_SYSTEM
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 */
114
153
sock_filter_ctx_t;
154
#endif /*HAVE_W32_SYSTEM*/
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
162
/* The context we use for the block filter (used to handle OpenPGP
163
length information header). */
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). */
134
175
block_filter_ctx_t;
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;
183
/* Local prototypes. */
138
184
static int underflow (iobuf_t a);
139
185
static int translate_file_handle (int fd, int for_write);
141
189
#ifndef FILE_FILTER_USES_STDIO
144
191
* Invalidate (i.e. close) a cached iobuf
147
194
fd_cache_invalidate (const char *fname)
2042
* Return the length of an open file
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. */
2045
iobuf_get_filelength (iobuf_t a)
2048
iobuf_get_filelength (iobuf_t a, int *overflow)
2047
2050
struct stat st;
2051
2057
FILE *fp = a->directfp;
2053
if (!fstat (fileno (fp), &st))
2055
log_error ("fstat() failed: %s\n", strerror (errno));
2059
if ( !fstat(fileno(fp), &st) )
2061
log_error("fstat() failed: %s\n", strerror(errno) );
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 )
2063
file_filter_ctx_t *b = a->filter_ov;
2064
FILEP_OR_FD fp = b->fp;
2066
#if defined(HAVE_DOSISH_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2069
if ((size = GetFileSize (fp, NULL)) != 0xffffffff)
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;
2072
#if defined(HAVE_W32_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
2074
static int (* __stdcall get_file_size_ex) (void *handle,
2075
LARGE_INTEGER *r_size);
2076
static int get_file_size_ex_initialized;
2078
if (!get_file_size_ex_initialized)
2082
handle = dlopen ("kernel32.dll", RTLD_LAZY);
2085
get_file_size_ex = dlsym (handle, "GetFileSizeEx");
2086
if (!get_file_size_ex)
2089
get_file_size_ex_initialized = 1;
2092
if (get_file_size_ex)
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;
2099
if (get_file_size_ex (fp, &exsize))
2101
if (!exsize.u.HighPart)
2102
return exsize.u.LowPart;
2110
if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
2113
log_error ("GetFileSize for handle %p failed: %s\n",
2114
fp, w32_strerror (0));
2074
if (!fstat (my_fileno (fp), &st))
2076
log_error ("fstat() failed: %s\n", strerror (errno));
2116
if ( !fstat(my_fileno(fp), &st) )
2118
log_error("fstat() failed: %s\n", strerror(errno) );
2120
break/*the for loop*/;
2127
/* Return the file descriptor of the underlying file or -1 if it is
2130
iobuf_get_fd (iobuf_t a)
2133
return fileno ( (FILE*)a->directfp );
2135
for ( ; a; a = a->chain )
2136
if (!a->chain && a->filter == file_filter)
2138
file_filter_ctx_t *b = a->filter_ov;
2139
fp_or_fd_t fp = b->fp;
2141
return my_fileno (fp);
2084
2149
/****************
2085
2150
* Tell the file position, where the next read will take place
2365
/* This is the non iobuf specific function */
2367
iobuf_translate_file_handle (int fd, int for_write)
2374
return fd; /* do not do this for error, stdin, stdout, stderr */
2376
x = _open_osfhandle (fd, for_write ? 1 : 0);
2378
log_error ("failed to translate osfhandle %p\n", (void *) fd);
2381
/*log_info ("_open_osfhandle %p yields %d%s\n",
2382
(void*)fd, x, for_write? " for writing":"" ); */
2391
2395
translate_file_handle (int fd, int for_write)
2394
#ifdef FILE_FILTER_USES_STDIO
2395
fd = iobuf_translate_file_handle (fd, for_write);
2397
#ifdef HAVE_W32_SYSTEM
2398
# ifdef FILE_FILTER_USES_STDIO
2399
fd = translate_sys2libc_fd (fd, for_write);