Skip to content

Commit 10b965a

Browse files
committed
add a diagnostic function debug_print_buffer, which might be useful in the future.
1 parent 5827792 commit 10b965a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

common.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,38 @@ void _inform(const char *thefilename, const int linenumber, const char *format,
574574
pthread_setcancelstate(oldState, NULL);
575575
}
576576

577+
void _debug_print_buffer(const char *thefilename, const int linenumber, int level, void *vbuf,
578+
size_t buf_len) {
579+
if (level > debuglev)
580+
return;
581+
char *buf = (char *)vbuf;
582+
char *obf =
583+
malloc(buf_len * 4 + 1); // to be on the safe side -- 4 characters on average for each byte
584+
if (obf != NULL) {
585+
char *obfp = obf;
586+
unsigned int obfc;
587+
for (obfc = 0; obfc < buf_len; obfc++) {
588+
snprintf(obfp, 3, "%02X", buf[obfc]);
589+
obfp += 2;
590+
if (obfc != buf_len - 1) {
591+
if (obfc % 32 == 31) {
592+
snprintf(obfp, 5, " || ");
593+
obfp += 4;
594+
} else if (obfc % 16 == 15) {
595+
snprintf(obfp, 4, " | ");
596+
obfp += 3;
597+
} else if (obfc % 4 == 3) {
598+
snprintf(obfp, 2, " ");
599+
obfp += 1;
600+
}
601+
}
602+
};
603+
*obfp = 0;
604+
_debug(thefilename, linenumber, level, "%s", obf);
605+
free(obf);
606+
}
607+
}
608+
577609
// The following two functions are adapted slightly and with thanks from Jonathan Leffler's sample
578610
// code at
579611
// https://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux

common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,14 @@ void _die(const char *filename, const int linenumber, const char *format, ...);
382382
void _warn(const char *filename, const int linenumber, const char *format, ...);
383383
void _inform(const char *filename, const int linenumber, const char *format, ...);
384384
void _debug(const char *filename, const int linenumber, int level, const char *format, ...);
385+
void _debug_print_buffer(const char *thefilename, const int linenumber, int level, void *buf,
386+
size_t buf_len);
385387

386388
#define die(...) _die(__FILE__, __LINE__, __VA_ARGS__)
387389
#define debug(...) _debug(__FILE__, __LINE__, __VA_ARGS__)
388390
#define warn(...) _warn(__FILE__, __LINE__, __VA_ARGS__)
389391
#define inform(...) _inform(__FILE__, __LINE__, __VA_ARGS__)
392+
#define debug_print_buffer(...) _debug_print_buffer(__FILE__, __LINE__, __VA_ARGS__)
390393

391394
uint8_t *base64_dec(char *input, int *outlen);
392395
char *base64_enc(uint8_t *input, int length);

0 commit comments

Comments
 (0)