Make sure the already existing X509_STORE->depth variable is initialized
[openssl.git] / apps / bss_file.c
1 /* crypto/bio/bss_file.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #define APPS_WIN16
60 #include <stdio.h>
61 #include <errno.h>
62 #include "cryptlib.h"
63 #include "bio.h"
64 #include "err.h"
65
66 #ifndef NOPROTO
67 static int MS_CALLBACK file_write(BIO *h,char *buf,int num);
68 static int MS_CALLBACK file_read(BIO *h,char *buf,int size);
69 static int MS_CALLBACK file_puts(BIO *h,char *str);
70 static int MS_CALLBACK file_gets(BIO *h,char *str,int size);
71 static long MS_CALLBACK file_ctrl(BIO *h,int cmd,long arg1,char *arg2);
72 static int MS_CALLBACK file_new(BIO *h);
73 static int MS_CALLBACK file_free(BIO *data);
74 #else
75 static int MS_CALLBACK file_write();
76 static int MS_CALLBACK file_read();
77 static int MS_CALLBACK file_puts();
78 static int MS_CALLBACK file_gets();
79 static long MS_CALLBACK file_ctrl();
80 static int MS_CALLBACK file_new();
81 static int MS_CALLBACK file_free();
82 #endif
83
84 static BIO_METHOD methods_filep=
85         {
86         BIO_TYPE_FILE,"FILE pointer",
87         file_write,
88         file_read,
89         file_puts,
90         file_gets,
91         file_ctrl,
92         file_new,
93         file_free,
94         };
95
96 BIO *BIO_new_file(filename,mode)
97 char *filename;
98 char *mode;
99         {
100         BIO *ret;
101         FILE *file;
102
103         if ((file=fopen(filename,mode)) == NULL)
104                 {
105                 SYSerr(SYS_F_FOPEN,errno);
106                 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
107                 return(NULL);
108                 }
109         if ((ret=BIO_new_fp(file,BIO_CLOSE)) == NULL)
110                 {
111                 fclose(file);
112                 return(NULL);
113                 }
114         return(ret);
115         }
116
117 BIO *BIO_new_fp(stream,close_flag)
118 FILE *stream;
119 int close_flag;
120         {
121         BIO *ret;
122
123         if ((ret=BIO_new(BIO_s_file())) == NULL)
124                 return(NULL);
125         BIO_set_fp(ret,stream,close_flag);
126         return(ret);
127         }
128
129 #if !defined(WIN16) || defined(APPS_WIN16)
130
131 BIO_METHOD *BIO_s_file()
132         {
133         return(&methods_filep);
134         }
135
136 #else
137
138 BIO_METHOD *BIO_s_file_internal_w16()
139         {
140         return(&methods_filep);
141         }
142
143 #endif
144
145 static int MS_CALLBACK file_new(bi)
146 BIO *bi;
147         {
148         bi->init=0;
149         bi->num=0;
150         bi->ptr=NULL;
151         return(1);
152         }
153
154 static int MS_CALLBACK file_free(a)
155 BIO *a;
156         {
157         if (a == NULL) return(0);
158         if (a->shutdown)
159                 {
160                 if ((a->init) && (a->ptr != NULL))
161                         {
162                         fclose((FILE *)a->ptr);
163                         a->ptr=NULL;
164                         }
165                 a->init=0;
166                 }
167         return(1);
168         }
169         
170 static int MS_CALLBACK file_read(b,out,outl)
171 BIO *b;
172 char *out;
173 int outl;
174         {
175         int ret=0;
176
177         if (b->init && (out != NULL))
178                 {
179                 ret=fread(out,1,(int)outl,(FILE *)b->ptr);
180                 }
181         return(ret);
182         }
183
184 static int MS_CALLBACK file_write(b,in,inl)
185 BIO *b;
186 char *in;
187 int inl;
188         {
189         int ret=0;
190
191         if (b->init && (in != NULL))
192                 {
193                 if (fwrite(in,(int)inl,1,(FILE *)b->ptr))
194                         ret=inl;
195                 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
196                 /* acording to Tim Hudson <tjh@cryptsoft.com>, the commented
197                  * out version above can cause 'inl' write calls under
198                  * some stupid stdio implementations (VMS) */
199                 }
200         return(ret);
201         }
202
203 static long MS_CALLBACK file_ctrl(b,cmd,num,ptr)
204 BIO *b;
205 int cmd;
206 long num;
207 char *ptr;
208         {
209         long ret=1;
210         FILE *fp=(FILE *)b->ptr;
211         FILE **fpp;
212         char p[4];
213
214         switch (cmd)
215                 {
216         case BIO_CTRL_RESET:
217                 ret=(long)fseek(fp,num,0);
218                 break;
219         case BIO_CTRL_EOF:
220                 ret=(long)feof(fp);
221                 break;
222         case BIO_CTRL_INFO:
223                 ret=ftell(fp);
224                 break;
225         case BIO_C_SET_FILE_PTR:
226                 file_free(b);
227                 b->shutdown=(int)num;
228                 b->ptr=(char *)ptr;
229                 b->init=1;
230                 break;
231         case BIO_C_SET_FILENAME:
232                 file_free(b);
233                 b->shutdown=(int)num&BIO_CLOSE;
234                 if (num & BIO_FP_APPEND)
235                         {
236                         if (num & BIO_FP_READ)
237                                 strcpy(p,"a+");
238                         else    strcpy(p,"a");
239                         }
240                 else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
241                         strcpy(p,"r+");
242                 else if (num & BIO_FP_WRITE)
243                         strcpy(p,"w");
244                 else if (num & BIO_FP_READ)
245                         strcpy(p,"r");
246                 else
247                         {
248                         BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE);
249                         ret=0;
250                         break;
251                         }
252 #if defined(MSDOS) || defined(WINDOWS)
253                 if (!(num & BIO_FP_TEXT))
254                         strcat(p,"b");
255                 else
256                         strcat(p,"t");
257 #endif
258                 fp=fopen(ptr,p);
259                 if (fp == NULL)
260                         {
261                         SYSerr(SYS_F_FOPEN,errno);
262                         BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB);
263                         ret=0;
264                         break;
265                         }
266                 b->ptr=(char *)fp;
267                 b->init=1;
268                 break;
269         case BIO_C_GET_FILE_PTR:
270                 /* the ptr parameter is actually a FILE ** in this case. */
271                 if (ptr != NULL)
272                         {
273                         fpp=(FILE **)ptr;
274                         *fpp=(FILE *)b->ptr;
275                         }
276                 break;
277         case BIO_CTRL_GET_CLOSE:
278                 ret=(long)b->shutdown;
279                 break;
280         case BIO_CTRL_SET_CLOSE:
281                 b->shutdown=(int)num;
282                 break;
283         case BIO_CTRL_FLUSH:
284                 fflush((FILE *)b->ptr);
285                 break;
286         case BIO_CTRL_DUP:
287                 ret=1;
288                 break;
289
290         case BIO_CTRL_PENDING:
291         case BIO_CTRL_PUSH:
292         case BIO_CTRL_POP:
293         default:
294                 ret=0;
295                 break;
296                 }
297         return(ret);
298         }
299
300 static int MS_CALLBACK file_gets(bp,buf,size)
301 BIO *bp;
302 char *buf;
303 int size;
304         {
305         int ret=0;
306
307         buf[0]='\0';
308         fgets(buf,size,(FILE *)bp->ptr);
309         if (buf[0] != '\0')
310                 ret=strlen(buf);
311         return(ret);
312         }
313
314 static int MS_CALLBACK file_puts(bp,str)
315 BIO *bp;
316 char *str;
317         {
318         int n,ret;
319
320         n=strlen(str);
321         ret=file_write(bp,str,n);
322         return(ret);
323         }
324