kernel: Bring in hexdump() from FreeBSD.
authorSascha Wildner <saw@online.de>
Mon, 29 Nov 2010 02:38:14 +0000 (03:38 +0100)
committerSascha Wildner <saw@online.de>
Tue, 30 Nov 2010 16:50:06 +0000 (17:50 +0100)
sys/kern/subr_prf.c
sys/sys/systm.h

index 68df895..3859b3f 100644 (file)
@@ -1181,3 +1181,57 @@ DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
 }
 
 #endif /* DDB */
+
+
+void
+hexdump(const void *ptr, int length, const char *hdr, int flags)
+{
+       int i, j, k;
+       int cols;
+       const unsigned char *cp;
+       char delim;
+
+       if ((flags & HD_DELIM_MASK) != 0)
+               delim = (flags & HD_DELIM_MASK) >> 8;
+       else
+               delim = ' ';
+
+       if ((flags & HD_COLUMN_MASK) != 0)
+               cols = flags & HD_COLUMN_MASK;
+       else
+               cols = 16;
+
+       cp = ptr;
+       for (i = 0; i < length; i+= cols) {
+               if (hdr != NULL)
+                       kprintf("%s", hdr);
+
+               if ((flags & HD_OMIT_COUNT) == 0)
+                       kprintf("%04x  ", i);
+
+               if ((flags & HD_OMIT_HEX) == 0) {
+                       for (j = 0; j < cols; j++) {
+                               k = i + j;
+                               if (k < length)
+                                       kprintf("%c%02x", delim, cp[k]);
+                               else
+                                       kprintf("   ");
+                       }
+               }
+
+               if ((flags & HD_OMIT_CHARS) == 0) {
+                       kprintf("  |");
+                       for (j = 0; j < cols; j++) {
+                               k = i + j;
+                               if (k >= length)
+                                       kprintf(" ");
+                               else if (cp[k] >= ' ' && cp[k] <= '~')
+                                       kprintf("%c", cp[k]);
+                               else
+                                       kprintf(".");
+                       }
+                       kprintf("|");
+               }
+               kprintf("\n");
+       }
+}
index 8342057..40a48d9 100644 (file)
@@ -202,6 +202,12 @@ int        kvasnrprintf (char **, size_t, int, const char *,
 int     kvsprintf (char *buf, const char *,
                        __va_list) __printflike(2, 0);
 int    ttyprintf (struct tty *, const char *, ...) __printflike(2, 3);
+void   hexdump (const void *ptr, int length, const char *hdr, int flags);
+#define        HD_COLUMN_MASK  0xff
+#define        HD_DELIM_MASK   0xff00
+#define        HD_OMIT_COUNT   (1 << 16)
+#define        HD_OMIT_HEX     (1 << 17)
+#define        HD_OMIT_CHARS   (1 << 18)
 int    ksscanf (const char *, char const *, ...) __scanflike(2, 3);
 int    kvsscanf (const char *, char const *, __va_list) __scanflike(2, 0);
 void   kvasfree(char **);