]>
Commit | Line | Data |
---|---|---|
b1322259 RS |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
f334461f | 3 | * |
b1322259 RS |
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 | |
f334461f MC |
8 | */ |
9 | ||
10 | #include "bio_lcl.h" | |
176db6dc | 11 | #include "internal/thread_once.h" |
f334461f | 12 | |
5a7ad1f0 RS |
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 | } | |
8b8d963d RS |
21 | |
22 | int BIO_get_new_index() | |
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 |
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->type = type; | |
42 | biom->name = name; | |
43 | } | |
44 | return biom; | |
45 | } | |
46 | ||
47 | void BIO_meth_free(BIO_METHOD *biom) | |
48 | { | |
49 | OPENSSL_free(biom); | |
50 | } | |
51 | ||
52 | int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int) | |
3befffa3 MC |
53 | { |
54 | return biom->bwrite_old; | |
55 | } | |
56 | ||
57 | int (*BIO_meth_get_write_ex(BIO_METHOD *biom)) (BIO *, const char *, size_t, | |
58 | size_t *) | |
f334461f MC |
59 | { |
60 | return biom->bwrite; | |
61 | } | |
62 | ||
3befffa3 | 63 | /* Conversion for old style bwrite to new style */ |
42c60460 | 64 | int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written) |
3befffa3 MC |
65 | { |
66 | int ret; | |
67 | ||
42c60460 MC |
68 | if (datal > INT_MAX) |
69 | datal = INT_MAX; | |
3befffa3 | 70 | |
42c60460 | 71 | ret = bio->method->bwrite_old(bio, data, (int)datal); |
3befffa3 MC |
72 | |
73 | if (ret <= 0) { | |
74 | *written = 0; | |
75 | return ret; | |
76 | } | |
77 | ||
78 | *written = (size_t)ret; | |
79 | ||
80 | return 1; | |
81 | } | |
82 | ||
f334461f | 83 | int BIO_meth_set_write(BIO_METHOD *biom, |
adb4076a | 84 | int (*bwrite) (BIO *, const char *, int)) |
f334461f | 85 | { |
3befffa3 MC |
86 | biom->bwrite_old = bwrite; |
87 | biom->bwrite = bwrite_conv; | |
88 | return 1; | |
89 | } | |
90 | ||
91 | int BIO_meth_set_write_ex(BIO_METHOD *biom, | |
92 | int (*bwrite) (BIO *, const char *, size_t, size_t *)) | |
93 | { | |
94 | biom->bwrite_old = NULL; | |
adb4076a | 95 | biom->bwrite = bwrite; |
f334461f MC |
96 | return 1; |
97 | } | |
98 | ||
a146ae55 | 99 | int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int) |
d07aee2c MC |
100 | { |
101 | return biom->bread_old; | |
102 | } | |
103 | ||
104 | int (*BIO_meth_get_read_ex(BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *) | |
f334461f MC |
105 | { |
106 | return biom->bread; | |
107 | } | |
108 | ||
d07aee2c | 109 | /* Conversion for old style bread to new style */ |
d62bf89c | 110 | int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes) |
d07aee2c MC |
111 | { |
112 | int ret; | |
113 | ||
42c60460 MC |
114 | if (datal > INT_MAX) |
115 | datal = INT_MAX; | |
d07aee2c | 116 | |
42c60460 | 117 | ret = bio->method->bread_old(bio, data, (int)datal); |
d07aee2c MC |
118 | |
119 | if (ret <= 0) { | |
d62bf89c | 120 | *readbytes = 0; |
d07aee2c MC |
121 | return ret; |
122 | } | |
123 | ||
d62bf89c | 124 | *readbytes = (size_t)ret; |
d07aee2c MC |
125 | |
126 | return 1; | |
127 | } | |
128 | ||
f334461f | 129 | int BIO_meth_set_read(BIO_METHOD *biom, |
adb4076a | 130 | int (*bread) (BIO *, char *, int)) |
d07aee2c MC |
131 | { |
132 | biom->bread_old = bread; | |
133 | biom->bread = bread_conv; | |
134 | return 1; | |
135 | } | |
136 | ||
137 | int BIO_meth_set_read_ex(BIO_METHOD *biom, | |
138 | int (*bread) (BIO *, char *, size_t, size_t *)) | |
f334461f | 139 | { |
3befffa3 | 140 | biom->bread_old = NULL; |
adb4076a | 141 | biom->bread = bread; |
f334461f MC |
142 | return 1; |
143 | } | |
144 | ||
145 | int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *) | |
146 | { | |
147 | return biom->bputs; | |
148 | } | |
149 | ||
150 | int BIO_meth_set_puts(BIO_METHOD *biom, | |
adb4076a | 151 | int (*bputs) (BIO *, const char *)) |
f334461f | 152 | { |
adb4076a | 153 | biom->bputs = bputs; |
f334461f MC |
154 | return 1; |
155 | } | |
156 | ||
157 | int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int) | |
158 | { | |
a146ae55 | 159 | return biom->bgets; |
f334461f MC |
160 | } |
161 | ||
162 | int BIO_meth_set_gets(BIO_METHOD *biom, | |
adb4076a | 163 | int (*bgets) (BIO *, char *, int)) |
f334461f | 164 | { |
adb4076a | 165 | biom->bgets = bgets; |
f334461f MC |
166 | return 1; |
167 | } | |
168 | ||
169 | long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *) | |
170 | { | |
171 | return biom->ctrl; | |
172 | } | |
173 | ||
174 | int BIO_meth_set_ctrl(BIO_METHOD *biom, | |
175 | long (*ctrl) (BIO *, int, long, void *)) | |
176 | { | |
177 | biom->ctrl = ctrl; | |
178 | return 1; | |
179 | } | |
180 | ||
a146ae55 | 181 | int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *) |
f334461f MC |
182 | { |
183 | return biom->create; | |
184 | } | |
185 | ||
186 | int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *)) | |
187 | { | |
188 | biom->create = create; | |
189 | return 1; | |
190 | } | |
191 | ||
192 | int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *) | |
193 | { | |
194 | return biom->destroy; | |
195 | } | |
196 | ||
197 | int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *)) | |
198 | { | |
199 | biom->destroy = destroy; | |
200 | return 1; | |
201 | } | |
202 | ||
203 | long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom)) (BIO *, int, bio_info_cb *) | |
204 | { | |
205 | return biom->callback_ctrl; | |
206 | } | |
207 | ||
208 | int BIO_meth_set_callback_ctrl(BIO_METHOD *biom, | |
209 | long (*callback_ctrl) (BIO *, int, | |
210 | bio_info_cb *)) | |
211 | { | |
212 | biom->callback_ctrl = callback_ctrl; | |
213 | return 1; | |
214 | } |