sample.c 3.36 KB
Newer Older
Sophie Brun's avatar
Sophie Brun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/* Fuzzy Hashing by Jesse Kornblum
   Copyright (C) 2010 ManTech International Corporation

   This program demonstrates some of the capabilities of 
   the fuzzy hashing library.
   
   To compile the program using gcc:

   $ gcc -Wall -I/usr/local/include -L/usr/local/lib sample.c -lfuzzy

   Using mingw:

   C:\> gcc -Wall -Ic:\path\to\includes sample.c fuzzy.dll

   Using Microsoft Visual C:

   C:\> lib /machine:i386 /def:fuzzy.def
   C:\> cl sample.c fuzzy.lib

   See the README that came with this file for more details on using
   the library on Windows systems with Microsoft Visual C. 


   The functions generate_random and write_data are generic routines to make
   random data for hashing. The real magic happens in the main() function.

   THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
   CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
   PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
   NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
   SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
   SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
   PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  THE AUTHOR
   SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
   HIGH RISK ACTIVITIES.   */

// $Id: sample.c 212 2014-07-24 00:05:35Z jessekornblum $

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#include <fuzzy.h>

#define FILENAME "foo.dat" 
#define SIZE 0x50000


void generate_random(unsigned char *buf, uint32_t sz)
{
  uint32_t i;

  for (i = 0 ; i < sz ; ++i)
    buf[i] = (unsigned char)(rand() % 255);
  buf[(sz-1)] = 0;
}


int write_data(const unsigned char *buf, 
	       const uint32_t sz, 
	       const char *fn)
{
  printf ("Writing to %s\n", fn);
  FILE * handle = fopen(fn,"wb");
  if (NULL == handle)
    return 1;
  fwrite(buf,sz,1,handle);
  fclose(handle);
  
  return 0;
}


int main(int argc, char **argv)
{
  unsigned char * buf;
  char * result, * result2;
  FILE *handle; 

  srand(1);

  buf     = (unsigned char *)malloc(SIZE);
  result  = (char *)malloc(FUZZY_MAX_RESULT);
  result2 = (char *)malloc(FUZZY_MAX_RESULT);
  if (NULL == result || NULL == buf || NULL == result2)
  {
    fprintf (stderr,"%s: Out of memory\n", argv[0]);
    return EXIT_FAILURE;
  }

  generate_random(buf,SIZE);

  if (write_data(buf,SIZE,FILENAME))
    return EXIT_FAILURE;

  printf ("Hashing buffer\n");
  int status = fuzzy_hash_buf(buf,SIZE,result);
  if (status)
    printf ("Error during buf hash\n");
  else
    printf ("%s\n", result);
 
  handle = fopen(FILENAME,"rb");
  if (NULL == handle)
    {
      perror(FILENAME);
      return EXIT_FAILURE;
    }

  printf ("Hashing file\n");
  status = fuzzy_hash_file(handle,result);
  if (status)
    printf ("Error during file hash\n");
  else
    printf ("%s\n", result);
  fclose(handle);


  printf ("Modifying buffer and comparing to file\n");
  int i;
  for (i = 0x100 ; i < 0x110 ; ++i)
    buf[i] = 37;
  status = fuzzy_hash_buf(buf,SIZE,result2);  
  if (status)
    printf ("Error during buffer hash\n");
  else
    printf ("%s\n", result2);

  i = fuzzy_compare(result,result2);
  if (-1 == i)
    printf ("An error occured during matching\n");
  else
  {
    if (i != 0)
      printf ("MATCH: score = %d\n", i);
    else
      printf ("did not match\n");
  }

  return EXIT_SUCCESS;
}