hexdump.c revision 1.2
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.2Schristos__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.2 2017/12/08 23:49:01 christos Exp $"); 311.1Schristos 321.2Schristos#ifndef _KERNEL 331.2Schristos#include <stdio.h> 341.2Schristos#include <ctype.h> 351.2Schristos#include <string.h> 361.2Schristos#include <stdlib.h> 371.2Schristosstatic const char hexdigits[] = "0123456789abcdef"; 381.2Schristos#else 391.1Schristos#include <lib/libkern/libkern.h> 401.1Schristos#include <sys/systm.h> 411.2Schristos#endif 421.2Schristos 431.2Schristos#define MID (3 * 8) 441.2Schristos#define BAR ((3 * 16) + 1) 451.2Schristos#define ASC (BAR + 2) 461.2Schristos#define NL (BAR + 18) 471.1Schristos 481.1Schristosvoid 491.1Schristoshexdump(const char *msg, const void *ptr, size_t len) 501.1Schristos{ 511.2Schristos size_t i, p, q; 521.1Schristos const unsigned char *u = ptr; 531.2Schristos char buf[NL + 2]; 541.1Schristos 551.1Schristos if (msg) 561.1Schristos printf("%s: %zu bytes @ %p\n", msg, len, ptr); 571.2Schristos 581.2Schristos buf[BAR] = '|'; 591.2Schristos buf[BAR + 1] = ' '; 601.2Schristos buf[NL] = '\n'; 611.2Schristos buf[NL + 1] = '\0'; 621.2Schristos memset(buf, ' ', BAR); 631.2Schristos for (q = p = i = 0; i < len; i++) { 641.2Schristos unsigned char c = u[i]; 651.2Schristos buf[p++] = hexdigits[(c >> 4) & 0xf]; 661.2Schristos buf[p++] = hexdigits[(c >> 0) & 0xf]; 671.2Schristos buf[p++] = ' '; 681.2Schristos if (q == 7) 691.2Schristos buf[p++] = ' '; 701.2Schristos 711.2Schristos buf[ASC + q++] = isprint(c) ? c : '.'; 721.2Schristos 731.2Schristos if (q == 16) { 741.2Schristos q = p = 0; 751.2Schristos printf("%s", buf); 761.2Schristos memset(buf, ' ', BAR); 771.1Schristos } 781.1Schristos } 791.2Schristos if (q) { 801.2Schristos buf[ASC + q++] = '\n'; 811.2Schristos buf[ASC + q] = '\0'; 821.2Schristos printf("%s", buf); 831.2Schristos } 841.2Schristos} 851.2Schristos 861.2Schristos#ifndef _KERNEL 871.2Schristosint 881.2Schristosmain(int argc, char *argv[]) 891.2Schristos{ 901.2Schristos hexdump("foo", main, atoi(argv[1])); 911.2Schristos return 0; 921.1Schristos} 931.2Schristos#endif 94