Remove code that prints "<SPACES/NULS>" in hexdumps
[openssl.git] / crypto / bio / b_dump.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 /*
11  * Stolen from tjh's ssl/ssl_trc.c stuff.
12  */
13
14 #include <stdio.h>
15 #include "bio_lcl.h"
16
17 #define DUMP_WIDTH      16
18 #define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
19
20 int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
21                 void *u, const char *s, int len)
22 {
23     return BIO_dump_indent_cb(cb, u, s, len, 0);
24 }
25
26 int BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
27                        void *u, const char *s, int len, int indent)
28 {
29     int ret = 0;
30     char buf[288 + 1], tmp[20], str[128 + 1];
31     int i, j, rows;
32     unsigned char ch;
33     int dump_width;
34
35     if (indent < 0)
36         indent = 0;
37     if (indent) {
38         if (indent > 128)
39             indent = 128;
40         memset(str, ' ', indent);
41     }
42     str[indent] = '\0';
43
44     dump_width = DUMP_WIDTH_LESS_INDENT(indent);
45     rows = (len / dump_width);
46     if ((rows * dump_width) < len)
47         rows++;
48     for (i = 0; i < rows; i++) {
49         OPENSSL_strlcpy(buf, str, sizeof(buf));
50         BIO_snprintf(tmp, sizeof(tmp), "%04x - ", i * dump_width);
51         OPENSSL_strlcat(buf, tmp, sizeof(buf));
52         for (j = 0; j < dump_width; j++) {
53             if (((i * dump_width) + j) >= len) {
54                 OPENSSL_strlcat(buf, "   ", sizeof(buf));
55             } else {
56                 ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
57                 BIO_snprintf(tmp, sizeof(tmp), "%02x%c", ch,
58                              j == 7 ? '-' : ' ');
59                 OPENSSL_strlcat(buf, tmp, sizeof(buf));
60             }
61         }
62         OPENSSL_strlcat(buf, "  ", sizeof(buf));
63         for (j = 0; j < dump_width; j++) {
64             if (((i * dump_width) + j) >= len)
65                 break;
66             ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
67 #ifndef CHARSET_EBCDIC
68             BIO_snprintf(tmp, sizeof(tmp), "%c",
69                          ((ch >= ' ') && (ch <= '~')) ? ch : '.');
70 #else
71             BIO_snprintf(tmp, sizeof(tmp), "%c",
72                          ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
73                          ? os_toebcdic[ch]
74                          : '.');
75 #endif
76             OPENSSL_strlcat(buf, tmp, sizeof(buf));
77         }
78         OPENSSL_strlcat(buf, "\n", sizeof(buf));
79         /*
80          * if this is the last call then update the ddt_dump thing so that we
81          * will move the selection point in the debug window
82          */
83         ret += cb((void *)buf, strlen(buf), u);
84     }
85     return ret;
86 }
87
88 #ifndef OPENSSL_NO_STDIO
89 static int write_fp(const void *data, size_t len, void *fp)
90 {
91     return UP_fwrite(data, len, 1, fp);
92 }
93
94 int BIO_dump_fp(FILE *fp, const char *s, int len)
95 {
96     return BIO_dump_cb(write_fp, fp, s, len);
97 }
98
99 int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
100 {
101     return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
102 }
103 #endif
104
105 static int write_bio(const void *data, size_t len, void *bp)
106 {
107     return BIO_write((BIO *)bp, (const char *)data, len);
108 }
109
110 int BIO_dump(BIO *bp, const char *s, int len)
111 {
112     return BIO_dump_cb(write_bio, bp, s, len);
113 }
114
115 int BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
116 {
117     return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
118 }
119
120 int BIO_hex_string(BIO *out, int indent, int width, unsigned char *data,
121                    int datalen)
122 {
123     int i, j = 0;
124
125     if (datalen < 1)
126         return 1;
127
128     for (i = 0; i < datalen - 1; i++) {
129         if (i && !j)
130             BIO_printf(out, "%*s", indent, "");
131
132         BIO_printf(out, "%02X:", data[i]);
133
134         j = (j + 1) % width;
135         if (!j)
136             BIO_printf(out, "\n");
137     }
138
139     if (i && !j)
140         BIO_printf(out, "%*s", indent, "");
141     BIO_printf(out, "%02X", data[datalen - 1]);
142     return 1;
143 }