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.
18 #define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
20 int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
21 void *u, const char *s, int len)
23 return BIO_dump_indent_cb(cb, u, s, len, 0);
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)
30 char buf[288 + 1], tmp[20], str[128 + 1];
40 memset(str, ' ', indent);
44 dump_width = DUMP_WIDTH_LESS_INDENT(indent);
45 rows = (len / dump_width);
46 if ((rows * dump_width) < len)
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));
56 ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
57 BIO_snprintf(tmp, sizeof(tmp), "%02x%c", ch,
59 OPENSSL_strlcat(buf, tmp, sizeof(buf));
62 OPENSSL_strlcat(buf, " ", sizeof(buf));
63 for (j = 0; j < dump_width; j++) {
64 if (((i * dump_width) + j) >= len)
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 : '.');
71 BIO_snprintf(tmp, sizeof(tmp), "%c",
72 ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
76 OPENSSL_strlcat(buf, tmp, sizeof(buf));
78 OPENSSL_strlcat(buf, "\n", sizeof(buf));
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
83 ret += cb((void *)buf, strlen(buf), u);
88 #ifndef OPENSSL_NO_STDIO
89 static int write_fp(const void *data, size_t len, void *fp)
91 return UP_fwrite(data, len, 1, fp);
94 int BIO_dump_fp(FILE *fp, const char *s, int len)
96 return BIO_dump_cb(write_fp, fp, s, len);
99 int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
101 return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
105 static int write_bio(const void *data, size_t len, void *bp)
107 return BIO_write((BIO *)bp, (const char *)data, len);
110 int BIO_dump(BIO *bp, const char *s, int len)
112 return BIO_dump_cb(write_bio, bp, s, len);
115 int BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
117 return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
120 int BIO_hex_string(BIO *out, int indent, int width, unsigned char *data,
128 for (i = 0; i < datalen - 1; i++) {
130 BIO_printf(out, "%*s", indent, "");
132 BIO_printf(out, "%02X:", data[i]);
136 BIO_printf(out, "\n");
140 BIO_printf(out, "%*s", indent, "");
141 BIO_printf(out, "%02X", data[datalen - 1]);