]> mj.ucw.cz Git - libucw.git/blob - lib/finger.c
The $(LIBxxx) mechanism proved useful, so I'm switching to it for all other
[libucw.git] / lib / finger.c
1 /*
2  *      Sherlock Library -- String Fingerprints
3  *
4  *      (c) 2001--2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 /*
11  *  We use a hashing function to map all the URL's and other
12  *  hairy strings we work with to a much simpler universe
13  *  of constant length bit strings (currently 96-bit ones).
14  *  With a random hashing function (which is equivalent to
15  *  having a fixed function and random input), the probability
16  *  of at least one collision happening is at most c*n^2/m
17  *  where n is the number of strings we hash, m is the size
18  *  of our bit string universe (2^96) and c is a small constant.
19  *  We set m sufficiently large and expect no collisions
20  *  to occur. On the other hand, the worst thing which could
21  *  be cause by a collision is mixing up two strings or labels
22  *  of two documents which is relatively harmless.
23  */
24
25 #include "lib/lib.h"
26 #include "lib/conf.h"
27 #include "lib/index.h"
28 #include "lib/md5.h"
29
30 #include <string.h>
31
32 static uns finger_www_hack;
33
34 static struct cfitem finger_config[] = {
35   { "Fingerprints",     CT_SECTION,     NULL },
36   { "WWWHack",          CT_INT,         &finger_www_hack },
37   { NULL,               CT_STOP,        NULL }
38 };
39
40 static void CONSTRUCTOR finger_conf_init(void)
41 {
42   cf_register(finger_config);
43 }
44
45 void
46 fingerprint(byte *string, struct fingerprint *fp)
47 {
48   struct MD5Context c;
49   uns len = strlen(string);
50   byte digest[16];
51
52   MD5Init(&c);
53   if (finger_www_hack && len >= 11 && !memcmp(string, "http://www.", 11))
54     {
55       /* FIXME: This is a dirty hack, but it has to stay until we get real handling of duplicates */
56       MD5Update(&c, string, 7);
57       MD5Update(&c, string+11, len-11);
58     }
59   else
60     MD5Update(&c, string, len);
61   MD5Final(digest, &c);
62   memcpy(fp->hash, digest, 12);
63 }