2 * Copyright 1995-2016 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 int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
22 void *u, const char *s, int len)
24 return BIO_dump_indent_cb(cb, u, s, len, 0);
27 int BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
28 void *u, const char *s, int len, int indent)
31 char buf[288 + 1], tmp[20], str[128 + 1];
39 for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--)
48 memset(str, ' ', indent);
52 dump_width = DUMP_WIDTH_LESS_INDENT(indent);
53 rows = (len / dump_width);
54 if ((rows * dump_width) < len)
56 for (i = 0; i < rows; i++) {
57 OPENSSL_strlcpy(buf, str, sizeof buf);
58 BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width);
59 OPENSSL_strlcat(buf, tmp, sizeof buf);
60 for (j = 0; j < dump_width; j++) {
61 if (((i * dump_width) + j) >= len) {
62 OPENSSL_strlcat(buf, " ", sizeof buf);
64 ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
65 BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch,
67 OPENSSL_strlcat(buf, tmp, sizeof buf);
70 OPENSSL_strlcat(buf, " ", sizeof buf);
71 for (j = 0; j < dump_width; j++) {
72 if (((i * dump_width) + j) >= len)
74 ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
75 #ifndef CHARSET_EBCDIC
76 BIO_snprintf(tmp, sizeof tmp, "%c",
77 ((ch >= ' ') && (ch <= '~')) ? ch : '.');
79 BIO_snprintf(tmp, sizeof tmp, "%c",
80 ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
84 OPENSSL_strlcat(buf, tmp, sizeof buf);
86 OPENSSL_strlcat(buf, "\n", sizeof buf);
88 * if this is the last call then update the ddt_dump thing so that we
89 * will move the selection point in the debug window
91 ret += cb((void *)buf, strlen(buf), u);
95 BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str,
97 ret += cb((void *)buf, strlen(buf), u);
103 #ifndef OPENSSL_NO_STDIO
104 static int write_fp(const void *data, size_t len, void *fp)
106 return UP_fwrite(data, len, 1, fp);
109 int BIO_dump_fp(FILE *fp, const char *s, int len)
111 return BIO_dump_cb(write_fp, fp, s, len);
114 int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
116 return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
120 static int write_bio(const void *data, size_t len, void *bp)
122 return BIO_write((BIO *)bp, (const char *)data, len);
125 int BIO_dump(BIO *bp, const char *s, int len)
127 return BIO_dump_cb(write_bio, bp, s, len);
130 int BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
132 return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
135 int BIO_hex_string(BIO *out, int indent, int width, unsigned char *data,
143 for (i = 0; i < datalen - 1; i++) {
145 BIO_printf(out, "%*s", indent, "");
147 BIO_printf(out, "%02X:", data[i]);
151 BIO_printf(out, "\n");
155 BIO_printf(out, "%*s", indent, "");
156 BIO_printf(out, "%02X", data[datalen - 1]);