Code

Geert's similarity
[git.git] / test-gsimm.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <libgen.h>
5 #include <stdio.h>
6 #include <assert.h>
7 #include <math.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/mman.h>
13 #include "rabinpoly.h"
14 #include "gsimm.h"
16 #define MIN(x,y) ((y)<(x) ? (y) : (x))
17 #define MAX(x,y) ((y)>(x) ? (y) : (x))
19 /* The RABIN_WINDOW_SIZE is the size of fingerprint window used by
20    Rabin algorithm. This is not a modifiable parameter.
22    The first RABIN_WINDOW_SIZE - 1 bytes are skipped, in order to ensure
23    fingerprints are good hashes. This does somewhat reduce the
24    influence of the first few bytes in the file (they're part of
25    fewer windows, like the last few bytes), but that actually isn't
26    so bad as files often start with fixed content that may bias comparisons.
27 */
29 typedef struct fileinfo
30 { char          *name;
31   size_t        length;
32   u_char        md[MD_LENGTH];
33   int           match;
34 } File;
36 int flag_verbose = 0;
37 int flag_debug = 0;
38 char *flag_relative = 0;
40 char cmd[12] = "        ...";
41 char md_strbuf[MD_LENGTH * 2 + 1];
42 u_char relative_md [MD_LENGTH];
44 File *file;
45 int    file_count;
46 size_t file_bytes;
48 char hex[17] = "0123456789abcdef";
50 void usage()
51 {  fprintf (stderr, "usage: %s [-dhvw] [-r fingerprint] file ...\n", cmd);
52    fprintf (stderr, " -d\tdebug output, repeate for more verbosity\n");
53    fprintf (stderr, " -h\tshow this usage information\n");
54    fprintf (stderr, " -r\tshow distance relative to fingerprint "
55                     "(%u hex digits)\n", MD_LENGTH * 2);
56    fprintf (stderr, " -v\tverbose output, repeat for even more verbosity\n");
57    fprintf (stderr, " -w\tenable warnings for suspect statistics\n");
58    exit (1);
59 }
61 char *md_to_str(u_char *md)
62 { int j;
64   for (j = 0; j < MD_LENGTH; j++)
65   { u_char ch = md[j];
67     md_strbuf[j*2] = hex[ch >> 4];
68     md_strbuf[j*2+1] = hex[ch & 0xF];
69   }
71   md_strbuf[j*2] = 0;
72   return md_strbuf;
73 }
75 void process_file (char *name)
76 { int fd;
77   struct stat fs;
78   u_char *data;
79   File *fi = file+file_count;;
81   fd = open (name, O_RDONLY, 0);
82   if (fd < 0)
83   { perror (name);
84     exit (2);
85   }
87   if (fstat (fd, &fs))
88   { perror (name);
89     exit (2);
90   }
92   if (fs.st_size >= GB_SIMM_MIN_FILE_SIZE
93       && fs.st_size <= GB_SIMM_MAX_FILE_SIZE)
94   { fi->length = fs.st_size;
95     fi->name = name;
97     data = (u_char *) mmap (0, fs.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
99     if (data == (u_char *) -1)
100     { perror (name);
101       exit (2);
102     }
104     gb_simm_process (data, fs.st_size, fi->md);
105     if (flag_relative)
106             fprintf (stdout, "%s %llu %u %s %u %3.1f\n",
107                      md_to_str (fi->md), (long long unsigned) 0,
108                      (unsigned) fs.st_size, name,
109                      (unsigned) 0,
110                      100.0 * gb_simm_score(fi->md, relative_md));
111     else
112     {
113       fprintf (stdout, "%s %llu %u %s\n",
114                md_to_str (fi->md), (long long unsigned) 0,
115                (unsigned) fs.st_size, name);
116     }
117     munmap (data, fs.st_size);
118     file_bytes += fs.st_size;
119     file_count++;
120   } else if (flag_verbose)
121   { fprintf (stdout, "skipping %s (size %llu)\n", name, (long long unsigned) fs.st_size); }
123   close (fd);
126 u_char *str_to_md(char *str, u_char *md)
127 { int j;
129   if (!md || !str) return 0;
131   bzero (md, MD_LENGTH);
133   for (j = 0; j < MD_LENGTH * 2; j++)
134   { char ch = str[j];
136     if (ch >= '0' && ch <= '9')
137     { md [j/2] = (md [j/2] << 4) + (ch - '0');
138     }
139     else
140     { ch |= 32;
142       if (ch < 'a' || ch > 'f') break;
143       md [j/2] = (md[j/2] << 4) + (ch - 'a' + 10);
144   } }
146   return (j != MD_LENGTH * 2 || str[j] != 0) ? 0 : md;
149 int main (int argc, char *argv[])
150 { int ch, j;
152   strncpy (cmd, basename (argv[0]), 8);
154   while ((ch = getopt(argc, argv, "dhr:vw")) != -1)
155   { switch (ch)
156     { case 'd': flag_debug++;
157                 break;
158       case 'r': if (!optarg)
159                 { fprintf (stderr, "%s: missing argument for -r\n", cmd);
160                   return 1;
161                 }
162                 if (str_to_md (optarg, relative_md)) flag_relative = optarg;
163                 else
164                 { fprintf (stderr, "%s: not a valid fingerprint\n", optarg);
165                   return 1;
166                 }
167                 break;
168       case 'v': flag_verbose++;
169                 break;
170       case 'w': break;
171       default : usage();
172                 return (ch != 'h');
173   } }
175   argc -= optind;
176   argv += optind;
178   if (argc == 0) usage();
180   rabin_reset ();
181   if (flag_verbose && flag_relative)
182   { fprintf (stdout, "distances are relative to %s\n", flag_relative);
183   }
185   file = (File *) calloc (argc, sizeof (File));
187   for (j = 0; j < argc; j++) process_file (argv[j]);
189   if (flag_verbose)
190   { fprintf (stdout, "%li bytes in %i files\n", (long) file_bytes, file_count);
191   }
193   return 0;