hexdump.c revision 1.1
11.1Schristos/*- 21.1Schristos * Copyright (c) 2017 The NetBSD Foundation, Inc. 31.1Schristos * All rights reserved. 41.1Schristos * 51.1Schristos * This code is derived from software contributed to The NetBSD Foundation 61.1Schristos * by Christos Zoulas. 71.1Schristos * 81.1Schristos * Redistribution and use in source and binary forms, with or without 91.1Schristos * modification, are permitted provided that the following conditions 101.1Schristos * are met: 111.1Schristos * 1. Redistributions of source code must retain the above copyright 121.1Schristos * notice, this list of conditions and the following disclaimer. 131.1Schristos * 2. Redistributions in binary form must reproduce the above copyright 141.1Schristos * notice, this list of conditions and the following disclaimer in the 151.1Schristos * documentation and/or other materials provided with the distribution. 161.1Schristos * 171.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 181.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 191.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 201.1Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 211.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 221.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 231.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 241.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 251.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 261.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 271.1Schristos * POSSIBILITY OF SUCH DAMAGE. 281.1Schristos */ 291.1Schristos#include <sys/cdefs.h> 301.1Schristos__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.1 2017/12/08 21:51:07 christos Exp $"); 311.1Schristos 321.1Schristos#include <lib/libkern/libkern.h> 331.1Schristos#include <sys/systm.h> 341.1Schristos 351.1Schristosvoid 361.1Schristoshexdump(const char *msg, const void *ptr, size_t len) 371.1Schristos{ 381.1Schristos size_t i; 391.1Schristos const unsigned char *u = ptr; 401.1Schristos 411.1Schristos if (msg) 421.1Schristos printf("%s: %zu bytes @ %p\n", msg, len, ptr); 431.1Schristos for (i = 0; i < len; ) { 441.1Schristos printf("%02x ", u[i++]); 451.1Schristos if (!(i & 0x7)) 461.1Schristos printf(" "); 471.1Schristos if (!(i & 0xf)) { 481.1Schristos printf("| "); 491.1Schristos for (size_t j = i - 16; j < i; j++) { 501.1Schristos unsigned char c = u[j]; 511.1Schristos printf("%c", isprint(c) ? c : '.'); 521.1Schristos } 531.1Schristos printf("\n"); 541.1Schristos } 551.1Schristos } 561.1Schristos if ((i = (len & 0xf)) != 0) { 571.1Schristos for (size_t j = 16 - i; j > 0; j--) { 581.1Schristos printf(" "); 591.1Schristos if (!(j & 0x7) && i != 8) 601.1Schristos printf(" "); 611.1Schristos } 621.1Schristos printf(" | "); 631.1Schristos for (size_t j = len - i; j < len; j++) { 641.1Schristos unsigned char c = u[j]; 651.1Schristos printf("%c", isprint(c) ? c : '.'); 661.1Schristos } 671.1Schristos printf("\n"); 681.1Schristos } 691.1Schristos} 70