Add BIO_get_new_index()
[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
12 CRYPTO_RWLOCK *bio_type_lock;
13 static int bio_count = BIO_TYPE_START;
14
15 int BIO_get_new_index()
16 {
17     int newval;
18
19     if (!CRYPTO_atomic_add(&bio_count, 1, &newval, bio_type_lock))
20         return -1;
21     return newval;
22 }
23
24 BIO_METHOD *BIO_meth_new(int type, const char *name)
25 {
26     BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
27
28     if (biom != NULL) {
29         biom->type = type;
30         biom->name = name;
31     }
32     return biom;
33 }
34
35 void BIO_meth_free(BIO_METHOD *biom)
36 {
37     OPENSSL_free(biom);
38 }
39
40 int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int)
41 {
42     return biom->bwrite;
43 }
44
45 int BIO_meth_set_write(BIO_METHOD *biom,
46                        int (*bwrite) (BIO *, const char *, int))
47 {
48     biom->bwrite = bwrite;
49     return 1;
50 }
51
52 int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int)
53 {
54     return biom->bread;
55 }
56
57 int BIO_meth_set_read(BIO_METHOD *biom,
58                       int (*bread) (BIO *, char *, int))
59 {
60     biom->bread = bread;
61     return 1;
62 }
63
64 int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *)
65 {
66     return biom->bputs;
67 }
68
69 int BIO_meth_set_puts(BIO_METHOD *biom,
70                       int (*bputs) (BIO *, const char *))
71 {
72     biom->bputs = bputs;
73     return 1;
74 }
75
76 int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int)
77 {
78     return biom->bgets;
79 }
80
81 int BIO_meth_set_gets(BIO_METHOD *biom,
82                       int (*bgets) (BIO *, char *, int))
83 {
84     biom->bgets = bgets;
85     return 1;
86 }
87
88 long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *)
89 {
90     return biom->ctrl;
91 }
92
93 int BIO_meth_set_ctrl(BIO_METHOD *biom,
94                       long (*ctrl) (BIO *, int, long, void *))
95 {
96     biom->ctrl = ctrl;
97     return 1;
98 }
99
100 int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *)
101 {
102     return biom->create;
103 }
104
105 int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
106 {
107     biom->create = create;
108     return 1;
109 }
110
111 int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *)
112 {
113     return biom->destroy;
114 }
115
116 int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
117 {
118     biom->destroy = destroy;
119     return 1;
120 }
121
122 long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom)) (BIO *, int, bio_info_cb *)
123 {
124     return biom->callback_ctrl;
125 }
126
127 int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
128                                long (*callback_ctrl) (BIO *, int,
129                                                       bio_info_cb *))
130 {
131     biom->callback_ctrl = callback_ctrl;
132     return 1;
133 }