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.
8296341Sdelphij *
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).
15296341Sdelphij *
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.
22296341Sdelphij *
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 :-).
37296341Sdelphij * 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)"
40296341Sdelphij *
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.
52296341Sdelphij *
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
59296341Sdelphij/*
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
68296341Sdelphij#define DUMP_WIDTH      16
6968651Skris#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
7055714Skris
71296341Sdelphijint BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
72296341Sdelphij                void *u, const char *s, int len)
73296341Sdelphij{
74296341Sdelphij    return BIO_dump_indent_cb(cb, u, s, len, 0);
75296341Sdelphij}
7655714Skris
77296341Sdelphijint BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
78296341Sdelphij                       void *u, const char *s, int len, int indent)
79296341Sdelphij{
80296341Sdelphij    int ret = 0;
81296341Sdelphij    char buf[288 + 1], tmp[20], str[128 + 1];
82296341Sdelphij    int i, j, rows, trc;
83296341Sdelphij    unsigned char ch;
84296341Sdelphij    int dump_width;
85160814Ssimon
86296341Sdelphij    trc = 0;
87160814Ssimon
8855714Skris#ifdef TRUNCATE
89296341Sdelphij    for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--)
90296341Sdelphij        trc++;
9155714Skris#endif
9255714Skris
93296341Sdelphij    if (indent < 0)
94296341Sdelphij        indent = 0;
95296341Sdelphij    if (indent) {
96296341Sdelphij        if (indent > 128)
97296341Sdelphij            indent = 128;
98296341Sdelphij        memset(str, ' ', indent);
99296341Sdelphij    }
100296341Sdelphij    str[indent] = '\0';
101160814Ssimon
102296341Sdelphij    dump_width = DUMP_WIDTH_LESS_INDENT(indent);
103296341Sdelphij    rows = (len / dump_width);
104296341Sdelphij    if ((rows * dump_width) < len)
105296341Sdelphij        rows++;
106296341Sdelphij    for (i = 0; i < rows; i++) {
107296341Sdelphij        buf[0] = '\0';          /* start with empty string */
108296341Sdelphij        BUF_strlcpy(buf, str, sizeof buf);
109296341Sdelphij        BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width);
110296341Sdelphij        BUF_strlcat(buf, tmp, sizeof buf);
111296341Sdelphij        for (j = 0; j < dump_width; j++) {
112296341Sdelphij            if (((i * dump_width) + j) >= len) {
113296341Sdelphij                BUF_strlcat(buf, "   ", sizeof buf);
114296341Sdelphij            } else {
115296341Sdelphij                ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
116296341Sdelphij                BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch,
117296341Sdelphij                             j == 7 ? '-' : ' ');
118296341Sdelphij                BUF_strlcat(buf, tmp, sizeof buf);
119296341Sdelphij            }
120296341Sdelphij        }
121296341Sdelphij        BUF_strlcat(buf, "  ", sizeof buf);
122296341Sdelphij        for (j = 0; j < dump_width; j++) {
123296341Sdelphij            if (((i * dump_width) + j) >= len)
124296341Sdelphij                break;
125296341Sdelphij            ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
12655714Skris#ifndef CHARSET_EBCDIC
127296341Sdelphij            BIO_snprintf(tmp, sizeof tmp, "%c",
128296341Sdelphij                         ((ch >= ' ') && (ch <= '~')) ? ch : '.');
12955714Skris#else
130296341Sdelphij            BIO_snprintf(tmp, sizeof tmp, "%c",
131296341Sdelphij                         ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
132296341Sdelphij                         ? os_toebcdic[ch]
133296341Sdelphij                         : '.');
13455714Skris#endif
135296341Sdelphij            BUF_strlcat(buf, tmp, sizeof buf);
136296341Sdelphij        }
137296341Sdelphij        BUF_strlcat(buf, "\n", sizeof buf);
138296341Sdelphij        /*
139296341Sdelphij         * if this is the last call then update the ddt_dump thing so that we
140296341Sdelphij         * will move the selection point in the debug window
141296341Sdelphij         */
142296341Sdelphij        ret += cb((void *)buf, strlen(buf), u);
143296341Sdelphij    }
14455714Skris#ifdef TRUNCATE
145296341Sdelphij    if (trc > 0) {
146296341Sdelphij        BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str,
147296341Sdelphij                     len + trc);
148296341Sdelphij        ret += cb((void *)buf, strlen(buf), u);
149296341Sdelphij    }
15055714Skris#endif
151296341Sdelphij    return (ret);
152296341Sdelphij}
153160814Ssimon
154160814Ssimon#ifndef OPENSSL_NO_FP_API
155160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp)
156296341Sdelphij{
157296341Sdelphij    return UP_fwrite(data, len, 1, fp);
158296341Sdelphij}
159296341Sdelphij
160160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len)
161296341Sdelphij{
162296341Sdelphij    return BIO_dump_cb(write_fp, fp, s, len);
163296341Sdelphij}
164296341Sdelphij
165160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
166296341Sdelphij{
167296341Sdelphij    return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
168296341Sdelphij}
169160814Ssimon#endif
170160814Ssimon
171160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp)
172296341Sdelphij{
173296341Sdelphij    return BIO_write((BIO *)bp, (const char *)data, len);
174296341Sdelphij}
175296341Sdelphij
176160814Ssimonint BIO_dump(BIO *bp, const char *s, int len)
177296341Sdelphij{
178296341Sdelphij    return BIO_dump_cb(write_bio, bp, s, len);
179296341Sdelphij}
180296341Sdelphij
181160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
182296341Sdelphij{
183296341Sdelphij    return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
184296341Sdelphij}
185