nfctool: sniffer: Reduce hexa dump line width
authorThierry Escande <thierry.escande@linux.intel.com>
Thu, 2 May 2013 16:19:28 +0000 (18:19 +0200)
committerSamuel Ortiz <sameo@linux.intel.com>
Fri, 3 May 2013 08:17:14 +0000 (10:17 +0200)
If we display less than 0xFFFF bytes (which will be the case 99.999%
of the time) the offset value is truncated and the 2 first zero bytes
are not displayed.

tools/nfctool/sniffer.c

index 8a6de03..7102e1a 100644 (file)
@@ -149,7 +149,7 @@ static void pcap_file_cleanup(void)
 
 
 #define LINE_SIZE (10 + 3 * 16 + 2 + 18 + 1)
-#define HUMAN_READABLE_OFFSET 59
+#define HUMAN_READABLE_OFFSET 55
 
 /*
  * Dumps data in Hex+ASCII format as:
@@ -166,6 +166,9 @@ void sniffer_print_hexdump(FILE *file, guint8 *data, guint32 len,
        guint32 output_len;
        gchar line[LINE_SIZE];
        gchar *hexa = NULL, *human = NULL;
+       guint8 offset_len;
+       guint8 human_offset;
+       gchar *fmt;
 
        if (len == 0)
                return;
@@ -179,6 +182,16 @@ void sniffer_print_hexdump(FILE *file, guint8 *data, guint32 len,
        else
                output_len = len;
 
+       if (output_len > 0xFFFF) {
+               offset_len = 8;
+               human_offset = HUMAN_READABLE_OFFSET + 4;
+               fmt = "%08X: ";
+       } else {
+               offset_len = 4;
+               human_offset = HUMAN_READABLE_OFFSET;
+               fmt = "%04X: ";
+       }
+
        if (print_len) {
                if (indent)
                        fprintf(file, "%*c", indent, ' ');
@@ -188,14 +201,15 @@ void sniffer_print_hexdump(FILE *file, guint8 *data, guint32 len,
 
        while (total < output_len) {
                if (digits == 0) {
-                       memset(line, ' ', HUMAN_READABLE_OFFSET);
+                       memset(line, ' ', human_offset);
+
+                       sprintf(line, fmt, offset);
 
-                       sprintf(line, "%08X: ", offset);
                        offset += 16;
 
-                       hexa = line + 8 + 2;
+                       hexa = line + offset_len + 2;
 
-                       human = line + HUMAN_READABLE_OFFSET;
+                       human = line + human_offset;
                        *human++ = '|';
                }