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 $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; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |