diff options
author | Guy Harris <guy@alum.mit.edu> | 2002-06-21 23:52:47 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2002-06-21 23:52:47 +0000 |
commit | b2277c9cfceda72c5278136e3cbf8b1d775ed806 (patch) | |
tree | f6664faa27e2d36292e47dbd352c08ac87c9236b /print.c | |
parent | 9e922721880969f23b7eda41a18408e4dc4e2494 (diff) |
Use common code to print the hex dump as text and PostScript.
svn path=/trunk/; revision=5727
Diffstat (limited to 'print.c')
-rw-r--r-- | print.c | 135 |
1 files changed, 72 insertions, 63 deletions
@@ -1,7 +1,7 @@ /* print.c * Routines for printing packet analysis trees. * - * $Id: print.c,v 1.48 2002/06/21 23:04:30 guy Exp $ + * $Id: print.c,v 1.49 2002/06/21 23:52:47 guy Exp $ * * Gilbert Ramirez <gram@alumni.rice.edu> * @@ -257,43 +257,76 @@ void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt) } } -/* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and -only slightly modified for ethereal by Gilbert Ramirez. */ +/* + * This routine is based on a routine created by Dan Lasley + * <DLASLEY@PROMUS.com>. + * + * It was modified for Ethereal by Gilbert Ramirez and others. + */ static -void print_hex_data_text(FILE *fh, register const u_char *cp, - register u_int length, char_enc encoding) +void print_hex_data_common(FILE *fh, register const u_char *cp, + register u_int length, char_enc encoding, + void (*print_hex_data_start)(FILE *), + void (*print_hex_data_line)(FILE *, u_int, u_char *), + void (*print_hex_data_end)(FILE *)) { - register unsigned int ad, i, j, k; - u_char c; - u_char line[50+16+1]; + register unsigned int ad, i, j, k; + u_char c; + u_char line[50+16+1]; static u_char binhex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - memset (line, ' ', sizeof line); - line[sizeof (line)-1] = 0; - for (ad=i=j=k=0; i<length; i++) { - c = *cp++; - line[j++] = binhex[c>>4]; - line[j++] = binhex[c&0xf]; - j++; + if (print_hex_data_start != NULL) + (*print_hex_data_start)(fh); + ad = 0; + j = 0; + k = 0; + memset(line, ' ', sizeof line); + line[sizeof (line)-1] = '\0'; + for (i=0; i<length; i++) { + c = *cp++; + line[j++] = binhex[c>>4]; + line[j++] = binhex[c&0xf]; + j++; if (encoding == CHAR_EBCDIC) { c = EBCDIC_to_ASCII1(c); } - line[50+k++] = c >= ' ' && c < 0x7f ? c : '.'; - if ((i & 15) == 15) { - fprintf (fh, "\n%04x %s", ad, line); - /*if (i==15) printf (" %d", length);*/ - memset (line, ' ', sizeof line); - line[sizeof (line)-1] = j = k = 0; - ad += 16; - } - } - - if (line[0] != ' ') fprintf (fh, "\n%04x %s", ad, line); - fprintf(fh, "\n"); - return; + line[50+k++] = c >= ' ' && c < 0x7f ? c : '.'; + if ((i & 15) == 15) { + (*print_hex_data_line)(fh, ad, line); + ad += 16; + j = 0; + k = 0; + memset(line, ' ', sizeof line); + line[sizeof (line)-1] = '\0'; + } + } + + if (line[0] != ' ') + (*print_hex_data_line)(fh, ad, line); + if (print_hex_data_end != NULL) + (*print_hex_data_end)(fh); +} +static void +print_hex_data_line_text(FILE *fh, u_int ad, u_char *line) +{ + fprintf(fh, "\n%04x %s", ad, line); +} + +static void +print_hex_data_end_text(FILE *fh) +{ + fprintf(fh, "\n"); +} + +static +void print_hex_data_text(FILE *fh, register const u_char *cp, + register u_int length, char_enc encoding) +{ + print_hex_data_common(fh, cp, length, encoding, NULL, + print_hex_data_line_text, print_hex_data_end_text); } #define MAX_LINE_LENGTH 256 @@ -372,45 +405,21 @@ void ps_clean_string(unsigned char *out, const unsigned char *in, } } -static -void print_hex_data_ps(FILE *fh, register const u_char *cp, - register u_int length, char_enc encoding) +static void +print_hex_data_line_ps(FILE *fh, u_int ad, u_char *line) { - register unsigned int ad, i, j, k; - u_char c; - u_char line[50+16+1]; - static u_char binhex[16] = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; u_char psline[MAX_LINE_LENGTH]; - print_ps_hex(fh); - memset (line, ' ', sizeof line); - line[sizeof (line)-1] = 0; - for (ad=i=j=k=0; i<length; i++) { - c = *cp++; - line[j++] = binhex[c>>4]; - line[j++] = binhex[c&0xf]; - j++; - if (encoding == CHAR_EBCDIC) { - c = EBCDIC_to_ASCII1(c); - } - line[50+k++] = c >= ' ' && c < 0x7f ? c : '.'; - if ((i & 15) == 15) { - ps_clean_string(psline, line, MAX_LINE_LENGTH); - fprintf (fh, "(%04x %s) hexdump\n", ad, psline); - memset (line, ' ', sizeof line); - line[sizeof (line)-1] = j = k = 0; - ad += 16; - } - } - - if (line[0] != ' ') { - ps_clean_string(psline, line, MAX_LINE_LENGTH); - fprintf (fh, "(%04x %s) hexdump\n", ad, psline); - } - return; + ps_clean_string(psline, line, MAX_LINE_LENGTH); + fprintf(fh, "(%04x %s) hexdump\n", ad, psline); +} +static +void print_hex_data_ps(FILE *fh, register const u_char *cp, + register u_int length, char_enc encoding) +{ + print_hex_data_common(fh, cp, length, encoding, + print_ps_hex, print_hex_data_line_ps, NULL); } void print_line(FILE *fh, gint format, char *line) |