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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.