Expanding on zudes theme. Provided you have say 10 times more RAM that the file size then this is a very simple approach that should be quite speedy as well. If memory is an issue then you would need to implment a sliding buffer.

my $data = 'abacad'; # slurp in whole file into scalar my $pattern = 'a'; my $ds = unpack "B*", $data; my $ps = unpack "B*", $pattern; my $proof = '-' x length($ds); my $x = 0; while ( 1 ) { $x = index($ds,$ps,$x); last if $x == -1; print "Match at bit offset $x\n"; # modify proof string to show match, could extract surrounding # bits just as easily using our $x offset and substr() on $ds substr( $proof, $x, length($ps), $ps ); $x++; # move offset pointer to next bit to avoid endless loop } print " Original: $ds Matches: $proof\n"; __DATA__ Match at bit offset 0 Match at bit offset 16 Match at bit offset 32 Original: 011000010110001001100001011000110110000101100100 Matches: 01100001--------01100001--------01100001--------

cheers

tachyon


In reply to Re: Binary File Manipulation across Byte boundaries by tachyon
in thread Binary File Manipulation across Byte boundaries by tperdue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.