xref: /freebsd/stand/libsa/hexdump.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
1*ed19b7c5SSimon J. Gerraty /*-
2*ed19b7c5SSimon J. Gerraty  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3*ed19b7c5SSimon J. Gerraty  * All rights reserved.
4*ed19b7c5SSimon J. Gerraty  *
5*ed19b7c5SSimon J. Gerraty  * Redistribution and use in source and binary forms, with or without
6*ed19b7c5SSimon J. Gerraty  * modification, are permitted provided that the following conditions
7*ed19b7c5SSimon J. Gerraty  * are met:
8*ed19b7c5SSimon J. Gerraty  * 1. Redistributions of source code must retain the above copyright
9*ed19b7c5SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer.
10*ed19b7c5SSimon J. Gerraty  * 2. Redistributions in binary form must reproduce the above copyright
11*ed19b7c5SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer in the
12*ed19b7c5SSimon J. Gerraty  *    documentation and/or other materials provided with the distribution.
13*ed19b7c5SSimon J. Gerraty  *
14*ed19b7c5SSimon J. Gerraty  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*ed19b7c5SSimon J. Gerraty  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*ed19b7c5SSimon J. Gerraty  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*ed19b7c5SSimon J. Gerraty  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*ed19b7c5SSimon J. Gerraty  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*ed19b7c5SSimon J. Gerraty  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*ed19b7c5SSimon J. Gerraty  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*ed19b7c5SSimon J. Gerraty  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*ed19b7c5SSimon J. Gerraty  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*ed19b7c5SSimon J. Gerraty  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*ed19b7c5SSimon J. Gerraty  * SUCH DAMAGE.
25*ed19b7c5SSimon J. Gerraty  */
26*ed19b7c5SSimon J. Gerraty 
27*ed19b7c5SSimon J. Gerraty #include <string.h>
28*ed19b7c5SSimon J. Gerraty #include <stand.h>
29*ed19b7c5SSimon J. Gerraty 
30*ed19b7c5SSimon J. Gerraty /*
31*ed19b7c5SSimon J. Gerraty  * Display a region in traditional hexdump format.
32*ed19b7c5SSimon J. Gerraty  */
33*ed19b7c5SSimon J. Gerraty void
hexdump(caddr_t region,size_t len)34*ed19b7c5SSimon J. Gerraty hexdump(caddr_t region, size_t len)
35*ed19b7c5SSimon J. Gerraty {
36*ed19b7c5SSimon J. Gerraty     caddr_t	line;
37*ed19b7c5SSimon J. Gerraty     int		x, c;
38*ed19b7c5SSimon J. Gerraty #ifdef HEXDUMP_PAGER
39*ed19b7c5SSimon J. Gerraty     /* pager causes linking issues for some apps */
40*ed19b7c5SSimon J. Gerraty #define emit(fmt, args...)	{sprintf(lbuf, fmt , ## args); pager_output(lbuf);}
41*ed19b7c5SSimon J. Gerraty     char	lbuf[80];
42*ed19b7c5SSimon J. Gerraty 
43*ed19b7c5SSimon J. Gerraty     pager_open();
44*ed19b7c5SSimon J. Gerraty #else
45*ed19b7c5SSimon J. Gerraty #define emit(fmt, args...) printf(fmt, ## args)
46*ed19b7c5SSimon J. Gerraty #endif
47*ed19b7c5SSimon J. Gerraty 
48*ed19b7c5SSimon J. Gerraty     for (line = region; line < (region + len); line += 16) {
49*ed19b7c5SSimon J. Gerraty 	emit("%08lx  ", (long) line);
50*ed19b7c5SSimon J. Gerraty 
51*ed19b7c5SSimon J. Gerraty 	for (x = 0; x < 16; x++) {
52*ed19b7c5SSimon J. Gerraty 	    if ((line + x) < (region + len)) {
53*ed19b7c5SSimon J. Gerraty 		emit("%02x ", *(uint8_t *)(line + x));
54*ed19b7c5SSimon J. Gerraty 	    } else {
55*ed19b7c5SSimon J. Gerraty 		emit("-- ");
56*ed19b7c5SSimon J. Gerraty 	    }
57*ed19b7c5SSimon J. Gerraty 	    if (x == 7)
58*ed19b7c5SSimon J. Gerraty 		emit(" ");
59*ed19b7c5SSimon J. Gerraty 	}
60*ed19b7c5SSimon J. Gerraty 	emit(" |");
61*ed19b7c5SSimon J. Gerraty 	for (x = 0; x < 16; x++) {
62*ed19b7c5SSimon J. Gerraty 	    if ((line + x) < (region + len)) {
63*ed19b7c5SSimon J. Gerraty 		c = *(uint8_t *)(line + x);
64*ed19b7c5SSimon J. Gerraty 		if ((c < ' ') || (c > '~'))	/* !isprint(c) */
65*ed19b7c5SSimon J. Gerraty 		    c = '.';
66*ed19b7c5SSimon J. Gerraty 		emit("%c", c);
67*ed19b7c5SSimon J. Gerraty 	    } else {
68*ed19b7c5SSimon J. Gerraty 		emit(" ");
69*ed19b7c5SSimon J. Gerraty 	    }
70*ed19b7c5SSimon J. Gerraty 	}
71*ed19b7c5SSimon J. Gerraty 	emit("|\n");
72*ed19b7c5SSimon J. Gerraty     }
73*ed19b7c5SSimon J. Gerraty #ifdef HEXDUMP_PAGER
74*ed19b7c5SSimon J. Gerraty     pager_close();
75*ed19b7c5SSimon J. Gerraty #endif
76*ed19b7c5SSimon J. Gerraty }
77