Hi All,
I need to look for a sequence of hex characters in a binary file and
remove them. the binary file has 00 00 02 02 01 00 sequence somewhere
in the file.
The script should open the file and look for this sequence 00 00 02 02
01 00 <18 variable bytes> and remove the 18 + 6 = 24 bytes from the
file.can someone please help. I can open the binary file and buffer
byte by byte but since the pattern can be anywhere in the file i dont
know how to proceed
regards
venkat
Here's the same example in binary mode (ie: the dummy file
is random binary, with the binary sequence embedded).
If this doesen't work for you, something else is wrong.
-sln
-------------------------
use strict;
use warnings;
my $sequence = "\x{00}\x{00}\x{02}\x{02}\x{01}\x{00}";
# or = pack('C*', 0x00, 0x00, 0x02, 0x02, 0x01, 0x00);
# Create dummy random binary file with embeded sequence
# ##
open my $ftest, '>:raw', 'dummy.bin' or die "can't create dummy.bin: $!";
for (1 .. 12_000) {
if ($_ == 2000) {
print $ftest $sequence;
} else {
print $ftest chr(int(rand(256)) & 0xff);
}
}
close $ftest;
# Read in binary, look for sequence, remove then write to file
# ##
open my $fin, '<:raw', 'dummy.bin' or die "can't open input file: $!";
open my $fout, '>:raw', 'dummy_o.bin' or die "can't open output file: $!";
my ($chunksize, $found) = (1024,0);
{
local $/ = \$chunksize;
my ($keep, $buf, $data) = (50,'','');
while (defined ($data = <$fin>)) {
$buf .= $data;
if (!$found) {
if ($buf =~ s/($sequence)(.{18})//s) {
print "Found sequence: ".ordsplit($1)."\n";
print "Next 18 bytes : ".ordsplit($2)."\n";
print "Sequence + 18 bytes, removed!\n";
$found = 1;
}
}
print $fout substr( $buf, 0, -$keep, "");
}
print $fout $buf;
}
if (!$found) {
print "Did not match sequence: '\$sequence.{18}'\n";
}
close $fout;
close $fin;
## End of program
exit 0;
sub ordsplit
{
my $string = shift;
my $buf = '';
for (map {ord $_} split //, $string) {
$buf.= sprintf ("%02x ",$_);
}
return $buf;
}
__END__
Found sequence: 00 00 02 02 01 00
Next 18 bytes : 25 6f e4 7e 6e fb fe 1e 47 af e6 2e 50 3f 31 54
dd 51
Sequence + 18 bytes, removed!