155714Skris/* crypto/bio/b_dump.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296465Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296465Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296465Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296465Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296465Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 59296465Sdelphij/* 6055714Skris * Stolen from tjh's ssl/ssl_trc.c stuff. 6155714Skris */ 6255714Skris 6355714Skris#include <stdio.h> 6455714Skris#include "cryptlib.h" 65160814Ssimon#include "bio_lcl.h" 6655714Skris 6755714Skris#define TRUNCATE 68296465Sdelphij#define DUMP_WIDTH 16 6968651Skris#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4)) 7055714Skris 71296465Sdelphijint BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u), 72296465Sdelphij void *u, const char *s, int len) 73296465Sdelphij{ 74296465Sdelphij return BIO_dump_indent_cb(cb, u, s, len, 0); 75296465Sdelphij} 7655714Skris 77296465Sdelphijint BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u), 78296465Sdelphij void *u, const char *s, int len, int indent) 79296465Sdelphij{ 80296465Sdelphij int ret = 0; 81296465Sdelphij char buf[288 + 1], tmp[20], str[128 + 1]; 82296465Sdelphij int i, j, rows, trc; 83296465Sdelphij unsigned char ch; 84296465Sdelphij int dump_width; 85160814Ssimon 86296465Sdelphij trc = 0; 87160814Ssimon 8855714Skris#ifdef TRUNCATE 89296465Sdelphij for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--) 90296465Sdelphij trc++; 9155714Skris#endif 9255714Skris 93296465Sdelphij if (indent < 0) 94296465Sdelphij indent = 0; 95296465Sdelphij if (indent) { 96296465Sdelphij if (indent > 128) 97296465Sdelphij indent = 128; 98296465Sdelphij memset(str, ' ', indent); 99296465Sdelphij } 100296465Sdelphij str[indent] = '\0'; 101160814Ssimon 102296465Sdelphij dump_width = DUMP_WIDTH_LESS_INDENT(indent); 103296465Sdelphij rows = (len / dump_width); 104296465Sdelphij if ((rows * dump_width) < len) 105296465Sdelphij rows++; 106296465Sdelphij for (i = 0; i < rows; i++) { 107296465Sdelphij buf[0] = '\0'; /* start with empty string */ 108296465Sdelphij BUF_strlcpy(buf, str, sizeof buf); 109296465Sdelphij BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width); 110296465Sdelphij BUF_strlcat(buf, tmp, sizeof buf); 111296465Sdelphij for (j = 0; j < dump_width; j++) { 112296465Sdelphij if (((i * dump_width) + j) >= len) { 113296465Sdelphij BUF_strlcat(buf, " ", sizeof buf); 114296465Sdelphij } else { 115296465Sdelphij ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 116296465Sdelphij BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch, 117296465Sdelphij j == 7 ? '-' : ' '); 118296465Sdelphij BUF_strlcat(buf, tmp, sizeof buf); 119296465Sdelphij } 120296465Sdelphij } 121296465Sdelphij BUF_strlcat(buf, " ", sizeof buf); 122296465Sdelphij for (j = 0; j < dump_width; j++) { 123296465Sdelphij if (((i * dump_width) + j) >= len) 124296465Sdelphij break; 125296465Sdelphij ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 12655714Skris#ifndef CHARSET_EBCDIC 127296465Sdelphij BIO_snprintf(tmp, sizeof tmp, "%c", 128296465Sdelphij ((ch >= ' ') && (ch <= '~')) ? ch : '.'); 12955714Skris#else 130296465Sdelphij BIO_snprintf(tmp, sizeof tmp, "%c", 131296465Sdelphij ((ch >= os_toascii[' ']) && (ch <= os_toascii['~'])) 132296465Sdelphij ? os_toebcdic[ch] 133296465Sdelphij : '.'); 13455714Skris#endif 135296465Sdelphij BUF_strlcat(buf, tmp, sizeof buf); 136296465Sdelphij } 137296465Sdelphij BUF_strlcat(buf, "\n", sizeof buf); 138296465Sdelphij /* 139296465Sdelphij * if this is the last call then update the ddt_dump thing so that we 140296465Sdelphij * will move the selection point in the debug window 141296465Sdelphij */ 142296465Sdelphij ret += cb((void *)buf, strlen(buf), u); 143296465Sdelphij } 14455714Skris#ifdef TRUNCATE 145296465Sdelphij if (trc > 0) { 146296465Sdelphij BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str, 147296465Sdelphij len + trc); 148296465Sdelphij ret += cb((void *)buf, strlen(buf), u); 149296465Sdelphij } 15055714Skris#endif 151296465Sdelphij return (ret); 152296465Sdelphij} 153160814Ssimon 154160814Ssimon#ifndef OPENSSL_NO_FP_API 155160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp) 156296465Sdelphij{ 157296465Sdelphij return UP_fwrite(data, len, 1, fp); 158296465Sdelphij} 159296465Sdelphij 160160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len) 161296465Sdelphij{ 162296465Sdelphij return BIO_dump_cb(write_fp, fp, s, len); 163296465Sdelphij} 164296465Sdelphij 165160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent) 166296465Sdelphij{ 167296465Sdelphij return BIO_dump_indent_cb(write_fp, fp, s, len, indent); 168296465Sdelphij} 169160814Ssimon#endif 170160814Ssimon 171160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp) 172296465Sdelphij{ 173296465Sdelphij return BIO_write((BIO *)bp, (const char *)data, len); 174296465Sdelphij} 175296465Sdelphij 176160814Ssimonint BIO_dump(BIO *bp, const char *s, int len) 177296465Sdelphij{ 178296465Sdelphij return BIO_dump_cb(write_bio, bp, s, len); 179296465Sdelphij} 180296465Sdelphij 181160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent) 182296465Sdelphij{ 183296465Sdelphij return BIO_dump_indent_cb(write_bio, bp, s, len, indent); 184296465Sdelphij} 185