xref: /dflybsd-src/lib/libutil/hexdump.c (revision d316f7c95d4b8b07a5557eb0a39cfa39b7114297)
16d29a580SJohn Marino /*-
26d29a580SJohn Marino  * Copyright (c) 1986, 1988, 1991, 1993
36d29a580SJohn Marino  *	The Regents of the University of California.  All rights reserved.
46d29a580SJohn Marino  * (c) UNIX System Laboratories, Inc.
56d29a580SJohn Marino  * All or some portions of this file are derived from material licensed
66d29a580SJohn Marino  * to the University of California by American Telephone and Telegraph
76d29a580SJohn Marino  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
86d29a580SJohn Marino  * the permission of UNIX System Laboratories, Inc.
96d29a580SJohn Marino  *
106d29a580SJohn Marino  * Redistribution and use in source and binary forms, with or without
116d29a580SJohn Marino  * modification, are permitted provided that the following conditions
126d29a580SJohn Marino  * are met:
136d29a580SJohn Marino  * 1. Redistributions of source code must retain the above copyright
146d29a580SJohn Marino  *    notice, this list of conditions and the following disclaimer.
156d29a580SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
166d29a580SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
176d29a580SJohn Marino  *    documentation and/or other materials provided with the distribution.
18dc71b7abSJustin C. Sherrill  * 3. Neither the name of the University nor the names of its contributors
196d29a580SJohn Marino  *    may be used to endorse or promote products derived from this software
206d29a580SJohn Marino  *    without specific prior written permission.
216d29a580SJohn Marino  *
226d29a580SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
236d29a580SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
246d29a580SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
256d29a580SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
266d29a580SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
276d29a580SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
286d29a580SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296d29a580SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
306d29a580SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
316d29a580SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326d29a580SJohn Marino  * SUCH DAMAGE.
336d29a580SJohn Marino  *
346d29a580SJohn Marino  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
35*d316f7c9SJohn Marino  * $FreeBSD: head/lib/libutil/hexdump.c 180161 2008-07-01 22:30:57Z jhb $
366d29a580SJohn Marino  */
376d29a580SJohn Marino 
386d29a580SJohn Marino #include <sys/types.h>
396d29a580SJohn Marino #include <libutil.h>
406d29a580SJohn Marino #include <stdio.h>
416d29a580SJohn Marino 
426d29a580SJohn Marino void
hexdump(const void * ptr,int length,const char * hdr,int flags)436d29a580SJohn Marino hexdump(const void *ptr, int length, const char *hdr, int flags)
446d29a580SJohn Marino {
456d29a580SJohn Marino 	int i, j, k;
466d29a580SJohn Marino 	int cols;
476d29a580SJohn Marino 	const unsigned char *cp;
486d29a580SJohn Marino 	char delim;
496d29a580SJohn Marino 
506d29a580SJohn Marino 	if ((flags & HD_DELIM_MASK) != 0)
516d29a580SJohn Marino 		delim = (flags & HD_DELIM_MASK) >> 8;
526d29a580SJohn Marino 	else
536d29a580SJohn Marino 		delim = ' ';
546d29a580SJohn Marino 
556d29a580SJohn Marino 	if ((flags & HD_COLUMN_MASK) != 0)
566d29a580SJohn Marino 		cols = flags & HD_COLUMN_MASK;
576d29a580SJohn Marino 	else
586d29a580SJohn Marino 		cols = 16;
596d29a580SJohn Marino 
606d29a580SJohn Marino 	cp = ptr;
616d29a580SJohn Marino 	for (i = 0; i < length; i+= cols) {
626d29a580SJohn Marino 		if (hdr != NULL)
636d29a580SJohn Marino 			printf("%s", hdr);
646d29a580SJohn Marino 
656d29a580SJohn Marino 		if ((flags & HD_OMIT_COUNT) == 0)
666d29a580SJohn Marino 			printf("%04x  ", i);
676d29a580SJohn Marino 
686d29a580SJohn Marino 		if ((flags & HD_OMIT_HEX) == 0) {
696d29a580SJohn Marino 			for (j = 0; j < cols; j++) {
706d29a580SJohn Marino 				k = i + j;
716d29a580SJohn Marino 				if (k < length)
726d29a580SJohn Marino 					printf("%c%02x", delim, cp[k]);
736d29a580SJohn Marino 				else
746d29a580SJohn Marino 					printf("   ");
756d29a580SJohn Marino 			}
766d29a580SJohn Marino 		}
776d29a580SJohn Marino 
786d29a580SJohn Marino 		if ((flags & HD_OMIT_CHARS) == 0) {
796d29a580SJohn Marino 			printf("  |");
806d29a580SJohn Marino 			for (j = 0; j < cols; j++) {
816d29a580SJohn Marino 				k = i + j;
826d29a580SJohn Marino 				if (k >= length)
836d29a580SJohn Marino 					printf(" ");
846d29a580SJohn Marino 				else if (cp[k] >= ' ' && cp[k] <= '~')
856d29a580SJohn Marino 					printf("%c", cp[k]);
866d29a580SJohn Marino 				else
876d29a580SJohn Marino 					printf(".");
886d29a580SJohn Marino 			}
896d29a580SJohn Marino 			printf("|");
906d29a580SJohn Marino 		}
916d29a580SJohn Marino 		printf("\n");
926d29a580SJohn Marino 	}
936d29a580SJohn Marino }
946d29a580SJohn Marino 
95