2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Stolen from tjh's ssl/ssl_trc.c stuff.
19 #define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH - ((i - (i > 6 ? 6 : i) + 3) / 4))
21 #define SPACE(buf, pos, n) (sizeof(buf) - (pos) > (n))
23 int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
24 void *u, const char *s, int len)
26 return BIO_dump_indent_cb(cb, u, s, len, 0);
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)
34 int i, j, rows, trc, n;
41 for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--)
47 else if (indent > 128)
50 dump_width = DUMP_WIDTH_LESS_INDENT(indent);
51 rows = len / dump_width;
52 if ((rows * dump_width) < len)
54 for (i = 0; i < rows; i++) {
55 n = BIO_snprintf(buf, sizeof(buf), "%*s%04x - ", indent, "",
57 for (j = 0; j < dump_width; j++) {
58 if (SPACE(buf, n, 3)) {
59 if (((i * dump_width) + j) >= len) {
62 ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
63 BIO_snprintf(buf + n, 4, "%02x%c", ch,
69 if (SPACE(buf, n, 2)) {
73 for (j = 0; j < dump_width; j++) {
74 if (((i * dump_width) + j) >= len)
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 : '.';
81 buf[n++] = ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
88 if (SPACE(buf, n, 1)) {
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
96 ret += cb((void *)buf, n, u);
100 n = BIO_snprintf(buf, sizeof(buf), "%*s%04x - <SPACES/NULS>\n",
101 indent, "", len + trc);
102 ret += cb((void *)buf, n, u);
108 #ifndef OPENSSL_NO_STDIO
109 static int write_fp(const void *data, size_t len, void *fp)
111 return UP_fwrite(data, len, 1, fp);
114 int BIO_dump_fp(FILE *fp, const char *s, int len)
116 return BIO_dump_cb(write_fp, fp, s, len);
119 int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
121 return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
125 static int write_bio(const void *data, size_t len, void *bp)
127 return BIO_write((BIO *)bp, (const char *)data, len);
130 int BIO_dump(BIO *bp, const char *s, int len)
132 return BIO_dump_cb(write_bio, bp, s, len);
135 int BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
137 return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
140 int BIO_hex_string(BIO *out, int indent, int width, unsigned char *data,
148 for (i = 0; i < datalen - 1; i++) {
150 BIO_printf(out, "%*s", indent, "");
152 BIO_printf(out, "%02X:", data[i]);
156 BIO_printf(out, "\n");
160 BIO_printf(out, "%*s", indent, "");
161 BIO_printf(out, "%02X", data[datalen - 1]);