From 4b76ec7159795d7b32f2355cb584b229abc65077 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 9 Aug 2011 19:17:52 -0700 Subject: [PATCH] gatchat: Add utility function for hexdump debugs --- gatchat/gatutil.c | 42 +++++++++++++++++++++++++++++++++++++++++- gatchat/gatutil.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/gatchat/gatutil.c b/gatchat/gatutil.c index 6a7e177f..d5f9e0d5 100644 --- a/gatchat/gatutil.c +++ b/gatchat/gatutil.c @@ -23,8 +23,9 @@ #include #endif -#include +#include #include +#include #include @@ -132,6 +133,45 @@ void g_at_util_debug_dump(gboolean in, const unsigned char *buf, gsize len, g_string_free(str, TRUE); } +void g_at_util_debug_hexdump(gboolean in, const unsigned char *buf, gsize len, + GAtDebugFunc debugf, gpointer user_data) +{ + static const char hexdigits[] = "0123456789abcdef"; + char str[68]; + gsize i; + + str[0] = in ? '<' : '>'; + + for (i = 0; i < len; i++) { + str[((i % 16) * 3) + 1] = ' '; + str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4]; + str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf]; + str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.'; + + if ((i + 1) % 16 == 0) { + str[49] = ' '; + str[50] = ' '; + str[67] = '\0'; + debugf(str, user_data); + str[0] = ' '; + } + } + + if ((i + 1) % 16 > 0) { + gsize j; + for (j = (i % 16); j < 16; j++) { + str[(j * 3) + 1] = ' '; + str[(j * 3) + 2] = ' '; + str[(j * 3) + 3] = ' '; + str[j + 51] = ' '; + } + str[49] = ' '; + str[50] = ' '; + str[67] = '\0'; + debugf(str, user_data); + } +} + gboolean g_at_util_setup_io(GIOChannel *io, GIOFlags flags) { GIOFlags io_flags; diff --git a/gatchat/gatutil.h b/gatchat/gatutil.h index 1a065af2..5c891086 100644 --- a/gatchat/gatutil.h +++ b/gatchat/gatutil.h @@ -34,6 +34,9 @@ void g_at_util_debug_chat(gboolean in, const char *str, gsize len, void g_at_util_debug_dump(gboolean in, const unsigned char *buf, gsize len, GAtDebugFunc debugf, gpointer user_data); +void g_at_util_debug_hexdump(gboolean in, const unsigned char *buf, gsize len, + GAtDebugFunc debugf, gpointer user_data); + gboolean g_at_util_setup_io(GIOChannel *io, GIOFlags flags); #ifdef __cplusplus