Show printable frame bytes in src/decode.c
This commit is contained in:
parent
fba41f93df
commit
627bc70e0f
1 changed files with 32 additions and 17 deletions
49
src/decode.c
49
src/decode.c
|
@ -32,42 +32,57 @@ static int callsign_fprint(FILE *stream, const patty_ax25_address *address) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define printable(c) \
|
||||||
|
(c >= 0x20 && c < 0x7f)
|
||||||
|
|
||||||
static int hexdump_fprint(FILE *stream, void *data, size_t len) {
|
static int hexdump_fprint(FILE *stream, void *data, size_t len) {
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for (i=0; i<len; i++) {
|
for (i=0; i<len; i+=16) {
|
||||||
if (i % 16 == 0) {
|
size_t x;
|
||||||
if (i) {
|
|
||||||
if (fprintf(stderr, "\n") < 0) {
|
if (fprintf(stream, "%08lx:", i) < 0) {
|
||||||
goto error_fprintf;
|
goto error_io;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (x=0; x<16; x++) {
|
||||||
|
if (!(x & 1)) {
|
||||||
|
if (fprintf(stream, " ") < 0) {
|
||||||
|
goto error_io;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fprintf(stderr, "%08lx: ", i) < 0) {
|
if (i+x < len) {
|
||||||
goto error_fprintf;
|
if (fprintf(stream, "%02x", ((uint8_t *)data)[i+x]) < 0) {
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (fprintf(stream, " ") < 0) {
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fprintf(stderr, "%02x", ((uint8_t *)data)[i]) < 0) {
|
if (fprintf(stream, " ") < 0) {
|
||||||
goto error_fprintf;
|
goto error_io;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i % 2) {
|
for (x=0; x<16 && i+x<len; x++) {
|
||||||
if (fprintf(stderr, " ") < 0) {
|
uint8_t c = ((uint8_t *)data)[i+x];
|
||||||
goto error_fprintf;
|
|
||||||
|
if (fputc(printable(c)? c: '.', stream) < 0) {
|
||||||
|
goto error_io;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (i) {
|
if (fprintf(stream, "\n") < 0) {
|
||||||
if (fprintf(stderr, "\n") < 0) {
|
goto error_io;
|
||||||
goto error_fprintf;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_fprintf:
|
error_io:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue