finding a binary pattern in a file.

Discussion in 'Perl Misc' started by Shashank Khanvilkar, Sep 20, 2005.

  1. Hi,
    I want to write a simple program that can search for a 13 _BIT_ pattern
    in a file opened in a binary mode. Is there a simple way of doing this?
    for example, can someone complete the below program code.

    open(IN,"$inFile");
    binmode(IN);

    while (<IN>) //while there is still data in the file
    {
    Read the IN file for the 13 bit pattern
    if found{
    ..do something
    }
    }
    Thanks
    Shashank
     
    Shashank Khanvilkar, Sep 20, 2005
    #1
    1. Advertising

  2. Shashank Khanvilkar

    Guest

    Shashank Khanvilkar <> wrote:
    > Hi,
    > I want to write a simple program that can search for a 13 _BIT_ pattern
    > in a file opened in a binary mode. Is there a simple way of doing this?
    > for example, can someone complete the below program code.


    Is the file much smaller than memory? I'll assume not...

    > open(IN,"$inFile");
    > binmode(IN);
    >
    > while (<IN>) //while there is still data in the file
    > {
    > Read the IN file for the 13 bit pattern
    > if found{
    > ..do something
    > }
    > }
    > Thanks
    > Shashank


    warn "Untested Code";
    my $thing_to_find = some_string_of_ones_and_zeros();
    my $count=0;
    my $buffer='';
    while (<IN>) { #presumably, $/ is set to something appropriate
    $buffer .= unpack "B*", $_;
    my $i=index $buffer, $thing_to_find;
    unless ($i == -1) {
    print "Found at ", ($i+$count), "\n";
    };
    $count+= length ( substr $buffer, 0, -length($thing_to_find),'');
    };

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Sep 20, 2005
    #2
    1. Advertising

  3. Shashank Khanvilkar

    News KF Guest

    Hi,

    I think this solution should work.
    However $buffer would grow during the parsing and could therefore blow
    up the memory.


    If the file is big and there is no delimiter, then you should probably
    use the read statement in order to read junks of data.

    Additionally you had to be sure, that the search pattern is not in the
    part, that is wrapping (pattern is half at end of previous junk and half
    at beginning of new junk).



    the code below should roughly work: (I did not try it, so some errors
    may be in):

    my $searchspace="";
    my $chunksize = 1024; # 1 kB in one chunk
    my $hitcount=0;
    my $positioninstream=0;

    while (!eof(IN)){
    read($chunk,$chunksize,$_); #### please check exact order of
    ### read or sysread. Don't know it by
    ### heart
    $searchspace .= unpack("B*", $_); #
    my $i;
    do {
    $i=index($searchspace,$thing_to_find);
    if($i>=0){
    print "Found at ", ($i+$positioninstream), "\n";
    $positioninstream += $i+length($thing_to_find);
    $searchspace =
    substr($searchspace,$i+$length($thing_to_find));
    }
    } while $i>=0;
    }


    bye


    nkf.


    P.S. This suggested solution is not optimized for speed thoug.

    wrote:
    > Shashank Khanvilkar <> wrote:
    >
    >>Hi,
    >>I want to write a simple program that can search for a 13 _BIT_ pattern
    >>in a file opened in a binary mode. Is there a simple way of doing this?
    >>for example, can someone complete the below program code.

    >
    >
    > Is the file much smaller than memory? I'll assume not...
    >
    >
    >>open(IN,"$inFile");
    >>binmode(IN);
    >>
    >>while (<IN>) //while there is still data in the file
    >>{
    >> Read the IN file for the 13 bit pattern
    >> if found{
    >> ..do something
    >> }
    >>}
    >>Thanks
    >>Shashank

    >
    >
    > warn "Untested Code";
    > my $thing_to_find = some_string_of_ones_and_zeros();
    > my $count=0;
    > my $buffer='';
    > while (<IN>) { #presumably, $/ is set to something appropriate
    > $buffer .= unpack "B*", $_;
    > my $i=index $buffer, $thing_to_find;
    > unless ($i == -1) {
    > print "Found at ", ($i+$count), "\n";
    > };
    > $count+= length ( substr $buffer, 0, -length($thing_to_find),'');
    > };
    >
    > Xho
    >
     
    News KF, Sep 20, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Tan via JavaKB.com

    Search for byte pattern in a binary file.

    Ryan Tan via JavaKB.com, Nov 18, 2004, in forum: Java
    Replies:
    20
    Views:
    1,956
    Thomas Weidenfeller
    Nov 19, 2004
  2. yaipa
    Replies:
    13
    Views:
    734
    yaipa
    Jan 19, 2005
  3. Reading binary file finding EOF

    , Dec 13, 2004, in forum: C Programming
    Replies:
    11
    Views:
    656
    Lawrence Kirby
    Dec 14, 2004
  4. vizzz
    Replies:
    16
    Views:
    1,345
  5. rob s.
    Replies:
    5
    Views:
    154
    David Jacobs
    Feb 25, 2011
Loading...

Share This Page