forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_func.h
110 lines (94 loc) · 2.57 KB
/
hash_func.h
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
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
*---------
*
* 2006-01-20 - new_hash1() added; support for configurable hash size
* added (bogdan)
* 2006-03-13 - new_hash1() and new_hash2() merged into core_hash();
* added core_case_hash() for case insensitive hashes;
* all TM dependet stuff moved to TM config file (bogdan)
*/
/*!
* \file
* \brief Hash functions
*/
#ifndef _HASH_FUNC_H_
#define _HASH_FUNC_H_
#include "str.h"
#define ch_h_inc h+=v^(v>>3)
#define ch_icase(_c) (((_c)>='A'&&(_c)<='Z')?((_c)|0x20):(_c))
static inline unsigned int core_hash(const str *s1, const str *s2, const unsigned int size)
{
char *p, *end;
register unsigned v;
register unsigned h;
h=0;
end=s1->s+s1->len;
for ( p=s1->s ; p<=(end-4) ; p+=4 ){
v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
ch_h_inc;
}
v=0;
for (; p<end ; p++){ v<<=8; v+=*p;}
ch_h_inc;
if (s2) {
end=s2->s+s2->len;
for (p=s2->s; p<=(end-4); p+=4){
v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
ch_h_inc;
}
v=0;
for (; p<end ; p++){ v<<=8; v+=*p;}
ch_h_inc;
}
h=((h)+(h>>11))+((h>>13)+(h>>23));
return size?((h)&(size-1)):h;
}
static inline unsigned int core_case_hash( str *s1, str *s2, unsigned int size)
{
char *p, *end;
register unsigned v;
register unsigned h;
h=0;
end=s1->s+s1->len;
for ( p=s1->s ; p<=(end-4) ; p+=4 ){
v=(ch_icase(*p)<<24)+(ch_icase(p[1])<<16)+(ch_icase(p[2])<<8)
+ ch_icase(p[3]);
ch_h_inc;
}
v=0;
for (; p<end ; p++){ v<<=8; v+=ch_icase(*p);}
ch_h_inc;
if (s2) {
end=s2->s+s2->len;
for (p=s2->s; p<=(end-4); p+=4){
v=(ch_icase(*p)<<24)+(ch_icase(p[1])<<16)+(ch_icase(p[2])<<8)
+ ch_icase(p[3]);
ch_h_inc;
}
v=0;
for (; p<end ; p++){ v<<=8; v+=ch_icase(*p);}
ch_h_inc;
}
h=((h)+(h>>11))+((h>>13)+(h>>23));
return size?((h)&(size-1)):h;
}
#endif