xref: /f-stack/tools/libutil/hexdump.c (revision 22ce4aff)
11eaf0ac3Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
41eaf0ac3Slogwang  * Copyright (c) 1986, 1988, 1991, 1993
51eaf0ac3Slogwang  *	The Regents of the University of California.  All rights reserved.
61eaf0ac3Slogwang  * (c) UNIX System Laboratories, Inc.
71eaf0ac3Slogwang  * All or some portions of this file are derived from material licensed
81eaf0ac3Slogwang  * to the University of California by American Telephone and Telegraph
91eaf0ac3Slogwang  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
101eaf0ac3Slogwang  * the permission of UNIX System Laboratories, Inc.
111eaf0ac3Slogwang  *
121eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
131eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
141eaf0ac3Slogwang  * are met:
151eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
161eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
171eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
181eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
191eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
20*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
211eaf0ac3Slogwang  *    may be used to endorse or promote products derived from this software
221eaf0ac3Slogwang  *    without specific prior written permission.
231eaf0ac3Slogwang  *
241eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341eaf0ac3Slogwang  * SUCH DAMAGE.
351eaf0ac3Slogwang  *
361eaf0ac3Slogwang  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
371eaf0ac3Slogwang  */
381eaf0ac3Slogwang 
391eaf0ac3Slogwang #include <sys/cdefs.h>
401eaf0ac3Slogwang __FBSDID("$FreeBSD$");
411eaf0ac3Slogwang 
421eaf0ac3Slogwang #include <sys/types.h>
431eaf0ac3Slogwang #include <libutil.h>
441eaf0ac3Slogwang #include <stdio.h>
451eaf0ac3Slogwang 
461eaf0ac3Slogwang void
hexdump(const void * ptr,int length,const char * hdr,int flags)471eaf0ac3Slogwang hexdump(const void *ptr, int length, const char *hdr, int flags)
481eaf0ac3Slogwang {
491eaf0ac3Slogwang 	int i, j, k;
501eaf0ac3Slogwang 	int cols;
511eaf0ac3Slogwang 	const unsigned char *cp;
521eaf0ac3Slogwang 	char delim;
531eaf0ac3Slogwang 
541eaf0ac3Slogwang 	if ((flags & HD_DELIM_MASK) != 0)
551eaf0ac3Slogwang 		delim = (flags & HD_DELIM_MASK) >> 8;
561eaf0ac3Slogwang 	else
571eaf0ac3Slogwang 		delim = ' ';
581eaf0ac3Slogwang 
591eaf0ac3Slogwang 	if ((flags & HD_COLUMN_MASK) != 0)
601eaf0ac3Slogwang 		cols = flags & HD_COLUMN_MASK;
611eaf0ac3Slogwang 	else
621eaf0ac3Slogwang 		cols = 16;
631eaf0ac3Slogwang 
641eaf0ac3Slogwang 	cp = ptr;
651eaf0ac3Slogwang 	for (i = 0; i < length; i+= cols) {
661eaf0ac3Slogwang 		if (hdr != NULL)
671eaf0ac3Slogwang 			printf("%s", hdr);
681eaf0ac3Slogwang 
691eaf0ac3Slogwang 		if ((flags & HD_OMIT_COUNT) == 0)
701eaf0ac3Slogwang 			printf("%04x  ", i);
711eaf0ac3Slogwang 
721eaf0ac3Slogwang 		if ((flags & HD_OMIT_HEX) == 0) {
731eaf0ac3Slogwang 			for (j = 0; j < cols; j++) {
741eaf0ac3Slogwang 				k = i + j;
751eaf0ac3Slogwang 				if (k < length)
761eaf0ac3Slogwang 					printf("%c%02x", delim, cp[k]);
771eaf0ac3Slogwang 				else
781eaf0ac3Slogwang 					printf("   ");
791eaf0ac3Slogwang 			}
801eaf0ac3Slogwang 		}
811eaf0ac3Slogwang 
821eaf0ac3Slogwang 		if ((flags & HD_OMIT_CHARS) == 0) {
831eaf0ac3Slogwang 			printf("  |");
841eaf0ac3Slogwang 			for (j = 0; j < cols; j++) {
851eaf0ac3Slogwang 				k = i + j;
861eaf0ac3Slogwang 				if (k >= length)
871eaf0ac3Slogwang 					printf(" ");
881eaf0ac3Slogwang 				else if (cp[k] >= ' ' && cp[k] <= '~')
891eaf0ac3Slogwang 					printf("%c", cp[k]);
901eaf0ac3Slogwang 				else
911eaf0ac3Slogwang 					printf(".");
921eaf0ac3Slogwang 			}
931eaf0ac3Slogwang 			printf("|");
941eaf0ac3Slogwang 		}
951eaf0ac3Slogwang 		printf("\n");
961eaf0ac3Slogwang 	}
971eaf0ac3Slogwang }
981eaf0ac3Slogwang 
99