117683Spst/* crypto/bio/b_dump.c */
217683Spst/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
317683Spst * All rights reserved.
417683Spst *
517683Spst * This package is an SSL implementation written
617683Spst * by Eric Young (eay@cryptsoft.com).
717683Spst * The implementation was written so as to conform with Netscapes SSL.
817683Spst *
917683Spst * This library is free for commercial and non-commercial use as long as
1017683Spst * the following conditions are aheared to.  The following conditions
1117683Spst * apply to all code found in this distribution, be it the RC4, RSA,
1217683Spst * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1317683Spst * included with this distribution is covered by the same copyright terms
1417683Spst * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1517683Spst *
1617683Spst * Copyright remains Eric Young's, and as such any Copyright notices in
1717683Spst * the code are not to be removed.
1817683Spst * If this package is used in a product, Eric Young should be given attribution
1917683Spst * as the author of the parts of the library used.
20190225Srpaulo * This can be in the form of a textual message at program startup or
2117683Spst * in documentation (online or textual) provided with the package.
2217683Spst *
2317683Spst * Redistribution and use in source and binary forms, with or without
2417683Spst * modification, are permitted provided that the following conditions
2517683Spst * are met:
2617683Spst * 1. Redistributions of source code must retain the copyright
2717683Spst *    notice, this list of conditions and the following disclaimer.
2817683Spst * 2. Redistributions in binary form must reproduce the above copyright
29190225Srpaulo *    notice, this list of conditions and the following disclaimer in the
30190225Srpaulo *    documentation and/or other materials provided with the distribution.
31190225Srpaulo * 3. All advertising materials mentioning features or use of this software
3217683Spst *    must display the following acknowledgement:
3375107Sfenner *    "This product includes cryptographic software written by
3417683Spst *     Eric Young (eay@cryptsoft.com)"
3575107Sfenner *    The word 'cryptographic' can be left out if the rouines from the library
36190225Srpaulo *    being used are not cryptographic related :-).
3775107Sfenner * 4. If you include any Windows specific code (or a derivative thereof) from
3817683Spst *    the apps directory (application code) you must include an acknowledgement:
3926175Sfenner *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4026175Sfenner *
4126175Sfenner * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4226175Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4317683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4417683Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4517683Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4617683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4717683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4817683Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4917683Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50127664Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51127664Sbms * SUCH DAMAGE.
52172677Smlaier *
53190225Srpaulo * The licence and distribution terms for any publically available version or
54146768Ssam * derivative of this code cannot be changed.  i.e. this code cannot simply be
55172677Smlaier * copied and put under another distribution licence
5617683Spst * [including the GNU Public Licence.]
5717683Spst */
5817683Spst
5917683Spst/*
6017683Spst * Stolen from tjh's ssl/ssl_trc.c stuff.
6175107Sfenner */
6275107Sfenner
6375107Sfenner#include <stdio.h>
6417683Spst#include "cryptlib.h"
6517683Spst#include "bio_lcl.h"
6617683Spst
6717683Spst#define TRUNCATE
6817683Spst#define DUMP_WIDTH	16
6917683Spst#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
7017683Spst
7117683Spstint BIO_dump_cb(int (*cb)(const void *data, size_t len, void *u),
7217683Spst	void *u, const char *s, int len)
7317683Spst	{
7417683Spst	return BIO_dump_indent_cb(cb, u, s, len, 0);
7517683Spst	}
7617683Spst
7717683Spstint BIO_dump_indent_cb(int (*cb)(const void *data, size_t len, void *u),
7826175Sfenner	void *u, const char *s, int len, int indent)
7917683Spst	{
80190225Srpaulo	int ret=0;
81127664Sbms	char buf[288+1],tmp[20],str[128+1];
82127664Sbms	int i,j,rows,trc;
8317683Spst	unsigned char ch;
8475107Sfenner	int dump_width;
8539291Sfenner
8675107Sfenner	trc=0;
8717683Spst
88127664Sbms#ifdef TRUNCATE
8917683Spst	for(; (len > 0) && ((s[len-1] == ' ') || (s[len-1] == '\0')); len--)
9017683Spst		trc++;
9117683Spst#endif
92127664Sbms
93190225Srpaulo	if (indent < 0)
94190225Srpaulo		indent = 0;
95190225Srpaulo	if (indent)
96190225Srpaulo		{
97190225Srpaulo		if (indent > 128) indent=128;
98190225Srpaulo		memset(str,' ',indent);
99190225Srpaulo		}
100190225Srpaulo	str[indent]='\0';
101190225Srpaulo
102190225Srpaulo	dump_width=DUMP_WIDTH_LESS_INDENT(indent);
103190225Srpaulo	rows=(len/dump_width);
104190225Srpaulo	if ((rows*dump_width)<len)
105190225Srpaulo		rows++;
106190225Srpaulo	for(i=0;i<rows;i++)
107190225Srpaulo		{
108190225Srpaulo		buf[0]='\0';	/* start with empty string */
109190225Srpaulo		BUF_strlcpy(buf,str,sizeof buf);
110190225Srpaulo		BIO_snprintf(tmp,sizeof tmp,"%04x - ",i*dump_width);
111190225Srpaulo		BUF_strlcat(buf,tmp,sizeof buf);
112190225Srpaulo		for(j=0;j<dump_width;j++)
113190225Srpaulo			{
114190225Srpaulo			if (((i*dump_width)+j)>=len)
115190225Srpaulo				{
11617683Spst				BUF_strlcat(buf,"   ",sizeof buf);
117190225Srpaulo				}
11817683Spst			else
11917683Spst				{
120127664Sbms				ch=((unsigned char)*(s+i*dump_width+j)) & 0xff;
12117683Spst				BIO_snprintf(tmp,sizeof tmp,"%02x%c",ch,
12217683Spst					j==7?'-':' ');
12317683Spst				BUF_strlcat(buf,tmp,sizeof buf);
12417683Spst				}
12517683Spst			}
12617683Spst		BUF_strlcat(buf,"  ",sizeof buf);
127190225Srpaulo		for(j=0;j<dump_width;j++)
12839291Sfenner			{
129190225Srpaulo			if (((i*dump_width)+j)>=len)
130190225Srpaulo				break;
131190225Srpaulo			ch=((unsigned char)*(s+i*dump_width+j)) & 0xff;
132190225Srpaulo#ifndef CHARSET_EBCDIC
133190225Srpaulo			BIO_snprintf(tmp,sizeof tmp,"%c",
134190225Srpaulo				((ch>=' ')&&(ch<='~'))?ch:'.');
135190225Srpaulo#else
136190225Srpaulo			BIO_snprintf(tmp,sizeof tmp,"%c",
137190225Srpaulo				((ch>=os_toascii[' '])&&(ch<=os_toascii['~']))
138190225Srpaulo				? os_toebcdic[ch]
139190225Srpaulo				: '.');
140190225Srpaulo#endif
141190225Srpaulo			BUF_strlcat(buf,tmp,sizeof buf);
142190225Srpaulo			}
143190225Srpaulo		BUF_strlcat(buf,"\n",sizeof buf);
144190225Srpaulo		/* if this is the last call then update the ddt_dump thing so
145190225Srpaulo		 * that we will move the selection point in the debug window
146190225Srpaulo		 */
147190225Srpaulo		ret+=cb((void *)buf,strlen(buf),u);
148190225Srpaulo		}
149190225Srpaulo#ifdef TRUNCATE
150190225Srpaulo	if (trc > 0)
151190225Srpaulo		{
152190225Srpaulo		BIO_snprintf(buf,sizeof buf,"%s%04x - <SPACES/NULS>\n",str,
153190225Srpaulo			len+trc);
154190225Srpaulo		ret+=cb((void *)buf,strlen(buf),u);
155190225Srpaulo		}
156190225Srpaulo#endif
157190225Srpaulo	return(ret);
158190225Srpaulo	}
159190225Srpaulo
160190225Srpaulo#ifndef OPENSSL_NO_FP_API
161190225Srpaulostatic int write_fp(const void *data, size_t len, void *fp)
162190225Srpaulo	{
163190225Srpaulo	return UP_fwrite(data, len, 1, fp);
164190225Srpaulo	}
165190225Srpauloint BIO_dump_fp(FILE *fp, const char *s, int len)
166190225Srpaulo	{
167190225Srpaulo	return BIO_dump_cb(write_fp, fp, s, len);
168190225Srpaulo	}
169190225Srpauloint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
170190225Srpaulo	{
171190225Srpaulo	return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
172190225Srpaulo	}
173190225Srpaulo#endif
174190225Srpaulo
175190225Srpaulostatic int write_bio(const void *data, size_t len, void *bp)
176190225Srpaulo	{
177190225Srpaulo	return BIO_write((BIO *)bp, (const char *)data, len);
178190225Srpaulo	}
179190225Srpauloint BIO_dump(BIO *bp, const char *s, int len)
180190225Srpaulo	{
181190225Srpaulo	return BIO_dump_cb(write_bio, bp, s, len);
182190225Srpaulo	}
183190225Srpauloint BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
184190225Srpaulo	{
185190225Srpaulo	return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
186190225Srpaulo	}
187190225Srpaulo
188190225Srpaulo