Sat Dec 9 00:51:52 2017 UTC ()
Even smaller and takes print function.


(christos)
diff -r1.3 -r1.4 src/sys/lib/libkern/hexdump.c
diff -r1.125 -r1.126 src/sys/lib/libkern/libkern.h

cvs diff -r1.3 -r1.4 src/sys/lib/libkern/hexdump.c (expand / switch to unified diff)

--- src/sys/lib/libkern/hexdump.c 2017/12/08 23:57:57 1.3
+++ src/sys/lib/libkern/hexdump.c 2017/12/09 00:51:52 1.4
@@ -17,77 +17,80 @@ @@ -17,77 +17,80 @@
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE. 27 * POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.3 2017/12/08 23:57:57 christos Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.4 2017/12/09 00:51:52 christos Exp $");
31 31
32#ifdef DEBUG_HEXDUMP 32#ifdef DEBUG_HEXDUMP
33#include <stdio.h> 33#include <stdio.h>
34#include <ctype.h> 34#include <ctype.h>
35#include <string.h> 35#include <string.h>
36#include <stdlib.h> 36#include <stdlib.h>
 37#define RET int
37static const char hexdigits[] = "0123456789abcdef"; 38static const char hexdigits[] = "0123456789abcdef";
38#else 39#else
 40#define RET void
39#include <lib/libkern/libkern.h> 41#include <lib/libkern/libkern.h>
40#include <sys/systm.h> 42#include <sys/systm.h>
41#endif 43#endif
42 44
43#define MID (3 * 8) 45#define MID (3 * 8)
44#define BAR ((3 * 16) + 1) 46#define BAR ((3 * 16) + 1)
45#define ASC (BAR + 2) 47#define ASC (BAR + 2)
46#define NL (BAR + 18) 48#define NL (BAR + 18)
47 49
48void 50void
49hexdump(const char *msg, const void *ptr, size_t len) 51hexdump(RET (*pr)(const char *, ...) __printflike(1, 2), const char *msg,
 52 const void *ptr, size_t len)
50{ 53{
51 size_t i, p, q; 54 size_t i, p, q;
52 const unsigned char *u = ptr; 55 const unsigned char *u = ptr;
53 char buf[NL + 2]; 56 char buf[NL + 2];
54 57
55 if (msg) 58 if (msg)
56 printf("%s: %zu bytes @ %p\n", msg, len, ptr); 59 (*pr)("%s: %zu bytes @ %p\n", msg, len, ptr);
57 60
58 buf[BAR] = '|'; 61 buf[BAR] = '|';
59 buf[BAR + 1] = ' '; 62 buf[BAR + 1] = ' ';
60 buf[NL] = '\n'; 63 buf[NL] = '\n';
61 buf[NL + 1] = '\0'; 64 buf[NL + 1] = '\0';
62 memset(buf, ' ', BAR); 
63 for (q = p = i = 0; i < len; i++) { 65 for (q = p = i = 0; i < len; i++) {
64 unsigned char c = u[i]; 66 unsigned char c = u[i];
65 buf[p++] = hexdigits[(c >> 4) & 0xf]; 67 buf[p++] = hexdigits[(c >> 4) & 0xf];
66 buf[p++] = hexdigits[(c >> 0) & 0xf]; 68 buf[p++] = hexdigits[(c >> 0) & 0xf];
67 buf[p++] = ' '; 69 buf[p++] = ' ';
68 if (q == 7) 70 if (q == 7)
69 buf[p++] = ' '; 71 buf[p++] = ' ';
70 72
71 buf[ASC + q++] = isprint(c) ? c : '.'; 73 buf[ASC + q++] = isprint(c) ? c : '.';
72 74
73 if (q == 16) { 75 if (q == 16) {
74 q = p = 0; 76 q = p = 0;
75 printf("%s", buf); 77 (*pr)("%s", buf);
76 memset(buf, ' ', BAR); 
77 } 78 }
78 } 79 }
79 if (q) { 80 if (q) {
 81 while (p < BAR)
 82 buf[p++] = ' ';
80 buf[ASC + q++] = '\n'; 83 buf[ASC + q++] = '\n';
81 buf[ASC + q] = '\0'; 84 buf[ASC + q] = '\0';
82 printf("%s", buf); 85 (*pr)("%s", buf);
83 } 86 }
84} 87}
85 88
86#ifdef DEBUG_HEXDUMP 89#ifdef DEBUG_HEXDUMP
87int 90int
88main(int argc, char *argv[]) 91main(int argc, char *argv[])
89{ 92{
90 hexdump("foo", main, atoi(argv[1])); 93 hexdump(printf, "foo", main, atoi(argv[1]));
91 return 0; 94 return 0;
92} 95}
93#endif 96#endif

