Extend hexdump function for tests

This commit is contained in:
Johannes Schriewer 2018-07-28 16:09:08 +02:00
parent 7e4afa462d
commit e072a11725
3 changed files with 43 additions and 31 deletions

View file

@ -200,11 +200,11 @@ static inline size_t buffer_append_buffer(Buffer *dest, Buffer *src) {
#if DEBUG
#include "debug.h"
static inline void buffer_hexdump(Buffer *buffer) {
hexdump(buffer->data, buffer->len);
static inline void buffer_hexdump(Buffer *buffer, int indent) {
hexdump(buffer->data, buffer->len, indent);
}
#else
#define buffer_hexdump(_buffer) /* */
#define buffer_hexdump(_buffer, _indent) /* */
#endif
#endif /* buffer_h__included */

View file

@ -4,31 +4,4 @@
#include "debug.h"
void hexdump(char *data, size_t len) {
for (int i = 0; i < len;) {
for (int col = 0; col < 16; col++) {
if (i + col < len) {
fprintf(stdout, "%02x ", data[i + col]);
} else {
fprintf(stdout, " ");
}
}
fprintf(stdout, " | ");
for (int col = 0; col < 16; col++) {
if (i + col < len) {
char c = data[i + col];
if ((c > 127) || (c < 32)) c = '.';
fprintf(stdout, "%c", c);
} else {
fprintf(stdout, " ");
}
}
fprintf(stdout, "\n");
i += 16;
}
}
#endif /* DEBUG */

View file

@ -2,10 +2,49 @@
#define debug_h__included
#include <stdlib.h>
#include <stdio.h>
#if DEBUG
void hexdump(char *data, size_t len);
static inline void hexdump(char *data, size_t len, int indent) {
for (int i = 0; i < len;) {
// indent
for (int col = 0; col < indent; col++) {
fprintf(stdout, " ");
}
// address
fprintf(stdout, "0x%04x: ", i);
// hex field
for (int col = 0; col < 16; col++) {
if (i + col < len) {
fprintf(stdout, "%02x ", (uint8_t)data[i + col]);
} else {
fprintf(stdout, " ");
}
}
// separator
fprintf(stdout, " | ");
// ascii field
for (int col = 0; col < 16; col++) {
if (i + col < len) {
char c = data[i + col];
if ((c > 127) || (c < 32)) c = '.';
fprintf(stdout, "%c", (uint8_t)c);
} else {
fprintf(stdout, " ");
}
}
fprintf(stdout, "\n");
i += 16;
}
}
#else /* DEBUG */