Since return is inconsistent, I removed unnecessary parentheses and
[openssl.git] / crypto / bio / bf_null.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
15 /*
16  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
17  */
18
19 static int nullf_write(BIO *h, const char *buf, int num);
20 static int nullf_read(BIO *h, char *buf, int size);
21 static int nullf_puts(BIO *h, const char *str);
22 static int nullf_gets(BIO *h, char *str, int size);
23 static long nullf_ctrl(BIO *h, int cmd, long arg1, void *arg2);
24 static int nullf_new(BIO *h);
25 static int nullf_free(BIO *data);
26 static long nullf_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
27 static const BIO_METHOD methods_nullf = {
28     BIO_TYPE_NULL_FILTER,
29     "NULL filter",
30     /* TODO: Convert to new style write function */
31     bwrite_conv,
32     nullf_write,
33     /* TODO: Convert to new style read function */
34     bread_conv,
35     nullf_read,
36     nullf_puts,
37     nullf_gets,
38     nullf_ctrl,
39     nullf_new,
40     nullf_free,
41     nullf_callback_ctrl,
42 };
43
44 const BIO_METHOD *BIO_f_null(void)
45 {
46     return (&methods_nullf);
47 }
48
49 static int nullf_new(BIO *bi)
50 {
51     bi->init = 1;
52     bi->ptr = NULL;
53     bi->flags = 0;
54     return 1;
55 }
56
57 static int nullf_free(BIO *a)
58 {
59     if (a == NULL)
60         return (0);
61     /*-
62     a->ptr=NULL;
63     a->init=0;
64     a->flags=0;
65     */
66     return 1;
67 }
68
69 static int nullf_read(BIO *b, char *out, int outl)
70 {
71     int ret = 0;
72
73     if (out == NULL)
74         return (0);
75     if (b->next_bio == NULL)
76         return (0);
77     ret = BIO_read(b->next_bio, out, outl);
78     BIO_clear_retry_flags(b);
79     BIO_copy_next_retry(b);
80     return (ret);
81 }
82
83 static int nullf_write(BIO *b, const char *in, int inl)
84 {
85     int ret = 0;
86
87     if ((in == NULL) || (inl <= 0))
88         return (0);
89     if (b->next_bio == NULL)
90         return (0);
91     ret = BIO_write(b->next_bio, in, inl);
92     BIO_clear_retry_flags(b);
93     BIO_copy_next_retry(b);
94     return (ret);
95 }
96
97 static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
98 {
99     long ret;
100
101     if (b->next_bio == NULL)
102         return (0);
103     switch (cmd) {
104     case BIO_C_DO_STATE_MACHINE:
105         BIO_clear_retry_flags(b);
106         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
107         BIO_copy_next_retry(b);
108         break;
109     case BIO_CTRL_DUP:
110         ret = 0L;
111         break;
112     default:
113         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
114     }
115     return (ret);
116 }
117
118 static long nullf_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
119 {
120     long ret = 1;
121
122     if (b->next_bio == NULL)
123         return (0);
124     switch (cmd) {
125     default:
126         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
127         break;
128     }
129     return (ret);
130 }
131
132 static int nullf_gets(BIO *bp, char *buf, int size)
133 {
134     if (bp->next_bio == NULL)
135         return (0);
136     return (BIO_gets(bp->next_bio, buf, size));
137 }
138
139 static int nullf_puts(BIO *bp, const char *str)
140 {
141     if (bp->next_bio == NULL)
142         return (0);
143     return (BIO_puts(bp->next_bio, str));
144 }