diff --git a/include/common/standard.h b/include/common/standard.h index 18459cdc9..6fb1c9442 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -1423,6 +1423,7 @@ int dump_text(struct buffer *out, const char *buf, int bsize); int dump_binary(struct buffer *out, const char *buf, int bsize); int dump_text_line(struct buffer *out, const char *buf, int bsize, int len, int *line, int ptr); +void dump_hex(struct buffer *out, const char *pfx, const void *buf, int len); /* same as realloc() except that ptr is also freed upon failure */ static inline void *my_realloc2(void *ptr, size_t size) diff --git a/src/standard.c b/src/standard.c index a81f5a0fc..ffabccb5e 100644 --- a/src/standard.c +++ b/src/standard.c @@ -4076,6 +4076,48 @@ int dump_binary(struct buffer *out, const char *buf, int bsize) return ptr; } +/* Appends into buffer a hex dump of memory area for bytes, + * prepending each line with prefix . The output is *not* initialized. + * The output will not wrap pas the buffer's end so it is more optimal if the + * caller makes sure the buffer is aligned first. A trailing zero will always + * be appended (and not counted) if there is room for it. The caller must make + * sure that the area is dumpable first. + */ +void dump_hex(struct buffer *out, const char *pfx, const void *buf, int len) +{ + const unsigned char *d = buf; + int i, j, start; + + d = (const unsigned char *)(((unsigned long)buf) & -16); + start = ((unsigned long)buf) & 15; + + for (i = 0; i < start + len; i += 16) { + chunk_appendf(out, (sizeof(void *) == 4) ? "%s%8p: " : "%s%16p: ", pfx, d + i); + + for (j = 0; j < 16; j++) { + if ((i + j >= start) && (i + j < start + len)) + chunk_appendf(out, "%02x ", d[i + j]); + else + chunk_strcat(out, "'' "); + + if (j == 7) + chunk_strcat(out, "- "); + } + chunk_strcat(out, " "); + for (j = 0; j < 16; j++) { + if ((i + j >= start) && (i + j < start + len)) { + if (isprint(d[i + j])) + chunk_appendf(out, "%c", d[i + j]); + else + chunk_strcat(out, "."); + } + else + chunk_strcat(out, "'"); + } + chunk_strcat(out, "\n"); + } +} + /* print a line of text buffer (limited to 70 bytes) to . The format is : * <2 spaces> <70 chars max> <\n> * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are