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;
|
||||
}
|
||||
|
||||
#define printable(c) \
|
||||
(c >= 0x20 && c < 0x7f)
|
||||
|
||||
static int hexdump_fprint(FILE *stream, void *data, size_t len) {
|
||||
size_t i;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
if (i % 16 == 0) {
|
||||
if (i) {
|
||||
if (fprintf(stderr, "\n") < 0) {
|
||||
goto error_fprintf;
|
||||
for (i=0; i<len; i+=16) {
|
||||
size_t x;
|
||||
|
||||
if (fprintf(stream, "%08lx:", i) < 0) {
|
||||
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) {
|
||||
goto error_fprintf;
|
||||
if (i+x < len) {
|
||||
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) {
|
||||
goto error_fprintf;
|
||||
if (fprintf(stream, " ") < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
if (i % 2) {
|
||||
if (fprintf(stderr, " ") < 0) {
|
||||
goto error_fprintf;
|
||||
for (x=0; x<16 && i+x<len; x++) {
|
||||
uint8_t c = ((uint8_t *)data)[i+x];
|
||||
|
||||
if (fputc(printable(c)? c: '.', stream) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
if (fprintf(stderr, "\n") < 0) {
|
||||
goto error_fprintf;
|
||||
if (fprintf(stream, "\n") < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_fprintf:
|
||||
error_io:
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue