[ale] Binary Grep

Joe Steele joe at madewell.com
Wed Nov 17 12:05:15 EST 2004


On Tuesday, November 16, 2004 9:32 PM, Fletch wrote:
>
> $ perl -le 'print "a" x 2048, "b" x 512, "\xff" x 16, "b" x 512' > foo
> $ perl -lne 'BEGIN{$/=\1024} print "hit byte ", ($.-1) * 1024 + $-[0] if /\xff{16}/' foo
> hit byte 2560
> $ perl -le 'print "a" x 2048, "b" x 1512, "\xff" x 16, "b" x 512' > foo
> $ perl -lne 'BEGIN{$/=\1024} print "hit byte ", ($.-1) * 1024 + $-[0] if /\xff{16}/' foo
> hit byte 3560
>

But what about:

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

^^^^^^ No hit byte found :(

Not being fluent in perl, I have no idea how this would be fixed.

Yet another alternative would be to create a simple scanner using 
flex.  Put the following in a file "scan.lex", then run "flex 
scan.lex; gcc lex.yy.c -o scan".  The result is an executable called 
"scan" which reads data from standard input. 

--Joe

%{
#include <stdio.h>
int  fpos = 0;
%}
%option noyywrap
%option nounput

%%
\xff{16}     printf ("Offset: %d\n", fpos); fpos+= 16;
.|\n       ++fpos;

%%
void main(void)
{
        yylex();
}






More information about the Ale mailing list