From 10b965a7aae0e7a10385d08aef9de92521160e18 Mon Sep 17 00:00:00 2001 From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:26:55 +0100 Subject: [PATCH] add a diagnostic function debug_print_buffer, which might be useful in the future. --- common.c | 32 ++++++++++++++++++++++++++++++++ common.h | 3 +++ 2 files changed, 35 insertions(+) diff --git a/common.c b/common.c index 0e2a0724..42411c5f 100644 --- a/common.c +++ b/common.c @@ -574,6 +574,38 @@ void _inform(const char *thefilename, const int linenumber, const char *format, pthread_setcancelstate(oldState, NULL); } +void _debug_print_buffer(const char *thefilename, const int linenumber, int level, void *vbuf, + size_t buf_len) { + if (level > debuglev) + return; + char *buf = (char *)vbuf; + char *obf = + malloc(buf_len * 4 + 1); // to be on the safe side -- 4 characters on average for each byte + if (obf != NULL) { + char *obfp = obf; + unsigned int obfc; + for (obfc = 0; obfc < buf_len; obfc++) { + snprintf(obfp, 3, "%02X", buf[obfc]); + obfp += 2; + if (obfc != buf_len - 1) { + if (obfc % 32 == 31) { + snprintf(obfp, 5, " || "); + obfp += 4; + } else if (obfc % 16 == 15) { + snprintf(obfp, 4, " | "); + obfp += 3; + } else if (obfc % 4 == 3) { + snprintf(obfp, 2, " "); + obfp += 1; + } + } + }; + *obfp = 0; + _debug(thefilename, linenumber, level, "%s", obf); + free(obf); + } +} + // The following two functions are adapted slightly and with thanks from Jonathan Leffler's sample // code at // https://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux diff --git a/common.h b/common.h index a1d50d44..9de26f63 100644 --- a/common.h +++ b/common.h @@ -382,11 +382,14 @@ void _die(const char *filename, const int linenumber, const char *format, ...); void _warn(const char *filename, const int linenumber, const char *format, ...); void _inform(const char *filename, const int linenumber, const char *format, ...); void _debug(const char *filename, const int linenumber, int level, const char *format, ...); +void _debug_print_buffer(const char *thefilename, const int linenumber, int level, void *buf, + size_t buf_len); #define die(...) _die(__FILE__, __LINE__, __VA_ARGS__) #define debug(...) _debug(__FILE__, __LINE__, __VA_ARGS__) #define warn(...) _warn(__FILE__, __LINE__, __VA_ARGS__) #define inform(...) _inform(__FILE__, __LINE__, __VA_ARGS__) +#define debug_print_buffer(...) _debug_print_buffer(__FILE__, __LINE__, __VA_ARGS__) uint8_t *base64_dec(char *input, int *outlen); char *base64_enc(uint8_t *input, int length); -- 2.47.2