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