1 /* Fuzzy Hashing by Jesse Kornblum
2 Copyright (C) 2010 ManTech International Corporation
3
4 This program demonstrates some of the capabilities of
5 the fuzzy hashing library.
6
7 To compile the program using gcc:
8
9 $ gcc -Wall -I/usr/local/include -L/usr/local/lib sample.c -lfuzzy
10
11 Using mingw:
12
13 C:\> gcc -Wall -Ic:\path\to\includes sample.c fuzzy.dll
14
15 Using Microsoft Visual C:
16
17 C:\> lib /machine:i386 /def:fuzzy.def
18 C:\> cl sample.c fuzzy.lib
19
20 See the README that came with this file for more details on using
21 the library on Windows systems with Microsoft Visual C.
22
23
24 The functions generate_random and write_data are generic routines to make
25 random data for hashing. The real magic happens in the main() function.
26
27 THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
28 CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
29 PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
30 NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
31 SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
32 SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
33 PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). THE AUTHOR
34 SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
35 HIGH RISK ACTIVITIES. */
36
37 // $Id: sample.c 212 2014-07-24 00:05:35Z jessekornblum $
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <inttypes.h>
42
43 #include "fuzzy.h"
44
45 #define FILENAME "foo.dat"
46 #define SIZE 0x50000
47
48
generate_random(unsigned char * buf,uint32_t sz)49 void generate_random(unsigned char *buf, uint32_t sz)
50 {
51 uint32_t i;
52
53 for (i = 0 ; i < sz ; ++i)
54 buf[i] = (unsigned char)(rand() % 255);
55 buf[(sz-1)] = 0;
56 }
57
58
write_data(const unsigned char * buf,const uint32_t sz,const char * fn)59 int write_data(const unsigned char *buf,
60 const uint32_t sz,
61 const char *fn)
62 {
63 printf ("Writing to %s\n", fn);
64 FILE * handle = fopen(fn,"wb");
65 if (NULL == handle)
66 return 1;
67 fwrite(buf,sz,1,handle);
68 fclose(handle);
69
70 return 0;
71 }
72
73
main(int argc,char ** argv)74 int main(int argc, char **argv)
75 {
76 unsigned char * buf;
77 char * result, * result2;
78 FILE *handle;
79
80 srand(1);
81
82 buf = (unsigned char *)malloc(SIZE);
83 result = (char *)malloc(FUZZY_MAX_RESULT);
84 result2 = (char *)malloc(FUZZY_MAX_RESULT);
85 if (NULL == result || NULL == buf || NULL == result2)
86 {
87 fprintf (stderr,"%s: Out of memory\n", argv[0]);
88 return EXIT_FAILURE;
89 }
90
91 generate_random(buf,SIZE);
92
93 if (write_data(buf,SIZE,FILENAME))
94 return EXIT_FAILURE;
95
96 printf ("Hashing buffer\n");
97 int status = fuzzy_hash_buf(buf,SIZE,result);
98 if (status)
99 printf ("Error during buf hash\n");
100 else
101 printf ("%s\n", result);
102
103 handle = fopen(FILENAME,"rb");
104 if (NULL == handle)
105 {
106 perror(FILENAME);
107 return EXIT_FAILURE;
108 }
109
110 printf ("Hashing file\n");
111 status = fuzzy_hash_file(handle,result);
112 if (status)
113 printf ("Error during file hash\n");
114 else
115 printf ("%s\n", result);
116 fclose(handle);
117
118
119 printf ("Modifying buffer and comparing to file\n");
120 int i;
121 for (i = 0x100 ; i < 0x110 ; ++i)
122 buf[i] = 37;
123 status = fuzzy_hash_buf(buf,SIZE,result2);
124 if (status)
125 printf ("Error during buffer hash\n");
126 else
127 printf ("%s\n", result2);
128
129 i = fuzzy_compare(result,result2);
130 if (-1 == i)
131 printf ("An error occured during matching\n");
132 else
133 {
134 if (i != 0)
135 printf ("MATCH: score = %d\n", i);
136 else
137 printf ("did not match\n");
138 }
139
140 return EXIT_SUCCESS;
141 }
142