xref: /openssl/crypto/bio/bss_null.c (revision 38fc02a7)
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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_local.h"
13 #include "internal/cryptlib.h"
14 
15 static int null_write(BIO *h, const char *buf, int num);
16 static int null_read(BIO *h, char *buf, int size);
17 static int null_puts(BIO *h, const char *str);
18 static int null_gets(BIO *h, char *str, int size);
19 static long null_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20 static const BIO_METHOD null_method = {
21     BIO_TYPE_NULL,
22     "NULL",
23     bwrite_conv,
24     null_write,
25     bread_conv,
26     null_read,
27     null_puts,
28     null_gets,
29     null_ctrl,
30     NULL,
31     NULL,
32     NULL,                     /* null_callback_ctrl */
33 };
34 
BIO_s_null(void)35 const BIO_METHOD *BIO_s_null(void)
36 {
37     return &null_method;
38 }
39 
null_read(BIO * b,char * out,int outl)40 static int null_read(BIO *b, char *out, int outl)
41 {
42     return 0;
43 }
44 
null_write(BIO * b,const char * in,int inl)45 static int null_write(BIO *b, const char *in, int inl)
46 {
47     return inl;
48 }
49 
null_ctrl(BIO * b,int cmd,long num,void * ptr)50 static long null_ctrl(BIO *b, int cmd, long num, void *ptr)
51 {
52     long ret = 1;
53 
54     switch (cmd) {
55     case BIO_CTRL_RESET:
56     case BIO_CTRL_EOF:
57     case BIO_CTRL_SET:
58     case BIO_CTRL_SET_CLOSE:
59     case BIO_CTRL_FLUSH:
60     case BIO_CTRL_DUP:
61         ret = 1;
62         break;
63     case BIO_CTRL_GET_CLOSE:
64     case BIO_CTRL_INFO:
65     case BIO_CTRL_GET:
66     case BIO_CTRL_PENDING:
67     case BIO_CTRL_WPENDING:
68     default:
69         ret = 0;
70         break;
71     }
72     return ret;
73 }
74 
null_gets(BIO * bp,char * buf,int size)75 static int null_gets(BIO *bp, char *buf, int size)
76 {
77     return 0;
78 }
79 
null_puts(BIO * bp,const char * str)80 static int null_puts(BIO *bp, const char *str)
81 {
82     if (str == NULL)
83         return 0;
84     return strlen(str);
85 }
86