annotate rvinterf/libprint/hexdump.c @ 990:f34261bb3355

pln-ppb-test: implement PPB erase
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Dec 2023 00:20:14 +0000
parents 65953c172f24
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
928
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * The function contained in this module implements hex dump functionality:
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * an arbitrary binary packet is dumped in hex, line by line, calling an output
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * function for each line.
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 void
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 packet_hex_dump(src, srclen, outfunc)
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 u_char *src;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 unsigned srclen;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 void (*outfunc)();
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 u_char *sp = src;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned remain = srclen;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 unsigned offset = 0;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 unsigned chunk;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 char line[80], *dp;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 int i, c;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 while (remain) {
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 sprintf(line, "%04X: ", offset);
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 dp = line + 7;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 chunk = remain;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (chunk > 16)
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 chunk = 16;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 for (i = 0; i < 16; i++) {
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (i < chunk)
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 sprintf(dp, "%02X ", sp[i]);
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 else
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 strcpy(dp, " ");
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 dp += 3;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (i == 7 || i == 15)
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 *dp++ = ' ';
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 for (i = 0; i < chunk; i++) {
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 c = sp[i];
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (c < ' ' || c > '~')
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 c = '.';
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 *dp++ = c;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 *dp = '\0';
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 outfunc(line);
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 sp += chunk;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 remain -= chunk;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 offset += chunk;
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }
65953c172f24 rvinterf/lowlevel: new hex dump format
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }