522a05369dc768717dbfbbf78e7e6dfade4f3e14
[openssl.git] / crypto / bio / bio_cb.c
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 #define OPENSSL_SUPPRESS_DEPRECATED
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "bio_local.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/err.h>
18
19 long BIO_debug_callback_ex(BIO *bio, int cmd, const char *argp, size_t len,
20                            int argi, long argl, int ret, size_t *processed)
21 {
22     BIO *b;
23     char buf[256];
24     char *p;
25     int left;
26     size_t l = 0;
27
28     if (processed != NULL)
29         l = *processed;
30
31     left = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio);
32
33     /* Ignore errors and continue printing the other information. */
34     if (left < 0)
35         left = 0;
36     p = buf + left;
37     left = sizeof(buf) - left;
38
39     switch (cmd) {
40     case BIO_CB_FREE:
41         BIO_snprintf(p, left, "Free - %s\n", bio->method->name);
42         break;
43     case BIO_CB_READ:
44         if (bio->method->type & BIO_TYPE_DESCRIPTOR)
45             BIO_snprintf(p, left, "read(%d,%zu) - %s fd=%d\n",
46                          bio->num, len,
47                          bio->method->name, bio->num);
48         else
49             BIO_snprintf(p, left, "read(%d,%zu) - %s\n",
50                     bio->num, len, bio->method->name);
51         break;
52     case BIO_CB_WRITE:
53         if (bio->method->type & BIO_TYPE_DESCRIPTOR)
54             BIO_snprintf(p, left, "write(%d,%zu) - %s fd=%d\n",
55                          bio->num, len,
56                          bio->method->name, bio->num);
57         else
58             BIO_snprintf(p, left, "write(%d,%zu) - %s\n",
59                          bio->num, len, bio->method->name);
60         break;
61     case BIO_CB_PUTS:
62         BIO_snprintf(p, left, "puts() - %s\n", bio->method->name);
63         break;
64     case BIO_CB_GETS:
65         BIO_snprintf(p, left, "gets(%zu) - %s\n", len,
66                      bio->method->name);
67         break;
68     case BIO_CB_CTRL:
69         BIO_snprintf(p, left, "ctrl(%d) - %s\n", argi,
70                      bio->method->name);
71         break;
72     case BIO_CB_RETURN | BIO_CB_READ:
73         BIO_snprintf(p, left, "read return %d processed: %zu\n", ret, l);
74         break;
75     case BIO_CB_RETURN | BIO_CB_WRITE:
76         BIO_snprintf(p, left, "write return %d processed: %zu\n", ret, l);
77         break;
78     case BIO_CB_RETURN | BIO_CB_GETS:
79         BIO_snprintf(p, left, "gets return %d processed: %zu\n", ret, l);
80         break;
81     case BIO_CB_RETURN | BIO_CB_PUTS:
82         BIO_snprintf(p, left, "puts return %d processed: %zu\n", ret, l);
83         break;
84     case BIO_CB_RETURN | BIO_CB_CTRL:
85         BIO_snprintf(p, left, "ctrl return %d\n", ret);
86         break;
87     default:
88         BIO_snprintf(p, left, "bio callback - unknown type (%d)\n", cmd);
89         break;
90     }
91
92     b = (BIO *)bio->cb_arg;
93     if (b != NULL)
94         BIO_write(b, buf, strlen(buf));
95 #if !defined(OPENSSL_NO_STDIO)
96     else
97         fputs(buf, stderr);
98 #endif
99     return ret;
100 }
101
102 #ifndef OPENSSL_NO_DEPRECATED_3_0
103 long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
104                         int argi, long argl, long ret)
105 {
106     size_t processed = 0;
107
108     if (ret > 0)
109         processed = (size_t)ret;
110     BIO_debug_callback_ex(bio, cmd, argp, (size_t)argi,
111                           argi, argl, ret > 0 ? 1 : (int)ret, &processed);
112     return ret;
113 }
114 #endif