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
67280297Sjkim#define DUMP_WIDTH      16
6868651Skris#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
6955714Skris
70280297Sjkimint BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
71280297Sjkim                void *u, const char *s, int len)
72280297Sjkim{
73280297Sjkim    return BIO_dump_indent_cb(cb, u, s, len, 0);
74280297Sjkim}
7555714Skris
76280297Sjkimint BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
77280297Sjkim                       void *u, const char *s, int len, int indent)
78280297Sjkim{
79280297Sjkim    int ret = 0;
80280297Sjkim    char buf[288 + 1], tmp[20], str[128 + 1];
81331638Sjkim    int i, j, rows;
82280297Sjkim    unsigned char ch;
83280297Sjkim    int dump_width;
84160814Ssimon
85280297Sjkim    if (indent < 0)
86280297Sjkim        indent = 0;
87280297Sjkim    if (indent) {
88280297Sjkim        if (indent > 128)
89280297Sjkim            indent = 128;
90280297Sjkim        memset(str, ' ', indent);
91280297Sjkim    }
92280297Sjkim    str[indent] = '\0';
93160814Ssimon
94280297Sjkim    dump_width = DUMP_WIDTH_LESS_INDENT(indent);
95280297Sjkim    rows = (len / dump_width);
96280297Sjkim    if ((rows * dump_width) < len)
97280297Sjkim        rows++;
98280297Sjkim    for (i = 0; i < rows; i++) {
99331638Sjkim        BUF_strlcpy(buf, str, sizeof(buf));
100331638Sjkim        BIO_snprintf(tmp, sizeof(tmp), "%04x - ", i * dump_width);
101331638Sjkim        BUF_strlcat(buf, tmp, sizeof(buf));
102280297Sjkim        for (j = 0; j < dump_width; j++) {
103280297Sjkim            if (((i * dump_width) + j) >= len) {
104331638Sjkim                BUF_strlcat(buf, "   ", sizeof(buf));
105280297Sjkim            } else {
106280297Sjkim                ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
107331638Sjkim                BIO_snprintf(tmp, sizeof(tmp), "%02x%c", ch,
108280297Sjkim                             j == 7 ? '-' : ' ');
109331638Sjkim                BUF_strlcat(buf, tmp, sizeof(buf));
110280297Sjkim            }
111280297Sjkim        }
112331638Sjkim        BUF_strlcat(buf, "  ", sizeof(buf));
113280297Sjkim        for (j = 0; j < dump_width; j++) {
114280297Sjkim            if (((i * dump_width) + j) >= len)
115280297Sjkim                break;
116280297Sjkim            ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
11755714Skris#ifndef CHARSET_EBCDIC
118331638Sjkim            BIO_snprintf(tmp, sizeof(tmp), "%c",
119280297Sjkim                         ((ch >= ' ') && (ch <= '~')) ? ch : '.');
12055714Skris#else
121331638Sjkim            BIO_snprintf(tmp, sizeof(tmp), "%c",
122280297Sjkim                         ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
123280297Sjkim                         ? os_toebcdic[ch]
124280297Sjkim                         : '.');
12555714Skris#endif
126331638Sjkim            BUF_strlcat(buf, tmp, sizeof(buf));
127280297Sjkim        }
128331638Sjkim        BUF_strlcat(buf, "\n", sizeof(buf));
129280297Sjkim        /*
130280297Sjkim         * if this is the last call then update the ddt_dump thing so that we
131280297Sjkim         * will move the selection point in the debug window
132280297Sjkim         */
133280297Sjkim        ret += cb((void *)buf, strlen(buf), u);
134280297Sjkim    }
135331638Sjkim    return ret;
136280297Sjkim}
137160814Ssimon
138160814Ssimon#ifndef OPENSSL_NO_FP_API
139160814Ssimonstatic int write_fp(const void *data, size_t len, void *fp)
140280297Sjkim{
141280297Sjkim    return UP_fwrite(data, len, 1, fp);
142280297Sjkim}
143280297Sjkim
144160814Ssimonint BIO_dump_fp(FILE *fp, const char *s, int len)
145280297Sjkim{
146280297Sjkim    return BIO_dump_cb(write_fp, fp, s, len);
147280297Sjkim}
148280297Sjkim
149160814Ssimonint BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
150280297Sjkim{
151280297Sjkim    return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
152280297Sjkim}
153160814Ssimon#endif
154160814Ssimon
155160814Ssimonstatic int write_bio(const void *data, size_t len, void *bp)
156280297Sjkim{
157280297Sjkim    return BIO_write((BIO *)bp, (const char *)data, len);
158280297Sjkim}
159280297Sjkim
160160814Ssimonint BIO_dump(BIO *bp, const char *s, int len)
161280297Sjkim{
162280297Sjkim    return BIO_dump_cb(write_bio, bp, s, len);
163280297Sjkim}
164280297Sjkim
165160814Ssimonint BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
166280297Sjkim{
167280297Sjkim    return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
168280297Sjkim}
169290207Sjkim
170290207Sjkimint BIO_hex_string(BIO *out, int indent, int width, unsigned char *data,
171290207Sjkim                   int datalen)
172290207Sjkim{
173290207Sjkim    int i, j = 0;
174290207Sjkim
175290207Sjkim    if (datalen < 1)
176290207Sjkim        return 1;
177290207Sjkim
178290207Sjkim    for (i = 0; i < datalen - 1; i++) {
179290207Sjkim        if (i && !j)
180290207Sjkim            BIO_printf(out, "%*s", indent, "");
181290207Sjkim
182290207Sjkim        BIO_printf(out, "%02X:", data[i]);
183290207Sjkim
184290207Sjkim        j = (j + 1) % width;
185290207Sjkim        if (!j)
186290207Sjkim            BIO_printf(out, "\n");
187290207Sjkim    }
188290207Sjkim
189290207Sjkim    if (i && !j)
190290207Sjkim        BIO_printf(out, "%*s", indent, "");
191290207Sjkim    BIO_printf(out, "%02X", data[datalen - 1]);
192290207Sjkim    return 1;
193290207Sjkim}
194