forked from diogocabral/sherlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sherlock.c
467 lines (395 loc) · 11.3 KB
/
sherlock.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* sherlock.c - written by Loki from Rob Pike's sig and comp programs.
*
* This program takes filenames given on the command line,
* and reads those files into memory, then compares them
* all pairwise to find those which are most similar.
*
* It uses a digital signature generation scheme to randomly
* discard information, thus allowing a better match.
* Essentially it hashes up N adjacent 'words' of input,
* and semi-randomly throws away many of the hashed values
* so that it become hard to hide the plagiarised text.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
char* Progname = "sherlock";
int Ntoken = 3;
int Zerobits = 4;
unsigned long zeromask;
int ntoken = 0;
char** token;
FILE* Outfile;
int Thresh = 0;
int Recursive = 0;
char* fileextension = "c";
int nfiles = 0;
char** filePath;
/* characters to ignore at start and end of each word */
char* Ignore = " \t\n";
/* characters to treat as word-separators or words on their own */
char* Punct_full = ",.<>/?;:'\"`~[]{}\\|!@#$%^&*()-+_=";
char* Punct = "";
typedef struct Sig Sig;
struct Sig {
int nval;
unsigned long* val;
};
void init_token_array(void);
Sig* signature(FILE*);
int compare(Sig*, Sig*);
int endsWith(char*, char*);
void usage(void)
{
fprintf(stderr, "%s: find similar files\n", Progname);
fprintf(stderr, "usage: %s", Progname);
fprintf(stderr, " [options] directory1 directory2 ...\n");
fprintf(stderr, "options:");
fprintf(stderr, " [-t threshold%%]");
fprintf(stderr, " [-z zerobits]");
fprintf(stderr, " [-n chainlength]");
fprintf(stderr, " [-e fileextension]");
fprintf(stderr, " [-r recursive]");
fprintf(stderr, " [-o outfile]");
fprintf(stderr, "\n");
fprintf(stderr, "defaults:");
fprintf(stderr, " threshold=0%%");
fprintf(stderr, " zerobits=3");
fprintf(stderr, " chainlength=4");
fprintf(stderr, " fileextension=c");
fprintf(stderr, " outfile=the screen");
fprintf(stderr, "\n");
exit(2);
}
int endsWith(char* word, char* suffix)
{
size_t wordLength = strlen(word);
size_t suffixLength = strlen(suffix);
if (wordLength < suffixLength)
return 0;
return strncmp(word + wordLength - suffixLength, suffix, suffixLength);
}
void listFiles(const char* name)
{
DIR* dir;
struct dirent* entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
if (Recursive == 1) {
int len;
char* path;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
len = strlen(name) + 2 + strlen(entry->d_name);
path = malloc(len * sizeof(char));
sprintf(path, "%s/%s", name, entry->d_name);
listFiles(path);
free(path);
}
}
else if (entry->d_type == DT_REG) {
if (endsWith(entry->d_name, fileextension) != 0)
continue;
filePath[nfiles] = malloc((strlen(name) + 2 + strlen(entry->d_name)) * sizeof(char));
strcpy(filePath[nfiles], name);
strcat(filePath[nfiles], "/");
strcat(filePath[nfiles], entry->d_name);
nfiles++;
if (nfiles % 1000 == 0)
filePath = realloc(filePath, (nfiles + 1000) * sizeof(char*));
}
}
closedir(dir);
}
int main(int argc, char* argv[])
{
FILE* f;
int i, j, start, percent;
char *s, *outname;
Sig** sig;
Outfile = stdout;
outname = NULL;
/* handle options */
for (start = 1; start < argc; start++) {
if (argv[start][0] != '-')
break;
switch (argv[start][1]) {
case 't':
s = argv[++start];
if (s == NULL)
usage();
Thresh = atoi(s);
if (Thresh < 0 || Thresh > 100)
usage();
break;
case 'z':
s = argv[++start];
if (s == NULL)
usage();
Zerobits = atoi(s);
if (Zerobits < 0 || Zerobits > 31)
usage();
break;
case 'n':
s = argv[++start];
if (s == NULL)
usage();
Ntoken = atoi(s);
if (Ntoken <= 0)
usage();
break;
case 'e':
s = argv[++start];
if (s == NULL)
usage();
fileextension = s;
break;
case 'r':
Recursive = 1;
break;
case 'o':
s = argv[++start];
if (s == NULL)
usage();
outname = s;
break;
default:
usage();
}
}
nfiles = argc - start;
if (nfiles < 1)
usage();
/* initialise */
if (outname != NULL)
Outfile = fopen(outname, "w");
init_token_array();
zeromask = (1 << Zerobits) - 1;
nfiles = 0;
filePath = malloc(1000 * sizeof(char*));
while (start < argc)
listFiles(argv[start++]);
/* shrink to smallest required space */
filePath = realloc(filePath, nfiles * sizeof(char*));
sig = malloc(nfiles * sizeof(Sig*));
/* generate signatures for each file */
for (i = 0; i < nfiles; i++) {
f = fopen(filePath[i], "r");
if (f == NULL) {
fprintf(stderr, "%s: can't open %s:", Progname, filePath[i]);
perror(NULL);
continue;
}
sig[i] = signature(f);
fclose(f);
}
/* compare each signature pair-wise */
for (i = 0; i < nfiles; i++)
for (j = i + 1; j < nfiles; j++) {
percent = compare(sig[i], sig[j]);
if (percent >= Thresh)
fprintf(Outfile, "%s;%s;%d%%\n", filePath[i], filePath[j], percent);
}
for (i = 0; i < nfiles; i++) {
free(filePath[i]);
}
free(filePath);
return 0;
}
/* read_word: read a 'word' from the input, ignoring leading characters
which are inside the 'ignore' string, and stopping if one of
the 'ignore' or 'punct' characters is found.
Uses memory allocation to avoid buffer overflow problems.
*/
char* read_word(FILE* f, int* length, char* ignore, char* punct)
{
long max;
char* word;
long pos;
char* c;
int ch, is_ignore, is_punct;
/* check for EOF first */
if (feof(f)) {
length = 0;
return NULL;
}
/* allocate a buffer to hold the string */
pos = 0;
max = 128;
word = malloc(sizeof(char) * max);
c = &word[pos];
/* initialise some defaults */
if (ignore == NULL)
ignore = "";
if (punct == NULL)
punct = "";
/* read characters into the buffer, resizing it if necessary */
while ((ch = getc(f)) != EOF) {
is_ignore = (strchr(ignore, ch) != NULL);
if (pos == 0) {
if (is_ignore)
/* ignorable char found at start, skip it */
continue;
}
if (is_ignore)
/* ignorable char found after start, stop */
break;
is_punct = (strchr(punct, ch) != NULL);
if (is_punct && (pos > 0)) {
ungetc(ch, f);
break;
}
*c = ch;
c++;
pos++;
if (is_punct)
break;
if (pos == max) {
/* realloc buffer twice the size */
max += max;
word = realloc(word, max);
c = &word[pos];
}
}
/* set length and check for EOF condition */
*length = pos;
if (pos == 0) {
free(word);
return NULL;
}
/* terminate the string and shrink to smallest required space */
*c = '\0';
word = realloc(word, pos + 1);
return word;
}
/* ulcmp: compare *p1 and *p2 */
int ulcmp(const void* p1, const void* p2)
{
unsigned long v1, v2;
v1 = *(unsigned long*)p1;
v2 = *(unsigned long*)p2;
if (v1 < v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}
/* hash: hash an array of char* into an unsigned long hash code */
unsigned long hash(char* tok[])
{
unsigned long h;
unsigned char* s;
int i;
h = 0;
for (i = 0; i < Ntoken; i++)
for (s = (unsigned char*)tok[i]; *s; s++)
h = h * 31 + *s;
return h;
}
void init_token_array(void)
{
int i;
/* create global array of char* and initialise all to NULL */
token = malloc(Ntoken * sizeof(char*));
for (i = 0; i < Ntoken; i++)
token[i] = NULL;
}
Sig* signature(FILE* f)
{
int nv, na;
unsigned long *v, h;
char* str;
int i, ntoken;
Sig* sig;
/* start loading hash values, after we have Ntoken of them */
v = NULL;
na = 0;
nv = 0;
ntoken = 0;
while ((str = read_word(f, &i, Ignore, Punct)) != NULL) {
/* step words down by one */
free(token[0]);
for (i = 0; i < Ntoken - 1; i++)
token[i] = token[i + 1];
/* add new word into array */
token[Ntoken - 1] = str;
/* if we don't yet have enough words in the array continue */
ntoken++;
if (ntoken < Ntoken)
continue;
/* hash the array of words */
h = hash(token);
if ((h & zeromask) != 0)
continue;
/* discard zeros from end of hash value */
h = h >> Zerobits;
/* add value into the signature array, resizing if needed */
if (nv == na) {
na += 100;
v = realloc(v, na * sizeof(unsigned long));
}
v[nv++] = h;
}
/* sort the array of hash values for speed */
qsort(v, nv, sizeof(v[0]), ulcmp);
/* allocate and return the Sig structure for this file */
sig = malloc(sizeof(Sig));
sig->nval = nv;
sig->val = v;
return sig;
}
int compare(Sig* s0, Sig* s1)
{
int i0, i1, nboth, nsimilar;
unsigned long v;
i0 = 0;
i1 = 0;
nboth = 0;
while (i0 < s0->nval && i1 < s1->nval) {
if (s0->val[i0] == s1->val[i1]) {
v = s0->val[i0];
while (i0 < s0->nval && v == s0->val[i0]) {
i0++;
nboth++;
}
while (i1 < s1->nval && v == s1->val[i1]) {
i1++;
nboth++;
}
continue;
}
if (s0->val[i0] < s1->val[i1])
i0++;
else
i1++;
}
if (s0->nval + s1->nval == 0)
return 0; /* ignore if both are empty files */
if (s0->nval + s1->nval == nboth)
return 100; /* perfect match if all hash codes match */
nsimilar = nboth / 2;
return 100 * nsimilar / (s0->nval + s1->nval - nsimilar);
}
/*
* Let f1 == filesize(file1) == A+B
* and f2 == filesize(file2) == A+C
* where A is the similar section and B or C are dissimilar
*
* Similarity = 100 * A / (f1 + f2 - A)
* = 100 * A / (A+B + A+C - A)
* = 100 * A / (A+B+C)
*
* Thus if A==B==C==n the similarity will be 33% (one third)
* This is desireable since we are finding the ratio of similarities
* as a fraction of (similarities+dissimilarities).
*
* The other way of doing things would be to find the ratio of
* the sum of similarities as a fraction of total file size:
* Similarity = 100 * (A+A) / (A+B + A+C)
* This produces higher percentages and more false matches.
*/