b_dump.c revision 290207
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. 8280297Sjkim * 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). 15280297Sjkim * 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. 22280297Sjkim * 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 :-). 37280297Sjkim * 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)" 40280297Sjkim * 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. 52280297Sjkim * 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 59280297Sjkim/* 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 68280297Sjkim#define DUMP_WIDTH 16 6968651Skris#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4)) 7055714Skris 71280297Sjkimint BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u), 72280297Sjkim void *u, const char *s, int len) 73280297Sjkim{ 74280297Sjkim return BIO_dump_indent_cb(cb, u, s, len, 0); 75280297Sjkim} 7655714Skris 77280297Sjkimint BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u), 78280297Sjkim void *u, const char *s, int len, int indent) 79280297Sjkim{ 80280297Sjkim int ret = 0; 81280297Sjkim char buf[288 + 1], tmp[20], str[128 + 1]; 82280297Sjkim int i, j, rows, trc; 83280297Sjkim unsigned char ch; 84280297Sjkim int dump_width; 85160814Ssimon 86280297Sjkim trc = 0; 87160814Ssimon 8855714Skris#ifdef TRUNCATE 89280297Sjkim for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--) 90280297Sjkim trc++; 9155714Skris#endif 9255714Skris 93280297Sjkim if (indent < 0) 94280297Sjkim indent = 0; 95280297Sjkim if (indent) { 96280297Sjkim if (indent > 128) 97280297Sjkim indent = 128; 98280297Sjkim memset(str, ' ', indent); 99280297Sjkim } 100280297Sjkim str[indent] = '\0'; 101160814Ssimon 102280297Sjkim dump_width = DUMP_WIDTH_LESS_INDENT(indent); 103280297Sjkim rows = (len / dump_width); 104280297Sjkim if ((rows * dump_width) < len) 105280297Sjkim rows++; 106280297Sjkim for (i = 0; i < rows; i++) { 107280297Sjkim buf[0] = '\0'; /* start with empty string */ 108280297Sjkim BUF_strlcpy(buf, str, sizeof buf); 109280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width); 110280297Sjkim BUF_strlcat(buf, tmp, sizeof buf); 111280297Sjkim for (j = 0; j < dump_width; j++) { 112280297Sjkim if (((i * dump_width) + j) >= len) { 113280297Sjkim BUF_strlcat(buf, " ", sizeof buf); 114280297Sjkim } else { 115280297Sjkim ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 116280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch, 117280297Sjkim j == 7 ? '-' : ' '); 118280297Sjkim BUF_strlcat(buf, tmp, sizeof buf); 119280297Sjkim } 120280297Sjkim } 121280297Sjkim BUF_strlcat(buf, " ", sizeof buf); 122280297Sjkim for (j = 0; j < dump_width; j++) { 123280297Sjkim if (((i * dump_width) + j) >= len) 124280297Sjkim break; 125280297Sjkim ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 12655714Skris#ifndef CHARSET_EBCDIC 127280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%c", 128280297Sjkim ((ch >= ' ') && (ch <= '~')) ? ch : '.'); 12955714Skris#else 130280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%c", 131280297Sjkim ((ch >= os_toascii[' ']) && (ch <= os_toascii['~'])) 132280297Sjkim ? os_toebcdic[ch] 133280297Sjkim : '.'); 13455714Skris#endif 135280297Sjkim BUF_strlcat(buf, tmp, sizeof buf); 136280297Sjkim } 137280297Sjkim BUF_strlcat(buf, "\n", sizeof buf); 138280297Sjkim /* 139280297Sjkim * if this is the last call then update the ddt_dump thing so that we 140280297Sjkim * will move the selection point in the debug window 141280297Sjkim */ 142280297Sjkim ret += cb((void *)buf, strlen(buf), u); 143280297Sjkim } 14455714Skris#ifdef TRUNCATE 145280297Sjkim if (trc > 0) { 146280297Sjkim BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str, 147280297Sjkim len + trc); 148280297Sjkim ret += cb((void *)buf, strlen(buf), u); 149280297Sjkim } 15055714Skris#endif 151280297Sjkim return (ret); 152280297Sjkim} 153160814Ssimon 154160814Ssimon#ifndef OPENSSL_NO_FP_API 155160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp) 156280297Sjkim{ 157280297Sjkim return UP_fwrite(data, len, 1, fp); 158280297Sjkim} 159280297Sjkim 160160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len) 161280297Sjkim{ 162280297Sjkim return BIO_dump_cb(write_fp, fp, s, len); 163280297Sjkim} 164280297Sjkim 165160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent) 166280297Sjkim{ 167280297Sjkim return BIO_dump_indent_cb(write_fp, fp, s, len, indent); 168280297Sjkim} 169160814Ssimon#endif 170160814Ssimon 171160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp) 172280297Sjkim{ 173280297Sjkim return BIO_write((BIO *)bp, (const char *)data, len); 174280297Sjkim} 175280297Sjkim 176160814Ssimonint BIO_dump(BIO *bp, const char *s, int len) 177280297Sjkim{ 178280297Sjkim return BIO_dump_cb(write_bio, bp, s, len); 179280297Sjkim} 180280297Sjkim 181160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent) 182280297Sjkim{ 183280297Sjkim return BIO_dump_indent_cb(write_bio, bp, s, len, indent); 184280297Sjkim} 185290207Sjkim 186290207Sjkimint BIO_hex_string(BIO *out, int indent, int width, unsigned char *data, 187290207Sjkim int datalen) 188290207Sjkim{ 189290207Sjkim int i, j = 0; 190290207Sjkim 191290207Sjkim if (datalen < 1) 192290207Sjkim return 1; 193290207Sjkim 194290207Sjkim for (i = 0; i < datalen - 1; i++) { 195290207Sjkim if (i && !j) 196290207Sjkim BIO_printf(out, "%*s", indent, ""); 197290207Sjkim 198290207Sjkim BIO_printf(out, "%02X:", data[i]); 199290207Sjkim 200290207Sjkim j = (j + 1) % width; 201290207Sjkim if (!j) 202290207Sjkim BIO_printf(out, "\n"); 203290207Sjkim } 204290207Sjkim 205290207Sjkim if (i && !j) 206290207Sjkim BIO_printf(out, "%*s", indent, ""); 207290207Sjkim BIO_printf(out, "%02X", data[datalen - 1]); 208290207Sjkim return 1; 209290207Sjkim} 210