cvs diff -r1.125 -r1.126 src/sys/lib/libkern/libkern.h (expand / switch to unified diff)

--- src/sys/lib/libkern/libkern.h 2017/12/08 21:51:07 1.125
+++ src/sys/lib/libkern/libkern.h 2017/12/09 00:51:52 1.126
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: libkern.h,v 1.125 2017/12/08 21:51:07 christos Exp $ */ 1/* $NetBSD: libkern.h,v 1.126 2017/12/09 00:51:52 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -438,27 +438,28 @@ int skpc(int, size_t, u_char *); @@ -438,27 +438,28 @@ int skpc(int, size_t, u_char *);
438int strcasecmp(const char *, const char *); 438int strcasecmp(const char *, const char *);
439size_t strlcpy(char *, const char *, size_t); 439size_t strlcpy(char *, const char *, size_t);
440size_t strlcat(char *, const char *, size_t); 440size_t strlcat(char *, const char *, size_t);
441int strncasecmp(const char *, const char *, size_t); 441int strncasecmp(const char *, const char *, size_t);
442u_long strtoul(const char *, char **, int); 442u_long strtoul(const char *, char **, int);
443long long strtoll(const char *, char **, int); 443long long strtoll(const char *, char **, int);
444unsigned long long strtoull(const char *, char **, int); 444unsigned long long strtoull(const char *, char **, int);
445intmax_t strtoimax(const char *, char **, int); 445intmax_t strtoimax(const char *, char **, int);
446uintmax_t strtoumax(const char *, char **, int); 446uintmax_t strtoumax(const char *, char **, int);
447intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t, 447intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
448 intmax_t, int *); 448 intmax_t, int *);
449uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t, 449uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
450 uintmax_t, int *); 450 uintmax_t, int *);
451void hexdump(const char *, const void *, size_t); 451void hexdump(void (*)(const char *, ...) __printflike(1, 2),
 452 const char *, const void *, size_t);
452 453
453int snprintb(char *, size_t, const char *, uint64_t); 454int snprintb(char *, size_t, const char *, uint64_t);
454int snprintb_m(char *, size_t, const char *, uint64_t, size_t); 455int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
455int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *), 456int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
456 void *); 457 void *);
457uint32_t crc32(uint32_t, const uint8_t *, size_t); 458uint32_t crc32(uint32_t, const uint8_t *, size_t);
458#if __GNUC_PREREQ__(4, 5) \ 459#if __GNUC_PREREQ__(4, 5) \
459 && (defined(__alpha_cix__) || defined(__mips_popcount)) 460 && (defined(__alpha_cix__) || defined(__mips_popcount))
460#define popcount __builtin_popcount 461#define popcount __builtin_popcount
461#define popcountl __builtin_popcountl 462#define popcountl __builtin_popcountl
462#define popcountll __builtin_popcountll 463#define popcountll __builtin_popcountll
463#define popcount32 __builtin_popcount 464#define popcount32 __builtin_popcount
464#define popcount64 __builtin_popcountll 465#define popcount64 __builtin_popcountll