f89bba8941e1cb0ffa3ee32413ace02694b1a5e0
[openssl.git] / crypto / bio / bio_meth.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "bio_lcl.h"
11 #include <internal/thread_once.h>
12
13 CRYPTO_RWLOCK *bio_type_lock = NULL;
14 static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
15
16 DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
17 {
18     bio_type_lock = CRYPTO_THREAD_lock_new();
19     return bio_type_lock != NULL;
20 }
21
22 int BIO_get_new_index()
23 {
24     static int bio_count = BIO_TYPE_START;
25     int newval;
26
27     if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
28         BIOerr(BIO_F_BIO_GET_NEW_INDEX, ERR_R_MALLOC_FAILURE);
29         return -1;
30     }
31     if (!CRYPTO_atomic_add(&bio_count, 1, &newval, bio_type_lock))
32         return -1;
33     return newval;
34 }
35
36 BIO_METHOD *BIO_meth_new(int type, const char *name)
37 {
38     BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
39
40     if (biom == NULL
41             || (biom->name = OPENSSL_strdup(name)) == NULL) {
42         OPENSSL_free(biom);
43         BIOerr(BIO_F_BIO_METH_NEW, ERR_R_MALLOC_FAILURE);
44         return NULL;
45     }
46     return biom;
47 }
48
49 void BIO_meth_free(BIO_METHOD *biom)
50 {
51     if (biom != NULL) {
52         OPENSSL_free(biom->name);
53         OPENSSL_free(biom);
54     }
55 }
56
57 int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int)
58 {
59     return biom->bwrite;
60 }
61
62 int BIO_meth_set_write(BIO_METHOD *biom,
63                        int (*bwrite) (BIO *, const char *, int))
64 {
65     biom->bwrite = bwrite;
66     return 1;
67 }
68
69 int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int)
70 {
71     return biom->bread;
72 }
73
74 int BIO_meth_set_read(BIO_METHOD *biom,
75                       int (*bread) (BIO *, char *, int))
76 {
77     biom->bread = bread;
78     return 1;
79 }
80
81 int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *)
82 {
83     return biom->bputs;
84 }
85
86 int BIO_meth_set_puts(BIO_METHOD *biom,
87                       int (*bputs) (BIO *, const char *))
88 {
89     biom->bputs = bputs;
90     return 1;
91 }
92
93 int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int)
94 {
95     return biom->bgets;
96 }
97
98 int BIO_meth_set_gets(BIO_METHOD *biom,
99                       int (*bgets) (BIO *, char *, int))
100 {
101     biom->bgets = bgets;
102     return 1;
103 }
104
105 long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *)
106 {
107     return biom->ctrl;
108 }
109
110 int BIO_meth_set_ctrl(BIO_METHOD *biom,
111                       long (*ctrl) (BIO *, int, long, void *))
112 {
113     biom->ctrl = ctrl;
114     return 1;
115 }
116
117 int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *)
118 {
119     return biom->create;
120 }
121
122 int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
123 {
124     biom->create = create;
125     return 1;
126 }
127
128 int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *)
129 {
130     return biom->destroy;
131 }
132
133 int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
134 {
135     biom->destroy = destroy;
136     return 1;
137 }
138
139 long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
140 {
141     return biom->callback_ctrl;
142 }
143
144 int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
145                                long (*callback_ctrl) (BIO *, int,
146                                                       BIO_info_cb *))
147 {
148     biom->callback_ctrl = callback_ctrl;
149     return 1;
150 }