-
Notifications
You must be signed in to change notification settings - Fork 14
/
rewrite-as.pl
112 lines (92 loc) · 2.22 KB
/
rewrite-as.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl
#
# Parse number-containing strings and rewrite them as either
# hex or binary strings.
#
# Input format:
# - bits, e.g.: 100100101
# - hex, e.g.: 0xcafecafecafecafe or deadbeefdeadbeef
# - hex, e.g.: 0x23, 0x5, 0x17
#
# Input data might contain spaces and comments (indicated by a hash mark '#')
#
# Output format:
# - bits, e.g., 1010010101010
# - hex, e.g.: ac1d
#
#
# Author: Martin Schobert <schobert@sitsec.net>
#
use strict;
use Data::Dumper;
use POSIX;
my $mode = shift;
my $file = shift;
my $verbose = 0;
if(not defined($file) or not( -f $file)) {
print "$0 [bits|hexbytes] <filename>\n";
exit(1);
}
# parse messages
my $msg_list = rewrite_messages($file);
foreach my $msg (@$msg_list) {
if($mode eq 'bits') {
print $msg, "\n";
}
elsif($mode eq 'hexbytes') {
print unpack("H*", pack("B*", $msg)), "\n";
}
}
sub line_to_bits {
my $line = shift;
# We cannot use the perl module Bit::Vector directly,
# because we have to know the vector's length. Thus,
# we have to preparse it by our own.
$line =~ s!\s*!!g;
# process comma seperated values, e.g.: 0x5, 0x23, 0x42
if($line =~ m!\,!) {
my $l = "0x";
foreach my $i (split(/\,/, $line)) {
if($i =~ m!0x(.*)!) {
$l .= sprintf("%02x", hex($1));
}
else {
die "Error: number [$i] in a value list [$line] is not in hex.\n";
}
}
$line = $l;
print "parsing pre-transformed message: [$line]\n" if($verbose);
}
my $bits;
# process binary string
if($line =~ m!^[01]+$!) {
$bits = $line;
}
# process hex strings possibly prefixed with 0x
elsif($line =~ m!^(0x)?([\da-f]+)$!i) {
my $h = $2;
my $hlen = length($h);
my $blen = $hlen * 4;
$bits = unpack("B$blen", pack("H$hlen", $h)); #XXX
}
else {
die "Error: Can't parse line [$line]\n";
}
return $bits;
}
sub rewrite_messages {
my $file = shift;
my @messages;
open(FILE, "< $file") or die "can't open file: $!\n";
while(defined(my $line = <FILE>)) {
chomp $line;
print "parsing line in message file: [$line]\n" if($verbose);
if($line =~ m!\s*\#!) {
# ignore comments
}
elsif($line =~ m![^\s]!) {
push @messages, line_to_bits($line);
}
}
return \@messages;
}