8d78c942315e7de610c948972af7dba0a46ab0e1
[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 long nullf_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
25 static const BIO_METHOD methods_nullf = {
26     BIO_TYPE_NULL_FILTER,
27     "NULL filter",
28     nullf_write,
29     nullf_read,
30     nullf_puts,
31     nullf_gets,
32     nullf_ctrl,
33     NULL,
34     NULL,
35     nullf_callback_ctrl,
36 };
37
38 const BIO_METHOD *BIO_f_null(void)
39 {
40     return (&methods_nullf);
41 }
42
43 static int nullf_read(BIO *b, char *out, int outl)
44 {
45     int ret = 0;
46
47     if (out == NULL)
48         return (0);
49     if (b->next_bio == NULL)
50         return (0);
51     ret = BIO_read(b->next_bio, out, outl);
52     BIO_clear_retry_flags(b);
53     BIO_copy_next_retry(b);
54     return (ret);
55 }
56
57 static int nullf_write(BIO *b, const char *in, int inl)
58 {
59     int ret = 0;
60
61     if ((in == NULL) || (inl <= 0))
62         return (0);
63     if (b->next_bio == NULL)
64         return (0);
65     ret = BIO_write(b->next_bio, in, inl);
66     BIO_clear_retry_flags(b);
67     BIO_copy_next_retry(b);
68     return (ret);
69 }
70
71 static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
72 {
73     long ret;
74
75     if (b->next_bio == NULL)
76         return (0);
77     switch (cmd) {
78     case BIO_C_DO_STATE_MACHINE:
79         BIO_clear_retry_flags(b);
80         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
81         BIO_copy_next_retry(b);
82         break;
83     case BIO_CTRL_DUP:
84         ret = 0L;
85         break;
86     default:
87         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
88     }
89     return (ret);
90 }
91
92 static long nullf_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
93 {
94     long ret = 1;
95
96     if (b->next_bio == NULL)
97         return (0);
98     switch (cmd) {
99     default:
100         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
101         break;
102     }
103     return (ret);
104 }
105
106 static int nullf_gets(BIO *bp, char *buf, int size)
107 {
108     if (bp->next_bio == NULL)
109         return (0);
110     return (BIO_gets(bp->next_bio, buf, size));
111 }
112
113 static int nullf_puts(BIO *bp, const char *str)
114 {
115     if (bp->next_bio == NULL)
116         return (0);
117     return (BIO_puts(bp->next_bio, str));
118 }