summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bss_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio/bss_file.c')
-rw-r--r--src/lib/libcrypto/bio/bss_file.c109
1 files changed, 12 insertions, 97 deletions
diff --git a/src/lib/libcrypto/bio/bss_file.c b/src/lib/libcrypto/bio/bss_file.c
index 4df9927c43..58fade9f29 100644
--- a/src/lib/libcrypto/bio/bss_file.c
+++ b/src/lib/libcrypto/bio/bss_file.c
@@ -65,34 +65,12 @@
65#ifndef HEADER_BSS_FILE_C 65#ifndef HEADER_BSS_FILE_C
66#define HEADER_BSS_FILE_C 66#define HEADER_BSS_FILE_C
67 67
68#if defined(__linux) || defined(__sun) || defined(__hpux)
69/* Following definition aliases fopen to fopen64 on above mentioned
70 * platforms. This makes it possible to open and sequentially access
71 * files larger than 2GB from 32-bit application. It does not allow to
72 * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
73 * 32-bit platform permits that, not with fseek/ftell. Not to mention
74 * that breaking 2GB limit for seeking would require surgery to *our*
75 * API. But sequential access suffices for practical cases when you
76 * can run into large files, such as fingerprinting, so we can let API
77 * alone. For reference, the list of 32-bit platforms which allow for
78 * sequential access of large files without extra "magic" comprise *BSD,
79 * Darwin, IRIX...
80 */
81#ifndef _FILE_OFFSET_BITS
82#define _FILE_OFFSET_BITS 64
83#endif
84#endif
85
86#include <stdio.h> 68#include <stdio.h>
87#include <errno.h> 69#include <errno.h>
88#include "cryptlib.h" 70#include "cryptlib.h"
89#include "bio_lcl.h" 71#include <openssl/bio.h>
90#include <openssl/err.h> 72#include <openssl/err.h>
91 73
92#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
93#include <nwfileio.h>
94#endif
95
96#if !defined(OPENSSL_NO_STDIO) 74#if !defined(OPENSSL_NO_STDIO)
97 75
98static int MS_CALLBACK file_write(BIO *h, const char *buf, int num); 76static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
@@ -132,12 +110,8 @@ BIO *BIO_new_file(const char *filename, const char *mode)
132 return(NULL); 110 return(NULL);
133 } 111 }
134 if ((ret=BIO_new(BIO_s_file_internal())) == NULL) 112 if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
135 {
136 fclose(file);
137 return(NULL); 113 return(NULL);
138 }
139 114
140 BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
141 BIO_set_fp(ret,file,BIO_CLOSE); 115 BIO_set_fp(ret,file,BIO_CLOSE);
142 return(ret); 116 return(ret);
143 } 117 }
@@ -149,7 +123,6 @@ BIO *BIO_new_fp(FILE *stream, int close_flag)
149 if ((ret=BIO_new(BIO_s_file())) == NULL) 123 if ((ret=BIO_new(BIO_s_file())) == NULL)
150 return(NULL); 124 return(NULL);
151 125
152 BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */
153 BIO_set_fp(ret,stream,close_flag); 126 BIO_set_fp(ret,stream,close_flag);
154 return(ret); 127 return(ret);
155 } 128 }
@@ -164,7 +137,6 @@ static int MS_CALLBACK file_new(BIO *bi)
164 bi->init=0; 137 bi->init=0;
165 bi->num=0; 138 bi->num=0;
166 bi->ptr=NULL; 139 bi->ptr=NULL;
167 bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */
168 return(1); 140 return(1);
169 } 141 }
170 142
@@ -175,12 +147,8 @@ static int MS_CALLBACK file_free(BIO *a)
175 { 147 {
176 if ((a->init) && (a->ptr != NULL)) 148 if ((a->init) && (a->ptr != NULL))
177 { 149 {
178 if (a->flags&BIO_FLAGS_UPLINK) 150 fclose((FILE *)a->ptr);
179 UP_fclose (a->ptr);
180 else
181 fclose (a->ptr);
182 a->ptr=NULL; 151 a->ptr=NULL;
183 a->flags=BIO_FLAGS_UPLINK;
184 } 152 }
185 a->init=0; 153 a->init=0;
186 } 154 }
@@ -193,11 +161,8 @@ static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
193 161
194 if (b->init && (out != NULL)) 162 if (b->init && (out != NULL))
195 { 163 {
196 if (b->flags&BIO_FLAGS_UPLINK) 164 ret=fread(out,1,(int)outl,(FILE *)b->ptr);
197 ret=UP_fread(out,1,(int)outl,b->ptr); 165 if(ret == 0 && ferror((FILE *)b->ptr))
198 else
199 ret=fread(out,1,(int)outl,(FILE *)b->ptr);
200 if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr))
201 { 166 {
202 SYSerr(SYS_F_FREAD,get_last_sys_error()); 167 SYSerr(SYS_F_FREAD,get_last_sys_error());
203 BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB); 168 BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB);
@@ -213,11 +178,7 @@ static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
213 178
214 if (b->init && (in != NULL)) 179 if (b->init && (in != NULL))
215 { 180 {
216 if (b->flags&BIO_FLAGS_UPLINK) 181 if (fwrite(in,(int)inl,1,(FILE *)b->ptr))
217 ret=UP_fwrite(in,(int)inl,1,b->ptr);
218 else
219 ret=fwrite(in,(int)inl,1,(FILE *)b->ptr);
220 if (ret)
221 ret=inl; 182 ret=inl;
222 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ 183 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
223 /* according to Tim Hudson <tjh@cryptsoft.com>, the commented 184 /* according to Tim Hudson <tjh@cryptsoft.com>, the commented
@@ -238,45 +199,20 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
238 { 199 {
239 case BIO_C_FILE_SEEK: 200 case BIO_C_FILE_SEEK:
240 case BIO_CTRL_RESET: 201 case BIO_CTRL_RESET:
241 if (b->flags&BIO_FLAGS_UPLINK) 202 ret=(long)fseek(fp,num,SEEK_SET);
242 ret=(long)UP_fseek(b->ptr,num,0);
243 else
244 ret=(long)fseek(fp,num,0);
245 break; 203 break;
246 case BIO_CTRL_EOF: 204 case BIO_CTRL_EOF:
247 if (b->flags&BIO_FLAGS_UPLINK) 205 ret=(long)feof(fp);
248 ret=(long)UP_feof(fp);
249 else
250 ret=(long)feof(fp);
251 break; 206 break;
252 case BIO_C_FILE_TELL: 207 case BIO_C_FILE_TELL:
253 case BIO_CTRL_INFO: 208 case BIO_CTRL_INFO:
254 if (b->flags&BIO_FLAGS_UPLINK) 209 ret=ftell(fp);
255 ret=UP_ftell(b->ptr);
256 else
257 ret=ftell(fp);
258 break; 210 break;
259 case BIO_C_SET_FILE_PTR: 211 case BIO_C_SET_FILE_PTR:
260 file_free(b); 212 file_free(b);
261 b->shutdown=(int)num&BIO_CLOSE; 213 b->shutdown=(int)num&BIO_CLOSE;
262 b->ptr=ptr; 214 b->ptr=(char *)ptr;
263 b->init=1; 215 b->init=1;
264#if BIO_FLAGS_UPLINK!=0
265#if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
266#define _IOB_ENTRIES 20
267#endif
268#if defined(_IOB_ENTRIES)
269 /* Safety net to catch purely internal BIO_set_fp calls */
270 if ((size_t)ptr >= (size_t)stdin &&
271 (size_t)ptr < (size_t)(stdin+_IOB_ENTRIES))
272 BIO_clear_flags(b,BIO_FLAGS_UPLINK);
273#endif
274#endif
275#ifdef UP_fsetmode
276 if (b->flags&BIO_FLAGS_UPLINK)
277 UP_fsetmode(b->ptr,num&BIO_FP_TEXT?'t':'b');
278 else
279#endif
280 { 216 {
281#if defined(OPENSSL_SYS_WINDOWS) 217#if defined(OPENSSL_SYS_WINDOWS)
282 int fd = fileno((FILE*)ptr); 218 int fd = fileno((FILE*)ptr);
@@ -284,14 +220,6 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
284 _setmode(fd,_O_TEXT); 220 _setmode(fd,_O_TEXT);
285 else 221 else
286 _setmode(fd,_O_BINARY); 222 _setmode(fd,_O_BINARY);
287#elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
288 int fd = fileno((FILE*)ptr);
289 /* Under CLib there are differences in file modes
290 */
291 if (num & BIO_FP_TEXT)
292 setmode(fd,O_TEXT);
293 else
294 setmode(fd,O_BINARY);
295#elif defined(OPENSSL_SYS_MSDOS) 223#elif defined(OPENSSL_SYS_MSDOS)
296 int fd = fileno((FILE*)ptr); 224 int fd = fileno((FILE*)ptr);
297 /* Set correct text/binary mode */ 225 /* Set correct text/binary mode */
@@ -344,12 +272,6 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
344 else 272 else
345 strcat(p,"t"); 273 strcat(p,"t");
346#endif 274#endif
347#if defined(OPENSSL_SYS_NETWARE)
348 if (!(num & BIO_FP_TEXT))
349 strcat(p,"b");
350 else
351 strcat(p,"t");
352#endif
353 fp=fopen(ptr,p); 275 fp=fopen(ptr,p);
354 if (fp == NULL) 276 if (fp == NULL)
355 { 277 {
@@ -359,9 +281,8 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
359 ret=0; 281 ret=0;
360 break; 282 break;
361 } 283 }
362 b->ptr=fp; 284 b->ptr=(char *)fp;
363 b->init=1; 285 b->init=1;
364 BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
365 break; 286 break;
366 case BIO_C_GET_FILE_PTR: 287 case BIO_C_GET_FILE_PTR:
367 /* the ptr parameter is actually a FILE ** in this case. */ 288 /* the ptr parameter is actually a FILE ** in this case. */
@@ -378,10 +299,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
378 b->shutdown=(int)num; 299 b->shutdown=(int)num;
379 break; 300 break;
380 case BIO_CTRL_FLUSH: 301 case BIO_CTRL_FLUSH:
381 if (b->flags&BIO_FLAGS_UPLINK) 302 fflush((FILE *)b->ptr);
382 UP_fflush(b->ptr);
383 else
384 fflush((FILE *)b->ptr);
385 break; 303 break;
386 case BIO_CTRL_DUP: 304 case BIO_CTRL_DUP:
387 ret=1; 305 ret=1;
@@ -403,10 +321,7 @@ static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
403 int ret=0; 321 int ret=0;
404 322
405 buf[0]='\0'; 323 buf[0]='\0';
406 if (bp->flags&BIO_FLAGS_UPLINK) 324 fgets(buf,size,(FILE *)bp->ptr);
407 UP_fgets(buf,size,bp->ptr);
408 else
409 fgets(buf,size,(FILE *)bp->ptr);
410 if (buf[0] != '\0') 325 if (buf[0] != '\0')
411 ret=strlen(buf); 326 ret=strlen(buf);
412 return(ret); 327 return(ret);