Re-import of fprobe-ulog
[fprobe-ulog.git] / src / hash.c
1 /*
2         Copyright (C) Slava Astashonok <sla@0n.ru>
3
4         This program is free software; you can redistribute it and/or
5         modify it under the terms of the GNU General Public License.
6
7         $Id: hash.c,v 1.3.2.2.2.1 2005/01/29 19:30:41 sla Exp $
8 */
9
10 #include <common.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <time.h>
17
18 #include <hash.h>
19
20
21 #ifdef HASH_TYPE_CRC
22 static uint16_t crc16_poly;
23 static uint8_t shuffle_table[256];
24 static uint16_t crc16_table[256];
25
26 uint16_t crc16(uint16_t crc, uint8_t val)
27 {
28         int i;
29
30         crc ^= val << 8;
31         for (i = 8; i--; )
32                 crc = crc & 0x8000 ? (crc << 1) ^ crc16_poly : crc << 1;
33         return crc;
34 }
35 #endif
36
37 hash_t hash(void *p, int size)
38 {
39         /*
40         ?FIXME?
41         Check for valid size (> 0)
42         */
43
44         hash_t hash = 0;
45
46 #if defined HASH_TYPE_XOR && HASH_BITS == 16
47         if (size & 1) hash = *((uint8_t *) p++);
48         size >>= 1;
49 #endif
50
51         for (;size--;) {
52 #ifdef HASH_TYPE_XOR
53                 hash ^= *((hash_t *) p++);
54 # if HASH_BITS == 16
55                 p++;
56 # endif
57 #endif
58 #ifdef HASH_TYPE_CRC
59                 hash = crc16_table[shuffle_table[*((uint8_t *) p++)] \
60                         ^ (hash >> 8)] ^ (hash << 8);
61 #endif
62         }
63         return hash;
64 }
65
66 void hash_init()
67 {
68
69 #ifdef HASH_TYPE_CRC
70         int rnd, i, j, m;
71         FILE *rnddev;
72
73         if ((rnddev = fopen(RNDDEV, "r"))) {
74                 fcntl(fileno(rnddev), F_SETFL, \
75                         fcntl(fileno(rnddev), F_GETFL) | O_NONBLOCK);
76                 fread(&rnd, sizeof(rnd), 1, rnddev);
77                 fclose(rnddev);
78         }
79         srand(time(NULL) ^ getpid() ^ rnd);
80         crc16_poly = rand() | 1;
81
82         for (i = 0; i < 256; i++) {
83             crc16_table[i] = crc16(0, i);
84             shuffle_table[i] = i;
85         }
86
87         for (i = 0; i < 256; i++) {
88             j = (int) (256.0 * rand() / (RAND_MAX + 1.0));
89             m = shuffle_table[i];
90             shuffle_table[i] = shuffle_table[j];
91             shuffle_table[j] = m;
92         }
93 #endif
94 }