2 Copyright (C) Slava Astashonok <sla@0n.ru>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License.
7 $Id: hash.c,v 1.3.2.2.2.1 2005/01/29 19:30:41 sla Exp $
22 static uint16_t crc16_poly;
23 static uint8_t shuffle_table[256];
24 static uint16_t crc16_table[256];
26 uint16_t crc16(uint16_t crc, uint8_t val)
32 crc = crc & 0x8000 ? (crc << 1) ^ crc16_poly : crc << 1;
37 hash_t hash(void *p, int size)
41 Check for valid size (> 0)
46 #if defined HASH_TYPE_XOR && HASH_BITS == 16
47 if (size & 1) hash = *((uint8_t *) p++);
53 hash ^= *((hash_t *) p++);
59 hash = crc16_table[shuffle_table[*((uint8_t *) p++)] \
60 ^ (hash >> 8)] ^ (hash << 8);
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);
79 srand(time(NULL) ^ getpid() ^ rnd);
80 crc16_poly = rand() | 1;
82 for (i = 0; i < 256; i++) {
83 crc16_table[i] = crc16(0, i);
87 for (i = 0; i < 256; i++) {
88 j = (int) (256.0 * rand() / (RAND_MAX + 1.0));
90 shuffle_table[i] = shuffle_table[j];