13 #include "rabinpoly.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.
29 typedef struct fileinfo
38 char *flag_relative = 0;
40 char cmd[12] = " ...";
41 char md_strbuf[MD_LENGTH * 2 + 1];
42 u_char relative_md [MD_LENGTH];
48 char hex[17] = "0123456789abcdef";
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");
61 int dist (u_char *l, u_char *r)
65 for (j = 0; j < MD_LENGTH; j++)
66 { u_char ch = l[j] ^ r[j];
68 for (k = 0; k < 8; k++) d += ((ch & (1<<k)) > 0);
74 char *md_to_str(u_char *md)
77 for (j = 0; j < MD_LENGTH; j++)
80 md_strbuf[j*2] = hex[ch >> 4];
81 md_strbuf[j*2+1] = hex[ch & 0xF];
88 void process_file (char *name)
92 File *fi = file+file_count;;
94 fd = open (name, O_RDONLY, 0);
105 if (fs.st_size >= MIN_FILE_SIZE
106 && fs.st_size <= MAX_FILE_SIZE)
107 { fi->length = fs.st_size;
110 data = (u_char *) mmap (0, fs.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
112 if (data == (u_char *) -1)
117 gb_simm_process (data, fs.st_size, fi->md);
119 { int d = dist (fi->md, relative_md);
120 double sim = 1.0 - MIN (1.0, (double) (d) / (MD_LENGTH * 4 - 1));
121 fprintf (stdout, "%s %llu %u %s %u %3.1f\n",
122 md_to_str (fi->md), (long long unsigned) 0,
123 (unsigned) fs.st_size, name,
128 fprintf (stdout, "%s %llu %u %s\n",
129 md_to_str (fi->md), (long long unsigned) 0,
130 (unsigned) fs.st_size, name);
132 munmap (data, fs.st_size);
133 file_bytes += fs.st_size;
135 } else if (flag_verbose)
136 { fprintf (stdout, "skipping %s (size %llu)\n", name, (long long unsigned) fs.st_size); }
141 u_char *str_to_md(char *str, u_char *md)
144 if (!md || !str) return 0;
146 bzero (md, MD_LENGTH);
148 for (j = 0; j < MD_LENGTH * 2; j++)
151 if (ch >= '0' && ch <= '9')
152 { md [j/2] = (md [j/2] << 4) + (ch - '0');
157 if (ch < 'a' || ch > 'f') break;
158 md [j/2] = (md[j/2] << 4) + (ch - 'a' + 10);
161 return (j != MD_LENGTH * 2 || str[j] != 0) ? 0 : md;
164 int main (int argc, char *argv[])
167 strncpy (cmd, basename (argv[0]), 8);
169 while ((ch = getopt(argc, argv, "dhr:vw")) != -1)
171 { case 'd': flag_debug++;
173 case 'r': if (!optarg)
174 { fprintf (stderr, "%s: missing argument for -r\n", cmd);
177 if (str_to_md (optarg, relative_md)) flag_relative = optarg;
179 { fprintf (stderr, "%s: not a valid fingerprint\n", optarg);
183 case 'v': flag_verbose++;
193 if (argc == 0) usage();
196 if (flag_verbose && flag_relative)
197 { fprintf (stdout, "distances are relative to %s\n", flag_relative);
200 file = (File *) calloc (argc, sizeof (File));
202 for (j = 0; j < argc; j++) process_file (argv[j]);
205 { fprintf (stdout, "%li bytes in %i files\n", (long) file_bytes, file_count);