Browse code

bigbang

Jiri Kuthan authored on 15/08/2002 12:57:47
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,150 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+
5
+#ifndef _CRC_H_
6
+#define _CRC_H_
7
+
8
+extern unsigned long int crc_32_tab[];
9
+extern unsigned short int ccitt_tab[];
10
+extern unsigned short int crc_16_tab[];
11
+
12
+#endif
13
+
14
+#include <stdio.h>
15
+#include <string.h>
16
+#include <stdlib.h>
17
+#include "hash_func.h"
18
+#include "dprint.h"
19
+#include "crc.h"
20
+#include "ut.h"
21
+
22
+int old_hash( str  call_id, str cseq_nr )
23
+{
24
+   int  hash_code = 0;
25
+   int  i;
26
+	
27
+#if 0 /*def i386*/
28
+   int ci_len, cs_len;
29
+   char *ci, *cs;
30
+   
31
+	trim_len( ci_len, ci, call_id );
32
+	trim_len( cs_len, cs, cseq_nr );
33
+
34
+		int dummy1;
35
+		if (call_id.len>=4){
36
+			asm(
37
+				"1: \n\r"
38
+				"addl (%1), %0 \n\r"
39
+				"add $4, %1 \n\r"
40
+				"cmp %2, %1 \n\r"
41
+				"jl 1b  \n\r"
42
+				: "=r"(hash_code), "=r"(dummy1)
43
+				:  "0" (hash_code), "1"(ci),
44
+				"r"( (ci_len & (~3)) +ci)
45
+			);
46
+		}
47
+#else
48
+    if ( call_id.len>0 )
49
+      for( i=0 ; i<call_id.len ; hash_code+=call_id.s[i++]  );
50
+#endif
51
+
52
+#if 0 /*def i386*/
53
+
54
+		int dummy2;
55
+		if (cseq_nr.len>=4){
56
+			asm(
57
+				"1: \n\r"
58
+				"addl (%1), %0 \n\r"
59
+				"add $4, %1 \n\r"
60
+				"cmp %2, %1 \n\r"
61
+				"jl 1b  \n\r"
62
+				: "=r"(hash_code), "=r"(dummy2)
63
+				:  "0" (hash_code), "1"(cs),
64
+				"r"((cs_len & (~3) )+ cs)
65
+			);
66
+		}
67
+#else
68
+    if ( cseq_nr.len>0 )
69
+      for( i=0 ; i<cseq_nr.len ; hash_code+=cseq_nr.s[i++] );
70
+#endif
71
+   return hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
72
+}
73
+
74
+int new_hash( str call_id, str cseq_nr )
75
+{
76
+	int hash_code = 0;
77
+	int i,j, k, third;
78
+	int ci_len, cs_len;
79
+	char *ci, *cs;
80
+
81
+	/* trim EoLs */
82
+/*
83
+	ci_len = call_id.len;
84
+	while (ci_len && ((c=call_id.s[ci_len-1])==0 || c=='\r' || c=='\n'))
85
+		ci_len--;
86
+	cs_len = cseq_nr.len;
87
+	while (cs_len && ((c=cseq_nr.s[cs_len-1])==0 || c=='\r' || c=='\n'))
88
+		cs_len--;
89
+*/
90
+	trim_len( ci_len, ci, call_id );
91
+	trim_len( cs_len, cs, cseq_nr );
92
+
93
+	/* run the cycle from the end ... we are interested in the
94
+	   most-right digits ... and just take the %10 value of it
95
+	*/
96
+	third=(ci_len-1)/3;
97
+	for ( i=ci_len-1, j=2*third, k=third;
98
+		k>0 ; i--, j--, k-- ) {
99
+		hash_code+=crc_16_tab[(unsigned char)(*(ci+i)) /*+7*/ ]+
100
+			ccitt_tab[*(ci+k)+63]+
101
+			ccitt_tab[*(ci+j)+13];
102
+	}
103
+	for( i=0 ; i<cs_len ; i++ )
104
+		//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
105
+		hash_code+=ccitt_tab[*(cs+i)+123];
106
+
107
+	hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
108
+   	return hash_code;
109
+}
110
+
111
+void hashtest_cycle( int hits[TABLE_ENTRIES], char *ip )
112
+{
113
+	long int i,j,k, l;
114
+	int  hashv;
115
+	static char buf1[1024];
116
+	static char buf2[1024];
117
+	str call_id; 
118
+	str cseq;
119
+
120
+	call_id.s=buf1;
121
+	cseq.s=buf2;
122
+
123
+	for (i=987654328;i<987654328+10;i++)
124
+		for (j=85296341;j<85296341+10;j++)
125
+			for (k=987654;k<=987654+10;k++)
126
+				for (l=101;l<201;l++) {
127
+					call_id.len=sprintf( buf1, "%d-%d-%d@%s",(int)i,(int)j,
128
+						(int)k, ip );
129
+					cseq.len=sprintf( buf2, "%d", (int)l );
130
+					printf("%s\t%s\n", buf1, buf2 );
131
+					hashv=hash( call_id, cseq );
132
+					hits[ hashv ]++;
133
+				}
134
+}
135
+
136
+void hashtest()
137
+{
138
+	int hits[TABLE_ENTRIES];
139
+	int i;
140
+	
141
+	memset( hits, 0, sizeof hits );
142
+	hashtest_cycle( hits, "192.168.99.100" );
143
+	hashtest_cycle( hits, "172.168.99.100" );
144
+	hashtest_cycle( hits, "142.168.99.100" );
145
+	for (i=0; i<TABLE_ENTRIES; i++)
146
+		printf("[%d. %d]\n", i, hits[i] );
147
+	exit(0);
148
+}
149
+
0 150
new file mode 100644
... ...
@@ -0,0 +1,20 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+
5
+#ifndef _HASH_H
6
+#define _HASH_H
7
+
8
+#include "str.h"
9
+
10
+/* always use a power of 2 for hash table size */
11
+#define T_TABLE_POWER    12
12
+#define TABLE_ENTRIES    (1 << (T_TABLE_POWER))
13
+
14
+int new_hash( str  call_id, str cseq_nr );
15
+int old_hash( str  call_id, str cseq_nr );
16
+
17
+#define hash( cid, cseq) new_hash( cid, cseq )
18
+
19
+#endif