perl scripts to convert packet data formats, some example packets
Stefan Schuermans

Stefan Schuermans commited on 2012-03-24 13:57:41
Showing 11 changed files, with 451 additions and 2 deletions.

... ...
@@ -0,0 +1,10 @@
1
+unsigned char c_data[] = {
2
+  0x00, 0x1D, 0x60, 0xDC, 0x75, 0x2D, 0x02, 0x4D,
3
+  0x49, 0x50, 0x53, 0x01, 0x08, 0x06, 0x00, 0x01,
4
+  0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x02, 0x4D,
5
+  0x49, 0x50, 0x53, 0x01, 0xC0, 0xA8, 0x00, 0x59,
6
+  0x00, 0x1D, 0x60, 0xDC, 0x75, 0x2D, 0xC0, 0xA8,
7
+  0x00, 0x0A, 0x01, 0x00, 0x37, 0x00, 0x00, 0x00,
8
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9
+  0x00, 0x00, 0x00, 0x00
10
+};
... ...
@@ -0,0 +1,144 @@
1
+ethernet TX: 5
2
+ethernet TX: 5
3
+ethernet TX: 5
4
+ethernet TX: 5
5
+ethernet TX: 5
6
+ethernet TX: 5
7
+ethernet TX: 5
8
+ethernet TX: 5
9
+ethernet TX: 5
10
+ethernet TX: 5
11
+ethernet TX: 5
12
+ethernet TX: 5
13
+ethernet TX: 5
14
+ethernet TX: 5
15
+ethernet TX: 5
16
+ethernet TX: 13
17
+ethernet TX: 0
18
+ethernet TX: 0
19
+ethernet TX: 13
20
+ethernet TX: 1
21
+ethernet TX: 0
22
+ethernet TX: 6
23
+ethernet TX: 12
24
+ethernet TX: 13
25
+ethernet TX: 5
26
+ethernet TX: 7
27
+ethernet TX: 13
28
+ethernet TX: 2
29
+ethernet TX: 2
30
+ethernet TX: 0
31
+ethernet TX: 13
32
+ethernet TX: 4
33
+ethernet TX: 9
34
+ethernet TX: 4
35
+ethernet TX: 0
36
+ethernet TX: 5
37
+ethernet TX: 3
38
+ethernet TX: 5
39
+ethernet TX: 1
40
+ethernet TX: 0
41
+ethernet TX: 8
42
+ethernet TX: 0
43
+ethernet TX: 6
44
+ethernet TX: 0
45
+ethernet TX: 0
46
+ethernet TX: 0
47
+ethernet TX: 1
48
+ethernet TX: 0
49
+ethernet TX: 8
50
+ethernet TX: 0
51
+ethernet TX: 0
52
+ethernet TX: 0
53
+ethernet TX: 6
54
+ethernet TX: 0
55
+ethernet TX: 4
56
+ethernet TX: 0
57
+ethernet TX: 0
58
+ethernet TX: 0
59
+ethernet TX: 2
60
+ethernet TX: 0
61
+ethernet TX: 2
62
+ethernet TX: 0
63
+ethernet TX: 13
64
+ethernet TX: 4
65
+ethernet TX: 9
66
+ethernet TX: 4
67
+ethernet TX: 0
68
+ethernet TX: 5
69
+ethernet TX: 3
70
+ethernet TX: 5
71
+ethernet TX: 1
72
+ethernet TX: 0
73
+ethernet TX: 0
74
+ethernet TX: 12
75
+ethernet TX: 8
76
+ethernet TX: 10
77
+ethernet TX: 0
78
+ethernet TX: 0
79
+ethernet TX: 9
80
+ethernet TX: 5
81
+ethernet TX: 0
82
+ethernet TX: 0
83
+ethernet TX: 13
84
+ethernet TX: 1
85
+ethernet TX: 0
86
+ethernet TX: 6
87
+ethernet TX: 12
88
+ethernet TX: 13
89
+ethernet TX: 5
90
+ethernet TX: 7
91
+ethernet TX: 13
92
+ethernet TX: 2
93
+ethernet TX: 0
94
+ethernet TX: 12
95
+ethernet TX: 8
96
+ethernet TX: 10
97
+ethernet TX: 0
98
+ethernet TX: 0
99
+ethernet TX: 10
100
+ethernet TX: 0
101
+ethernet TX: 1
102
+ethernet TX: 0
103
+ethernet TX: 0
104
+ethernet TX: 0
105
+ethernet TX: 7
106
+ethernet TX: 3
107
+ethernet TX: 0
108
+ethernet TX: 0
109
+ethernet TX: 0
110
+ethernet TX: 0
111
+ethernet TX: 0
112
+ethernet TX: 0
113
+ethernet TX: 0
114
+ethernet TX: 0
115
+ethernet TX: 0
116
+ethernet TX: 0
117
+ethernet TX: 0
118
+ethernet TX: 0
119
+ethernet TX: 0
120
+ethernet TX: 0
121
+ethernet TX: 0
122
+ethernet TX: 0
123
+ethernet TX: 0
124
+ethernet TX: 0
125
+ethernet TX: 0
126
+ethernet TX: 0
127
+ethernet TX: 0
128
+ethernet TX: 0
129
+ethernet TX: 0
130
+ethernet TX: 0
131
+ethernet TX: 0
132
+ethernet TX: 0
133
+ethernet TX: 0
134
+ethernet TX: 0
135
+ethernet TX: 0
136
+ethernet TX: 0
137
+ethernet TX: 8
138
+ethernet TX: 12
139
+ethernet TX: 14
140
+ethernet TX: 15
141
+ethernet TX: 11
142
+ethernet TX: 10
143
+ethernet TX: 10
144
+ethernet TX: 12
... ...
@@ -0,0 +1,21 @@
1
+    TYPE t_eth_data IS ARRAY(0 TO 144 - 1) OF std_logic_vector(3 DOWNTO 0);
2
+    CONSTANT eth_data: t_eth_data := (
3
+        X"5", X"5", X"5", X"5", X"5", X"5", X"5", X"5",
4
+        X"5", X"5", X"5", X"5", X"5", X"5", X"5", X"D",
5
+        X"0", X"0", X"D", X"1", X"0", X"6", X"C", X"D",
6
+        X"5", X"7", X"D", X"2", X"2", X"0", X"D", X"4",
7
+        X"9", X"4", X"0", X"5", X"3", X"5", X"1", X"0",
8
+        X"8", X"0", X"6", X"0", X"0", X"0", X"1", X"0",
9
+        X"8", X"0", X"0", X"0", X"6", X"0", X"4", X"0",
10
+        X"0", X"0", X"2", X"0", X"2", X"0", X"D", X"4",
11
+        X"9", X"4", X"0", X"5", X"3", X"5", X"1", X"0",
12
+        X"0", X"C", X"8", X"A", X"0", X"0", X"9", X"5",
13
+        X"0", X"0", X"D", X"1", X"0", X"6", X"C", X"D",
14
+        X"5", X"7", X"D", X"2", X"0", X"C", X"8", X"A",
15
+        X"0", X"0", X"A", X"0", X"1", X"0", X"0", X"0",
16
+        X"7", X"3", X"0", X"0", X"0", X"0", X"0", X"0",
17
+        X"0", X"0", X"0", X"0", X"0", X"0", X"0", X"0",
18
+        X"0", X"0", X"0", X"0", X"0", X"0", X"0", X"0",
19
+        X"0", X"0", X"0", X"0", X"0", X"0", X"0", X"0",
20
+        X"8", X"C", X"E", X"F", X"B", X"A", X"A", X"C"
21
+        );
... ...
@@ -0,0 +1,8 @@
1
+unsigned char c_data[] = {
2
+  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x03,
3
+  0x04, 0x05, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01,
4
+  0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x1D,
5
+  0x60, 0xDC, 0x75, 0x2D, 0xC0, 0xA8, 0x00, 0x0A,
6
+  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0xA8,
7
+  0x00, 0x59
8
+};
... ...
@@ -0,0 +1,17 @@
1
+    TYPE t_eth_data IS ARRAY(0 TO 108 - 1) OF std_logic_vector(3 DOWNTO 0);
2
+    CONSTANT eth_data: t_eth_data := (
3
+        X"5", X"5", X"5", X"5", X"5", X"5", X"5", X"5",
4
+        X"5", X"5", X"5", X"5", X"5", X"5", X"5", X"D",
5
+        X"F", X"F", X"F", X"F", X"F", X"F", X"F", X"F",
6
+        X"F", X"F", X"F", X"F", X"2", X"0", X"3", X"0",
7
+        X"4", X"0", X"5", X"0", X"6", X"0", X"7", X"0",
8
+        X"8", X"0", X"6", X"0", X"0", X"0", X"1", X"0",
9
+        X"8", X"0", X"0", X"0", X"6", X"0", X"4", X"0",
10
+        X"0", X"0", X"1", X"0", X"0", X"0", X"D", X"1",
11
+        X"0", X"6", X"C", X"D", X"5", X"7", X"D", X"2",
12
+        X"0", X"C", X"8", X"A", X"0", X"0", X"A", X"0",
13
+        X"F", X"F", X"F", X"F", X"F", X"F", X"F", X"F",
14
+        X"F", X"F", X"F", X"F", X"0", X"C", X"8", X"A",
15
+        X"0", X"0", X"9", X"5", X"0", X"E", X"8", X"0",
16
+        X"8", X"C", X"1", X"B"
17
+        );
... ...
@@ -0,0 +1,100 @@
1
+#! /usr/bin/perl
2
+
3
+use strict;
4
+use warnings;
5
+
6
+require("crc32.pl");
7
+
8
+# parse C data
9
+my @bytes = ();
10
+while (my $line = <>) {
11
+  chomp $line;
12
+  # tcpdump -XX
13
+  if ($line =~ /^ *[0-9A-FXa-fx]+: +([0-9A-Fa-f ]+)/) {
14
+    my $data = $1;
15
+    $data =~ s/  .*$//; # remove ASCII in case the begin was [0-9A-Fa-f]+
16
+    $data =~ s/[^0-9A-Fa-f]//g;
17
+    $data =~ s/(..)/$1,/g;
18
+    for my $part (split(/,/, $data)) {
19
+      if ($part =~ /^([0-9A-Fa-f]{2})/) {
20
+        my $val = $1;
21
+        push (@bytes, hex($val));
22
+      }
23
+    }
24
+  }
25
+  # C data
26
+  elsif ($line =~ /^ *([0-9A-FXa-fx, ]+)/) {
27
+    my $data = $1;
28
+    for my $part (split(/,/, $data)) {
29
+      if ($part =~ /^ *0[xX]([0-9A-Fa-f]{1,2})/) {
30
+        my $val = $1;
31
+        push (@bytes, hex($val));
32
+      }
33
+    }
34
+  }
35
+}
36
+
37
+# add CRC
38
+if (@bytes >= 4) {
39
+  my @bytes_no_crc = @bytes;
40
+  my @crc_bytes = ();
41
+  unshift (@crc_bytes, pop(@bytes_no_crc));
42
+  unshift (@crc_bytes, pop(@bytes_no_crc));
43
+  unshift (@crc_bytes, pop(@bytes_no_crc));
44
+  unshift (@crc_bytes, pop(@bytes_no_crc));
45
+  my $crc_calc = crc32([@bytes_no_crc]);
46
+  my $mismatch = 0;
47
+  for (my $i = 0; $i < 4; $i++) {
48
+    if (shift(@crc_bytes) != shift(@{$crc_calc})) {
49
+      $mismatch = 1;
50
+    }
51
+  }
52
+  if ($mismatch) {
53
+    my $crc = crc32([@bytes]);
54
+    push(@bytes, @{$crc});
55
+  } else {
56
+    print STDERR "warning: CRC32 already present -> not added\n";
57
+  }
58
+} else {
59
+  my $crc = crc32([@bytes]);
60
+  push(@bytes, @{$crc});
61
+}
62
+
63
+# add preamble
64
+my $pos = 0;
65
+while ($pos < @bytes && $bytes[$pos] == 0x55) {
66
+  $pos++;
67
+}
68
+if ($pos > 4 && $pos < @bytes && $bytes[$pos] == 0xD5) {
69
+  print STDERR "warning: preamble already present -> not added\n";
70
+} else {
71
+  unshift(@bytes, 0xD5);
72
+  for (my $i = 0; $i < 7; $i++) {
73
+    unshift(@bytes, 0x55);
74
+  }
75
+}
76
+
77
+# bytes to nibbles
78
+my @nibbles = ();
79
+foreach my $byte (@bytes) {
80
+  push(@nibbles, $byte & 0xF);
81
+  push(@nibbles, $byte >> 4 & 0xF);
82
+}
83
+
84
+# output VHDL data
85
+printf("    TYPE t_eth_data IS ARRAY(0 TO %u - 1) OF " . 
86
+       "std_logic_vector(3 DOWNTO 0);\n", @nibbles + 0);
87
+print("    CONSTANT eth_data: t_eth_data := (\n");
88
+for (my $pos = 0; $pos < @nibbles; ) {
89
+  print("       ");
90
+  for (my $i = 0; $i < 8 and $pos < @nibbles; ++$i, ++$pos) {
91
+    if ($pos + 1 < @nibbles) {
92
+      printf(" X\"%X\",", $nibbles[$pos]);
93
+    } else {
94
+      printf(" X\"%X\"", $nibbles[$pos]);
95
+    }
96
+  }
97
+  print("\n");
98
+}
99
+print("        );\n");
100
+
... ...
@@ -0,0 +1,39 @@
1
+#! /usr/bin/perl
2
+
3
+use strict;
4
+use warnings;
5
+
6
+# calculate CRC32
7
+sub crc32
8
+{
9
+  my $bytes = shift; # array reference
10
+
11
+  my @tab = (
12
+    0x77073096, 0xEE0E612C, 0x076DC419, 0x0EDB8832,
13
+    0x1DB71064, 0x3B6E20C8, 0x76DC4190, 0xEDB88320
14
+  );
15
+
16
+  my $crc = 0xFFFFFFFF;
17
+  for (my $i = 0; $i < @{$bytes}; ++$i) {
18
+    my $val = $crc ^ @{$bytes}[$i];
19
+    $crc >>= 8;
20
+    for (my $b = 0; $b < 8; ++$b) {
21
+      if ($val & 1 << $b) {
22
+        $crc ^= $tab[$b];
23
+      }
24
+    }
25
+  }
26
+  $crc ^= 0xFFFFFFFF;
27
+
28
+  $crc = [
29
+    $crc & 0xFF,
30
+    $crc >> 8 & 0xFF,
31
+    $crc >> 16 & 0xFF,
32
+    $crc >> 24 & 0xFF
33
+  ];
34
+
35
+  return $crc; # array reference
36
+}
37
+
38
+1;
39
+
... ...
@@ -3,27 +3,84 @@
3 3
 use strict;
