Home | History | Annotate | Line # | Download | only in libkern
      1 /*-
      2  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Christos Zoulas.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.4 2017/12/09 00:51:52 christos Exp $");
     31 
     32 #ifdef DEBUG_HEXDUMP
     33 #include <stdio.h>
     34 #include <ctype.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 #define RET int
     38 static const char hexdigits[] = "0123456789abcdef";
     39 #else
     40 #define RET void
     41 #include <lib/libkern/libkern.h>
     42 #include <sys/systm.h>
     43 #endif
     44 
     45 #define MID (3 * 8)
     46 #define BAR ((3 * 16) + 1)
     47 #define ASC (BAR + 2)
     48 #define NL (BAR + 18)
     49 
     50 void
     51 hexdump(RET (*pr)(const char *, ...) __printflike(1, 2), const char *msg,
     52     const void *ptr, size_t len)
     53 {
     54 	size_t i, p, q;
     55 	const unsigned char *u = ptr;
     56 	char buf[NL + 2];
     57 
     58 	if (msg)
     59 		(*pr)("%s: %zu bytes @ %p\n", msg, len, ptr);
     60 
     61 	buf[BAR] = '|';
     62 	buf[BAR + 1] = ' ';
     63 	buf[NL] = '\n';
     64 	buf[NL + 1] = '\0';
     65         for (q = p = i = 0; i < len; i++) {
     66 		unsigned char c = u[i];
     67 		buf[p++] = hexdigits[(c >> 4) & 0xf];
     68 		buf[p++] = hexdigits[(c >> 0) & 0xf];
     69 		buf[p++] = ' ';
     70                 if (q == 7)
     71 		       buf[p++] = ' ';
     72 
     73 		buf[ASC + q++] = isprint(c) ? c : '.';
     74 
     75 		if (q == 16) {
     76 			q = p = 0;
     77 			(*pr)("%s", buf);
     78 		}
     79         }
     80 	if (q) {
     81 		while (p < BAR)
     82 			buf[p++] = ' ';
     83 		buf[ASC + q++] = '\n';
     84 		buf[ASC + q] = '\0';
     85 		(*pr)("%s", buf);
     86 	}
     87 }
     88 
     89 #ifdef DEBUG_HEXDUMP
     90 int
     91 main(int argc, char *argv[])
     92 {
     93 	hexdump(printf, "foo", main, atoi(argv[1]));
     94 	return 0;
     95 }
     96 #endif
     97