Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/file.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #if defined(__linux) || defined(__sun) || defined(__hpux)
5 // Following definition aliases fopen to fopen64 on above mentioned
6 // platforms. This makes it possible to open and sequentially access
7 // files larger than 2GB from 32-bit application. It does not allow to
8 // traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
9 // 32-bit platform permits that, not with fseek/ftell. Not to mention
10 // that breaking 2GB limit for seeking would require surgery to *our*
11 // API. But sequential access suffices for practical cases when you
12 // can run into large files, such as fingerprinting, so we can let API
13 // alone. For reference, the list of 32-bit platforms which allow for
14 // sequential access of large files without extra "magic" comprise *BSD,
15 // Darwin, IRIX...
16 #ifndef _FILE_OFFSET_BITS
17 #define _FILE_OFFSET_BITS 64
18 #endif
19 #endif
20
21 #include <openssl/bio.h>
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #if defined(OPENSSL_WINDOWS)
28 #include <fcntl.h>
29 #include <io.h>
30 #endif // OPENSSL_WINDOWS
31
32 #include <openssl/err.h>
33 #include <openssl/mem.h>
34
35 #include "../internal.h"
36
37
38 #define BIO_FP_READ 0x02
39 #define BIO_FP_WRITE 0x04
40 #define BIO_FP_APPEND 0x08
41
42 #if !defined(OPENSSL_NO_FILESYSTEM)
43 #define fopen_if_available fopen
44 #else
45 static FILE *fopen_if_available(const char *path, const char *mode) {
46 errno = ENOENT;
47 return NULL;
48 }
49 #endif
50
51 BIO *BIO_new_file(const char *filename, const char *mode) {
52 BIO *ret;
53 FILE *file;
54
55 file = fopen_if_available(filename, mode);
56 if (file == NULL) {
57 OPENSSL_PUT_SYSTEM_ERROR();
58
59 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
60 if (errno == ENOENT) {
61 OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE);
62 } else {
63 OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB);
64 }
65 return NULL;
66 }
67
68 ret = BIO_new_fp(file, BIO_CLOSE);
69 if (ret == NULL) {
70 fclose(file);
71 return NULL;
72 }
73
74 return ret;
75 }
76
77 BIO *BIO_new_fp(FILE *stream, int close_flag) {
78 BIO *ret = BIO_new(BIO_s_file());
79
80 if (ret == NULL) {
81 return NULL;
82 }
83
84 BIO_set_fp(ret, stream, close_flag);
85 return ret;
86 }
87
88 static int file_free(BIO *bio) {
89 if (!bio->shutdown) {
90 return 1;
91 }
92
93 if (bio->init && bio->ptr != NULL) {
94 fclose(bio->ptr);
95 bio->ptr = NULL;
96 }
97 bio->init = 0;
98
99 return 1;
100 }
101
102 static int file_read(BIO *b, char *out, int outl) {
103 if (!b->init) {
104 return 0;
105 }
106
107 size_t ret = fread(out, 1, outl, (FILE *)b->ptr);
108 if (ret == 0 && ferror((FILE *)b->ptr)) {
109 OPENSSL_PUT_SYSTEM_ERROR();
110 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
111 return -1;
112 }
113
114 // fread reads at most |outl| bytes, so |ret| fits in an int.
115 return (int)ret;
116 }
117
118 static int file_write(BIO *b, const char *in, int inl) {
119 if (!b->init) {
120 return 0;
121 }
122
123 int ret = (int)fwrite(in, inl, 1, (FILE *)b->ptr);
124 if (ret > 0) {
125 ret = inl;
126 }
127 return ret;
128 }
129
130 static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
131 long ret = 1;
132 FILE *fp = (FILE *)b->ptr;
133 FILE **fpp;
134
135 switch (cmd) {
136 case BIO_CTRL_RESET:
137 num = 0;
138 OPENSSL_FALLTHROUGH;
139 case BIO_C_FILE_SEEK:
140 ret = (long)fseek(fp, num, 0);
141 break;
142 case BIO_CTRL_EOF:
143 ret = (long)feof(fp);
144 break;
145 case BIO_C_FILE_TELL:
146 case BIO_CTRL_INFO:
147 ret = ftell(fp);
148 break;
149 case BIO_C_SET_FILE_PTR:
150 file_free(b);
151 b->shutdown = (int)num & BIO_CLOSE;
152 b->ptr = ptr;
153 b->init = 1;
154 #if defined(OPENSSL_WINDOWS)
155 // Windows differentiates between "text" and "binary" file modes, so set
156 // the file to text mode if caller specifies BIO_FP_TEXT flag. If
157 // |BIO_FP_TEXT| is not set, we still call |_setmode| with |_O_BINARY| to
158 // match OpenSSL's behavior. BoringSSL, on the other hand, does nothing in
159 // this case.
160 //
161 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170#remarks
162 // https://github.com/google/boringssl/commit/5ee4e9512e9a99f97c4a3fad397034028b3457c2
163 _setmode(_fileno(b->ptr), num & BIO_FP_TEXT ? _O_TEXT : _O_BINARY);
164 #endif
165 break;
166 case BIO_C_SET_FILENAME:
167 file_free(b);
168 b->shutdown = (int)num & BIO_CLOSE;
169 const char *mode;
170 if (num & BIO_FP_APPEND) {
171 if (num & BIO_FP_READ) {
172 mode = "ab+";
173 } else {
174 mode = "ab";
175 }
176 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
177 mode = "rb+";
178 } else if (num & BIO_FP_WRITE) {
179 mode = "wb";
180 } else if (num & BIO_FP_READ) {
181 mode = "rb";
182 } else {
183 OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
184 ret = 0;
185 break;
186 }
187 fp = fopen_if_available(ptr, mode);
188 if (fp == NULL) {
189 OPENSSL_PUT_SYSTEM_ERROR();
190 ERR_add_error_data(5, "fopen('", ptr, "','", mode, "')");
191 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
192 ret = 0;
193 break;
194 }
195 b->ptr = fp;
196 b->init = 1;
197 break;
198 case BIO_C_GET_FILE_PTR:
199 // the ptr parameter is actually a FILE ** in this case.
200 if (ptr != NULL) {
201 fpp = (FILE **)ptr;
202 *fpp = (FILE *)b->ptr;
203 }
204 break;
205 case BIO_CTRL_GET_CLOSE:
206 ret = (long)b->shutdown;
207 break;
208 case BIO_CTRL_SET_CLOSE:
209 b->shutdown = (int)num;
210 break;
211 case BIO_CTRL_FLUSH:
212 ret = 0 == fflush((FILE *)b->ptr);
213 break;
214 case BIO_CTRL_WPENDING:
215 case BIO_CTRL_PENDING:
216 default:
217 ret = 0;
218 break;
219 }
220 return ret;
221 }
222
223 static int file_gets(BIO *bp, char *buf, int size) {
224 if (size == 0) {
225 return 0;
226 }
227
228 if (!fgets(buf, size, (FILE *)bp->ptr)) {
229 buf[0] = 0;
230 // TODO(davidben): This doesn't distinguish error and EOF. This should check
231 // |ferror| as in |file_read|.
232 return 0;
233 }
234
235 return (int)strlen(buf);
236 }
237
238 static const BIO_METHOD methods_filep = {
239 BIO_TYPE_FILE, "FILE pointer",
240 file_write, file_read,
241 NULL /* puts */, file_gets,
242 file_ctrl, NULL /* create */,
243 file_free, NULL /* callback_ctrl */,
244 };
245
246 const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
247
248
249 int BIO_get_fp(BIO *bio, FILE **out_file) {
250 return (int)BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char *)out_file);
251 }
252
253 int BIO_set_fp(BIO *bio, FILE *file, int flags) {
254 return (int)BIO_ctrl(bio, BIO_C_SET_FILE_PTR, flags, (char *)file);
255 }
256
257 int BIO_read_filename(BIO *bio, const char *filename) {
258 return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
259 (char *)filename);
260 }
261
262 int BIO_write_filename(BIO *bio, const char *filename) {
263 return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
264 (char *)filename);
265 }
266
267 int BIO_append_filename(BIO *bio, const char *filename) {
268 return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
269 (char *)filename);
270 }
271
272 int BIO_rw_filename(BIO *bio, const char *filename) {
273 return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME,
274 BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE,
275 (char *)filename);
276 }
277
278 long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); }
279
280 long BIO_seek(BIO *bio, long offset) {
281 return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL);
282 }