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