b_dump.c revision 160814
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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
5955714Skris/*
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
6855714Skris#define DUMP_WIDTH	16
6968651Skris#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
7055714Skris
71160814Ssimonint BIO_dump_cb(int (*cb)(const void *data, size_t len, void *u),
72160814Ssimon	void *u, const char *s, int len)
7368651Skris	{
74160814Ssimon	return BIO_dump_indent_cb(cb, u, s, len, 0);
7568651Skris	}
7655714Skris
77160814Ssimonint BIO_dump_indent_cb(int (*cb)(const void *data, size_t len, void *u),
78160814Ssimon	void *u, const char *s, int len, int indent)
7968651Skris	{
8068651Skris	int ret=0;
8168651Skris	char buf[288+1],tmp[20],str[128+1];
82160814Ssimon	int i,j,rows,trc;
8368651Skris	unsigned char ch;
8468651Skris	int dump_width;
85160814Ssimon
86160814Ssimon	trc=0;
87160814Ssimon
8855714Skris#ifdef TRUNCATE
89160814Ssimon	for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--)
90160814Ssimon		trc++;
9155714Skris#endif
9255714Skris
9368651Skris	if (indent < 0)
9468651Skris		indent = 0;
9568651Skris	if (indent)
9668651Skris		{
9768651Skris		if (indent > 128) indent=128;
9868651Skris		memset(str,' ',indent);
9968651Skris		}
10068651Skris	str[indent]='\0';
101160814Ssimon
10268651Skris	dump_width=DUMP_WIDTH_LESS_INDENT(indent);
10368651Skris	rows=(len/dump_width);
10468651Skris	if ((rows*dump_width)<len)
10568651Skris		rows++;
10668651Skris	for(i=0;i<rows;i++)
10768651Skris		{
10868651Skris		buf[0]='\0';	/* start with empty string */
109127128Snectar		BUF_strlcpy(buf,str,sizeof buf);
110127128Snectar		BIO_snprintf(tmp,sizeof tmp,"%04x - ",i*dump_width);
111127128Snectar		BUF_strlcat(buf,tmp,sizeof buf);
11268651Skris		for(j=0;j<dump_width;j++)
11368651Skris			{
11468651Skris			if (((i*dump_width)+j)>=len)
11568651Skris				{
116127128Snectar				BUF_strlcat(buf,"   ",sizeof buf);
11768651Skris				}
11868651Skris			else
11968651Skris				{
12068651Skris				ch=((unsigned char)*(s+i*dump_width+j)) & 0xff;
121127128Snectar				BIO_snprintf(tmp,sizeof tmp,"%02x%c",ch,
122160814Ssimon					j==7?'-':' ');
123127128Snectar				BUF_strlcat(buf,tmp,sizeof buf);
12468651Skris				}
12568651Skris			}
126127128Snectar		BUF_strlcat(buf,"  ",sizeof buf);
12768651Skris		for(j=0;j<dump_width;j++)
12868651Skris			{
12968651Skris			if (((i*dump_width)+j)>=len)
13068651Skris				break;
13168651Skris			ch=((unsigned char)*(s+i*dump_width+j)) & 0xff;
13255714Skris#ifndef CHARSET_EBCDIC
133127128Snectar			BIO_snprintf(tmp,sizeof tmp,"%c",
134160814Ssimon				((ch>=' ')&&(ch<='~'))?ch:'.');
13555714Skris#else
136127128Snectar			BIO_snprintf(tmp,sizeof tmp,"%c",
137160814Ssimon				((ch>=os_toascii[' '])&&(ch<=os_toascii['~']))
138160814Ssimon				? os_toebcdic[ch]
139160814Ssimon				: '.');
14055714Skris#endif
141127128Snectar			BUF_strlcat(buf,tmp,sizeof buf);
14268651Skris			}
143127128Snectar		BUF_strlcat(buf,"\n",sizeof buf);
144160814Ssimon		/* if this is the last call then update the ddt_dump thing so
145160814Ssimon		 * that we will move the selection point in the debug window
14668651Skris		 */
147160814Ssimon		ret+=cb((void *)buf,strlen(buf),u);
14868651Skris		}
14955714Skris#ifdef TRUNCATE
150160814Ssimon	if (trc > 0)
15168651Skris		{
152127128Snectar		BIO_snprintf(buf,sizeof buf,"%s%04x - <SPACES/NULS>\n",str,
153160814Ssimon			len+trc);
154160814Ssimon		ret+=cb((void *)buf,strlen(buf),u);
15568651Skris		}
15655714Skris#endif
15768651Skris	return(ret);
15868651Skris	}
159160814Ssimon
160160814Ssimon#ifndef OPENSSL_NO_FP_API
161160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp)
162160814Ssimon	{
163160814Ssimon	return UP_fwrite(data, len, 1, fp);
164160814Ssimon	}
165160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len)
166160814Ssimon	{
167160814Ssimon	return BIO_dump_cb(write_fp, fp, s, len);
168160814Ssimon	}
169160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
170160814Ssimon	{
171160814Ssimon	return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
172160814Ssimon	}
173160814Ssimon#endif
174160814Ssimon
175160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp)
176160814Ssimon	{
177160814Ssimon	return BIO_write((BIO *)bp, (const char *)data, len);
178160814Ssimon	}
179160814Ssimonint BIO_dump(BIO *bp, const char *s, int len)
180160814Ssimon	{
181160814Ssimon	return BIO_dump_cb(write_bio, bp, s, len);
182160814Ssimon	}
183160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
184160814Ssimon	{
185160814Ssimon	return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
186160814Ssimon	}
187160814Ssimon
188