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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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 59280304Sjkim/* 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 68280304Sjkim#define DUMP_WIDTH 16 6968651Skris#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4)) 7055714Skris 71280304Sjkimint BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u), 72280304Sjkim void *u, const char *s, int len) 73280304Sjkim{ 74280304Sjkim return BIO_dump_indent_cb(cb, u, s, len, 0); 75280304Sjkim} 7655714Skris 77280304Sjkimint BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u), 78280304Sjkim void *u, const char *s, int len, int indent) 79280304Sjkim{ 80280304Sjkim int ret = 0; 81280304Sjkim char buf[288 + 1], tmp[20], str[128 + 1]; 82280304Sjkim int i, j, rows, trc; 83280304Sjkim unsigned char ch; 84280304Sjkim int dump_width; 85160814Ssimon 86280304Sjkim trc = 0; 87160814Ssimon 8855714Skris#ifdef TRUNCATE 89280304Sjkim for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--) 90280304Sjkim trc++; 9155714Skris#endif 9255714Skris 93280304Sjkim if (indent < 0) 94280304Sjkim indent = 0; 95280304Sjkim if (indent) { 96280304Sjkim if (indent > 128) 97280304Sjkim indent = 128; 98280304Sjkim memset(str, ' ', indent); 99280304Sjkim } 100280304Sjkim str[indent] = '\0'; 101160814Ssimon 102280304Sjkim dump_width = DUMP_WIDTH_LESS_INDENT(indent); 103280304Sjkim rows = (len / dump_width); 104280304Sjkim if ((rows * dump_width) < len) 105280304Sjkim rows++; 106280304Sjkim for (i = 0; i < rows; i++) { 107280304Sjkim BUF_strlcpy(buf, str, sizeof buf); 108280304Sjkim BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width); 109280304Sjkim BUF_strlcat(buf, tmp, sizeof buf); 110280304Sjkim for (j = 0; j < dump_width; j++) { 111280304Sjkim if (((i * dump_width) + j) >= len) { 112280304Sjkim BUF_strlcat(buf, " ", sizeof buf); 113280304Sjkim } else { 114280304Sjkim ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 115280304Sjkim BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch, 116280304Sjkim j == 7 ? '-' : ' '); 117280304Sjkim BUF_strlcat(buf, tmp, sizeof buf); 118280304Sjkim } 119280304Sjkim } 120280304Sjkim BUF_strlcat(buf, " ", sizeof buf); 121280304Sjkim for (j = 0; j < dump_width; j++) { 122280304Sjkim if (((i * dump_width) + j) >= len) 123280304Sjkim break; 124280304Sjkim ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 12555714Skris#ifndef CHARSET_EBCDIC 126280304Sjkim BIO_snprintf(tmp, sizeof tmp, "%c", 127280304Sjkim ((ch >= ' ') && (ch <= '~')) ? ch : '.'); 12855714Skris#else 129280304Sjkim BIO_snprintf(tmp, sizeof tmp, "%c", 130280304Sjkim ((ch >= os_toascii[' ']) && (ch <= os_toascii['~'])) 131280304Sjkim ? os_toebcdic[ch] 132280304Sjkim : '.'); 13355714Skris#endif 134280304Sjkim BUF_strlcat(buf, tmp, sizeof buf); 135280304Sjkim } 136280304Sjkim BUF_strlcat(buf, "\n", sizeof buf); 137280304Sjkim /* 138280304Sjkim * if this is the last call then update the ddt_dump thing so that we 139280304Sjkim * will move the selection point in the debug window 140280304Sjkim */ 141280304Sjkim ret += cb((void *)buf, strlen(buf), u); 142280304Sjkim } 14355714Skris#ifdef TRUNCATE 144280304Sjkim if (trc > 0) { 145280304Sjkim BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str, 146280304Sjkim len + trc); 147280304Sjkim ret += cb((void *)buf, strlen(buf), u); 148280304Sjkim } 14955714Skris#endif 150280304Sjkim return (ret); 151280304Sjkim} 152160814Ssimon 153160814Ssimon#ifndef OPENSSL_NO_FP_API 154160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp) 155280304Sjkim{ 156280304Sjkim return UP_fwrite(data, len, 1, fp); 157280304Sjkim} 158280304Sjkim 159160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len) 160280304Sjkim{ 161280304Sjkim return BIO_dump_cb(write_fp, fp, s, len); 162280304Sjkim} 163280304Sjkim 164160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent) 165280304Sjkim{ 166280304Sjkim return BIO_dump_indent_cb(write_fp, fp, s, len, indent); 167280304Sjkim} 168160814Ssimon#endif 169160814Ssimon 170160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp) 171280304Sjkim{ 172280304Sjkim return BIO_write((BIO *)bp, (const char *)data, len); 173280304Sjkim} 174280304Sjkim 175160814Ssimonint BIO_dump(BIO *bp, const char *s, int len) 176280304Sjkim{ 177280304Sjkim return BIO_dump_cb(write_bio, bp, s, len); 178280304Sjkim} 179280304Sjkim 180160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent) 181280304Sjkim{ 182280304Sjkim return BIO_dump_indent_cb(write_bio, bp, s, len, indent); 183280304Sjkim} 184