]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bio_meth.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / bio / bio_meth.c
CommitLineData
b1322259 1/*
6738bf14 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
f334461f 3 *
09abbca1 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
f334461f
MC
8 */
9
706457b7 10#include "bio_local.h"
176db6dc 11#include "internal/thread_once.h"
f334461f 12
5a7ad1f0
RS
13CRYPTO_RWLOCK *bio_type_lock = NULL;
14static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
15
16DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
17{
63ab5ea1 18 bio_type_lock = CRYPTO_THREAD_lock_new();
5a7ad1f0
RS
19 return bio_type_lock != NULL;
20}
8b8d963d 21
3cb7c5cf 22int BIO_get_new_index(void)
8b8d963d 23{
2f545ae4 24 static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
8b8d963d
RS
25 int newval;
26
5a7ad1f0
RS
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 }
2f545ae4 31 if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
8b8d963d
RS
32 return -1;
33 return newval;
34}
35
f334461f
MC
36BIO_METHOD *BIO_meth_new(int type, const char *name)
37{
38 BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
39
6dbe4dc4
RS
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;
f334461f 45 }
6dbe4dc4 46 biom->type = type;
f334461f
MC
47 return biom;
48}
49
50void BIO_meth_free(BIO_METHOD *biom)
51{
6dbe4dc4
RS
52 if (biom != NULL) {
53 OPENSSL_free(biom->name);
54 OPENSSL_free(biom);
55 }
f334461f
MC
56}
57
693be9a2 58int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int)
3befffa3
MC
59{
60 return biom->bwrite_old;
61}
62
693be9a2 63int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t,
3befffa3 64 size_t *)
f334461f
MC
65{
66 return biom->bwrite;
67}
68
3befffa3 69/* Conversion for old style bwrite to new style */
42c60460 70int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
3befffa3
MC
71{
72 int ret;
73
42c60460
MC
74 if (datal > INT_MAX)
75 datal = INT_MAX;
3befffa3 76
42c60460 77 ret = bio->method->bwrite_old(bio, data, (int)datal);
3befffa3
MC
78
79 if (ret <= 0) {
80 *written = 0;
81 return ret;
82 }
83
84 *written = (size_t)ret;
85
86 return 1;
87}
88
f334461f 89int BIO_meth_set_write(BIO_METHOD *biom,
adb4076a 90 int (*bwrite) (BIO *, const char *, int))
f334461f 91{
3befffa3
MC
92 biom->bwrite_old = bwrite;
93 biom->bwrite = bwrite_conv;
94 return 1;
95}
96
97int BIO_meth_set_write_ex(BIO_METHOD *biom,
98 int (*bwrite) (BIO *, const char *, size_t, size_t *))
99{
100 biom->bwrite_old = NULL;
adb4076a 101 biom->bwrite = bwrite;
f334461f
MC
102 return 1;
103}
104
693be9a2 105int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int)
d07aee2c
MC
106{
107 return biom->bread_old;
108}
109
693be9a2 110int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
f334461f
MC
111{
112 return biom->bread;
113}
114
d07aee2c 115/* Conversion for old style bread to new style */
d62bf89c 116int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
d07aee2c
MC
117{
118 int ret;
119
42c60460
MC
120 if (datal > INT_MAX)
121 datal = INT_MAX;
d07aee2c 122
42c60460 123 ret = bio->method->bread_old(bio, data, (int)datal);
d07aee2c
MC
124
125 if (ret <= 0) {
d62bf89c 126 *readbytes = 0;
d07aee2c
MC
127 return ret;
128 }
129
d62bf89c 130 *readbytes = (size_t)ret;
d07aee2c
MC
131
132 return 1;
133}
134
f334461f 135int BIO_meth_set_read(BIO_METHOD *biom,
adb4076a 136 int (*bread) (BIO *, char *, int))
d07aee2c
MC
137{
138 biom->bread_old = bread;
139 biom->bread = bread_conv;
140 return 1;
141}
142
143int BIO_meth_set_read_ex(BIO_METHOD *biom,
144 int (*bread) (BIO *, char *, size_t, size_t *))
f334461f 145{
3befffa3 146 biom->bread_old = NULL;
adb4076a 147 biom->bread = bread;
f334461f
MC
148 return 1;
149}
150
693be9a2 151int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *)
f334461f
MC
152{
153 return biom->bputs;
154}
155
156int BIO_meth_set_puts(BIO_METHOD *biom,
adb4076a 157 int (*bputs) (BIO *, const char *))
f334461f 158{
adb4076a 159 biom->bputs = bputs;
f334461f
MC
160 return 1;
161}
162
693be9a2 163int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int)
f334461f 164{
a146ae55 165 return biom->bgets;
f334461f
MC
166}
167
168int BIO_meth_set_gets(BIO_METHOD *biom,
adb4076a 169 int (*bgets) (BIO *, char *, int))
f334461f 170{
adb4076a 171 biom->bgets = bgets;
f334461f
MC
172 return 1;
173}
174
693be9a2 175long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *)
f334461f
MC
176{
177 return biom->ctrl;
178}
179
180int BIO_meth_set_ctrl(BIO_METHOD *biom,
181 long (*ctrl) (BIO *, int, long, void *))
182{
183 biom->ctrl = ctrl;
184 return 1;
185}
186
693be9a2 187int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *)
f334461f
MC
188{
189 return biom->create;
190}
191
192int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
193{
194 biom->create = create;
195 return 1;
196}
197
693be9a2 198int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *)
f334461f
MC
199{
200 return biom->destroy;
201}
202
203int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
204{
205 biom->destroy = destroy;
206 return 1;
207}
208
693be9a2 209long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
f334461f
MC
210{
211 return biom->callback_ctrl;
212}
213
214int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
215 long (*callback_ctrl) (BIO *, int,
fce78bd4 216 BIO_info_cb *))
f334461f
MC
217{
218 biom->callback_ctrl = callback_ctrl;
219 return 1;
220}