[ale] Binary Matching

Christopher Fowler cfowler at outpostsentinel.com
Mon Jun 6 11:02:27 EDT 2005


The only problem i see with that is the fact that the string of bytes my
span multiple freads()

Here is one way I did it in a serial comm progam

static void
check_for_escape(int fd, char c) {
  static char *ptr = NULL;

  if(ptr == NULL) {
    ptr = escape_string;
  }

  if(*ptr == c) {
    if(*(++ptr) == '\0') { /** Null Termination **/
        tcsetattr(0, TCSANOW, &local_old);
        printf("\n[Exiting]\n");
        exit(0);
    }
  } else {
    ptr = escape_string ;
  }
  write(fd, &c, 1);
  return;
}

Make escape_string the string of bytes you want to search for.
Do a read(fd,byte, 1) and the send that that check_for_escape().
The only problem with this is that it expects the escape_string  to be a
null-terminated string but you can easily modify that.  

We had an issue where when we would transfer files via XMODEM if escape
sequences were in the data string it would screw up communications.  We
wrote a special program that would allow the user to sepcify their own
escape sequence.  This would allow a user to dial into our device via
Hyperterm then use this program 'raw' to attach to any of the serial
ports in that device.  They then could transfer files via XMODEM through
hyperterm into raw and then out to the serial port that has the device. 
Useful for sending patches to Nortel PBX equipment via XMODEM.

To execute this program the user would simply do this
./raw -e 'LET ME OUT' /dev/ports/1

So if the user typed 'LET ME OUT" that would terminate the program.


On Mon, 2005-06-06 at 10:27, James Sumners wrote:
> Here is one way to do it (off the top of my head):
> 
> <?php
> $initial_offset = 10; // 10 bytes
> $bytes_to_read = 500; // 500 bytes
> $first_file = '/some/file.bin';
> $files_to_read = array('/file/one.bin', '/file/two.bin', '/file/three.bin');
> 
> $f = fopen($first_file, 'r');
> fseek($f, $initial_offset);
> $data = fread($f, $bytes_to_read);
> fclose($f);
> 
> foreach ($files_to_read as $x) {
>      $f = fopen($x, 'r');
>      $cmp_data = fread($f, $bytes_to_read);
>      if ( $data == $cmp_data ) {
>           printf("{$first_file} matches some data in {$x}.\n");
>      }
>      fclose($f);
> }
> ?>
> 
> On 6/6/05, Mister Anonymous <atlantalinuxenthusiast at gmail.com> wrote:
> > I need to do some binary matching across many files and was wanting to
> > get some recommendations on how to do it.  I would like to be able to
> > grab a section of binary data from one file (approximately 500
> > continuous bytes) and check and see if that is included in other
> > binary files.  Any recommendations on an easy way to extract the
> > initial 500 bytes (assume I would like to do it from some particular
> > offset in the file) and then search for that data in the other file?
> > 
> > I am afraid that perl might be the best way to do this, but I am not
> > that much of a perl hacker.  If there is an easy way to do it using
> > python or a shell script, that would be preferable.
> > 
> > Thanks.
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://www.ale.org/mailman/listinfo/ale
> > 
> 



More information about the Ale mailing list