2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/thread_once.h"
13 CRYPTO_RWLOCK *bio_type_lock = NULL;
14 static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
16 DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
18 bio_type_lock = CRYPTO_THREAD_glock_new("bio_type");
19 return bio_type_lock != NULL;
22 int BIO_get_new_index()
24 static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
27 if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
28 BIOerr(BIO_F_BIO_GET_NEW_INDEX, ERR_R_MALLOC_FAILURE);
31 if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
36 BIO_METHOD *BIO_meth_new(int type, const char *name)
38 BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
47 void BIO_meth_free(BIO_METHOD *biom)
52 int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int)
54 return biom->bwrite_old;
57 int (*BIO_meth_get_write_ex(BIO_METHOD *biom)) (BIO *, const char *, size_t,
63 /* Conversion for old style bwrite to new style */
64 int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
71 ret = bio->method->bwrite_old(bio, data, (int)datal);
78 *written = (size_t)ret;
83 int BIO_meth_set_write(BIO_METHOD *biom,
84 int (*bwrite) (BIO *, const char *, int))
86 biom->bwrite_old = bwrite;
87 biom->bwrite = bwrite_conv;
91 int BIO_meth_set_write_ex(BIO_METHOD *biom,
92 int (*bwrite) (BIO *, const char *, size_t, size_t *))
94 biom->bwrite_old = NULL;
95 biom->bwrite = bwrite;
99 int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int)
101 return biom->bread_old;
104 int (*BIO_meth_get_read_ex(BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
109 /* Conversion for old style bread to new style */
110 int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
117 ret = bio->method->bread_old(bio, data, (int)datal);
124 *readbytes = (size_t)ret;
129 int BIO_meth_set_read(BIO_METHOD *biom,
130 int (*bread) (BIO *, char *, int))
132 biom->bread_old = bread;
133 biom->bread = bread_conv;
137 int BIO_meth_set_read_ex(BIO_METHOD *biom,
138 int (*bread) (BIO *, char *, size_t, size_t *))
140 biom->bread_old = NULL;
145 int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *)
150 int BIO_meth_set_puts(BIO_METHOD *biom,
151 int (*bputs) (BIO *, const char *))
157 int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int)
162 int BIO_meth_set_gets(BIO_METHOD *biom,
163 int (*bgets) (BIO *, char *, int))
169 long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *)
174 int BIO_meth_set_ctrl(BIO_METHOD *biom,
175 long (*ctrl) (BIO *, int, long, void *))
181 int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *)
186 int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
188 biom->create = create;
192 int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *)
194 return biom->destroy;
197 int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
199 biom->destroy = destroy;
203 long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom)) (BIO *, int, bio_info_cb *)
205 return biom->callback_ctrl;
208 int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
209 long (*callback_ctrl) (BIO *, int,
212 biom->callback_ctrl = callback_ctrl;