[ale] Binary Grep

Fletch fletch at phydeaux.org
Wed Nov 17 13:35:28 EST 2004


>>>>> "Greg" == Greg Freemyer <greg.freemyer at gmail.com> writes:

    
[...]


    Greg> I was so impressed with Perl and Fletch that it could do
    Greg> this in a single line.

    Greg> I don't speak Perl at all, so I did not realize he was only
    Greg> scanning 1K blocks and any patterns that crossed the
    Greg> boundary were ignored.  (I assume that is the issue you
    Greg> found.)

Hey, pay no attention to that match split across buffer boundaries. :)


That of course was the quick and dirty one-liner that catches most
cases.  (Yeah, that's the ticket . . .)  To really do it right you
want to append into a buffer and search that buffer each time, then
clear all but the last (patternlength-1) characters from your buffer
before the next iteration.


That'd look something akin to:

$ perl -le 'print "a" x 2047, "\xff" x 16, "b" x 512' > foo
$ perl -lne 'BEGIN{$/=\1024}$o=($.-1)*1024-length$b;$b.=$_;print "hit byte ", $o + $-[0] if $b =~ /\xff{16}/;$b=substr($b,-15,15)' foo
hit byte 2047
$ perl -le 'print "a" x 2040, "\xff" x 16, "b" x 512' > foo
$ perl -lne 'BEGIN{$/=\1024}$o=($.-1)*1024-length$b;$b.=$_;print "hit byte ", $o + $-[0] if $b =~ /\xff{16}/;$b=substr($b,-15,15)' foo
hit byte 2040


Of course genericising this (and dealing with figuring out the longest
possible length string that an arbitrary regex could match) is left as
an exercise for the reader . . . :)

-- 
Fletch                | "If you find my answers frightening,       __`'/|
fletch at phydeaux.org   |  Vincent, you should cease askin'          \ o.O'
                      |  scary questions." -- Jules                =(___)=
                      |                                               U



More information about the Ale mailing list