Liu Aleaxander
2009-Dec-10 14:59 UTC
[syslinux] [Syslinux] [PATCH] [RFC] lib: add a hex dump lib function
Hi all, Date: Thu, 10 Dec 2009 22:55:20 +0800 Subject: [PATCH] [RFC] lib: add a hex dump lib function I think it would be better to have a hex dump routine; it would make debugging much easier since it can dump the data, like fs meta data, in a hex style, just like what the 'hexdump -C file' command does. BTW, I'm not sure where should I to put the hexdump function declaration. For now, I just put it in stdio.h, since I think it's a little better than adding a new hexdump.h Signed-off-by: Liu Aleaxander <Aleaxander at gmail.com> --- com32/include/stdio.h | 2 ++ com32/lib/Makefile | 2 +- com32/lib/hexdump.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletions(-) create mode 100644 com32/lib/hexdump.c diff --git a/com32/include/stdio.h b/com32/include/stdio.h index f37bdd9..731ebe8 100644 --- a/com32/include/stdio.h +++ b/com32/include/stdio.h @@ -114,4 +114,6 @@ __extern void perror(const char *); __extern int rename(const char *, const char *); +__extern void hexdump(const void *, int, int); + #endif /* _STDIO_H */ diff --git a/com32/lib/Makefile b/com32/lib/Makefile index c5b1981..50fd07f 100644 --- a/com32/lib/Makefile +++ b/com32/lib/Makefile @@ -25,7 +25,7 @@ LIBOBJS = \ strtoimax.o strtok.o strtol.o strtoll.o strtoul.o strtoull.o \ strtoumax.o vfprintf.o vprintf.o vsnprintf.o vsprintf.o \ asprintf.o vasprintf.o strlcpy.o strlcat.o \ - vsscanf.o zalloc.o \ + vsscanf.o zalloc.o hexdump.o \ \ opendir.o readdir.o closedir.o getcwd.o chdir.o fdopendir.o \ \ diff --git a/com32/lib/hexdump.c b/com32/lib/hexdump.c new file mode 100644 index 0000000..04edbcf --- /dev/null +++ b/com32/lib/hexdump.c @@ -0,0 +1,42 @@ +/* + * The hex dump lib. + * + * Copyright (C) 2009 Liu Aleaxander -- All rights reserved. This file + * may be redistributed under the terms of the GNU Public License. + */ + +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +void hexdump(const void *data, int len, int base) +{ + int i = 0; + int index = 0; + uint8_t *p = data; + uint8_t hex[16 * 3 + 1] = {0, }; + uint8_t text[16 + 1] = {0, }; + uint8_t c; + + for (i = 0; i < len; i++) { + index = i & 0xf; + if (i && index == 0) { + /* print the buffer before reset it */ + printf("%08x %-48s |%-16s|\n", base, hex, text); + base += 0x10; + memset(hex, 0, sizeof hex); + memset(text, 0, sizeof text); + } + + c = *p++; + sprintf((char *)&hex[index * 3], "%02x ", c); + if (c < 0x20 || c > 0x7f) + text[index] = '.'; + else + text[index] = c; + } + + /* print the last part */ + printf("%08x %-48s |%-16s|\n", base, hex, text); +} -- 1.6.4.1 -- regards Liu Aleaxander
Sebastian Herbszt
2009-Dec-12 14:08 UTC
[syslinux] [Syslinux] [PATCH] [RFC] lib: add a hex dump lib function
Liu Aleaxander wrote:> Hi all, > > Date: Thu, 10 Dec 2009 22:55:20 +0800 > Subject: [PATCH] [RFC] lib: add a hex dump lib function > > I think it would be better to have a hex dump routine; it would make > debugging much easier since it can dump the data, like fs meta data, > in a hex style, just like what the 'hexdump -C file' command does. > > BTW, I'm not sure where should I to put the hexdump function declaration. > For now, I just put it in stdio.h, since I think it's a little better than > adding a new hexdump.hI would prefer hexdump.h.> Signed-off-by: Liu Aleaxander <Aleaxander at gmail.com> > --- > com32/include/stdio.h | 2 ++ > com32/lib/Makefile | 2 +- > com32/lib/hexdump.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 45 insertions(+), 1 deletions(-) > create mode 100644 com32/lib/hexdump.c[snip]> diff --git a/com32/lib/hexdump.c b/com32/lib/hexdump.c > new file mode 100644 > index 0000000..04edbcf > --- /dev/null > +++ b/com32/lib/hexdump.c > @@ -0,0 +1,42 @@ > +/* > + * The hex dump lib. > + * > + * Copyright (C) 2009 Liu Aleaxander -- All rights reserved. This file > + * may be redistributed under the terms of the GNU Public License. > + */ > +com32/LICENCE says libcom32 is under the MIT license. - Sebastian