sl@0: /* crypto/bio/bss_file.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* sl@0: * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout sl@0: * with binary data (e.g. asn1parse -inform DER < xxx) under sl@0: * Windows sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #ifndef HEADER_BSS_FILE_C sl@0: #define HEADER_BSS_FILE_C sl@0: sl@0: #if defined(__linux) || defined(__sun) || defined(__hpux) sl@0: /* Following definition aliases fopen to fopen64 on above mentioned sl@0: * platforms. This makes it possible to open and sequentially access sl@0: * files larger than 2GB from 32-bit application. It does not allow to sl@0: * traverse them beyond 2GB with fseek/ftell, but on the other hand *no* sl@0: * 32-bit platform permits that, not with fseek/ftell. Not to mention sl@0: * that breaking 2GB limit for seeking would require surgery to *our* sl@0: * API. But sequential access suffices for practical cases when you sl@0: * can run into large files, such as fingerprinting, so we can let API sl@0: * alone. For reference, the list of 32-bit platforms which allow for sl@0: * sequential access of large files without extra "magic" comprise *BSD, sl@0: * Darwin, IRIX... sl@0: */ sl@0: #ifndef _FILE_OFFSET_BITS sl@0: #define _FILE_OFFSET_BITS 64 sl@0: #endif sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include "bio_lcl.h" sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: #if !defined(OPENSSL_NO_STDIO) sl@0: sl@0: static int MS_CALLBACK file_write(BIO *h, const char *buf, int num); sl@0: static int MS_CALLBACK file_read(BIO *h, char *buf, int size); sl@0: static int MS_CALLBACK file_puts(BIO *h, const char *str); sl@0: static int MS_CALLBACK file_gets(BIO *h, char *str, int size); sl@0: static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2); sl@0: static int MS_CALLBACK file_new(BIO *h); sl@0: static int MS_CALLBACK file_free(BIO *data); sl@0: #ifndef EMULATOR sl@0: static BIO_METHOD methods_filep= sl@0: { sl@0: BIO_TYPE_FILE, sl@0: "FILE pointer", sl@0: file_write, sl@0: file_read, sl@0: file_puts, sl@0: file_gets, sl@0: file_ctrl, sl@0: file_new, sl@0: file_free, sl@0: NULL, sl@0: }; sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(methods_filep,bss_file,BIO_METHOD) sl@0: #define methods_filep (*GET_WSD_VAR_NAME(methods_filep,bss_file,s)()) sl@0: const BIO_METHOD temp_s_methods_filep= sl@0: { sl@0: BIO_TYPE_FILE, sl@0: "FILE pointer", sl@0: file_write, sl@0: file_read, sl@0: file_puts, sl@0: file_gets, sl@0: file_ctrl, sl@0: file_new, sl@0: file_free, sl@0: NULL, sl@0: }; sl@0: sl@0: #endif sl@0: sl@0: sl@0: EXPORT_C BIO *BIO_new_file(const char *filename, const char *mode) sl@0: { sl@0: BIO *ret; sl@0: FILE *file; sl@0: sl@0: if ((file=fopen(filename,mode)) == NULL) sl@0: { sl@0: SYSerr(SYS_F_FOPEN,get_last_sys_error()); sl@0: ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); sl@0: if (errno == ENOENT) sl@0: BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE); sl@0: else sl@0: BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); sl@0: return(NULL); sl@0: } sl@0: if ((ret=BIO_new(BIO_s_file_internal())) == NULL) sl@0: { sl@0: fclose(file); sl@0: return(NULL); sl@0: } sl@0: sl@0: BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */ sl@0: BIO_set_fp(ret,file,BIO_CLOSE); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C BIO *BIO_new_fp(FILE *stream, int close_flag) sl@0: { sl@0: BIO *ret; sl@0: sl@0: if ((ret=BIO_new(BIO_s_file())) == NULL) sl@0: return(NULL); sl@0: sl@0: BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */ sl@0: BIO_set_fp(ret,stream,close_flag); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C BIO_METHOD *BIO_s_file(void) sl@0: { sl@0: return(&methods_filep); sl@0: } sl@0: sl@0: static int MS_CALLBACK file_new(BIO *bi) sl@0: { sl@0: bi->init=0; sl@0: bi->num=0; sl@0: bi->ptr=NULL; sl@0: bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */ sl@0: return(1); sl@0: } sl@0: sl@0: static int MS_CALLBACK file_free(BIO *a) sl@0: { sl@0: if (a == NULL) return(0); sl@0: if (a->shutdown) sl@0: { sl@0: if ((a->init) && (a->ptr != NULL)) sl@0: { sl@0: if (a->flags&BIO_FLAGS_UPLINK) sl@0: UP_fclose (a->ptr); sl@0: else sl@0: fclose (a->ptr); sl@0: a->ptr=NULL; sl@0: a->flags=BIO_FLAGS_UPLINK; sl@0: } sl@0: a->init=0; sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: static int MS_CALLBACK file_read(BIO *b, char *out, int outl) sl@0: { sl@0: int ret=0; sl@0: sl@0: if (b->init && (out != NULL)) sl@0: { sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: ret=UP_fread(out,1,(int)outl,b->ptr); sl@0: else sl@0: ret=fread(out,1,(int)outl,(FILE *)b->ptr); sl@0: if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr)) sl@0: { sl@0: SYSerr(SYS_F_FREAD,get_last_sys_error()); sl@0: BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB); sl@0: ret=-1; sl@0: } sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static int MS_CALLBACK file_write(BIO *b, const char *in, int inl) sl@0: { sl@0: int ret=0; sl@0: sl@0: if (b->init && (in != NULL)) sl@0: { sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: ret=UP_fwrite(in,(int)inl,1,b->ptr); sl@0: else sl@0: ret=fwrite(in,(int)inl,1,(FILE *)b->ptr); sl@0: if (ret) sl@0: ret=inl; sl@0: /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ sl@0: /* according to Tim Hudson , the commented sl@0: * out version above can cause 'inl' write calls under sl@0: * some stupid stdio implementations (VMS) */ sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) sl@0: { sl@0: long ret=1; sl@0: FILE *fp=(FILE *)b->ptr; sl@0: FILE **fpp; sl@0: char p[4]; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case BIO_C_FILE_SEEK: sl@0: case BIO_CTRL_RESET: sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: ret=(long)UP_fseek(b->ptr,num,0); sl@0: else sl@0: ret=(long)fseek(fp,num,0); sl@0: break; sl@0: case BIO_CTRL_EOF: sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: ret=(long)UP_feof(fp); sl@0: else sl@0: ret=(long)feof(fp); sl@0: break; sl@0: case BIO_C_FILE_TELL: sl@0: case BIO_CTRL_INFO: sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: ret=UP_ftell(b->ptr); sl@0: else sl@0: ret=ftell(fp); sl@0: break; sl@0: case BIO_C_SET_FILE_PTR: sl@0: file_free(b); sl@0: b->shutdown=(int)num&BIO_CLOSE; sl@0: b->ptr=ptr; sl@0: b->init=1; sl@0: #if BIO_FLAGS_UPLINK!=0 sl@0: #if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES) sl@0: #define _IOB_ENTRIES 20 sl@0: #endif sl@0: #if defined(_IOB_ENTRIES) sl@0: /* Safety net to catch purely internal BIO_set_fp calls */ sl@0: if ((size_t)ptr >= (size_t)stdin && sl@0: (size_t)ptr < (size_t)(stdin+_IOB_ENTRIES)) sl@0: BIO_clear_flags(b,BIO_FLAGS_UPLINK); sl@0: #endif sl@0: #endif sl@0: #ifdef UP_fsetmode sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: UP_fsetmode(b->ptr,num&BIO_FP_TEXT?'t':'b'); sl@0: else sl@0: #endif sl@0: { sl@0: #if defined(OPENSSL_SYS_WINDOWS) sl@0: int fd = fileno((FILE*)ptr); sl@0: if (num & BIO_FP_TEXT) sl@0: _setmode(fd,_O_TEXT); sl@0: else sl@0: _setmode(fd,_O_BINARY); sl@0: #elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) sl@0: int fd = fileno((FILE*)ptr); sl@0: /* Under CLib there are differences in file modes sl@0: */ sl@0: if (num & BIO_FP_TEXT) sl@0: _setmode(fd,O_TEXT); sl@0: else sl@0: _setmode(fd,O_BINARY); sl@0: #elif defined(OPENSSL_SYS_MSDOS) sl@0: int fd = fileno((FILE*)ptr); sl@0: /* Set correct text/binary mode */ sl@0: if (num & BIO_FP_TEXT) sl@0: _setmode(fd,_O_TEXT); sl@0: /* Dangerous to set stdin/stdout to raw (unless redirected) */ sl@0: else sl@0: { sl@0: if (fd == STDIN_FILENO || fd == STDOUT_FILENO) sl@0: { sl@0: if (isatty(fd) <= 0) sl@0: _setmode(fd,_O_BINARY); sl@0: } sl@0: else sl@0: _setmode(fd,_O_BINARY); sl@0: } sl@0: #elif defined(OPENSSL_SYS_OS2) sl@0: int fd = fileno((FILE*)ptr); sl@0: if (num & BIO_FP_TEXT) sl@0: setmode(fd, O_TEXT); sl@0: else sl@0: setmode(fd, O_BINARY); sl@0: #endif sl@0: } sl@0: break; sl@0: case BIO_C_SET_FILENAME: sl@0: file_free(b); sl@0: b->shutdown=(int)num&BIO_CLOSE; sl@0: if (num & BIO_FP_APPEND) sl@0: { sl@0: if (num & BIO_FP_READ) sl@0: BUF_strlcpy(p,"a+",sizeof p); sl@0: else BUF_strlcpy(p,"a",sizeof p); sl@0: } sl@0: else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) sl@0: BUF_strlcpy(p,"r+",sizeof p); sl@0: else if (num & BIO_FP_WRITE) sl@0: BUF_strlcpy(p,"w",sizeof p); sl@0: else if (num & BIO_FP_READ) sl@0: BUF_strlcpy(p,"r",sizeof p); sl@0: else sl@0: { sl@0: BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE); sl@0: ret=0; sl@0: break; sl@0: } sl@0: #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN) sl@0: if (!(num & BIO_FP_TEXT)) sl@0: strcat(p,"b"); sl@0: else sl@0: strcat(p,"t"); sl@0: #endif sl@0: #if defined(OPENSSL_SYS_NETWARE) sl@0: if (!(num & BIO_FP_TEXT)) sl@0: strcat(p,"b"); sl@0: else sl@0: strcat(p,"t"); sl@0: #endif sl@0: fp=fopen(ptr,p); sl@0: if (fp == NULL) sl@0: { sl@0: SYSerr(SYS_F_FOPEN,get_last_sys_error()); sl@0: ERR_add_error_data(5,"fopen('",ptr,"','",p,"')"); sl@0: BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB); sl@0: ret=0; sl@0: break; sl@0: } sl@0: b->ptr=fp; sl@0: b->init=1; sl@0: BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */ sl@0: break; sl@0: case BIO_C_GET_FILE_PTR: sl@0: /* the ptr parameter is actually a FILE ** in this case. */ sl@0: if (ptr != NULL) sl@0: { sl@0: fpp=(FILE **)ptr; sl@0: *fpp=(FILE *)b->ptr; sl@0: } sl@0: break; sl@0: case BIO_CTRL_GET_CLOSE: sl@0: ret=(long)b->shutdown; sl@0: break; sl@0: case BIO_CTRL_SET_CLOSE: sl@0: b->shutdown=(int)num; sl@0: break; sl@0: case BIO_CTRL_FLUSH: sl@0: if (b->flags&BIO_FLAGS_UPLINK) sl@0: UP_fflush(b->ptr); sl@0: else sl@0: fflush((FILE *)b->ptr); sl@0: break; sl@0: case BIO_CTRL_DUP: sl@0: ret=1; sl@0: break; sl@0: sl@0: case BIO_CTRL_WPENDING: sl@0: case BIO_CTRL_PENDING: sl@0: case BIO_CTRL_PUSH: sl@0: case BIO_CTRL_POP: sl@0: default: sl@0: ret=0; sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size) sl@0: { sl@0: int ret=0; sl@0: sl@0: buf[0]='\0'; sl@0: if (bp->flags&BIO_FLAGS_UPLINK) sl@0: UP_fgets(buf,size,bp->ptr); sl@0: else sl@0: fgets(buf,size,(FILE *)bp->ptr); sl@0: if (buf[0] != '\0') sl@0: ret=strlen(buf); sl@0: return(ret); sl@0: } sl@0: sl@0: static int MS_CALLBACK file_puts(BIO *bp, const char *str) sl@0: { sl@0: int n,ret; sl@0: sl@0: n=strlen(str); sl@0: ret=file_write(bp,str,n); sl@0: return(ret); sl@0: } sl@0: sl@0: #endif /* OPENSSL_NO_STDIO */ sl@0: sl@0: #endif /* HEADER_BSS_FILE_C */ sl@0: sl@0: