Very good start, but I think some more can be squeezed out of it.
my $buffer; my $delim = '1234'; my $delim_len = length $delim; my $chunk_len = 65536 - $delim_len; my $read_len = read $fh, $buffer, $delim_len; my $pos = 0; while ($read_len) { my $rel_pos = -1; print "delim at offset: ", $pos + $rel_pos while ($rel_pos = index $buffer, $delim, $rel_pos + 1) > -1; $buffer = substr $buffer, -$delim_len; $pos += $read_len; $read_len = read $fh, $buffer, $chunk_len, $delim_len; }
A slower, but single pass alternative would be to shift the buffer back every time we find a match.
my $buffer = ''; my $delim = '1234'; my $chunk_len = 65536; my $delim_len = length $delim; read $fh, $buffer, $chunk_len, length $buffer; my $rel_pos = -1; while (length $buffer) { $rel_pos = index $buffer, $delim, $rel_pos + 1; if($rel_pos > -1) { do_checks_on(substr $buffer, 0, $rel_pos - 1); $buffer = substr $buffer, $rel_pos + $delim_len; } else { $buffer = substr $buffer, -$delim_len; } read $fh, $buffer, $chunk_len - length $buffer, length $buffer; }

Warning: this is untested code. I don't see any glaring mistakes though. It pulls the match to the front of the buffer, refills the back of the buffer and then looks for where the next match is. If none is found, it takes another whole bufferfull bite out of the file.

Both of these snippets pay careful attention to always copy the last $delim_len bytes to the front of the buffer, reading the next load into the buffer at that offset, so any delimiters falling across the top boundary of the buffer are not a concern.

Makeshifts last the longest.


In reply to Re^2: How do I search this binary file? by Aristotle
in thread How do I search this binary file? by John M. Dlugosz

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.