2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef HEADER_BSS_FILE_C
11 # define HEADER_BSS_FILE_C
13 # if defined(__linux) || defined(__sun) || defined(__hpux)
15 * Following definition aliases fopen to fopen64 on above mentioned
16 * platforms. This makes it possible to open and sequentially access files
17 * larger than 2GB from 32-bit application. It does not allow to traverse
18 * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
19 * platform permits that, not with fseek/ftell. Not to mention that breaking
20 * 2GB limit for seeking would require surgery to *our* API. But sequential
21 * access suffices for practical cases when you can run into large files,
22 * such as fingerprinting, so we can let API alone. For reference, the list
23 * of 32-bit platforms which allow for sequential access of large files
24 * without extra "magic" comprise *BSD, Darwin, IRIX...
26 # ifndef _FILE_OFFSET_BITS
27 # define _FILE_OFFSET_BITS 64
34 # include <openssl/err.h>
36 # if !defined(OPENSSL_NO_STDIO)
38 static int file_write(BIO *h, const char *buf, int num);
39 static int file_read(BIO *h, char *buf, int size);
40 static int file_puts(BIO *h, const char *str);
41 static int file_gets(BIO *h, char *str, int size);
42 static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
43 static int file_new(BIO *h);
44 static int file_free(BIO *data);
45 static const BIO_METHOD methods_filep = {
48 /* TODO: Convert to new style write function */
51 /* TODO: Convert to new style read function */
59 NULL, /* file_callback_ctrl */
62 BIO *BIO_new_file(const char *filename, const char *mode)
65 FILE *file = openssl_fopen(filename, mode);
66 int fp_flags = BIO_CLOSE;
68 if (strchr(mode, 'b') == NULL)
69 fp_flags |= BIO_FP_TEXT;
72 FUNCerr("fopen", get_last_sys_error());
73 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
79 BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
81 BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
84 if ((ret = BIO_new(BIO_s_file())) == NULL) {
89 /* we did fopen -> we disengage UPLINK */
90 BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
91 BIO_set_fp(ret, file, fp_flags);
95 BIO *BIO_new_fp(FILE *stream, int close_flag)
99 if ((ret = BIO_new(BIO_s_file())) == NULL)
102 /* redundant flag, left for documentation purposes */
103 BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
104 BIO_set_fp(ret, stream, close_flag);
108 const BIO_METHOD *BIO_s_file(void)
110 return &methods_filep;
113 static int file_new(BIO *bi)
118 bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
122 static int file_free(BIO *a)
127 if ((a->init) && (a->ptr != NULL)) {
128 if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
133 a->flags = BIO_FLAGS_UPLINK_INTERNAL;
140 static int file_read(BIO *b, char *out, int outl)
144 if (b->init && (out != NULL)) {
145 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
146 ret = UP_fread(out, 1, (int)outl, b->ptr);
148 ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
150 && (b->flags & BIO_FLAGS_UPLINK_INTERNAL
151 ? UP_ferror((FILE *)b->ptr) : ferror((FILE *)b->ptr))) {
152 FUNCerr("fread", get_last_sys_error());
153 BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB);
160 static int file_write(BIO *b, const char *in, int inl)
164 if (b->init && (in != NULL)) {
165 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
166 ret = UP_fwrite(in, (int)inl, 1, b->ptr);
168 ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
171 /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
173 * according to Tim Hudson <tjh@openssl.org>, the commented out
174 * version above can cause 'inl' write calls under some stupid stdio
175 * implementations (VMS)
181 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
184 FILE *fp = (FILE *)b->ptr;
190 case BIO_C_FILE_SEEK:
192 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
193 ret = (long)UP_fseek(b->ptr, num, 0);
195 ret = (long)fseek(fp, num, 0);
198 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
199 ret = (long)UP_feof(fp);
201 ret = (long)feof(fp);
203 case BIO_C_FILE_TELL:
205 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
206 ret = UP_ftell(b->ptr);
210 case BIO_C_SET_FILE_PTR:
212 b->shutdown = (int)num & BIO_CLOSE;
215 # if BIO_FLAGS_UPLINK_INTERNAL!=0
216 # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
217 # define _IOB_ENTRIES 20
219 /* Safety net to catch purely internal BIO_set_fp calls */
220 # if defined(_MSC_VER) && _MSC_VER>=1900
221 if (ptr == stdin || ptr == stdout || ptr == stderr)
222 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
223 # elif defined(_IOB_ENTRIES)
224 if ((size_t)ptr >= (size_t)stdin &&
225 (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
226 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
230 if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
231 UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
235 # if defined(OPENSSL_SYS_WINDOWS)
236 int fd = _fileno((FILE *)ptr);
237 if (num & BIO_FP_TEXT)
238 _setmode(fd, _O_TEXT);
240 _setmode(fd, _O_BINARY);
241 # elif defined(OPENSSL_SYS_MSDOS)
242 int fd = fileno((FILE *)ptr);
243 /* Set correct text/binary mode */
244 if (num & BIO_FP_TEXT)
245 _setmode(fd, _O_TEXT);
246 /* Dangerous to set stdin/stdout to raw (unless redirected) */
248 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
250 _setmode(fd, _O_BINARY);
252 _setmode(fd, _O_BINARY);
254 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
255 int fd = fileno((FILE *)ptr);
256 if (!(num & BIO_FP_TEXT))
257 setmode(fd, O_BINARY);
261 case BIO_C_SET_FILENAME:
263 b->shutdown = (int)num & BIO_CLOSE;
264 if (num & BIO_FP_APPEND) {
265 if (num & BIO_FP_READ)
266 OPENSSL_strlcpy(p, "a+", sizeof(p));
268 OPENSSL_strlcpy(p, "a", sizeof(p));
269 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
270 OPENSSL_strlcpy(p, "r+", sizeof(p));
271 else if (num & BIO_FP_WRITE)
272 OPENSSL_strlcpy(p, "w", sizeof(p));
273 else if (num & BIO_FP_READ)
274 OPENSSL_strlcpy(p, "r", sizeof(p));
276 BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE);
280 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
281 if (!(num & BIO_FP_TEXT))
282 OPENSSL_strlcat(p, "b", sizeof(p));
284 OPENSSL_strlcat(p, "t", sizeof(p));
285 # elif defined(OPENSSL_SYS_WIN32_CYGWIN)
286 if (!(num & BIO_FP_TEXT))
287 OPENSSL_strlcat(p, "b", sizeof(p));
289 fp = openssl_fopen(ptr, p);
291 FUNCerr("fopen", get_last_sys_error());
292 ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
293 BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
299 /* we did fopen -> we disengage UPLINK */
300 BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
302 case BIO_C_GET_FILE_PTR:
303 /* the ptr parameter is actually a FILE ** in this case. */
306 *fpp = (FILE *)b->ptr;
309 case BIO_CTRL_GET_CLOSE:
310 ret = (long)b->shutdown;
312 case BIO_CTRL_SET_CLOSE:
313 b->shutdown = (int)num;
316 st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
317 ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
319 FUNCerr("fflush", get_last_sys_error());
320 ERR_add_error_data(1, "fflush()");
321 BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB);
329 case BIO_CTRL_WPENDING:
330 case BIO_CTRL_PENDING:
340 static int file_gets(BIO *bp, char *buf, int size)
345 if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
346 if (!UP_fgets(buf, size, bp->ptr))
349 if (!fgets(buf, size, (FILE *)bp->ptr))
358 static int file_puts(BIO *bp, const char *str)
363 ret = file_write(bp, str, n);
369 static int file_write(BIO *b, const char *in, int inl)
373 static int file_read(BIO *b, char *out, int outl)
377 static int file_puts(BIO *bp, const char *str)
381 static int file_gets(BIO *bp, char *buf, int size)
385 static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
389 static int file_new(BIO *bi)
393 static int file_free(BIO *a)
398 static const BIO_METHOD methods_filep = {
401 /* TODO: Convert to new style write function */
404 /* TODO: Convert to new style read function */
412 NULL, /* file_callback_ctrl */
415 const BIO_METHOD *BIO_s_file(void)
417 return &methods_filep;
420 BIO *BIO_new_file(const char *filename, const char *mode)
425 # endif /* OPENSSL_NO_STDIO */
427 #endif /* HEADER_BSS_FILE_C */