Use common code to print the hex dump as text and PostScript.
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Fri, 21 Jun 2002 23:52:47 +0000 (23:52 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Fri, 21 Jun 2002 23:52:47 +0000 (23:52 +0000)
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@5727 f5534014-38df-0310-8fa8-9805f1628bb7

print.c

diff --git a/print.c b/print.c
index b0cdc25eb25848ecab7fe111d64a169bfbbbd138..364fb94c1f97a8ef67988bee5a9bed6f8e28eb41 100644 (file)
--- a/print.c
+++ b/print.c
@@ -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)