xref: /freebsd/lib/libutil/hexdump.c (revision e1355b07eede4d5a1abe1462a81fc253e51b83e3)
1e1355b07SJohn Baldwin /*-
2e1355b07SJohn Baldwin  * Copyright (c) 1986, 1988, 1991, 1993
3e1355b07SJohn Baldwin  *	The Regents of the University of California.  All rights reserved.
4e1355b07SJohn Baldwin  * (c) UNIX System Laboratories, Inc.
5e1355b07SJohn Baldwin  * All or some portions of this file are derived from material licensed
6e1355b07SJohn Baldwin  * to the University of California by American Telephone and Telegraph
7e1355b07SJohn Baldwin  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8e1355b07SJohn Baldwin  * the permission of UNIX System Laboratories, Inc.
9e1355b07SJohn Baldwin  *
10e1355b07SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
11e1355b07SJohn Baldwin  * modification, are permitted provided that the following conditions
12e1355b07SJohn Baldwin  * are met:
13e1355b07SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
14e1355b07SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
15e1355b07SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
16e1355b07SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
17e1355b07SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
18e1355b07SJohn Baldwin  * 4. Neither the name of the University nor the names of its contributors
19e1355b07SJohn Baldwin  *    may be used to endorse or promote products derived from this software
20e1355b07SJohn Baldwin  *    without specific prior written permission.
21e1355b07SJohn Baldwin  *
22e1355b07SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23e1355b07SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24e1355b07SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25e1355b07SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26e1355b07SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27e1355b07SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28e1355b07SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29e1355b07SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30e1355b07SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31e1355b07SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e1355b07SJohn Baldwin  * SUCH DAMAGE.
33e1355b07SJohn Baldwin  *
34e1355b07SJohn Baldwin  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
35e1355b07SJohn Baldwin  */
36e1355b07SJohn Baldwin 
37e1355b07SJohn Baldwin #include <sys/cdefs.h>
38e1355b07SJohn Baldwin __FBSDID("$FreeBSD$");
39e1355b07SJohn Baldwin 
40e1355b07SJohn Baldwin #include <sys/types.h>
41e1355b07SJohn Baldwin #include <libutil.h>
42e1355b07SJohn Baldwin #include <stdio.h>
43e1355b07SJohn Baldwin 
44e1355b07SJohn Baldwin void
45e1355b07SJohn Baldwin hexdump(const void *ptr, int length, const char *hdr, int flags)
46e1355b07SJohn Baldwin {
47e1355b07SJohn Baldwin 	int i, j, k;
48e1355b07SJohn Baldwin 	int cols;
49e1355b07SJohn Baldwin 	const unsigned char *cp;
50e1355b07SJohn Baldwin 	char delim;
51e1355b07SJohn Baldwin 
52e1355b07SJohn Baldwin 	if ((flags & HD_DELIM_MASK) != 0)
53e1355b07SJohn Baldwin 		delim = (flags & HD_DELIM_MASK) >> 8;
54e1355b07SJohn Baldwin 	else
55e1355b07SJohn Baldwin 		delim = ' ';
56e1355b07SJohn Baldwin 
57e1355b07SJohn Baldwin 	if ((flags & HD_COLUMN_MASK) != 0)
58e1355b07SJohn Baldwin 		cols = flags & HD_COLUMN_MASK;
59e1355b07SJohn Baldwin 	else
60e1355b07SJohn Baldwin 		cols = 16;
61e1355b07SJohn Baldwin 
62e1355b07SJohn Baldwin 	cp = ptr;
63e1355b07SJohn Baldwin 	for (i = 0; i < length; i+= cols) {
64e1355b07SJohn Baldwin 		if (hdr != NULL)
65e1355b07SJohn Baldwin 			printf("%s", hdr);
66e1355b07SJohn Baldwin 
67e1355b07SJohn Baldwin 		if ((flags & HD_OMIT_COUNT) == 0)
68e1355b07SJohn Baldwin 			printf("%04x  ", i);
69e1355b07SJohn Baldwin 
70e1355b07SJohn Baldwin 		if ((flags & HD_OMIT_HEX) == 0) {
71e1355b07SJohn Baldwin 			for (j = 0; j < cols; j++) {
72e1355b07SJohn Baldwin 				k = i + j;
73e1355b07SJohn Baldwin 				if (k < length)
74e1355b07SJohn Baldwin 					printf("%c%02x", delim, cp[k]);
75e1355b07SJohn Baldwin 				else
76e1355b07SJohn Baldwin 					printf("   ");
77e1355b07SJohn Baldwin 			}
78e1355b07SJohn Baldwin 		}
79e1355b07SJohn Baldwin 
80e1355b07SJohn Baldwin 		if ((flags & HD_OMIT_CHARS) == 0) {
81e1355b07SJohn Baldwin 			printf("  |");
82e1355b07SJohn Baldwin 			for (j = 0; j < cols; j++) {
83e1355b07SJohn Baldwin 				k = i + j;
84e1355b07SJohn Baldwin 				if (k >= length)
85e1355b07SJohn Baldwin 					printf(" ");
86e1355b07SJohn Baldwin 				else if (cp[k] >= ' ' && cp[k] <= '~')
87e1355b07SJohn Baldwin 					printf("%c", cp[k]);
88e1355b07SJohn Baldwin 				else
89e1355b07SJohn Baldwin 					printf(".");
90e1355b07SJohn Baldwin 			}
91e1355b07SJohn Baldwin 			printf("|");
92e1355b07SJohn Baldwin 		}
93e1355b07SJohn Baldwin 		printf("\n");
94e1355b07SJohn Baldwin 	}
95e1355b07SJohn Baldwin }
96e1355b07SJohn Baldwin 
97