xref: /minix3/crypto/external/bsd/openssl/dist/crypto/bio/b_dump.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/bio/b_dump.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc /*
60ebfedea0SLionel Sambuc  * Stolen from tjh's ssl/ssl_trc.c stuff.
61ebfedea0SLionel Sambuc  */
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc #include <stdio.h>
64ebfedea0SLionel Sambuc #include "cryptlib.h"
65ebfedea0SLionel Sambuc #include "bio_lcl.h"
66ebfedea0SLionel Sambuc 
67ebfedea0SLionel Sambuc #define TRUNCATE
68ebfedea0SLionel Sambuc #define DUMP_WIDTH      16
69ebfedea0SLionel Sambuc #define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH-((i-(i>6?6:i)+3)/4))
70ebfedea0SLionel Sambuc 
BIO_dump_cb(int (* cb)(const void * data,size_t len,void * u),void * u,const char * s,int len)71ebfedea0SLionel Sambuc int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
72ebfedea0SLionel Sambuc                 void *u, const char *s, int len)
73ebfedea0SLionel Sambuc {
74ebfedea0SLionel Sambuc     return BIO_dump_indent_cb(cb, u, s, len, 0);
75ebfedea0SLionel Sambuc }
76ebfedea0SLionel Sambuc 
BIO_dump_indent_cb(int (* cb)(const void * data,size_t len,void * u),void * u,const char * s,int len,int indent)77ebfedea0SLionel Sambuc int BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
78ebfedea0SLionel Sambuc                        void *u, const char *s, int len, int indent)
79ebfedea0SLionel Sambuc {
80ebfedea0SLionel Sambuc     int ret = 0;
81ebfedea0SLionel Sambuc     char buf[288 + 1], tmp[20], str[128 + 1];
82ebfedea0SLionel Sambuc     int i, j, rows, trc;
83ebfedea0SLionel Sambuc     unsigned char ch;
84ebfedea0SLionel Sambuc     int dump_width;
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc     trc = 0;
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc #ifdef TRUNCATE
89ebfedea0SLionel Sambuc     for (; (len > 0) && ((s[len - 1] == ' ') || (s[len - 1] == '\0')); len--)
90ebfedea0SLionel Sambuc         trc++;
91ebfedea0SLionel Sambuc #endif
92ebfedea0SLionel Sambuc 
93ebfedea0SLionel Sambuc     if (indent < 0)
94ebfedea0SLionel Sambuc         indent = 0;
95*0a6a1f1dSLionel Sambuc     if (indent) {
96*0a6a1f1dSLionel Sambuc         if (indent > 128)
97*0a6a1f1dSLionel Sambuc             indent = 128;
98ebfedea0SLionel Sambuc         memset(str, ' ', indent);
99ebfedea0SLionel Sambuc     }
100ebfedea0SLionel Sambuc     str[indent] = '\0';
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc     dump_width = DUMP_WIDTH_LESS_INDENT(indent);
103ebfedea0SLionel Sambuc     rows = (len / dump_width);
104ebfedea0SLionel Sambuc     if ((rows * dump_width) < len)
105ebfedea0SLionel Sambuc         rows++;
106*0a6a1f1dSLionel Sambuc     for (i = 0; i < rows; i++) {
107ebfedea0SLionel Sambuc         buf[0] = '\0';          /* start with empty string */
108ebfedea0SLionel Sambuc         BUF_strlcpy(buf, str, sizeof buf);
109ebfedea0SLionel Sambuc         BIO_snprintf(tmp, sizeof tmp, "%04x - ", i * dump_width);
110ebfedea0SLionel Sambuc         BUF_strlcat(buf, tmp, sizeof buf);
111*0a6a1f1dSLionel Sambuc         for (j = 0; j < dump_width; j++) {
112*0a6a1f1dSLionel Sambuc             if (((i * dump_width) + j) >= len) {
113ebfedea0SLionel Sambuc                 BUF_strlcat(buf, "   ", sizeof buf);
114*0a6a1f1dSLionel Sambuc             } else {
115ebfedea0SLionel Sambuc                 ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
116ebfedea0SLionel Sambuc                 BIO_snprintf(tmp, sizeof tmp, "%02x%c", ch,
117ebfedea0SLionel Sambuc                              j == 7 ? '-' : ' ');
118ebfedea0SLionel Sambuc                 BUF_strlcat(buf, tmp, sizeof buf);
119ebfedea0SLionel Sambuc             }
120ebfedea0SLionel Sambuc         }
121ebfedea0SLionel Sambuc         BUF_strlcat(buf, "  ", sizeof buf);
122*0a6a1f1dSLionel Sambuc         for (j = 0; j < dump_width; j++) {
123ebfedea0SLionel Sambuc             if (((i * dump_width) + j) >= len)
124ebfedea0SLionel Sambuc                 break;
125ebfedea0SLionel Sambuc             ch = ((unsigned char)*(s + i * dump_width + j)) & 0xff;
126ebfedea0SLionel Sambuc #ifndef CHARSET_EBCDIC
127ebfedea0SLionel Sambuc             BIO_snprintf(tmp, sizeof tmp, "%c",
128ebfedea0SLionel Sambuc                          ((ch >= ' ') && (ch <= '~')) ? ch : '.');
129ebfedea0SLionel Sambuc #else
130ebfedea0SLionel Sambuc             BIO_snprintf(tmp, sizeof tmp, "%c",
131ebfedea0SLionel Sambuc                          ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
132ebfedea0SLionel Sambuc                          ? os_toebcdic[ch]
133ebfedea0SLionel Sambuc                          : '.');
134ebfedea0SLionel Sambuc #endif
135ebfedea0SLionel Sambuc             BUF_strlcat(buf, tmp, sizeof buf);
136ebfedea0SLionel Sambuc         }
137ebfedea0SLionel Sambuc         BUF_strlcat(buf, "\n", sizeof buf);
138*0a6a1f1dSLionel Sambuc         /*
139*0a6a1f1dSLionel Sambuc          * if this is the last call then update the ddt_dump thing so that we
140*0a6a1f1dSLionel Sambuc          * will move the selection point in the debug window
141ebfedea0SLionel Sambuc          */
142ebfedea0SLionel Sambuc         ret += cb((void *)buf, strlen(buf), u);
143ebfedea0SLionel Sambuc     }
144ebfedea0SLionel Sambuc #ifdef TRUNCATE
145*0a6a1f1dSLionel Sambuc     if (trc > 0) {
146ebfedea0SLionel Sambuc         BIO_snprintf(buf, sizeof buf, "%s%04x - <SPACES/NULS>\n", str,
147ebfedea0SLionel Sambuc                      len + trc);
148ebfedea0SLionel Sambuc         ret += cb((void *)buf, strlen(buf), u);
149ebfedea0SLionel Sambuc     }
150ebfedea0SLionel Sambuc #endif
151ebfedea0SLionel Sambuc     return (ret);
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_FP_API
write_fp(const void * data,size_t len,void * fp)155ebfedea0SLionel Sambuc static int write_fp(const void *data, size_t len, void *fp)
156ebfedea0SLionel Sambuc {
157ebfedea0SLionel Sambuc     return UP_fwrite(data, len, 1, fp);
158ebfedea0SLionel Sambuc }
159*0a6a1f1dSLionel Sambuc 
BIO_dump_fp(FILE * fp,const char * s,int len)160ebfedea0SLionel Sambuc int BIO_dump_fp(FILE *fp, const char *s, int len)
161ebfedea0SLionel Sambuc {
162ebfedea0SLionel Sambuc     return BIO_dump_cb(write_fp, fp, s, len);
163ebfedea0SLionel Sambuc }
164*0a6a1f1dSLionel Sambuc 
BIO_dump_indent_fp(FILE * fp,const char * s,int len,int indent)165ebfedea0SLionel Sambuc int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent)
166ebfedea0SLionel Sambuc {
167ebfedea0SLionel Sambuc     return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc #endif
170ebfedea0SLionel Sambuc 
write_bio(const void * data,size_t len,void * bp)171ebfedea0SLionel Sambuc static int write_bio(const void *data, size_t len, void *bp)
172ebfedea0SLionel Sambuc {
173ebfedea0SLionel Sambuc     return BIO_write((BIO *)bp, (const char *)data, len);
174ebfedea0SLionel Sambuc }
175*0a6a1f1dSLionel Sambuc 
BIO_dump(BIO * bp,const char * s,int len)176ebfedea0SLionel Sambuc int BIO_dump(BIO *bp, const char *s, int len)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc     return BIO_dump_cb(write_bio, bp, s, len);
179ebfedea0SLionel Sambuc }
180*0a6a1f1dSLionel Sambuc 
BIO_dump_indent(BIO * bp,const char * s,int len,int indent)181ebfedea0SLionel Sambuc int BIO_dump_indent(BIO *bp, const char *s, int len, int indent)
182ebfedea0SLionel Sambuc {
183ebfedea0SLionel Sambuc     return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
184ebfedea0SLionel Sambuc }
185