/src/openssl32/crypto/bio/bio_meth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include "bio_local.h" |
11 | | #include "internal/thread_once.h" |
12 | | |
13 | | CRYPTO_REF_COUNT bio_type_count; |
14 | | static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT; |
15 | | |
16 | | DEFINE_RUN_ONCE_STATIC(do_bio_type_init) |
17 | 0 | { |
18 | 0 | return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START); |
19 | 0 | } |
20 | | |
21 | | int BIO_get_new_index(void) |
22 | 0 | { |
23 | 0 | int newval; |
24 | |
|
25 | 0 | if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) { |
26 | | /* Perhaps the error should be raised in do_bio_type_init()? */ |
27 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB); |
28 | 0 | return -1; |
29 | 0 | } |
30 | 0 | if (!CRYPTO_UP_REF(&bio_type_count, &newval)) |
31 | 0 | return -1; |
32 | 0 | return newval; |
33 | 0 | } |
34 | | |
35 | | BIO_METHOD *BIO_meth_new(int type, const char *name) |
36 | 45 | { |
37 | 45 | BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD)); |
38 | | |
39 | 45 | if (biom == NULL |
40 | 45 | || (biom->name = OPENSSL_strdup(name)) == NULL) { |
41 | 0 | OPENSSL_free(biom); |
42 | 0 | return NULL; |
43 | 0 | } |
44 | 45 | biom->type = type; |
45 | 45 | return biom; |
46 | 45 | } |
47 | | |
48 | | void BIO_meth_free(BIO_METHOD *biom) |
49 | 43 | { |
50 | 43 | if (biom != NULL) { |
51 | 43 | OPENSSL_free(biom->name); |
52 | 43 | OPENSSL_free(biom); |
53 | 43 | } |
54 | 43 | } |
55 | | |
56 | | int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int) |
57 | 0 | { |
58 | 0 | return biom->bwrite_old; |
59 | 0 | } |
60 | | |
61 | | int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t, |
62 | | size_t *) |
63 | 0 | { |
64 | 0 | return biom->bwrite; |
65 | 0 | } |
66 | | |
67 | | /* Conversion for old style bwrite to new style */ |
68 | | int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written) |
69 | 336M | { |
70 | 336M | int ret; |
71 | | |
72 | 336M | if (datal > INT_MAX) |
73 | 0 | datal = INT_MAX; |
74 | | |
75 | 336M | ret = bio->method->bwrite_old(bio, data, (int)datal); |
76 | | |
77 | 336M | if (ret <= 0) { |
78 | 0 | *written = 0; |
79 | 0 | return ret; |
80 | 0 | } |
81 | | |
82 | 336M | *written = (size_t)ret; |
83 | | |
84 | 336M | return 1; |
85 | 336M | } |
86 | | |
87 | | int BIO_meth_set_write(BIO_METHOD *biom, |
88 | | int (*bwrite) (BIO *, const char *, int)) |
89 | 0 | { |
90 | 0 | biom->bwrite_old = bwrite; |
91 | 0 | biom->bwrite = bwrite_conv; |
92 | 0 | return 1; |
93 | 0 | } |
94 | | |
95 | | int BIO_meth_set_write_ex(BIO_METHOD *biom, |
96 | | int (*bwrite) (BIO *, const char *, size_t, size_t *)) |
97 | 45 | { |
98 | 45 | biom->bwrite_old = NULL; |
99 | 45 | biom->bwrite = bwrite; |
100 | 45 | return 1; |
101 | 45 | } |
102 | | |
103 | | int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int) |
104 | 0 | { |
105 | 0 | return biom->bread_old; |
106 | 0 | } |
107 | | |
108 | | int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *) |
109 | 0 | { |
110 | 0 | return biom->bread; |
111 | 0 | } |
112 | | |
113 | | /* Conversion for old style bread to new style */ |
114 | | int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes) |
115 | 1.42G | { |
116 | 1.42G | int ret; |
117 | | |
118 | 1.42G | if (datal > INT_MAX) |
119 | 0 | datal = INT_MAX; |
120 | | |
121 | 1.42G | ret = bio->method->bread_old(bio, data, (int)datal); |
122 | | |
123 | 1.42G | if (ret <= 0) { |
124 | 3.24M | *readbytes = 0; |
125 | 3.24M | return ret; |
126 | 3.24M | } |
127 | | |
128 | 1.41G | *readbytes = (size_t)ret; |
129 | | |
130 | 1.41G | return 1; |
131 | 1.42G | } |
132 | | |
133 | | int BIO_meth_set_read(BIO_METHOD *biom, |
134 | | int (*bread) (BIO *, char *, int)) |
135 | 0 | { |
136 | 0 | biom->bread_old = bread; |
137 | 0 | biom->bread = bread_conv; |
138 | 0 | return 1; |
139 | 0 | } |
140 | | |
141 | | int BIO_meth_set_read_ex(BIO_METHOD *biom, |
142 | | int (*bread) (BIO *, char *, size_t, size_t *)) |
143 | 45 | { |
144 | 45 | biom->bread_old = NULL; |
145 | 45 | biom->bread = bread; |
146 | 45 | return 1; |
147 | 45 | } |
148 | | |
149 | | int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *) |
150 | 0 | { |
151 | 0 | return biom->bputs; |
152 | 0 | } |
153 | | |
154 | | int BIO_meth_set_puts(BIO_METHOD *biom, |
155 | | int (*bputs) (BIO *, const char *)) |
156 | 45 | { |
157 | 45 | biom->bputs = bputs; |
158 | 45 | return 1; |
159 | 45 | } |
160 | | |
161 | | int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int) |
162 | 0 | { |
163 | 0 | return biom->bgets; |
164 | 0 | } |
165 | | |
166 | | int BIO_meth_set_gets(BIO_METHOD *biom, |
167 | | int (*bgets) (BIO *, char *, int)) |
168 | 45 | { |
169 | 45 | biom->bgets = bgets; |
170 | 45 | return 1; |
171 | 45 | } |
172 | | |
173 | | long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *) |
174 | 0 | { |
175 | 0 | return biom->ctrl; |
176 | 0 | } |
177 | | |
178 | | int BIO_meth_set_ctrl(BIO_METHOD *biom, |
179 | | long (*ctrl) (BIO *, int, long, void *)) |
180 | 45 | { |
181 | 45 | biom->ctrl = ctrl; |
182 | 45 | return 1; |
183 | 45 | } |
184 | | |
185 | | int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *) |
186 | 0 | { |
187 | 0 | return biom->create; |
188 | 0 | } |
189 | | |
190 | | int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *)) |
191 | 45 | { |
192 | 45 | biom->create = create; |
193 | 45 | return 1; |
194 | 45 | } |
195 | | |
196 | | int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *) |
197 | 0 | { |
198 | 0 | return biom->destroy; |
199 | 0 | } |
200 | | |
201 | | int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *)) |
202 | 45 | { |
203 | 45 | biom->destroy = destroy; |
204 | 45 | return 1; |
205 | 45 | } |
206 | | |
207 | | long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *) |
208 | 0 | { |
209 | 0 | return biom->callback_ctrl; |
210 | 0 | } |
211 | | |
212 | | int BIO_meth_set_callback_ctrl(BIO_METHOD *biom, |
213 | | long (*callback_ctrl) (BIO *, int, |
214 | | BIO_info_cb *)) |
215 | 0 | { |
216 | 0 | biom->callback_ctrl = callback_ctrl; |
217 | 0 | return 1; |
218 | 0 | } |
219 | | |
220 | | int BIO_meth_set_sendmmsg(BIO_METHOD *biom, |
221 | | int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)) |
222 | 0 | { |
223 | 0 | biom->bsendmmsg = bsendmmsg; |
224 | 0 | return 1; |
225 | 0 | } |
226 | | |
227 | 0 | int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) { |
228 | 0 | return biom->bsendmmsg; |
229 | 0 | } |
230 | | |
231 | | int BIO_meth_set_recvmmsg(BIO_METHOD *biom, |
232 | | int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)) |
233 | 0 | { |
234 | 0 | biom->brecvmmsg = brecvmmsg; |
235 | 0 | return 1; |
236 | 0 | } |
237 | | |
238 | 0 | int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) { |
239 | 0 | return biom->brecvmmsg; |
240 | 0 | } |