xref: /third_party/openssl/crypto/bio/bss_file.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#if defined(__linux) || defined(__sun) || defined(__hpux)
11e1051a39Sopenharmony_ci/*
12e1051a39Sopenharmony_ci * Following definition aliases fopen to fopen64 on above mentioned
13e1051a39Sopenharmony_ci * platforms. This makes it possible to open and sequentially access files
14e1051a39Sopenharmony_ci * larger than 2GB from 32-bit application. It does not allow to traverse
15e1051a39Sopenharmony_ci * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit
16e1051a39Sopenharmony_ci * platform permits that, not with fseek/ftell. Not to mention that breaking
17e1051a39Sopenharmony_ci * 2GB limit for seeking would require surgery to *our* API. But sequential
18e1051a39Sopenharmony_ci * access suffices for practical cases when you can run into large files,
19e1051a39Sopenharmony_ci * such as fingerprinting, so we can let API alone. For reference, the list
20e1051a39Sopenharmony_ci * of 32-bit platforms which allow for sequential access of large files
21e1051a39Sopenharmony_ci * without extra "magic" comprise *BSD, Darwin, IRIX...
22e1051a39Sopenharmony_ci */
23e1051a39Sopenharmony_ci# ifndef _FILE_OFFSET_BITS
24e1051a39Sopenharmony_ci#  define _FILE_OFFSET_BITS 64
25e1051a39Sopenharmony_ci# endif
26e1051a39Sopenharmony_ci#endif
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci#include <stdio.h>
29e1051a39Sopenharmony_ci#include <errno.h>
30e1051a39Sopenharmony_ci#include "bio_local.h"
31e1051a39Sopenharmony_ci#include <openssl/err.h>
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_STDIO)
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_cistatic int file_write(BIO *h, const char *buf, int num);
36e1051a39Sopenharmony_cistatic int file_read(BIO *h, char *buf, int size);
37e1051a39Sopenharmony_cistatic int file_puts(BIO *h, const char *str);
38e1051a39Sopenharmony_cistatic int file_gets(BIO *h, char *str, int size);
39e1051a39Sopenharmony_cistatic long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
40e1051a39Sopenharmony_cistatic int file_new(BIO *h);
41e1051a39Sopenharmony_cistatic int file_free(BIO *data);
42e1051a39Sopenharmony_cistatic const BIO_METHOD methods_filep = {
43e1051a39Sopenharmony_ci    BIO_TYPE_FILE,
44e1051a39Sopenharmony_ci    "FILE pointer",
45e1051a39Sopenharmony_ci    bwrite_conv,
46e1051a39Sopenharmony_ci    file_write,
47e1051a39Sopenharmony_ci    bread_conv,
48e1051a39Sopenharmony_ci    file_read,
49e1051a39Sopenharmony_ci    file_puts,
50e1051a39Sopenharmony_ci    file_gets,
51e1051a39Sopenharmony_ci    file_ctrl,
52e1051a39Sopenharmony_ci    file_new,
53e1051a39Sopenharmony_ci    file_free,
54e1051a39Sopenharmony_ci    NULL,                      /* file_callback_ctrl */
55e1051a39Sopenharmony_ci};
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ciBIO *BIO_new_file(const char *filename, const char *mode)
58e1051a39Sopenharmony_ci{
59e1051a39Sopenharmony_ci    BIO  *ret;
60e1051a39Sopenharmony_ci    FILE *file = openssl_fopen(filename, mode);
61e1051a39Sopenharmony_ci    int fp_flags = BIO_CLOSE;
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    if (strchr(mode, 'b') == NULL)
64e1051a39Sopenharmony_ci        fp_flags |= BIO_FP_TEXT;
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ci    if (file == NULL) {
67e1051a39Sopenharmony_ci        ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
68e1051a39Sopenharmony_ci                       "calling fopen(%s, %s)",
69e1051a39Sopenharmony_ci                       filename, mode);
70e1051a39Sopenharmony_ci        if (errno == ENOENT
71e1051a39Sopenharmony_ci#ifdef ENXIO
72e1051a39Sopenharmony_ci            || errno == ENXIO
73e1051a39Sopenharmony_ci#endif
74e1051a39Sopenharmony_ci            )
75e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE);
76e1051a39Sopenharmony_ci        else
77e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
78e1051a39Sopenharmony_ci        return NULL;
79e1051a39Sopenharmony_ci    }
80e1051a39Sopenharmony_ci    if ((ret = BIO_new(BIO_s_file())) == NULL) {
81e1051a39Sopenharmony_ci        fclose(file);
82e1051a39Sopenharmony_ci        return NULL;
83e1051a39Sopenharmony_ci    }
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci    /* we did fopen -> we disengage UPLINK */
86e1051a39Sopenharmony_ci    BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
87e1051a39Sopenharmony_ci    BIO_set_fp(ret, file, fp_flags);
88e1051a39Sopenharmony_ci    return ret;
89e1051a39Sopenharmony_ci}
90e1051a39Sopenharmony_ci
91e1051a39Sopenharmony_ciBIO *BIO_new_fp(FILE *stream, int close_flag)
92e1051a39Sopenharmony_ci{
93e1051a39Sopenharmony_ci    BIO *ret;
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ci    if ((ret = BIO_new(BIO_s_file())) == NULL)
96e1051a39Sopenharmony_ci        return NULL;
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci    /* redundant flag, left for documentation purposes */
99e1051a39Sopenharmony_ci    BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL);
100e1051a39Sopenharmony_ci    BIO_set_fp(ret, stream, close_flag);
101e1051a39Sopenharmony_ci    return ret;
102e1051a39Sopenharmony_ci}
103e1051a39Sopenharmony_ci
104e1051a39Sopenharmony_ciconst BIO_METHOD *BIO_s_file(void)
105e1051a39Sopenharmony_ci{
106e1051a39Sopenharmony_ci    return &methods_filep;
107e1051a39Sopenharmony_ci}
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_cistatic int file_new(BIO *bi)
110e1051a39Sopenharmony_ci{
111e1051a39Sopenharmony_ci    bi->init = 0;
112e1051a39Sopenharmony_ci    bi->num = 0;
113e1051a39Sopenharmony_ci    bi->ptr = NULL;
114e1051a39Sopenharmony_ci    bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */
115e1051a39Sopenharmony_ci    return 1;
116e1051a39Sopenharmony_ci}
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_cistatic int file_free(BIO *a)
119e1051a39Sopenharmony_ci{
120e1051a39Sopenharmony_ci    if (a == NULL)
121e1051a39Sopenharmony_ci        return 0;
122e1051a39Sopenharmony_ci    if (a->shutdown) {
123e1051a39Sopenharmony_ci        if ((a->init) && (a->ptr != NULL)) {
124e1051a39Sopenharmony_ci            if (a->flags & BIO_FLAGS_UPLINK_INTERNAL)
125e1051a39Sopenharmony_ci                UP_fclose(a->ptr);
126e1051a39Sopenharmony_ci            else
127e1051a39Sopenharmony_ci                fclose(a->ptr);
128e1051a39Sopenharmony_ci            a->ptr = NULL;
129e1051a39Sopenharmony_ci            a->flags = BIO_FLAGS_UPLINK_INTERNAL;
130e1051a39Sopenharmony_ci        }
131e1051a39Sopenharmony_ci        a->init = 0;
132e1051a39Sopenharmony_ci    }
133e1051a39Sopenharmony_ci    return 1;
134e1051a39Sopenharmony_ci}
135e1051a39Sopenharmony_ci
136e1051a39Sopenharmony_cistatic int file_read(BIO *b, char *out, int outl)
137e1051a39Sopenharmony_ci{
138e1051a39Sopenharmony_ci    int ret = 0;
139e1051a39Sopenharmony_ci
140e1051a39Sopenharmony_ci    if (b->init && (out != NULL)) {
141e1051a39Sopenharmony_ci        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
142e1051a39Sopenharmony_ci            ret = UP_fread(out, 1, (int)outl, b->ptr);
143e1051a39Sopenharmony_ci        else
144e1051a39Sopenharmony_ci            ret = fread(out, 1, (int)outl, (FILE *)b->ptr);
145e1051a39Sopenharmony_ci        if (ret == 0
146e1051a39Sopenharmony_ci            && (b->flags & BIO_FLAGS_UPLINK_INTERNAL
147e1051a39Sopenharmony_ci                ? UP_ferror((FILE *)b->ptr) : ferror((FILE *)b->ptr))) {
148e1051a39Sopenharmony_ci            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
149e1051a39Sopenharmony_ci                           "calling fread()");
150e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
151e1051a39Sopenharmony_ci            ret = -1;
152e1051a39Sopenharmony_ci        }
153e1051a39Sopenharmony_ci    }
154e1051a39Sopenharmony_ci    return ret;
155e1051a39Sopenharmony_ci}
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_cistatic int file_write(BIO *b, const char *in, int inl)
158e1051a39Sopenharmony_ci{
159e1051a39Sopenharmony_ci    int ret = 0;
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci    if (b->init && (in != NULL)) {
162e1051a39Sopenharmony_ci        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
163e1051a39Sopenharmony_ci            ret = UP_fwrite(in, (int)inl, 1, b->ptr);
164e1051a39Sopenharmony_ci        else
165e1051a39Sopenharmony_ci            ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr);
166e1051a39Sopenharmony_ci        if (ret)
167e1051a39Sopenharmony_ci            ret = inl;
168e1051a39Sopenharmony_ci        /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
169e1051a39Sopenharmony_ci        /*
170e1051a39Sopenharmony_ci         * according to Tim Hudson <tjh@openssl.org>, the commented out
171e1051a39Sopenharmony_ci         * version above can cause 'inl' write calls under some stupid stdio
172e1051a39Sopenharmony_ci         * implementations (VMS)
173e1051a39Sopenharmony_ci         */
174e1051a39Sopenharmony_ci    }
175e1051a39Sopenharmony_ci    return ret;
176e1051a39Sopenharmony_ci}
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_cistatic long file_ctrl(BIO *b, int cmd, long num, void *ptr)
179e1051a39Sopenharmony_ci{
180e1051a39Sopenharmony_ci    long ret = 1;
181e1051a39Sopenharmony_ci    FILE *fp = (FILE *)b->ptr;
182e1051a39Sopenharmony_ci    FILE **fpp;
183e1051a39Sopenharmony_ci    char p[4];
184e1051a39Sopenharmony_ci    int st;
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci    switch (cmd) {
187e1051a39Sopenharmony_ci    case BIO_C_FILE_SEEK:
188e1051a39Sopenharmony_ci    case BIO_CTRL_RESET:
189e1051a39Sopenharmony_ci        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
190e1051a39Sopenharmony_ci            ret = (long)UP_fseek(b->ptr, num, 0);
191e1051a39Sopenharmony_ci        else
192e1051a39Sopenharmony_ci            ret = (long)fseek(fp, num, 0);
193e1051a39Sopenharmony_ci        break;
194e1051a39Sopenharmony_ci    case BIO_CTRL_EOF:
195e1051a39Sopenharmony_ci        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
196e1051a39Sopenharmony_ci            ret = (long)UP_feof(fp);
197e1051a39Sopenharmony_ci        else
198e1051a39Sopenharmony_ci            ret = (long)feof(fp);
199e1051a39Sopenharmony_ci        break;
200e1051a39Sopenharmony_ci    case BIO_C_FILE_TELL:
201e1051a39Sopenharmony_ci    case BIO_CTRL_INFO:
202e1051a39Sopenharmony_ci        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
203e1051a39Sopenharmony_ci            ret = UP_ftell(b->ptr);
204e1051a39Sopenharmony_ci        else
205e1051a39Sopenharmony_ci            ret = ftell(fp);
206e1051a39Sopenharmony_ci        break;
207e1051a39Sopenharmony_ci    case BIO_C_SET_FILE_PTR:
208e1051a39Sopenharmony_ci        file_free(b);
209e1051a39Sopenharmony_ci        b->shutdown = (int)num & BIO_CLOSE;
210e1051a39Sopenharmony_ci        b->ptr = ptr;
211e1051a39Sopenharmony_ci        b->init = 1;
212e1051a39Sopenharmony_ci# if BIO_FLAGS_UPLINK_INTERNAL!=0
213e1051a39Sopenharmony_ci#  if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
214e1051a39Sopenharmony_ci#   define _IOB_ENTRIES 20
215e1051a39Sopenharmony_ci#  endif
216e1051a39Sopenharmony_ci        /* Safety net to catch purely internal BIO_set_fp calls */
217e1051a39Sopenharmony_ci#  if (defined(_MSC_VER) && _MSC_VER>=1900) || defined(__BORLANDC__)
218e1051a39Sopenharmony_ci        if (ptr == stdin || ptr == stdout || ptr == stderr)
219e1051a39Sopenharmony_ci            BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
220e1051a39Sopenharmony_ci#  elif defined(_IOB_ENTRIES)
221e1051a39Sopenharmony_ci        if ((size_t)ptr >= (size_t)stdin &&
222e1051a39Sopenharmony_ci            (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES))
223e1051a39Sopenharmony_ci            BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
224e1051a39Sopenharmony_ci#  endif
225e1051a39Sopenharmony_ci# endif
226e1051a39Sopenharmony_ci# ifdef UP_fsetmod
227e1051a39Sopenharmony_ci        if (b->flags & BIO_FLAGS_UPLINK_INTERNAL)
228e1051a39Sopenharmony_ci            UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b'));
229e1051a39Sopenharmony_ci        else
230e1051a39Sopenharmony_ci# endif
231e1051a39Sopenharmony_ci        {
232e1051a39Sopenharmony_ci# if defined(OPENSSL_SYS_WINDOWS)
233e1051a39Sopenharmony_ci            int fd = _fileno((FILE *)ptr);
234e1051a39Sopenharmony_ci            if (num & BIO_FP_TEXT)
235e1051a39Sopenharmony_ci                _setmode(fd, _O_TEXT);
236e1051a39Sopenharmony_ci            else
237e1051a39Sopenharmony_ci                _setmode(fd, _O_BINARY);
238e1051a39Sopenharmony_ci            /*
239e1051a39Sopenharmony_ci             * Reports show that ftell() isn't trustable in text mode.
240e1051a39Sopenharmony_ci             * This has been confirmed as a bug in the Universal C RTL, see
241e1051a39Sopenharmony_ci             * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html
242e1051a39Sopenharmony_ci             * The suggested work-around from Microsoft engineering is to
243e1051a39Sopenharmony_ci             * turn off buffering until the bug is resolved.
244e1051a39Sopenharmony_ci             */
245e1051a39Sopenharmony_ci            if ((num & BIO_FP_TEXT) != 0)
246e1051a39Sopenharmony_ci                setvbuf((FILE *)ptr, NULL, _IONBF, 0);
247e1051a39Sopenharmony_ci# elif defined(OPENSSL_SYS_MSDOS)
248e1051a39Sopenharmony_ci            int fd = fileno((FILE *)ptr);
249e1051a39Sopenharmony_ci            /* Set correct text/binary mode */
250e1051a39Sopenharmony_ci            if (num & BIO_FP_TEXT)
251e1051a39Sopenharmony_ci                _setmode(fd, _O_TEXT);
252e1051a39Sopenharmony_ci            /* Dangerous to set stdin/stdout to raw (unless redirected) */
253e1051a39Sopenharmony_ci            else {
254e1051a39Sopenharmony_ci                if (fd == STDIN_FILENO || fd == STDOUT_FILENO) {
255e1051a39Sopenharmony_ci                    if (isatty(fd) <= 0)
256e1051a39Sopenharmony_ci                        _setmode(fd, _O_BINARY);
257e1051a39Sopenharmony_ci                } else
258e1051a39Sopenharmony_ci                    _setmode(fd, _O_BINARY);
259e1051a39Sopenharmony_ci            }
260e1051a39Sopenharmony_ci# elif defined(OPENSSL_SYS_WIN32_CYGWIN)
261e1051a39Sopenharmony_ci            int fd = fileno((FILE *)ptr);
262e1051a39Sopenharmony_ci            if (!(num & BIO_FP_TEXT))
263e1051a39Sopenharmony_ci                setmode(fd, O_BINARY);
264e1051a39Sopenharmony_ci# endif
265e1051a39Sopenharmony_ci        }
266e1051a39Sopenharmony_ci        break;
267e1051a39Sopenharmony_ci    case BIO_C_SET_FILENAME:
268e1051a39Sopenharmony_ci        file_free(b);
269e1051a39Sopenharmony_ci        b->shutdown = (int)num & BIO_CLOSE;
270e1051a39Sopenharmony_ci        if (num & BIO_FP_APPEND) {
271e1051a39Sopenharmony_ci            if (num & BIO_FP_READ)
272e1051a39Sopenharmony_ci                OPENSSL_strlcpy(p, "a+", sizeof(p));
273e1051a39Sopenharmony_ci            else
274e1051a39Sopenharmony_ci                OPENSSL_strlcpy(p, "a", sizeof(p));
275e1051a39Sopenharmony_ci        } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
276e1051a39Sopenharmony_ci            OPENSSL_strlcpy(p, "r+", sizeof(p));
277e1051a39Sopenharmony_ci        else if (num & BIO_FP_WRITE)
278e1051a39Sopenharmony_ci            OPENSSL_strlcpy(p, "w", sizeof(p));
279e1051a39Sopenharmony_ci        else if (num & BIO_FP_READ)
280e1051a39Sopenharmony_ci            OPENSSL_strlcpy(p, "r", sizeof(p));
281e1051a39Sopenharmony_ci        else {
282e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE);
283e1051a39Sopenharmony_ci            ret = 0;
284e1051a39Sopenharmony_ci            break;
285e1051a39Sopenharmony_ci        }
286e1051a39Sopenharmony_ci# if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
287e1051a39Sopenharmony_ci        if (!(num & BIO_FP_TEXT))
288e1051a39Sopenharmony_ci            OPENSSL_strlcat(p, "b", sizeof(p));
289e1051a39Sopenharmony_ci        else
290e1051a39Sopenharmony_ci            OPENSSL_strlcat(p, "t", sizeof(p));
291e1051a39Sopenharmony_ci# elif defined(OPENSSL_SYS_WIN32_CYGWIN)
292e1051a39Sopenharmony_ci        if (!(num & BIO_FP_TEXT))
293e1051a39Sopenharmony_ci            OPENSSL_strlcat(p, "b", sizeof(p));
294e1051a39Sopenharmony_ci# endif
295e1051a39Sopenharmony_ci        fp = openssl_fopen(ptr, p);
296e1051a39Sopenharmony_ci        if (fp == NULL) {
297e1051a39Sopenharmony_ci            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
298e1051a39Sopenharmony_ci                           "calling fopen(%s, %s)",
299e1051a39Sopenharmony_ci                           ptr, p);
300e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
301e1051a39Sopenharmony_ci            ret = 0;
302e1051a39Sopenharmony_ci            break;
303e1051a39Sopenharmony_ci        }
304e1051a39Sopenharmony_ci        b->ptr = fp;
305e1051a39Sopenharmony_ci        b->init = 1;
306e1051a39Sopenharmony_ci        /* we did fopen -> we disengage UPLINK */
307e1051a39Sopenharmony_ci        BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL);
308e1051a39Sopenharmony_ci        break;
309e1051a39Sopenharmony_ci    case BIO_C_GET_FILE_PTR:
310e1051a39Sopenharmony_ci        /* the ptr parameter is actually a FILE ** in this case. */
311e1051a39Sopenharmony_ci        if (ptr != NULL) {
312e1051a39Sopenharmony_ci            fpp = (FILE **)ptr;
313e1051a39Sopenharmony_ci            *fpp = (FILE *)b->ptr;
314e1051a39Sopenharmony_ci        }
315e1051a39Sopenharmony_ci        break;
316e1051a39Sopenharmony_ci    case BIO_CTRL_GET_CLOSE:
317e1051a39Sopenharmony_ci        ret = (long)b->shutdown;
318e1051a39Sopenharmony_ci        break;
319e1051a39Sopenharmony_ci    case BIO_CTRL_SET_CLOSE:
320e1051a39Sopenharmony_ci        b->shutdown = (int)num;
321e1051a39Sopenharmony_ci        break;
322e1051a39Sopenharmony_ci    case BIO_CTRL_FLUSH:
323e1051a39Sopenharmony_ci        st = b->flags & BIO_FLAGS_UPLINK_INTERNAL
324e1051a39Sopenharmony_ci                ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr);
325e1051a39Sopenharmony_ci        if (st == EOF) {
326e1051a39Sopenharmony_ci            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
327e1051a39Sopenharmony_ci                           "calling fflush()");
328e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB);
329e1051a39Sopenharmony_ci            ret = 0;
330e1051a39Sopenharmony_ci        }
331e1051a39Sopenharmony_ci        break;
332e1051a39Sopenharmony_ci    case BIO_CTRL_DUP:
333e1051a39Sopenharmony_ci        ret = 1;
334e1051a39Sopenharmony_ci        break;
335e1051a39Sopenharmony_ci
336e1051a39Sopenharmony_ci    case BIO_CTRL_WPENDING:
337e1051a39Sopenharmony_ci    case BIO_CTRL_PENDING:
338e1051a39Sopenharmony_ci    case BIO_CTRL_PUSH:
339e1051a39Sopenharmony_ci    case BIO_CTRL_POP:
340e1051a39Sopenharmony_ci    default:
341e1051a39Sopenharmony_ci        ret = 0;
342e1051a39Sopenharmony_ci        break;
343e1051a39Sopenharmony_ci    }
344e1051a39Sopenharmony_ci    return ret;
345e1051a39Sopenharmony_ci}
346e1051a39Sopenharmony_ci
347e1051a39Sopenharmony_cistatic int file_gets(BIO *bp, char *buf, int size)
348e1051a39Sopenharmony_ci{
349e1051a39Sopenharmony_ci    int ret = 0;
350e1051a39Sopenharmony_ci
351e1051a39Sopenharmony_ci    buf[0] = '\0';
352e1051a39Sopenharmony_ci    if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) {
353e1051a39Sopenharmony_ci        if (!UP_fgets(buf, size, bp->ptr))
354e1051a39Sopenharmony_ci            goto err;
355e1051a39Sopenharmony_ci    } else {
356e1051a39Sopenharmony_ci        if (!fgets(buf, size, (FILE *)bp->ptr))
357e1051a39Sopenharmony_ci            goto err;
358e1051a39Sopenharmony_ci    }
359e1051a39Sopenharmony_ci    if (buf[0] != '\0')
360e1051a39Sopenharmony_ci        ret = strlen(buf);
361e1051a39Sopenharmony_ci err:
362e1051a39Sopenharmony_ci    return ret;
363e1051a39Sopenharmony_ci}
364e1051a39Sopenharmony_ci
365e1051a39Sopenharmony_cistatic int file_puts(BIO *bp, const char *str)
366e1051a39Sopenharmony_ci{
367e1051a39Sopenharmony_ci    int n, ret;
368e1051a39Sopenharmony_ci
369e1051a39Sopenharmony_ci    n = strlen(str);
370e1051a39Sopenharmony_ci    ret = file_write(bp, str, n);
371e1051a39Sopenharmony_ci    return ret;
372e1051a39Sopenharmony_ci}
373e1051a39Sopenharmony_ci
374e1051a39Sopenharmony_ci#else
375e1051a39Sopenharmony_ci
376e1051a39Sopenharmony_cistatic int file_write(BIO *b, const char *in, int inl)
377e1051a39Sopenharmony_ci{
378e1051a39Sopenharmony_ci    return -1;
379e1051a39Sopenharmony_ci}
380e1051a39Sopenharmony_cistatic int file_read(BIO *b, char *out, int outl)
381e1051a39Sopenharmony_ci{
382e1051a39Sopenharmony_ci    return -1;
383e1051a39Sopenharmony_ci}
384e1051a39Sopenharmony_cistatic int file_puts(BIO *bp, const char *str)
385e1051a39Sopenharmony_ci{
386e1051a39Sopenharmony_ci    return -1;
387e1051a39Sopenharmony_ci}
388e1051a39Sopenharmony_cistatic int file_gets(BIO *bp, char *buf, int size)
389e1051a39Sopenharmony_ci{
390e1051a39Sopenharmony_ci    return 0;
391e1051a39Sopenharmony_ci}
392e1051a39Sopenharmony_cistatic long file_ctrl(BIO *b, int cmd, long num, void *ptr)
393e1051a39Sopenharmony_ci{
394e1051a39Sopenharmony_ci    return 0;
395e1051a39Sopenharmony_ci}
396e1051a39Sopenharmony_cistatic int file_new(BIO *bi)
397e1051a39Sopenharmony_ci{
398e1051a39Sopenharmony_ci    return 0;
399e1051a39Sopenharmony_ci}
400e1051a39Sopenharmony_cistatic int file_free(BIO *a)
401e1051a39Sopenharmony_ci{
402e1051a39Sopenharmony_ci    return 0;
403e1051a39Sopenharmony_ci}
404e1051a39Sopenharmony_ci
405e1051a39Sopenharmony_cistatic const BIO_METHOD methods_filep = {
406e1051a39Sopenharmony_ci    BIO_TYPE_FILE,
407e1051a39Sopenharmony_ci    "FILE pointer",
408e1051a39Sopenharmony_ci    bwrite_conv,
409e1051a39Sopenharmony_ci    file_write,
410e1051a39Sopenharmony_ci    bread_conv,
411e1051a39Sopenharmony_ci    file_read,
412e1051a39Sopenharmony_ci    file_puts,
413e1051a39Sopenharmony_ci    file_gets,
414e1051a39Sopenharmony_ci    file_ctrl,
415e1051a39Sopenharmony_ci    file_new,
416e1051a39Sopenharmony_ci    file_free,
417e1051a39Sopenharmony_ci    NULL,                      /* file_callback_ctrl */
418e1051a39Sopenharmony_ci};
419e1051a39Sopenharmony_ci
420e1051a39Sopenharmony_ciconst BIO_METHOD *BIO_s_file(void)
421e1051a39Sopenharmony_ci{
422e1051a39Sopenharmony_ci    return &methods_filep;
423e1051a39Sopenharmony_ci}
424e1051a39Sopenharmony_ci
425e1051a39Sopenharmony_ciBIO *BIO_new_file(const char *filename, const char *mode)
426e1051a39Sopenharmony_ci{
427e1051a39Sopenharmony_ci    return NULL;
428e1051a39Sopenharmony_ci}
429e1051a39Sopenharmony_ci
430e1051a39Sopenharmony_ci#endif                         /* OPENSSL_NO_STDIO */
431