]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
59e539e6 | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
b1322259 RS |
4 | * Licensed under the OpenSSL license (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 | |
d02b48c6 RE |
8 | */ |
9 | ||
58964a49 | 10 | #ifndef HEADER_BSS_FILE_C |
0f113f3e MC |
11 | # define HEADER_BSS_FILE_C |
12 | ||
13 | # if defined(__linux) || defined(__sun) || defined(__hpux) | |
14 | /* | |
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... | |
8fa6a40b | 25 | */ |
0f113f3e MC |
26 | # ifndef _FILE_OFFSET_BITS |
27 | # define _FILE_OFFSET_BITS 64 | |
28 | # endif | |
29 | # endif | |
8fa6a40b | 30 | |
0f113f3e MC |
31 | # include <stdio.h> |
32 | # include <errno.h> | |
0f113f3e MC |
33 | # include "bio_lcl.h" |
34 | # include <openssl/err.h> | |
d02b48c6 | 35 | |
0f113f3e | 36 | # if !defined(OPENSSL_NO_STDIO) |
58964a49 | 37 | |
6d23cf97 RS |
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); | |
04f6b0fd | 45 | static const BIO_METHOD methods_filep = { |
0f113f3e MC |
46 | BIO_TYPE_FILE, |
47 | "FILE pointer", | |
3befffa3 MC |
48 | /* TODO: Convert to new style write function */ |
49 | bwrite_conv, | |
0f113f3e | 50 | file_write, |
d07aee2c MC |
51 | /* TODO: Convert to new style read function */ |
52 | bread_conv, | |
0f113f3e MC |
53 | file_read, |
54 | file_puts, | |
55 | file_gets, | |
56 | file_ctrl, | |
57 | file_new, | |
58 | file_free, | |
59 | NULL, | |
60 | }; | |
d02b48c6 | 61 | |
ff03599a DW |
62 | BIO *BIO_new_file(const char *filename, const char *mode) |
63 | { | |
64 | BIO *ret; | |
09487816 | 65 | FILE *file = openssl_fopen(filename, mode); |
2d897ae4 RL |
66 | int fp_flags = BIO_CLOSE; |
67 | ||
68 | if (strchr(mode, 'b') == NULL) | |
69 | fp_flags |= BIO_FP_TEXT; | |
ff03599a | 70 | |
0f113f3e MC |
71 | if (file == NULL) { |
72 | SYSerr(SYS_F_FOPEN, get_last_sys_error()); | |
73 | ERR_add_error_data(5, "fopen('", filename, "','", mode, "')"); | |
e82e2186 RL |
74 | if (errno == ENOENT |
75 | # ifdef ENXIO | |
76 | || errno == ENXIO | |
77 | # endif | |
78 | ) | |
0f113f3e MC |
79 | BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE); |
80 | else | |
81 | BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB); | |
59e539e6 | 82 | return NULL; |
0f113f3e MC |
83 | } |
84 | if ((ret = BIO_new(BIO_s_file())) == NULL) { | |
85 | fclose(file); | |
59e539e6 | 86 | return NULL; |
0f113f3e MC |
87 | } |
88 | ||
89 | BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage | |
90 | * UPLINK */ | |
2d897ae4 | 91 | BIO_set_fp(ret, file, fp_flags); |
59e539e6 | 92 | return ret; |
0f113f3e | 93 | } |
d02b48c6 | 94 | |
6b691a5c | 95 | BIO *BIO_new_fp(FILE *stream, int close_flag) |
0f113f3e MC |
96 | { |
97 | BIO *ret; | |
d02b48c6 | 98 | |
0f113f3e | 99 | if ((ret = BIO_new(BIO_s_file())) == NULL) |
59e539e6 | 100 | return NULL; |
d02b48c6 | 101 | |
0d4fb843 F |
102 | /* redundant flag, left for documentation purposes */ |
103 | BIO_set_flags(ret, BIO_FLAGS_UPLINK); | |
0f113f3e | 104 | BIO_set_fp(ret, stream, close_flag); |
59e539e6 | 105 | return ret; |
0f113f3e | 106 | } |
d02b48c6 | 107 | |
04f6b0fd | 108 | const BIO_METHOD *BIO_s_file(void) |
0f113f3e | 109 | { |
59e539e6 | 110 | return &methods_filep; |
0f113f3e | 111 | } |
d02b48c6 | 112 | |
6d23cf97 | 113 | static int file_new(BIO *bi) |
0f113f3e MC |
114 | { |
115 | bi->init = 0; | |
116 | bi->num = 0; | |
117 | bi->ptr = NULL; | |
118 | bi->flags = BIO_FLAGS_UPLINK; /* default to UPLINK */ | |
59e539e6 | 119 | return 1; |
0f113f3e | 120 | } |
d02b48c6 | 121 | |
6d23cf97 | 122 | static int file_free(BIO *a) |
0f113f3e MC |
123 | { |
124 | if (a == NULL) | |
59e539e6 | 125 | return 0; |
0f113f3e MC |
126 | if (a->shutdown) { |
127 | if ((a->init) && (a->ptr != NULL)) { | |
128 | if (a->flags & BIO_FLAGS_UPLINK) | |
129 | UP_fclose(a->ptr); | |
130 | else | |
131 | fclose(a->ptr); | |
132 | a->ptr = NULL; | |
133 | a->flags = BIO_FLAGS_UPLINK; | |
134 | } | |
135 | a->init = 0; | |
136 | } | |
59e539e6 | 137 | return 1; |
0f113f3e | 138 | } |
d02b48c6 | 139 | |
0f113f3e MC |
140 | static int file_read(BIO *b, char *out, int outl) |
141 | { | |
142 | int ret = 0; | |
143 | ||
144 | if (b->init && (out != NULL)) { | |
145 | if (b->flags & BIO_FLAGS_UPLINK) | |
146 | ret = UP_fread(out, 1, (int)outl, b->ptr); | |
147 | else | |
148 | ret = fread(out, 1, (int)outl, (FILE *)b->ptr); | |
149 | if (ret == 0 | |
739a5eee | 150 | && (b->flags & BIO_FLAGS_UPLINK) ? UP_ferror((FILE *)b->ptr) : |
800b299b | 151 | ferror((FILE *)b->ptr)) { |
0f113f3e MC |
152 | SYSerr(SYS_F_FREAD, get_last_sys_error()); |
153 | BIOerr(BIO_F_FILE_READ, ERR_R_SYS_LIB); | |
154 | ret = -1; | |
155 | } | |
156 | } | |
59e539e6 | 157 | return ret; |
0f113f3e | 158 | } |
d02b48c6 | 159 | |
6d23cf97 | 160 | static int file_write(BIO *b, const char *in, int inl) |
0f113f3e MC |
161 | { |
162 | int ret = 0; | |
163 | ||
164 | if (b->init && (in != NULL)) { | |
165 | if (b->flags & BIO_FLAGS_UPLINK) | |
166 | ret = UP_fwrite(in, (int)inl, 1, b->ptr); | |
167 | else | |
168 | ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr); | |
169 | if (ret) | |
170 | ret = inl; | |
171 | /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ | |
172 | /* | |
e3713c36 | 173 | * according to Tim Hudson <tjh@openssl.org>, the commented out |
0f113f3e MC |
174 | * version above can cause 'inl' write calls under some stupid stdio |
175 | * implementations (VMS) | |
176 | */ | |
177 | } | |
59e539e6 | 178 | return ret; |
0f113f3e | 179 | } |
d02b48c6 | 180 | |
6d23cf97 | 181 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) |
0f113f3e MC |
182 | { |
183 | long ret = 1; | |
184 | FILE *fp = (FILE *)b->ptr; | |
185 | FILE **fpp; | |
186 | char p[4]; | |
595b2a42 | 187 | int st; |
0f113f3e MC |
188 | |
189 | switch (cmd) { | |
190 | case BIO_C_FILE_SEEK: | |
191 | case BIO_CTRL_RESET: | |
192 | if (b->flags & BIO_FLAGS_UPLINK) | |
193 | ret = (long)UP_fseek(b->ptr, num, 0); | |
194 | else | |
195 | ret = (long)fseek(fp, num, 0); | |
196 | break; | |
197 | case BIO_CTRL_EOF: | |
198 | if (b->flags & BIO_FLAGS_UPLINK) | |
199 | ret = (long)UP_feof(fp); | |
200 | else | |
201 | ret = (long)feof(fp); | |
202 | break; | |
203 | case BIO_C_FILE_TELL: | |
204 | case BIO_CTRL_INFO: | |
205 | if (b->flags & BIO_FLAGS_UPLINK) | |
206 | ret = UP_ftell(b->ptr); | |
207 | else | |
208 | ret = ftell(fp); | |
209 | break; | |
210 | case BIO_C_SET_FILE_PTR: | |
211 | file_free(b); | |
212 | b->shutdown = (int)num & BIO_CLOSE; | |
213 | b->ptr = ptr; | |
214 | b->init = 1; | |
215 | # if BIO_FLAGS_UPLINK!=0 | |
216 | # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES) | |
217 | # define _IOB_ENTRIES 20 | |
218 | # endif | |
0f113f3e | 219 | /* Safety net to catch purely internal BIO_set_fp calls */ |
800b299b AP |
220 | # if defined(_MSC_VER) && _MSC_VER>=1900 |
221 | if (ptr == stdin || ptr == stdout || ptr == stderr) | |
222 | BIO_clear_flags(b, BIO_FLAGS_UPLINK); | |
223 | # elif defined(_IOB_ENTRIES) | |
0f113f3e MC |
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); | |
227 | # endif | |
228 | # endif | |
229 | # ifdef UP_fsetmod | |
230 | if (b->flags & BIO_FLAGS_UPLINK) | |
231 | UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b')); | |
232 | else | |
233 | # endif | |
234 | { | |
235 | # if defined(OPENSSL_SYS_WINDOWS) | |
236 | int fd = _fileno((FILE *)ptr); | |
237 | if (num & BIO_FP_TEXT) | |
238 | _setmode(fd, _O_TEXT); | |
239 | else | |
240 | _setmode(fd, _O_BINARY); | |
0f113f3e MC |
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) */ | |
247 | else { | |
248 | if (fd == STDIN_FILENO || fd == STDOUT_FILENO) { | |
249 | if (isatty(fd) <= 0) | |
250 | _setmode(fd, _O_BINARY); | |
251 | } else | |
252 | _setmode(fd, _O_BINARY); | |
253 | } | |
1fbab1dc | 254 | # elif defined(OPENSSL_SYS_WIN32_CYGWIN) |
0f113f3e MC |
255 | int fd = fileno((FILE *)ptr); |
256 | if (num & BIO_FP_TEXT) | |
257 | setmode(fd, O_TEXT); | |
258 | else | |
259 | setmode(fd, O_BINARY); | |
260 | # endif | |
261 | } | |
262 | break; | |
263 | case BIO_C_SET_FILENAME: | |
264 | file_free(b); | |
265 | b->shutdown = (int)num & BIO_CLOSE; | |
266 | if (num & BIO_FP_APPEND) { | |
267 | if (num & BIO_FP_READ) | |
59e539e6 | 268 | OPENSSL_strlcpy(p, "a+", sizeof(p)); |
0f113f3e | 269 | else |
59e539e6 | 270 | OPENSSL_strlcpy(p, "a", sizeof(p)); |
0f113f3e | 271 | } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) |
59e539e6 | 272 | OPENSSL_strlcpy(p, "r+", sizeof(p)); |
0f113f3e | 273 | else if (num & BIO_FP_WRITE) |
59e539e6 | 274 | OPENSSL_strlcpy(p, "w", sizeof(p)); |
0f113f3e | 275 | else if (num & BIO_FP_READ) |
59e539e6 | 276 | OPENSSL_strlcpy(p, "r", sizeof(p)); |
0f113f3e MC |
277 | else { |
278 | BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE); | |
279 | ret = 0; | |
280 | break; | |
281 | } | |
1fbab1dc | 282 | # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32_CYGWIN) |
0f113f3e | 283 | if (!(num & BIO_FP_TEXT)) |
59e539e6 | 284 | OPENSSL_strlcat(p, "b", sizeof(p)); |
0f113f3e | 285 | else |
59e539e6 | 286 | OPENSSL_strlcat(p, "t", sizeof(p)); |
0f113f3e | 287 | # endif |
09487816 | 288 | fp = openssl_fopen(ptr, p); |
0f113f3e MC |
289 | if (fp == NULL) { |
290 | SYSerr(SYS_F_FOPEN, get_last_sys_error()); | |
291 | ERR_add_error_data(5, "fopen('", ptr, "','", p, "')"); | |
292 | BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB); | |
293 | ret = 0; | |
294 | break; | |
295 | } | |
296 | b->ptr = fp; | |
297 | b->init = 1; | |
298 | BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage | |
299 | * UPLINK */ | |
300 | break; | |
301 | case BIO_C_GET_FILE_PTR: | |
302 | /* the ptr parameter is actually a FILE ** in this case. */ | |
303 | if (ptr != NULL) { | |
304 | fpp = (FILE **)ptr; | |
305 | *fpp = (FILE *)b->ptr; | |
306 | } | |
307 | break; | |
308 | case BIO_CTRL_GET_CLOSE: | |
309 | ret = (long)b->shutdown; | |
310 | break; | |
311 | case BIO_CTRL_SET_CLOSE: | |
312 | b->shutdown = (int)num; | |
313 | break; | |
314 | case BIO_CTRL_FLUSH: | |
595b2a42 RS |
315 | st = b->flags & BIO_FLAGS_UPLINK |
316 | ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr); | |
317 | if (st == EOF) { | |
318 | SYSerr(SYS_F_FFLUSH, get_last_sys_error()); | |
319 | ERR_add_error_data(1, "fflush()"); | |
320 | BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB); | |
321 | ret = 0; | |
322 | } | |
0f113f3e MC |
323 | break; |
324 | case BIO_CTRL_DUP: | |
325 | ret = 1; | |
326 | break; | |
327 | ||
328 | case BIO_CTRL_WPENDING: | |
329 | case BIO_CTRL_PENDING: | |
330 | case BIO_CTRL_PUSH: | |
331 | case BIO_CTRL_POP: | |
332 | default: | |
333 | ret = 0; | |
334 | break; | |
335 | } | |
59e539e6 | 336 | return ret; |
0f113f3e | 337 | } |
d02b48c6 | 338 | |
6d23cf97 | 339 | static int file_gets(BIO *bp, char *buf, int size) |
0f113f3e MC |
340 | { |
341 | int ret = 0; | |
342 | ||
343 | buf[0] = '\0'; | |
344 | if (bp->flags & BIO_FLAGS_UPLINK) { | |
345 | if (!UP_fgets(buf, size, bp->ptr)) | |
346 | goto err; | |
347 | } else { | |
348 | if (!fgets(buf, size, (FILE *)bp->ptr)) | |
349 | goto err; | |
350 | } | |
351 | if (buf[0] != '\0') | |
352 | ret = strlen(buf); | |
353 | err: | |
59e539e6 | 354 | return ret; |
0f113f3e | 355 | } |
d02b48c6 | 356 | |
6d23cf97 | 357 | static int file_puts(BIO *bp, const char *str) |
0f113f3e MC |
358 | { |
359 | int n, ret; | |
58964a49 | 360 | |
0f113f3e MC |
361 | n = strlen(str); |
362 | ret = file_write(bp, str, n); | |
59e539e6 | 363 | return ret; |
0f113f3e | 364 | } |
58964a49 | 365 | |
984d6c60 DW |
366 | #else |
367 | ||
368 | static int file_write(BIO *b, const char *in, int inl) | |
369 | { | |
370 | return -1; | |
371 | } | |
372 | static int file_read(BIO *b, char *out, int outl) | |
373 | { | |
374 | return -1; | |
375 | } | |
376 | static int file_puts(BIO *bp, const char *str) | |
377 | { | |
378 | return -1; | |
379 | } | |
380 | static int file_gets(BIO *bp, char *buf, int size) | |
381 | { | |
382 | return 0; | |
383 | } | |
384 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) | |
385 | { | |
386 | return 0; | |
387 | } | |
388 | static int file_new(BIO *bi) | |
389 | { | |
390 | return 0; | |
391 | } | |
392 | static int file_free(BIO *a) | |
393 | { | |
394 | return 0; | |
395 | } | |
396 | ||
04f6b0fd | 397 | static const BIO_METHOD methods_filep = { |
984d6c60 DW |
398 | BIO_TYPE_FILE, |
399 | "FILE pointer", | |
f7970f30 MC |
400 | /* TODO: Convert to new style write function */ |
401 | bwrite_conv, | |
984d6c60 | 402 | file_write, |
f7970f30 MC |
403 | /* TODO: Convert to new style read function */ |
404 | bread_conv, | |
984d6c60 DW |
405 | file_read, |
406 | file_puts, | |
407 | file_gets, | |
408 | file_ctrl, | |
409 | file_new, | |
410 | file_free, | |
411 | NULL, | |
412 | }; | |
413 | ||
04f6b0fd | 414 | const BIO_METHOD *BIO_s_file(void) |
984d6c60 | 415 | { |
59e539e6 | 416 | return &methods_filep; |
984d6c60 DW |
417 | } |
418 | ||
419 | BIO *BIO_new_file(const char *filename, const char *mode) | |
420 | { | |
421 | return NULL; | |
422 | } | |
423 | ||
0f113f3e | 424 | # endif /* OPENSSL_NO_STDIO */ |
58964a49 | 425 | |
0f113f3e | 426 | #endif /* HEADER_BSS_FILE_C */ |