in reply to Re: how to search binary file
in thread how to search binary file

No need to slurp the whole thing. Just set $/ to whatever you want to look for and see if it chomps.
my $fi = "foo\xd9bar\xd9baz\xd9nothing"; open IN, '<', \$fi or die "Die fi!\n"; $/ = "\xd9"; while (<IN>) { print chomp() ? "Found after $_\n" : "Not after $_\n"; }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^3: how to search binary file
by ikegami (Patriarch) on Apr 25, 2005 at 17:18 UTC

    Of course, if the pattern is not found (or found late), your solution still slurps the whole file. The following solution keeps at most {buffer size}+{pattern length)-1 bytes of the file in memory at a time.

    my $pattern = "\xFF\xD9"; open(FILE, "< $file") or die("Unable to open input file: $!\n"); binmode(FILE); # Disable "\n" translation. $/ = \4096; # Arbitrary buffer size. my $block; my $partial = ''; my $base = 0; my $matches = 0; while ($block = <FILE>) { my $lookbehind = $partial . $block; my $pos = -1; for (;;) { $pos = index($lookbehind, $pattern, $offset+1); last if $pos == -1; print("Found a match at ", $base+$pos-length($partial), "\n"); $matches++; } $partial = substr($block, 1 - length($pattern)); $base += length($block); } if ($matches) { print("Found $matches matches.\n"); } else { print("Pattern not found.\n"); }

    Will report overlapping matches.

    Untested.