hexdump.c revision 180161
118334Speter/*- 2132718Skan * Copyright (c) 1986, 1988, 1991, 1993 318334Speter * The Regents of the University of California. All rights reserved. 4132718Skan * (c) UNIX System Laboratories, Inc. 518334Speter * All or some portions of this file are derived from material licensed 6132718Skan * to the University of California by American Telephone and Telegraph 718334Speter * Co. or Unix System Laboratories, Inc. and are reproduced herein with 818334Speter * the permission of UNIX System Laboratories, Inc. 918334Speter * 1018334Speter * Redistribution and use in source and binary forms, with or without 11132718Skan * modification, are permitted provided that the following conditions 1218334Speter * are met: 1318334Speter * 1. Redistributions of source code must retain the above copyright 1418334Speter * notice, this list of conditions and the following disclaimer. 1518334Speter * 2. Redistributions in binary form must reproduce the above copyright 1618334Speter * notice, this list of conditions and the following disclaimer in the 17132718Skan * documentation and/or other materials provided with the distribution. 1818334Speter * 4. Neither the name of the University nor the names of its contributors 1918334Speter * may be used to endorse or promote products derived from this software 2018334Speter * without specific prior written permission. 2118334Speter * 22132718Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24132718Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25132718Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26132718Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2718334Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28132718Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29132718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30132718Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31132718Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32132718Skan * SUCH DAMAGE. 3318334Speter * 34132718Skan * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 35132718Skan */ 36132718Skan 37132718Skan#include <sys/cdefs.h> 38132718Skan__FBSDID("$FreeBSD: head/lib/libutil/hexdump.c 180161 2008-07-01 22:30:57Z jhb $"); 39132718Skan 40132718Skan#include <sys/types.h> 41132718Skan#include <libutil.h> 42132718Skan#include <stdio.h> 43132718Skan 4418334Spetervoid 45132718Skanhexdump(const void *ptr, int length, const char *hdr, int flags) 46132718Skan{ 4718334Speter int i, j, k; 48132718Skan int cols; 49132718Skan const unsigned char *cp; 50132718Skan char delim; 51132718Skan 52132718Skan if ((flags & HD_DELIM_MASK) != 0) 53132718Skan delim = (flags & HD_DELIM_MASK) >> 8; 5418334Speter else 55132718Skan delim = ' '; 56132718Skan 57132718Skan if ((flags & HD_COLUMN_MASK) != 0) 58132718Skan cols = flags & HD_COLUMN_MASK; 59132718Skan else 60132718Skan cols = 16; 61132718Skan 62132718Skan cp = ptr; 63132718Skan for (i = 0; i < length; i+= cols) { 64132718Skan if (hdr != NULL) 65132718Skan printf("%s", hdr); 66132718Skan 67132718Skan if ((flags & HD_OMIT_COUNT) == 0) 68132718Skan printf("%04x ", i); 69132718Skan 70132718Skan if ((flags & HD_OMIT_HEX) == 0) { 71132718Skan for (j = 0; j < cols; j++) { 72132718Skan k = i + j; 73132718Skan if (k < length) 74132718Skan printf("%c%02x", delim, cp[k]); 75132718Skan else 76132718Skan printf(" "); 77132718Skan } 78132718Skan } 79132718Skan 80132718Skan if ((flags & HD_OMIT_CHARS) == 0) { 81 printf(" |"); 82 for (j = 0; j < cols; j++) { 83 k = i + j; 84 if (k >= length) 85 printf(" "); 86 else if (cp[k] >= ' ' && cp[k] <= '~') 87 printf("%c", cp[k]); 88 else 89 printf("."); 90 } 91 printf("|"); 92 } 93 printf("\n"); 94 } 95} 96 97