Fix invalid function type casts.
[openssl.git] / crypto / bio / bf_nbio.c
1 /*
2  * Copyright 1995-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 <stdio.h>
11 #include <errno.h>
12 #include "bio_lcl.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/rand.h>
15
16 /*
17  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18  */
19
20 static int nbiof_write(BIO *h, const char *buf, int num);
21 static int nbiof_read(BIO *h, char *buf, int size);
22 static int nbiof_puts(BIO *h, const char *str);
23 static int nbiof_gets(BIO *h, char *str, int size);
24 static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25 static int nbiof_new(BIO *h);
26 static int nbiof_free(BIO *data);
27 static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
28 typedef struct nbio_test_st {
29     /* only set if we sent a 'should retry' error */
30     int lrn;
31     int lwn;
32 } NBIO_TEST;
33
34 static const BIO_METHOD methods_nbiof = {
35     BIO_TYPE_NBIO_TEST,
36     "non-blocking IO test filter",
37     /* TODO: Convert to new style write function */
38     bwrite_conv,
39     nbiof_write,
40     /* TODO: Convert to new style read function */
41     bread_conv,
42     nbiof_read,
43     nbiof_puts,
44     nbiof_gets,
45     nbiof_ctrl,
46     nbiof_new,
47     nbiof_free,
48     nbiof_callback_ctrl,
49 };
50
51 const BIO_METHOD *BIO_f_nbio_test(void)
52 {
53     return &methods_nbiof;
54 }
55
56 static int nbiof_new(BIO *bi)
57 {
58     NBIO_TEST *nt;
59
60     if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
61         return 0;
62     nt->lrn = -1;
63     nt->lwn = -1;
64     bi->ptr = (char *)nt;
65     bi->init = 1;
66     return 1;
67 }
68
69 static int nbiof_free(BIO *a)
70 {
71     if (a == NULL)
72         return 0;
73     OPENSSL_free(a->ptr);
74     a->ptr = NULL;
75     a->init = 0;
76     a->flags = 0;
77     return 1;
78 }
79
80 static int nbiof_read(BIO *b, char *out, int outl)
81 {
82     int ret = 0;
83     int num;
84     unsigned char n;
85
86     if (out == NULL)
87         return 0;
88     if (b->next_bio == NULL)
89         return 0;
90
91     BIO_clear_retry_flags(b);
92     if (RAND_bytes(&n, 1) <= 0)
93         return -1;
94     num = (n & 0x07);
95
96     if (outl > num)
97         outl = num;
98
99     if (num == 0) {
100         ret = -1;
101         BIO_set_retry_read(b);
102     } else {
103         ret = BIO_read(b->next_bio, out, outl);
104         if (ret < 0)
105             BIO_copy_next_retry(b);
106     }
107     return ret;
108 }
109
110 static int nbiof_write(BIO *b, const char *in, int inl)
111 {
112     NBIO_TEST *nt;
113     int ret = 0;
114     int num;
115     unsigned char n;
116
117     if ((in == NULL) || (inl <= 0))
118         return 0;
119     if (b->next_bio == NULL)
120         return 0;
121     nt = (NBIO_TEST *)b->ptr;
122
123     BIO_clear_retry_flags(b);
124
125     if (nt->lwn > 0) {
126         num = nt->lwn;
127         nt->lwn = 0;
128     } else {
129         if (RAND_bytes(&n, 1) <= 0)
130             return -1;
131         num = (n & 7);
132     }
133
134     if (inl > num)
135         inl = num;
136
137     if (num == 0) {
138         ret = -1;
139         BIO_set_retry_write(b);
140     } else {
141         ret = BIO_write(b->next_bio, in, inl);
142         if (ret < 0) {
143             BIO_copy_next_retry(b);
144             nt->lwn = inl;
145         }
146     }
147     return ret;
148 }
149
150 static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
151 {
152     long ret;
153
154     if (b->next_bio == NULL)
155         return 0;
156     switch (cmd) {
157     case BIO_C_DO_STATE_MACHINE:
158         BIO_clear_retry_flags(b);
159         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
160         BIO_copy_next_retry(b);
161         break;
162     case BIO_CTRL_DUP:
163         ret = 0L;
164         break;
165     default:
166         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
167         break;
168     }
169     return ret;
170 }
171
172 static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
173 {
174     long ret = 1;
175
176     if (b->next_bio == NULL)
177         return 0;
178     switch (cmd) {
179     default:
180         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
181         break;
182     }
183     return ret;
184 }
185
186 static int nbiof_gets(BIO *bp, char *buf, int size)
187 {
188     if (bp->next_bio == NULL)
189         return 0;
190     return BIO_gets(bp->next_bio, buf, size);
191 }
192
193 static int nbiof_puts(BIO *bp, const char *str)
194 {
195     if (bp->next_bio == NULL)
196         return 0;
197     return BIO_puts(bp->next_bio, str);
198 }