rochlin has asked for the wisdom of the Perl Monks concerning the following question:
My natural thought was to do a regexp in Perl. I'm running activestate Perl 5.8 on Windows, so I know I have to use binmode to deal with binary files. Some trial and error revealed that to do anything with a regexp with a binary search I have to escape all of the interesting characters in the clip I'm searching for. Here's some code just to try to get the basic concept working:
#!/usr/bin/perl -w use strict; my $wordsound = 'balks.wav'; my $lettersound = 'a.wav'; my $letter; open (WORDSOUND, $wordsound) or die "can't open $wordsound: $!"; binmode (WORDSOUND); open (LETTERSOUND, $lettersound) or die "can't open $lettersound: $!"; binmode (LETTERSOUND); #actual data starts on byte 4097 seek (LETTERSOUND, 4097,0); #100 bytes ought to be OK for identifying the clip my $error = read (LETTERSOUND, $letter, 100); my $word = <WORDSOUND>; #escape all potentially nasty characters with \'s #I'm not sure if I'm escaping everything I need to (?) $letter =~ s/(\\|\||\(|\)|\[|\{|\^|\$|\*|\+|\?|\.)/\\$&/gsm; $word =~ /$letter/gms; print pos($word);
If anyone has ever looked for a binary string within a binary file and has any code, that'd be great. I would have thought it would be a pretty routine thing, but googling didn't yield much. I didn't see it in the Perl Monks search either. Hmmmm.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: searching for a binary string in a binary file
by Zaxo (Archbishop) on Dec 12, 2004 at 00:55 UTC | |
by thor (Priest) on Dec 12, 2004 at 06:24 UTC | |
by rochlin (Acolyte) on Dec 12, 2004 at 02:18 UTC | |
|
Re: searching for a binary string in a binary file
by Mr. Muskrat (Canon) on Dec 13, 2004 at 04:00 UTC |