blob: 17ef3a2859afb4b5f115ef55eb8527f51be9ffb3 [file] [log] [blame]
willy tarreau12350152005-12-18 01:03:27 +01001/*
2 This File is copied from
3
4 http://www.oreilly.com/catalog/masteralgoc/index.html
5 Mastering Algorithms with C
6 By Kyle Loudon
7 ISBN: 1-56592-453-3
8 Publishd by O'Reilly
9
10 We have added our own struct to these function.
11 */
12
13/*****************************************************************************
14* *
15* ------------------------------- hashpjw.c ------------------------------ *
16* *
17*****************************************************************************/
18
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020019#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/hashpjw.h>
21#include <common/appsession.h>
willy tarreau12350152005-12-18 01:03:27 +010022
23/*****************************************************************************
24* *
25* -------------------------------- hashpjw ------------------------------- *
26* *
27*****************************************************************************/
28
29int hashpjw(const void *key) {
30
Willy Tarreaubaaee002006-06-26 02:48:02 +020031 const char *ptr;
32 unsigned int val;
33 appsess *appsession_temp;
willy tarreau12350152005-12-18 01:03:27 +010034
Willy Tarreaubaaee002006-06-26 02:48:02 +020035 /*********************************************************************
36 * *
37 * Hash the key by performing a number of bit operations on it. *
38 * *
39 *********************************************************************/
willy tarreau12350152005-12-18 01:03:27 +010040
Willy Tarreaubaaee002006-06-26 02:48:02 +020041 val = 0;
42 appsession_temp = (appsess *)key;
43 ptr = appsession_temp->sessid;
willy tarreau12350152005-12-18 01:03:27 +010044
Willy Tarreaubaaee002006-06-26 02:48:02 +020045 while (*ptr != '\0') {
willy tarreau12350152005-12-18 01:03:27 +010046
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 int tmp;
willy tarreau12350152005-12-18 01:03:27 +010048
Willy Tarreaubaaee002006-06-26 02:48:02 +020049 val = (val << 4) + (*ptr);
willy tarreau12350152005-12-18 01:03:27 +010050
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 if((tmp = (val & 0xf0000000))) {
52 val = val ^ (tmp >> 24);
53 val = val ^ tmp;
54 }
55 ptr++;
56 }/* end while */
willy tarreau12350152005-12-18 01:03:27 +010057
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 /*********************************************************************
59 * *
60 * In practice, replace PRIME_TBLSIZ with the actual table size. *
61 * *
62 *********************************************************************/
63 return val % PRIME_TBLSIZ;
willy tarreau12350152005-12-18 01:03:27 +010064}/* end hashpjw */
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
66/*
67 * Local variables:
68 * c-indent-level: 8
69 * c-basic-offset: 8
70 * End:
71 */