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:
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));
}
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?
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.