]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bio_meth.c
Create BIO_read_ex() which handles size_t arguments
[thirdparty/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->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)
53 {
54 return biom->bwrite;
55 }
56
57 int BIO_meth_set_write(BIO_METHOD *biom,
58 int (*bwrite) (BIO *, const char *, int))
59 {
60 biom->bwrite = bwrite;
61 return 1;
62 }
63
64 int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int)
65 {
66 return biom->bread_old;
67 }
68
69 int (*BIO_meth_get_read_ex(BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
70 {
71 return biom->bread;
72 }
73
74 /* Conversion for old style bread to new style */
75 int bread_conv(BIO *bio, char *out, size_t outl, size_t *read)
76 {
77 int ret;
78
79 if (outl > INT_MAX)
80 return 0;
81
82 ret = bio->method->bread_old(bio, out, (int)outl);
83
84 if (ret <= 0) {
85 *read = 0;
86 return ret;
87 }
88
89 *read = (size_t)ret;
90
91 return 1;
92 }
93
94 int BIO_meth_set_read(BIO_METHOD *biom,
95 int (*bread) (BIO *, char *, int))
96 {
97 biom->bread_old = bread;
98 biom->bread = bread_conv;
99 return 1;
100 }
101
102 int BIO_meth_set_read_ex(BIO_METHOD *biom,
103 int (*bread) (BIO *, char *, size_t, size_t *))
104 {
105 biom->bread = bread;
106 return 1;
107 }
108
109 int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *)
110 {
111 return biom->bputs;
112 }
113
114 int BIO_meth_set_puts(BIO_METHOD *biom,
115 int (*bputs) (BIO *, const char *))
116 {
117 biom->bputs = bputs;
118 return 1;
119 }
120
121 int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int)
122 {
123 return biom->bgets;
124 }
125
126 int BIO_meth_set_gets(BIO_METHOD *biom,
127 int (*bgets) (BIO *, char *, int))
128 {
129 biom->bgets = bgets;
130 return 1;
131 }
132
133 long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *)
134 {
135 return biom->ctrl;
136 }
137
138 int BIO_meth_set_ctrl(BIO_METHOD *biom,
139 long (*ctrl) (BIO *, int, long, void *))
140 {
141 biom->ctrl = ctrl;
142 return 1;
143 }
144
145 int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *)
146 {
147 return biom->create;
148 }
149
150 int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
151 {
152 biom->create = create;
153 return 1;
154 }
155
156 int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *)
157 {
158 return biom->destroy;
159 }
160
161 int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
162 {
163 biom->destroy = destroy;
164 return 1;
165 }
166
167 long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom)) (BIO *, int, bio_info_cb *)
168 {
169 return biom->callback_ctrl;
170 }
171
172 int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
173 long (*callback_ctrl) (BIO *, int,
174 bio_info_cb *))
175 {
176 biom->callback_ctrl = callback_ctrl;
177 return 1;
178 }