b_dump.c revision 280297
1255809Sdes/* crypto/bio/b_dump.c */
2255809Sdes/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3255809Sdes * All rights reserved.
4255809Sdes *
5255809Sdes * This package is an SSL implementation written
6255809Sdes * by Eric Young (eay@cryptsoft.com).
7255825Sdes * The implementation was written so as to conform with Netscapes SSL.
8279499Sngie *
9255809Sdes * This library is free for commercial and non-commercial use as long as
10255809Sdes * the following conditions are aheared to.  The following conditions
11255809Sdes * apply to all code found in this distribution, be it the RC4, RSA,
12255809Sdes * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13255809Sdes * included with this distribution is covered by the same copyright terms
14255809Sdes * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15255809Sdes *
16255809Sdes * Copyright remains Eric Young's, and as such any Copyright notices in
17255809Sdes * the code are not to be removed.
18255809Sdes * If this package is used in a product, Eric Young should be given attribution
19255809Sdes * as the author of the parts of the library used.
20291767Sdes * This can be in the form of a textual message at program startup or
21255809Sdes * in documentation (online or textual) provided with the package.
22255809Sdes *
23255809Sdes * Redistribution and use in source and binary forms, with or without
24255809Sdes * modification, are permitted provided that the following conditions
25255809Sdes * are met:
26255809Sdes * 1. Redistributions of source code must retain the copyright
27291767Sdes *    notice, this list of conditions and the following disclaimer.
28291767Sdes * 2. Redistributions in binary form must reproduce the above copyright
29255809Sdes *    notice, this list of conditions and the following disclaimer in the
30255809Sdes *    documentation and/or other materials provided with the distribution.
31291767Sdes * 3. All advertising materials mentioning features or use of this software
32255809Sdes *    must display the following acknowledgement:
33294786Sdes *    "This product includes cryptographic software written by
34255809Sdes *     Eric Young (eay@cryptsoft.com)"
35255809Sdes *    The word 'cryptographic' can be left out if the rouines from the library
36255809Sdes *    being used are not cryptographic related :-).
37255809Sdes * 4. If you include any Windows specific code (or a derivative thereof) from
38255809Sdes *    the apps directory (application code) you must include an acknowledgement:
39255809Sdes *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40255809Sdes *
41255809Sdes * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42255809Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43255809Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44255809Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45255809Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46255809Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47255809Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48255809Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49255809Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50255809Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51255809Sdes * SUCH DAMAGE.
52255809Sdes *
53255809Sdes * The licence and distribution terms for any publically available version or
54255809Sdes * derivative of this code cannot be changed.  i.e. this code cannot simply be
55255809Sdes * copied and put under another distribution licence
56255809Sdes * [including the GNU Public Licence.]
57255809Sdes */
58255809Sdes
59255809Sdes/*
60255809Sdes * Stolen from tjh's ssl/ssl_trc.c stuff.
61255809Sdes */
62255809Sdes
63255809Sdes#include <stdio.h>
64255809Sdes#include "cryptlib.h"
65255809Sdes#include "bio_lcl.h"
66255809Sdes
67255809Sdes#define TRUNCATE
68255809Sdes#define DUMP_WIDTH      16
69255809Sdes#define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
70255809Sdes
71255809Sdesint BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
72294786Sdes                void *u, const char *s, int len)
73255809Sdes{
74255809Sdes    return BIO_dump_indent_cb(cb, u, s, len, 0);
75255809Sdes}
76255809Sdes
77255809Sdesint BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
78255809Sdes                       void *u, const char *s, int len, int indent)
79255809Sdes{
80255809Sdes    int ret = 0;
81255809Sdes    char buf[288 + 1], tmp[20], str[128 + 1];
82255809Sdes    int i, j, rows, trc;
83255809Sdes    unsigned char ch;
84255809Sdes    int dump_width;
85255809Sdes
86255809Sdes    trc = 0;
87255809Sdes
88255809Sdes#ifdef TRUNCATE
89255809Sdes    for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--)
90255809Sdes        trc++;
91255809Sdes#endif
92255809Sdes
93255809Sdes    if (indent < 0)
94291767Sdes        indent = 0;
95291767Sdes    if (indent) {
96291767Sdes        if (indent > 128)
97291767Sdes            indent = 128;
98291767Sdes        memset(str, ' ', indent);
99291767Sdes    }
100291767Sdes    str[indent] = '\0';
101291767Sdes
102291767Sdes    dump_width = DUMP_WIDTH_LESS_INDENT(indent);
103291767Sdes    rows = (len / dump_width);
104291767Sdes    if ((rows * dump_width) < len)
105291767Sdes        rows++;
106291767Sdes    for (i = 0; i < rows; i++) {
107291767Sdes        buf[0] = '\0';          /* start with empty string */
108291767Sdes        BUF_strlcpy(buf, str, sizeof buf);
109291767Sdes        BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width);
110291767Sdes        BUF_strlcat(buf, tmp, sizeof buf);
111291767Sdes        for (j = 0; j < dump_width; j++) {
112291767Sdes            if (((i * dump_width) + j) >= len) {
113291767Sdes                BUF_strlcat(buf, "   ", sizeof buf);
114255809Sdes            } else {
115255809Sdes                ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
116                BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch,
117                             j == 7 ? '-' : ' ');
118                BUF_strlcat(buf, tmp, sizeof buf);
119            }
120        }
121        BUF_strlcat(buf, "  ", sizeof buf);
122        for (j = 0; j < dump_width; j++) {
123            if (((i * dump_width) + j) >= len)
124                break;
125            ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
126#ifndef CHARSET_EBCDIC
127            BIO_snprintf(tmp, sizeof tmp, "%c",
128                         ((ch >= ' ') && (ch <= '~')) ? ch : '.');
129#else
130            BIO_snprintf(tmp, sizeof tmp, "%c",
131                         ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
132                         ? os_toebcdic[ch]
133                         : '.');
134#endif
135            BUF_strlcat(buf, tmp, sizeof buf);
136        }
137        BUF_strlcat(buf, "\n", sizeof buf);
138        /*
139         * if this is the last call then update the ddt_dump thing so that we
140         * will move the selection point in the debug window
141         */
142        ret += cb((void *)buf, strlen(buf), u);
143    }
144#ifdef TRUNCATE
145    if (trc > 0) {
146        BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str,
147                     len + trc);
148        ret += cb((void *)buf, strlen(buf), u);
149    }
150#endif
151    return (ret);
152}
153
154#ifndef OPENSSL_NO_FP_API
155static int write_fp(const void *data, size_t len, void *fp)
156{
157    return UP_fwrite(data, len, 1, fp);
158}
159
160int BIO_dump_fp(FILE *fp, const char *s, int len)
161{
162    return BIO_dump_cb(write_fp, fp, s, len);
163}
164
165int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
166{
167    return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
168}
169#endif
170
171static int write_bio(const void *data, size_t len, void *bp)
172{
173    return BIO_write((BIO *)bp, (const char *)data, len);
174}
175
176int BIO_dump(BIO *bp, const char *s, int len)
177{
178    return BIO_dump_cb(write_bio, bp, s, len);
179}
180
181int BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
182{
183    return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
184}
185