xref: /minix3/crypto/external/bsd/netpgp/dist/src/libverify/dump.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
7*ebfedea0SLionel Sambuc  * are met:
8*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
9*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
10*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
11*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
12*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
13*ebfedea0SLionel Sambuc  *
14*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*ebfedea0SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*ebfedea0SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*ebfedea0SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*ebfedea0SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*ebfedea0SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*ebfedea0SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*ebfedea0SLionel Sambuc  */
25*ebfedea0SLionel Sambuc #include <inttypes.h>
26*ebfedea0SLionel Sambuc #include <stdio.h>
27*ebfedea0SLionel Sambuc #include <stdlib.h>
28*ebfedea0SLionel Sambuc #include <string.h>
29*ebfedea0SLionel Sambuc #include <unistd.h>
30*ebfedea0SLionel Sambuc 
31*ebfedea0SLionel Sambuc #ifndef isprint
32*ebfedea0SLionel Sambuc #define isprint(x)	((x) >= ' ' && (x) <= '~')
33*ebfedea0SLionel Sambuc #endif
34*ebfedea0SLionel Sambuc 
35*ebfedea0SLionel Sambuc #define HEXDUMP_LINELEN	16
36*ebfedea0SLionel Sambuc 
37*ebfedea0SLionel Sambuc #ifndef PRIsize
38*ebfedea0SLionel Sambuc #define PRIsize	"z"
39*ebfedea0SLionel Sambuc #endif
40*ebfedea0SLionel Sambuc 
41*ebfedea0SLionel Sambuc /* show hexadecimal/ascii dump */
42*ebfedea0SLionel Sambuc static ssize_t
hexdump(const char * in,const size_t len,void * outvp,size_t size)43*ebfedea0SLionel Sambuc hexdump(const char *in, const size_t len, void *outvp, size_t size)
44*ebfedea0SLionel Sambuc {
45*ebfedea0SLionel Sambuc 	size_t	 i;
46*ebfedea0SLionel Sambuc 	char	 line[HEXDUMP_LINELEN + 1];
47*ebfedea0SLionel Sambuc 	char	*out = (char *)outvp;
48*ebfedea0SLionel Sambuc 	int	 o;
49*ebfedea0SLionel Sambuc 
50*ebfedea0SLionel Sambuc 	for (i = 0, o = 0 ; i < len ; i++) {
51*ebfedea0SLionel Sambuc 		if (i % HEXDUMP_LINELEN == 0) {
52*ebfedea0SLionel Sambuc 			o += snprintf(&out[o], size - o,
53*ebfedea0SLionel Sambuc 					"%.5" PRIsize "u |  ", i);
54*ebfedea0SLionel Sambuc 		}
55*ebfedea0SLionel Sambuc 		o += snprintf(&out[o], size - o, "%.02x ", (uint8_t)in[i]);
56*ebfedea0SLionel Sambuc 		line[i % HEXDUMP_LINELEN] =
57*ebfedea0SLionel Sambuc 			(isprint((uint8_t)in[i])) ? in[i] : '.';
58*ebfedea0SLionel Sambuc 		if (i % HEXDUMP_LINELEN == HEXDUMP_LINELEN - 1) {
59*ebfedea0SLionel Sambuc 			line[HEXDUMP_LINELEN] = 0x0;
60*ebfedea0SLionel Sambuc 			o += snprintf(&out[o], size - o, " | %s\n", line);
61*ebfedea0SLionel Sambuc 		}
62*ebfedea0SLionel Sambuc 	}
63*ebfedea0SLionel Sambuc 	if (i % HEXDUMP_LINELEN != 0) {
64*ebfedea0SLionel Sambuc 		for ( ; i % HEXDUMP_LINELEN != 0 ; i++) {
65*ebfedea0SLionel Sambuc 			o += snprintf(&out[o], size - o, "   ");
66*ebfedea0SLionel Sambuc 			line[i % HEXDUMP_LINELEN] = ' ';
67*ebfedea0SLionel Sambuc 		}
68*ebfedea0SLionel Sambuc 		line[HEXDUMP_LINELEN] = 0x0;
69*ebfedea0SLionel Sambuc 		o += snprintf(&out[o], size - o, " | %s\n", line);
70*ebfedea0SLionel Sambuc 	}
71*ebfedea0SLionel Sambuc 	return (ssize_t)o;
72*ebfedea0SLionel Sambuc }
73*ebfedea0SLionel Sambuc 
74*ebfedea0SLionel Sambuc void dumpmem(void */*p*/, size_t /*size*/);
75*ebfedea0SLionel Sambuc 
76*ebfedea0SLionel Sambuc /* just dump an area of memory to stdout */
77*ebfedea0SLionel Sambuc void
dumpmem(void * vp,size_t size)78*ebfedea0SLionel Sambuc dumpmem(void *vp, size_t size)
79*ebfedea0SLionel Sambuc {
80*ebfedea0SLionel Sambuc 	ssize_t	cc;
81*ebfedea0SLionel Sambuc 	uint8_t	*p = (uint8_t *)vp;
82*ebfedea0SLionel Sambuc 	char	*buf;
83*ebfedea0SLionel Sambuc 
84*ebfedea0SLionel Sambuc 	buf = calloc(1, size * 5);
85*ebfedea0SLionel Sambuc 	cc = hexdump((const char *)p, size, buf, size * 5);
86*ebfedea0SLionel Sambuc 	fprintf(stdout, "%.*s\n", (int)cc, buf);
87*ebfedea0SLionel Sambuc 	free(buf);
88*ebfedea0SLionel Sambuc }
89