Extend hexdump function for tests
This commit is contained in:
parent
7e4afa462d
commit
e072a11725
3 changed files with 43 additions and 31 deletions
|
@ -200,11 +200,11 @@ static inline size_t buffer_append_buffer(Buffer *dest, Buffer *src) {
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
static inline void buffer_hexdump(Buffer *buffer) {
|
static inline void buffer_hexdump(Buffer *buffer, int indent) {
|
||||||
hexdump(buffer->data, buffer->len);
|
hexdump(buffer->data, buffer->len, indent);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#define buffer_hexdump(_buffer) /* */
|
#define buffer_hexdump(_buffer, _indent) /* */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* buffer_h__included */
|
#endif /* buffer_h__included */
|
||||||
|
|
27
src/debug.c
27
src/debug.c
|
@ -4,31 +4,4 @@
|
||||||
|
|
||||||
#include "debug.h"
|
#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 */
|
#endif /* DEBUG */
|
||||||
|
|
41
src/debug.h
41
src/debug.h
|
@ -2,10 +2,49 @@
|
||||||
#define debug_h__included
|
#define debug_h__included
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
#if DEBUG
|
#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 */
|
#else /* DEBUG */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue