-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff-analyze.pl
executable file
·278 lines (221 loc) · 6.54 KB
/
ff-analyze.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/perl
use strict;
use warnings;
use Cwd qw( cwd );
use File::Basename;
use File::ReadBackwards;
use Getopt::Std;
use List::MoreUtils qw( uniq );
my $CWD = cwd( $0 );
my $HEADER_LENGTH_BYTES = 28;
my $FF_HEADER_LOCATION = "$CWD/ff-headers";
my $FF_DEFLATED_LOCATION = "$CWD/ff-deflated";
# COMMON UTILITY FUNCTIONS
sub parse_byte_range($)
{
my ( $byte_range_str ) = @_;
my @byte_range = split( /\s*,\s*/, $byte_range_str );
my @full_byte_range = (int 0) x $HEADER_LENGTH_BYTES;
foreach my $byte ( @byte_range )
{
if( $byte =~ /^0x[0-9a-fA-F]+$/ )
{
my $index = hex $byte;
$full_byte_range[$index] = 1;
}
elsif( $byte =~ /^(0x[0-9a-fA-F]+)\s*[-]\s*(0x[0-9a-fA-F]+)$/ )
{
my $start_index = hex $1;
my $end_index = hex $2;
foreach my $index ( int( $start_index ) .. int( $end_index ) )
{
$full_byte_range[$index] = 1;
}
}
else
{
return;
}
}
return \@full_byte_range;
}
sub read_header($)
{
my ( $ff_file ) = @_;
my $ff_file_basename = basename $ff_file;
my $header_file = "$FF_HEADER_LOCATION/$ff_file_basename.bin";
my $header = `od -t x1 $header_file`;
$header =~ s/\d{7}//g;
$header =~ s/^\s+|\s+$//g;
my @header = split( /\s+/, $header );
return \@header;
}
# COMMANDS
sub extract_headers($)
{
my ( $ff_files ) = @_;
foreach my $ff_file ( @$ff_files )
{
my $ff_extract_header = "$CWD/ff-extract-header.sh";
my $output = `$ff_extract_header -fp $ff_file`;
print "Extracted header: $output\n";
}
return;
}
sub deflate($)
{
my ( $ff_files ) = @_;
foreach my $ff_file ( @$ff_files )
{
my $ff_deflate = "$CWD/ff-deflate.sh";
my $output = `$ff_deflate -fp $ff_file`;
my @output_lines = split( /\r/, $output );
my $output_file = $output_lines[( scalar @output_lines ) - 1];
print "Deflated file: $output_file\n";
}
return;
}
sub print_offset_byte_frequencies($;$)
{
my ( $ff_files, $byte_range ) = @_;
my @indexes;
foreach my $ff_file ( @$ff_files )
{
my $header = read_header $ff_file;
push( @{$indexes[$_]}, $header->[$_] ) for 0 .. ( ( scalar @$header ) - 1 )
}
foreach my $i ( 0 .. ( ( scalar @indexes ) - 1 ) )
{
my @byte_list = @{$indexes[$i]};
my $frequencies = {};
foreach my $byte ( @byte_list )
{
if( !defined $frequencies->{$byte} )
{
$frequencies->{$byte} = 1;
}
else
{
$frequencies->{$byte}++;
}
}
my @unique_bytes = uniq @byte_list;
my $size = scalar @unique_bytes;
my $offset = sprintf( '0x%X', $i );
print "OFFSET $offset - $size UNIQUE BYTES:\n";
print " $_ ($frequencies->{$_})\n" for sort keys %$frequencies;
print "\n";
}
return;
}
sub print_header_and_file_size($;$)
{
my ( $ff_files, $byte_range ) = @_;
foreach my $ff_file ( @$ff_files )
{
my $file_basename = basename $ff_file;
my $file_size = -s "$FF_DEFLATED_LOCATION/$file_basename.bin";
my $header = read_header $ff_file;
my @header_to_print;
if( @$byte_range )
{
@header_to_print = grep { defined $_ } (
map
{ $byte_range->[$_] ? $header->[$_] : undef }
( 0 .. $HEADER_LENGTH_BYTES )
);
}
else
{
@header_to_print = @$header;
}
my $header_to_print_str = join( ' ', @header_to_print );
print "$ff_file\nFILE SIZE: $file_size bytes\nHEADER: $header_to_print_str\n\n";
}
return;
}
sub print_footer_errors($;$)
{
my ( $dumped_ff_files, $byte_range ) = @_;
foreach my $dumped_ff_file ( @$dumped_ff_files )
{
my $back = File::ReadBackwards->new( $dumped_ff_file )
or die "Error reading file $dumped_ff_file $!\n";
my $line;
my $do_loop = 1;
print "$dumped_ff_file:\n";
while( $do_loop && ( $line = $back->readline() ) )
{
if( $line =~ /^ERROR/ )
{
print $line;
}
elsif( $line =~ /^.+ERROR/ )
{
$line =~ s/^.+(ERROR.*)/$1/;
print $line;
$do_loop = 0;
}
my $unpacked = unpack( 'H*', $line );
if( $unpacked =~ /ffffffff/ )
{
$do_loop = 0;
}
}
print "\n";
}
return;
}
my $CMD_SWITCHES = {
'freq' => {
'sub' => \&print_offset_byte_frequencies,
desc => ( 'get the frequencies of bytes that occur at each offset ' .
'in the file headers.' ),
},
'size' => {
'sub' => \&print_header_and_file_size,
desc => 'print the size and header of each file.',
},
'errors' => {
'sub' => \&print_footer_errors,
desc => ( 'print any error messages that appear in the footer of ' .
'each file.' ),
},
'extract-header' => {
'sub' => \&extract_headers,
desc => 'extract headers from all fast file args.',
},
'deflate' => {
'sub' => \&deflate,
desc => 'deflate all fast file args.',
},
};
my $bname = basename $0;
my $usage = <<"EOL";
Usage: $bname -c <command> [-r <byte range>] <CoD4 pre-alpha root dir>
<command> can be any of the following:
[-r <byte range>] can be a comma-separated list of individual bytes
or byte ranges. Examples:
-r 0x2
-r 0xA,0xB
-r 0x1-0x3
-r 0x0-0x3,0x5-0x8,0xF
EOL
$usage .= " $_ - $CMD_SWITCHES->{$_}->{desc}\n" for keys %$CMD_SWITCHES;
my $opts = {};
getopts( 'c:r:', $opts );
my $command = $opts->{c} or die $usage;
my $command_sub = $CMD_SWITCHES->{$command}->{'sub'};
die "Invalid command: $command\n" unless $command_sub;
my $byte_range_str = $opts->{r};
my $byte_range = [];
if( defined $byte_range_str )
{
$byte_range = parse_byte_range $byte_range_str
or die "Invalid byte range: $byte_range_str\n";
}
my @ff_files = @ARGV;
my $num_files = scalar @ff_files;
print "Reading $num_files .ff files.\n\n";
$command_sub->( \@ff_files, $byte_range );
exit 0;