2 * Copyright 2021 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 #include <openssl/core.h>
11 #include "bio_local.h"
15 * This is distinct from a BIO to prevent casting between the two which could
16 * lead to versioning problems.
18 struct ossl_core_bio_st {
19 CRYPTO_REF_COUNT ref_cnt;
20 CRYPTO_RWLOCK *ref_lock;
24 static OSSL_CORE_BIO *core_bio_new(void)
26 OSSL_CORE_BIO *cb = OPENSSL_malloc(sizeof(*cb));
28 if (cb == NULL || (cb->ref_lock = CRYPTO_THREAD_lock_new()) == NULL) {
36 int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb)
40 return CRYPTO_UP_REF(&cb->ref_cnt, &ref, cb->ref_lock);
43 int ossl_core_bio_free(OSSL_CORE_BIO *cb)
48 CRYPTO_DOWN_REF(&cb->ref_cnt, &ref, cb->ref_lock);
50 res = BIO_free(cb->bio);
51 CRYPTO_THREAD_lock_free(cb->ref_lock);
58 OSSL_CORE_BIO *ossl_core_bio_new_from_bio(BIO *bio)
60 OSSL_CORE_BIO *cb = core_bio_new();
62 if (cb == NULL || !BIO_up_ref(bio)) {
63 ossl_core_bio_free(cb);
70 static OSSL_CORE_BIO *core_bio_new_from_new_bio(BIO *bio)
72 OSSL_CORE_BIO *cb = NULL;
76 if ((cb = core_bio_new()) == NULL) {
84 OSSL_CORE_BIO *ossl_core_bio_new_file(const char *filename, const char *mode)
86 return core_bio_new_from_new_bio(BIO_new_file(filename, mode));
89 OSSL_CORE_BIO *ossl_core_bio_new_mem_buf(const void *buf, int len)
91 return core_bio_new_from_new_bio(BIO_new_mem_buf(buf, len));
94 int ossl_core_bio_read_ex(OSSL_CORE_BIO *cb, void *data, size_t dlen,
97 return BIO_read_ex(cb->bio, data, dlen, readbytes);
100 int ossl_core_bio_write_ex(OSSL_CORE_BIO *cb, const void *data, size_t dlen,
103 return BIO_write_ex(cb->bio, data, dlen, written);
106 int ossl_core_bio_gets(OSSL_CORE_BIO *cb, char *buf, int size)
108 return BIO_gets(cb->bio, buf, size);
111 int ossl_core_bio_puts(OSSL_CORE_BIO *cb, const char *buf)
113 return BIO_puts(cb->bio, buf);
116 long ossl_core_bio_ctrl(OSSL_CORE_BIO *cb, int cmd, long larg, void *parg)
118 return BIO_ctrl(cb->bio, cmd, larg, parg);
121 int ossl_core_bio_vprintf(OSSL_CORE_BIO *cb, const char *format, va_list args)
123 return BIO_vprintf(cb->bio, format, args);