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_strlcpy(buf, str, sizeof buf); 108280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width); 109280297Sjkim BUF_strlcat(buf, tmp, sizeof buf); 110280297Sjkim for (j = 0; j < dump_width; j++) { 111280297Sjkim if (((i * dump_width) + j) >= len) { 112280297Sjkim BUF_strlcat(buf, " ", sizeof buf); 113280297Sjkim } else { 114280297Sjkim ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 115280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch, 116280297Sjkim j == 7 ? '-' : ' '); 117280297Sjkim BUF_strlcat(buf, tmp, sizeof buf); 118280297Sjkim } 119280297Sjkim } 120280297Sjkim BUF_strlcat(buf, " ", sizeof buf); 121280297Sjkim for (j = 0; j < dump_width; j++) { 122280297Sjkim if (((i * dump_width) + j) >= len) 123280297Sjkim break; 124280297Sjkim ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff; 12555714Skris#ifndef CHARSET_EBCDIC 126280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%c", 127280297Sjkim ((ch >= ' ') && (ch <= '~')) ? ch : '.'); 12855714Skris#else 129280297Sjkim BIO_snprintf(tmp, sizeof tmp, "%c", 130280297Sjkim ((ch >= os_toascii[' ']) && (ch <= os_toascii['~'])) 131280297Sjkim ? os_toebcdic[ch] 132280297Sjkim : '.'); 13355714Skris#endif 134280297Sjkim BUF_strlcat(buf, tmp, sizeof buf); 135280297Sjkim } 136280297Sjkim BUF_strlcat(buf, "\n", sizeof buf); 137280297Sjkim /* 138280297Sjkim * if this is the last call then update the ddt_dump thing so that we 139280297Sjkim * will move the selection point in the debug window 140280297Sjkim */ 141280297Sjkim ret += cb((void *)buf, strlen(buf), u); 142280297Sjkim } 14355714Skris#ifdef TRUNCATE 144280297Sjkim if (trc > 0) { 145280297Sjkim BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str, 146280297Sjkim len + trc); 147280297Sjkim ret += cb((void *)buf, strlen(buf), u); 148280297Sjkim } 14955714Skris#endif 150280297Sjkim return (ret); 151280297Sjkim} 152160814Ssimon 153160814Ssimon#ifndef OPENSSL_NO_FP_API 154160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp) 155280297Sjkim{ 156280297Sjkim return UP_fwrite(data, len, 1, fp); 157280297Sjkim} 158280297Sjkim 159160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len) 160280297Sjkim{ 161280297Sjkim return BIO_dump_cb(write_fp, fp, s, len); 162280297Sjkim} 163280297Sjkim 164160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent) 165280297Sjkim{ 166280297Sjkim return BIO_dump_indent_cb(write_fp, fp, s, len, indent); 167280297Sjkim} 168160814Ssimon#endif 169160814Ssimon 170160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp) 171280297Sjkim{ 172280297Sjkim return BIO_write((BIO *)bp, (const char *)data, len); 173280297Sjkim} 174280297Sjkim 175160814Ssimonint BIO_dump(BIO *bp, const char *s, int len) 176280297Sjkim{ 177280297Sjkim return BIO_dump_cb(write_bio, bp, s, len); 178280297Sjkim} 179280297Sjkim 180160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent) 181280297Sjkim{ 182280297Sjkim return BIO_dump_indent_cb(write_bio, bp, s, len, indent); 183280297Sjkim} 184290207Sjkim 185290207Sjkimint BIO_hex_string(BIO *out, int indent, int width, unsigned char *data, 186290207Sjkim int datalen) 187290207Sjkim{ 188290207Sjkim int i, j = 0; 189290207Sjkim 190290207Sjkim if (datalen < 1) 191290207Sjkim return 1; 192290207Sjkim 193290207Sjkim for (i = 0; i < datalen - 1; i++) { 194290207Sjkim if (i && !j) 195290207Sjkim BIO_printf(out, "%*s", indent, ""); 196290207Sjkim 197290207Sjkim BIO_printf(out, "%02X:", data[i]); 198290207Sjkim 199290207Sjkim j = (j + 1) % width; 200290207Sjkim if (!j) 201290207Sjkim BIO_printf(out, "\n"); 202290207Sjkim } 203290207Sjkim 204290207Sjkim if (i && !j) 205290207Sjkim BIO_printf(out, "%*s", indent, ""); 206290207Sjkim BIO_printf(out, "%02X", data[datalen - 1]); 207290207Sjkim return 1; 208290207Sjkim} 209