[SCSI] aacraid: regression fix
[linux-2.6.22.y-op.git] / fs / reiserfs / hashes.c
blob08d0508c2d3915ea6d305eb82992be1c555e1436
2 /*
3 * Keyed 32-bit hash function using TEA in a Davis-Meyer function
4 * H0 = Key
5 * Hi = E Mi(Hi-1) + Hi-1
7 * (see Applied Cryptography, 2nd edition, p448).
9 * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
11 * Jeremy has agreed to the contents of reiserfs/README. -Hans
12 * Yura's function is added (04/07/2000)
16 // keyed_hash
17 // yura_hash
18 // r5_hash
21 #include <linux/kernel.h>
22 #include <asm/types.h>
23 #include <asm/bug.h>
26 #define DELTA 0x9E3779B9
27 #define FULLROUNDS 10 /* 32 is overkill, 16 is strong crypto */
28 #define PARTROUNDS 6 /* 6 gets complete mixing */
30 /* a, b, c, d - data; h0, h1 - accumulated hash */
31 #define TEACORE(rounds) \
32 do { \
33 u32 sum = 0; \
34 int n = rounds; \
35 u32 b0, b1; \
37 b0 = h0; \
38 b1 = h1; \
40 do \
41 { \
42 sum += DELTA; \
43 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); \
44 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); \
45 } while(--n); \
47 h0 += b0; \
48 h1 += b1; \
49 } while(0)
52 u32 keyed_hash(const signed char *msg, int len)
54 u32 k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3};
56 u32 h0 = k[0], h1 = k[1];
57 u32 a, b, c, d;
58 u32 pad;
59 int i;
61 // assert(len >= 0 && len < 256);
63 pad = (u32)len | ((u32)len << 8);
64 pad |= pad << 16;
66 while(len >= 16)
68 a = (u32)msg[ 0] |
69 (u32)msg[ 1] << 8 |
70 (u32)msg[ 2] << 16|
71 (u32)msg[ 3] << 24;
72 b = (u32)msg[ 4] |
73 (u32)msg[ 5] << 8 |
74 (u32)msg[ 6] << 16|
75 (u32)msg[ 7] << 24;
76 c = (u32)msg[ 8] |
77 (u32)msg[ 9] << 8 |
78 (u32)msg[10] << 16|
79 (u32)msg[11] << 24;
80 d = (u32)msg[12] |
81 (u32)msg[13] << 8 |
82 (u32)msg[14] << 16|
83 (u32)msg[15] << 24;
85 TEACORE(PARTROUNDS);
87 len -= 16;
88 msg += 16;
91 if (len >= 12)
93 a = (u32)msg[ 0] |
94 (u32)msg[ 1] << 8 |
95 (u32)msg[ 2] << 16|
96 (u32)msg[ 3] << 24;
97 b = (u32)msg[ 4] |
98 (u32)msg[ 5] << 8 |
99 (u32)msg[ 6] << 16|
100 (u32)msg[ 7] << 24;
101 c = (u32)msg[ 8] |
102 (u32)msg[ 9] << 8 |
103 (u32)msg[10] << 16|
104 (u32)msg[11] << 24;
106 d = pad;
107 for(i = 12; i < len; i++)
109 d <<= 8;
110 d |= msg[i];
113 else if (len >= 8)
115 a = (u32)msg[ 0] |
116 (u32)msg[ 1] << 8 |
117 (u32)msg[ 2] << 16|
118 (u32)msg[ 3] << 24;
119 b = (u32)msg[ 4] |
120 (u32)msg[ 5] << 8 |
121 (u32)msg[ 6] << 16|
122 (u32)msg[ 7] << 24;
124 c = d = pad;
125 for(i = 8; i < len; i++)
127 c <<= 8;
128 c |= msg[i];
131 else if (len >= 4)
133 a = (u32)msg[ 0] |
134 (u32)msg[ 1] << 8 |
135 (u32)msg[ 2] << 16|
136 (u32)msg[ 3] << 24;
138 b = c = d = pad;
139 for(i = 4; i < len; i++)
141 b <<= 8;
142 b |= msg[i];
145 else
147 a = b = c = d = pad;
148 for(i = 0; i < len; i++)
150 a <<= 8;
151 a |= msg[i];
155 TEACORE(FULLROUNDS);
157 /* return 0;*/
158 return h0^h1;
161 /* What follows in this file is copyright 2000 by Hans Reiser, and the
162 * licensing of what follows is governed by reiserfs/README */
164 u32 yura_hash (const signed char *msg, int len)
166 int j, pow;
167 u32 a, c;
168 int i;
170 for (pow=1,i=1; i < len; i++) pow = pow * 10;
172 if (len == 1)
173 a = msg[0]-48;
174 else
175 a = (msg[0] - 48) * pow;
177 for (i=1; i < len; i++) {
178 c = msg[i] - 48;
179 for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
180 a = a + c * pow;
183 for (; i < 40; i++) {
184 c = '0' - 48;
185 for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
186 a = a + c * pow;
189 for (; i < 256; i++) {
190 c = i;
191 for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
192 a = a + c * pow;
195 a = a << 7;
196 return a;
199 u32 r5_hash (const signed char *msg, int len)
201 u32 a=0;
202 while(*msg) {
203 a += *msg << 4;
204 a += *msg >> 4;
205 a *= 11;
206 msg++;
208 return a;