Use common code to print the hex dump as text and PostScript.

svn path=/trunk/; revision=5727
This commit is contained in:
Guy Harris 2002-06-21 23:52:47 +00:00
parent 9e92272188
commit b2277c9cfc
1 changed files with 71 additions and 62 deletions

133
print.c
View File

@ -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;
}
}
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] != ' ') fprintf (fh, "\n%04x %s", ad, line);
fprintf(fh, "\n");
return;
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_line_ps(FILE *fh, u_int ad, u_char *line)
{
u_char psline[MAX_LINE_LENGTH];
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)
{
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;
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)