Shendal has asked for the wisdom of the Perl Monks concerning the following question:
Given a binary file, I want to search for a section of content, and return the next few bytes in the file. Currently, I am using something like this code:
BTW, the searching section of the code works very well. It's the conversion to hex that takes all the time.
Cheers,
Shendal
This is unbearably slow for large files. I'd like to search without having to convert to/from hex. Any ideas on how to accomplish this, or another way to speed up the parsing?my $file = '/path/to/binaryfile'; # string to match, in hex my $matchstr = '53005f00560045005200530049004f004e005f0049004e00460 +04f0000000000bd04effe00000100'; # read in file and convert to a textual (hex) representation open(FILE,"$file") or die "Unable to open file: $!\n"; binmode(FILE); my $binhex = ''; while (eof(FILE) != 1) { my $buf; my $num_byte_read = read(FILE,$buf,16); foreach ($buf =~ m/./gs) { $binhex .= sprintf("%02x",ord($_)); } } close(FILE); # search it if ($binhex =~ /$matchstr(\S\S\S\S)(\S\S\S\S)(\S\S\S\S)(\S\S\S\S)/) + { print "Found: " . join(',',($1,$2,$3,$4)); }
BTW, the searching section of the code works very well. It's the conversion to hex that takes all the time.
Cheers,
Shendal
Edit by tye
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Searching binary data
by dws (Chancellor) on Mar 14, 2002 at 18:18 UTC | |
by rnahi (Curate) on Mar 16, 2002 at 08:48 UTC | |
Re: Searching binary data
by talexb (Chancellor) on Mar 14, 2002 at 18:25 UTC | |
Re: Searching binary data
by Juerd (Abbot) on Mar 14, 2002 at 17:20 UTC | |
by theorbtwo (Prior) on Mar 14, 2002 at 17:55 UTC | |
by Juerd (Abbot) on Mar 14, 2002 at 17:58 UTC | |
by theorbtwo (Prior) on Mar 14, 2002 at 18:08 UTC | |
by Shendal (Hermit) on Mar 14, 2002 at 19:05 UTC | |
UNIX Strings Command? - Re: Searching binary data
by metadoktor (Hermit) on Mar 14, 2002 at 17:23 UTC | |
by Shendal (Hermit) on Mar 14, 2002 at 17:37 UTC |
Back to
Seekers of Perl Wisdom