4 4
 use warnings;
5 5
 
6
+require("crc32.pl");
7
+
8
+# parse eth TX log data
6 9
 my @nibbles = ();
7 10
 while (my $line = <>) {
8 11
   chomp $line;
9
-
12
+  # ethernet TX log
10 13
   if ($line =~ /^ethernet TX: ([0-9]+)$/) {
11 14
     my $nibble = $1;
12 15
     push (@nibbles, $nibble & 0x0F);
13 16
   }
17
+  # VHDL data
18
+  elsif ($line =~ /^ *([0-9A-FXa-fx", ]+)/) {
19
+    my $data = $1;
20
+    for my $part (split(/,/, $data)) {
21
+      if ($part =~ /^ *[xX]"([0-9A-Fa-f])"/) {
22
+        my $val = $1;
23
+        push (@nibbles, hex($val));
24
+      }
25
+    }
26
+  }
14 27
 }
15 28
 
29
+# nibbles to bytes
16 30
 my @bytes = ();
17 31
 for (my $i = 0; $i < @nibbles; $i += 2) {
18 32
   my $byte = $nibbles[$i + 1] << 4 | $nibbles[$i];
19 33
   push (@bytes, $byte);
20 34
 }
21 35
 
36
+# remove preamble
37
+my $pos = 0;
38
+while ($pos < @bytes && $bytes[$pos] == 0x55) {
39
+  $pos++;
40
+}
41
+if ($pos > 4 && $pos < @bytes && $bytes[$pos] == 0xD5) {
42
+  $pos++;
43
+  for ( ; $pos > 0; $pos--) {
44
+    shift(@bytes);
45
+  }
46
+} else {
47
+  print STDERR "warning: preamble not found -> not removed\n";
48
+}
49
+
50
+# remove CRC
51
+if (@bytes >= 4) {
52
+  my @bytes_no_crc = @bytes;
53
+  my @crc_bytes = ();
54
+  unshift (@crc_bytes, pop(@bytes_no_crc));
55
+  unshift (@crc_bytes, pop(@bytes_no_crc));
56
+  unshift (@crc_bytes, pop(@bytes_no_crc));
57
+  unshift (@crc_bytes, pop(@bytes_no_crc));
58
+  my $crc_calc = crc32([@bytes_no_crc]);
59
+  my $mismatch = 0;
60
+  for (my $i = 0; $i < 4; $i++) {
61
+    if (shift(@crc_bytes) != shift(@{$crc_calc})) {
62
+      $mismatch = 1;
63
+    }
64
+  }
65
+  if ($mismatch) {
66
+    print STDERR "warning: invalid CRC32 -> not removed\n";
67
+  } else {
68
+    @bytes = @bytes_no_crc;
69
+  }
70
+} else {
71
+  print STDERR "warning: too short for detecting CRC32 -> noting removed\n";
72
+}
73
+
74
+# print C data
22 75
 print("unsigned char c_data[] = {\n");
23 76
 for (my $pos = 0; $pos < @bytes; ) {
24 77
   print(" ");
25
-  for (my $i = 0; $i < 8 and $i < @bytes; ++$i, ++$pos) {
78
+  for (my $i = 0; $i < 8 and $pos < @bytes; ++$i, ++$pos) {
79
+    if ($pos + 1 < @bytes) {
26 80
       printf(" 0x%02X,", $bytes[$pos]);
81
+    } else {
82
+      printf(" 0x%02X", $bytes[$pos]);
83
+    }
27 84
   }
28 85
   print("\n");
29 86
 }
... ...
@@ -0,0 +1,15 @@
1
+unsigned char c_data[] = {
2
+  0x02, 0x4D, 0x49, 0x50, 0x53, 0x01, 0x00, 0x1D,
3
+  0x60, 0xDC, 0x75, 0x2D, 0x08, 0x00, 0x45, 0x00,
4
+  0x00, 0x54, 0x00, 0x00, 0x40, 0x00, 0x40, 0x01,
5
+  0xB8, 0xF5, 0xC0, 0xA8, 0x00, 0x0A, 0xC0, 0xA8,
6
+  0x00, 0x59, 0x08, 0x00, 0x8E, 0x57, 0x0F, 0x62,
7
+  0x00, 0x01, 0xE6, 0xB4, 0x6D, 0x4F, 0x00, 0x00,
8
+  0x00, 0x00, 0x44, 0x6E, 0x03, 0x00, 0x00, 0x00,
9
+  0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
10
+  0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
11
+  0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
12
+  0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
13
+  0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
14
+  0x36, 0x37
15
+};
... ...
@@ -0,0 +1,7 @@
1
+0x0000:  024d 4950 5301 001d 60dc 752d 0800 4500  .MIPS...`.u-..E.
2
+0x0010:  0054 0000 4000 4001 b8f5 c0a8 000a c0a8  .T..@.@.........
3
+0x0020:  0059 0800 8e57 0f62 0001 e6b4 6d4f 0000  .Y...W.b....mO..
4
+0x0030:  0000 446e 0300 0000 0000 1011 1213 1415  ..Dn............
5
+0x0040:  1617 1819 1a1b 1c1d 1e1f 2021 2223 2425  ...........!"#$%
6
+0x0050:  2627 2829 2a2b 2c2d 2e2f 3031 3233 3435  &'()*+,-./012345
7
+0x0060:  3637                                     67
... ...
@@ -0,0 +1,31 @@
1
+    TYPE t_eth_data IS ARRAY(0 TO 220 - 1) OF std_logic_vector(3 DOWNTO 0);
2
+    CONSTANT eth_data: t_eth_data := (
3
+        X"5", X"5", X"5", X"5", X"5", X"5", X"5", X"5",
4
+        X"5", X"5", X"5", X"5", X"5", X"5", X"5", X"D",
5
+        X"2", X"0", X"D", X"4", X"9", X"4", X"0", X"5",
6
+        X"3", X"5", X"1", X"0", X"0", X"0", X"D", X"1",
7
+        X"0", X"6", X"C", X"D", X"5", X"7", X"D", X"2",
8
+        X"8", X"0", X"0", X"0", X"5", X"4", X"0", X"0",
9
+        X"0", X"0", X"4", X"5", X"0", X"0", X"0", X"0",
10
+        X"0", X"4", X"0", X"0", X"0", X"4", X"1", X"0",
11
+        X"8", X"B", X"5", X"F", X"0", X"C", X"8", X"A",
12
+        X"0", X"0", X"A", X"0", X"0", X"C", X"8", X"A",
13
+        X"0", X"0", X"9", X"5", X"8", X"0", X"0", X"0",
14
+        X"E", X"8", X"7", X"5", X"F", X"0", X"2", X"6",
15
+        X"0", X"0", X"1", X"0", X"6", X"E", X"4", X"B",
16
+        X"D", X"6", X"F", X"4", X"0", X"0", X"0", X"0",
17
+        X"0", X"0", X"0", X"0", X"4", X"4", X"E", X"6",
18
+        X"3", X"0", X"0", X"0", X"0", X"0", X"0", X"0",
19
+        X"0", X"0", X"0", X"0", X"0", X"1", X"1", X"1",
20
+        X"2", X"1", X"3", X"1", X"4", X"1", X"5", X"1",
21
+        X"6", X"1", X"7", X"1", X"8", X"1", X"9", X"1",
22
+        X"A", X"1", X"B", X"1", X"C", X"1", X"D", X"1",
23
+        X"E", X"1", X"F", X"1", X"0", X"2", X"1", X"2",
24
+        X"2", X"2", X"3", X"2", X"4", X"2", X"5", X"2",
25
+        X"6", X"2", X"7", X"2", X"8", X"2", X"9", X"2",
26
+        X"A", X"2", X"B", X"2", X"C", X"2", X"D", X"2",
27
+        X"E", X"2", X"F", X"2", X"0", X"3", X"1", X"3",
28
+        X"2", X"3", X"3", X"3", X"4", X"3", X"5", X"3",
29
+        X"6", X"3", X"7", X"3", X"2", X"9", X"1", X"C",
30
+        X"0", X"1", X"2", X"C"
31
+        );
0 32