file.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #if defined(__linux) || defined(__sun) || defined(__hpux)
58 // Following definition aliases fopen to fopen64 on above mentioned
59 // platforms. This makes it possible to open and sequentially access
60 // files larger than 2GB from 32-bit application. It does not allow to
61 // traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
62 // 32-bit platform permits that, not with fseek/ftell. Not to mention
63 // that breaking 2GB limit for seeking would require surgery to *our*
64 // API. But sequential access suffices for practical cases when you
65 // can run into large files, such as fingerprinting, so we can let API
66 // alone. For reference, the list of 32-bit platforms which allow for
67 // sequential access of large files without extra "magic" comprise *BSD,
68 // Darwin, IRIX...
69 #ifndef _FILE_OFFSET_BITS
70 #define _FILE_OFFSET_BITS 64
71 #endif
72 #endif
73 
74 #include <openssl/bio.h>
75 
76 #if !defined(OPENSSL_TRUSTY)
77 
78 #include <errno.h>
79 #include <stdio.h>
80 #include <string.h>
81 
82 #include <openssl/err.h>
83 #include <openssl/mem.h>
84 
85 #include "../internal.h"
86 
87 
88 #define BIO_FP_READ 0x02
89 #define BIO_FP_WRITE 0x04
90 #define BIO_FP_APPEND 0x08
91 
92 BIO *BIO_new_file(const char *filename, const char *mode) {
93  BIO *ret;
94  FILE *file;
95 
96  file = fopen(filename, mode);
97  if (file == NULL) {
99 
100  ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
101  if (errno == ENOENT) {
103  } else {
105  }
106  return NULL;
107  }
108 
110  if (ret == NULL) {
111  fclose(file);
112  return NULL;
113  }
114 
115  return ret;
116 }
117 
118 BIO *BIO_new_fp(FILE *stream, int close_flag) {
119  BIO *ret = BIO_new(BIO_s_file());
120 
121  if (ret == NULL) {
122  return NULL;
123  }
124 
125  BIO_set_fp(ret, stream, close_flag);
126  return ret;
127 }
128 
129 static int file_free(BIO *bio) {
130  if (!bio->shutdown) {
131  return 1;
132  }
133 
134  if (bio->init && bio->ptr != NULL) {
135  fclose(bio->ptr);
136  bio->ptr = NULL;
137  }
138  bio->init = 0;
139 
140  return 1;
141 }
142 
143 static int file_read(BIO *b, char *out, int outl) {
144  if (!b->init) {
145  return 0;
146  }
147 
148  size_t ret = fread(out, 1, outl, (FILE *)b->ptr);
149  if (ret == 0 && ferror((FILE *)b->ptr)) {
152  return -1;
153  }
154 
155  // fread reads at most |outl| bytes, so |ret| fits in an int.
156  return (int)ret;
157 }
158 
159 static int file_write(BIO *b, const char *in, int inl) {
160  int ret = 0;
161 
162  if (!b->init) {
163  return 0;
164  }
165 
166  ret = fwrite(in, inl, 1, (FILE *)b->ptr);
167  if (ret > 0) {
168  ret = inl;
169  }
170  return ret;
171 }
172 
173 static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
174  long ret = 1;
175  FILE *fp = (FILE *)b->ptr;
176  FILE **fpp;
177  char p[4];
178 
179  switch (cmd) {
180  case BIO_CTRL_RESET:
181  num = 0;
183  case BIO_C_FILE_SEEK:
184  ret = (long)fseek(fp, num, 0);
185  break;
186  case BIO_CTRL_EOF:
187  ret = (long)feof(fp);
188  break;
189  case BIO_C_FILE_TELL:
190  case BIO_CTRL_INFO:
191  ret = ftell(fp);
192  break;
193  case BIO_C_SET_FILE_PTR:
194  file_free(b);
195  b->shutdown = (int)num & BIO_CLOSE;
196  b->ptr = ptr;
197  b->init = 1;
198  break;
199  case BIO_C_SET_FILENAME:
200  file_free(b);
201  b->shutdown = (int)num & BIO_CLOSE;
202  if (num & BIO_FP_APPEND) {
203  if (num & BIO_FP_READ) {
204  OPENSSL_strlcpy(p, "a+", sizeof(p));
205  } else {
206  OPENSSL_strlcpy(p, "a", sizeof(p));
207  }
208  } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
209  OPENSSL_strlcpy(p, "r+", sizeof(p));
210  } else if (num & BIO_FP_WRITE) {
211  OPENSSL_strlcpy(p, "w", sizeof(p));
212  } else if (num & BIO_FP_READ) {
213  OPENSSL_strlcpy(p, "r", sizeof(p));
214  } else {
216  ret = 0;
217  break;
218  }
219  fp = fopen(ptr, p);
220  if (fp == NULL) {
222  ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
224  ret = 0;
225  break;
226  }
227  b->ptr = fp;
228  b->init = 1;
229  break;
230  case BIO_C_GET_FILE_PTR:
231  // the ptr parameter is actually a FILE ** in this case.
232  if (ptr != NULL) {
233  fpp = (FILE **)ptr;
234  *fpp = (FILE *)b->ptr;
235  }
236  break;
237  case BIO_CTRL_GET_CLOSE:
238  ret = (long)b->shutdown;
239  break;
240  case BIO_CTRL_SET_CLOSE:
241  b->shutdown = (int)num;
242  break;
243  case BIO_CTRL_FLUSH:
244  ret = 0 == fflush((FILE *)b->ptr);
245  break;
246  case BIO_CTRL_WPENDING:
247  case BIO_CTRL_PENDING:
248  default:
249  ret = 0;
250  break;
251  }
252  return ret;
253 }
254 
255 static int file_gets(BIO *bp, char *buf, int size) {
256  int ret = 0;
257 
258  if (size == 0) {
259  return 0;
260  }
261 
262  if (!fgets(buf, size, (FILE *)bp->ptr)) {
263  buf[0] = 0;
264  goto err;
265  }
266  ret = strlen(buf);
267 
268 err:
269  return ret;
270 }
271 
272 static const BIO_METHOD methods_filep = {
273  BIO_TYPE_FILE, "FILE pointer",
275  NULL /* puts */, file_gets,
276  file_ctrl, NULL /* create */,
277  file_free, NULL /* callback_ctrl */,
278 };
279 
280 const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
281 
282 
283 int BIO_get_fp(BIO *bio, FILE **out_file) {
284  return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file);
285 }
286 
287 int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
288  return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file);
289 }
290 
291 int BIO_read_filename(BIO *bio, const char *filename) {
293  (char *)filename);
294 }
295 
296 int BIO_write_filename(BIO *bio, const char *filename) {
298  (char *)filename);
299 }
300 
301 int BIO_append_filename(BIO *bio, const char *filename) {
303  (char *)filename);
304 }
305 
306 int BIO_rw_filename(BIO *bio, const char *filename) {
307  return BIO_ctrl(bio, BIO_C_SET_FILENAME,
309 }
310 
311 #endif // OPENSSL_TRUSTY
bio_method_st
Definition: bio.h:808
BIO_C_FILE_TELL
#define BIO_C_FILE_TELL
Definition: bio.h:877
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
BIO_CTRL_WPENDING
#define BIO_CTRL_WPENDING
Definition: bio.h:715
BIO_C_SET_FILE_PTR
#define BIO_C_SET_FILE_PTR
Definition: bio.h:851
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
BIO_CTRL_INFO
#define BIO_CTRL_INFO
Definition: bio.h:698
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
BIO_C_SET_FILENAME
#define BIO_C_SET_FILENAME
Definition: bio.h:853
file_write
static int file_write(BIO *b, const char *in, int inl)
Definition: file.c:159
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
bio_st
Definition: bio.h:822
bio_st::ptr
void * ptr
Definition: bio.h:838
BIO_TYPE_FILE
#define BIO_TYPE_FILE
Definition: bio.h:776
BIO_FP_READ
#define BIO_FP_READ
Definition: file.c:88
ERR_R_SYS_LIB
#define ERR_R_SYS_LIB
Definition: err.h:330
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
bio.h
string.h
bio_st::init
int init
Definition: bio.h:826
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
error_ref_leak.err
err
Definition: error_ref_leak.py:35
BIO_s_file
const BIO_METHOD * BIO_s_file(void)
Definition: file.c:280
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
BIO_write_filename
int BIO_write_filename(BIO *bio, const char *filename)
Definition: file.c:296
xds_manager.p
p
Definition: xds_manager.py:60
BIO_set_fp
int BIO_set_fp(BIO *bio, FILE *file, int close_flag)
Definition: file.c:287
BIO_new_file
BIO * BIO_new_file(const char *filename, const char *mode)
Definition: file.c:92
BIO_CTRL_EOF
#define BIO_CTRL_EOF
Definition: bio.h:693
BIO_get_fp
int BIO_get_fp(BIO *bio, FILE **out_file)
Definition: file.c:283
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
file_read
static int file_read(BIO *b, char *out, int outl)
Definition: file.c:143
xds_interop_client.int
int
Definition: xds_interop_client.py:113
file_free
static int file_free(BIO *bio)
Definition: file.c:129
BIO_C_GET_FILE_PTR
#define BIO_C_GET_FILE_PTR
Definition: bio.h:852
BIO_ctrl
#define BIO_ctrl
Definition: boringssl_prefix_symbols.h:779
BIO_C_FILE_SEEK
#define BIO_C_FILE_SEEK
Definition: bio.h:872
regen-readme.cmd
cmd
Definition: regen-readme.py:21
err.h
file_gets
static int file_gets(BIO *bp, char *buf, int size)
Definition: file.c:255
BIO_read_filename
int BIO_read_filename(BIO *bio, const char *filename)
Definition: file.c:291
methods_filep
static const BIO_METHOD methods_filep
Definition: file.c:272
BIO_new_fp
BIO * BIO_new_fp(FILE *stream, int close_flag)
Definition: file.c:118
BIO_new
#define BIO_new
Definition: boringssl_prefix_symbols.h:814
BIO_FP_APPEND
#define BIO_FP_APPEND
Definition: file.c:90
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
bio_st::shutdown
int shutdown
Definition: bio.h:831
BIO_CTRL_GET_CLOSE
#define BIO_CTRL_GET_CLOSE
Definition: bio.h:702
BIO_CTRL_FLUSH
#define BIO_CTRL_FLUSH
Definition: bio.h:712
BIO_CTRL_PENDING
#define BIO_CTRL_PENDING
Definition: bio.h:709
BIO_R_BAD_FOPEN_MODE
#define BIO_R_BAD_FOPEN_MODE
Definition: bio.h:919
benchmark.FILE
FILE
Definition: benchmark.py:21
BIO_rw_filename
int BIO_rw_filename(BIO *bio, const char *filename)
Definition: file.c:306
OPENSSL_strlcpy
#define OPENSSL_strlcpy
Definition: boringssl_prefix_symbols.h:1894
BIO_CTRL_SET_CLOSE
#define BIO_CTRL_SET_CLOSE
Definition: bio.h:706
grpc::fclose
fclose(creds_file)
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
OPENSSL_PUT_SYSTEM_ERROR
#define OPENSSL_PUT_SYSTEM_ERROR()
Definition: err.h:429
ERR_add_error_data
#define ERR_add_error_data
Definition: boringssl_prefix_symbols.h:1411
xds_manager.num
num
Definition: xds_manager.py:56
BIO_FP_WRITE
#define BIO_FP_WRITE
Definition: file.c:89
BIO_R_NO_SUCH_FILE
#define BIO_R_NO_SUCH_FILE
Definition: bio.h:929
BIO_CTRL_RESET
#define BIO_CTRL_RESET
Definition: bio.h:690
mem.h
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
file_ctrl
static long file_ctrl(BIO *b, int cmd, long num, void *ptr)
Definition: file.c:173
BIO_append_filename
int BIO_append_filename(BIO *bio, const char *filename)
Definition: file.c:301
BIO_R_SYS_LIB
#define BIO_R_SYS_LIB
Definition: bio.h:931
OPENSSL_FALLTHROUGH
#define OPENSSL_FALLTHROUGH
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:201
BIO_CLOSE
#define BIO_CLOSE
Definition: bio.h:374
errno.h
google::protobuf.internal.decoder.long
long
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/decoder.py:89
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:20