Fix some issues in b_print.c code
[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     nullf_write,
31     nullf_read,
32     nullf_puts,
33     nullf_gets,
34     nullf_ctrl,
35     nullf_new,
36     nullf_free,
37     nullf_callback_ctrl,
38 };
39
40 const BIO_METHOD *BIO_f_null(void)
41 {
42     return (&methods_nullf);
43 }
44
45 static int nullf_new(BIO *bi)
46 {
47     bi->init = 1;
48     bi->ptr = NULL;
49     bi->flags = 0;
50     return (1);
51 }
52
53 static int nullf_free(BIO *a)
54 {
55     if (a == NULL)
56         return (0);
57     /*-
58     a->ptr=NULL;
59     a->init=0;
60     a->flags=0;
61     */
62     return (1);
63 }
64
65 static int nullf_read(BIO *b, char *out, int outl)
66 {
67     int ret = 0;
68
69     if (out == NULL)
70         return (0);
71     if (b->next_bio == NULL)
72         return (0);
73     ret = BIO_read(b->next_bio, out, outl);
74     BIO_clear_retry_flags(b);
75     BIO_copy_next_retry(b);
76     return (ret);
77 }
78
79 static int nullf_write(BIO *b, const char *in, int inl)
80 {
81     int ret = 0;
82
83     if ((in == NULL) || (inl <= 0))
84         return (0);
85     if (b->next_bio == NULL)
86         return (0);
87     ret = BIO_write(b->next_bio, in, inl);
88     BIO_clear_retry_flags(b);
89     BIO_copy_next_retry(b);
90     return (ret);
91 }
92
93 static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
94 {
95     long ret;
96
97     if (b->next_bio == NULL)
98         return (0);
99     switch (cmd) {
100     case BIO_C_DO_STATE_MACHINE:
101         BIO_clear_retry_flags(b);
102         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
103         BIO_copy_next_retry(b);
104         break;
105     case BIO_CTRL_DUP:
106         ret = 0L;
107         break;
108     default:
109         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
110     }
111     return (ret);
112 }
113
114 static long nullf_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
115 {
116     long ret = 1;
117
118     if (b->next_bio == NULL)
119         return (0);
120     switch (cmd) {
121     default:
122         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
123         break;
124     }
125     return (ret);
126 }
127
128 static int nullf_gets(BIO *bp, char *buf, int size)
129 {
130     if (bp->next_bio == NULL)
131         return (0);
132     return (BIO_gets(bp->next_bio, buf, size));
133 }
134
135 static int nullf_puts(BIO *bp, const char *str)
136 {
137     if (bp->next_bio == NULL)
138         return (0);
139     return (BIO_puts(bp->next_bio, str));
140 }