1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
7 #include "plugin_header.h"
12 #include <linux/types.h>
14 /* old rupasov (yura) hash */
15 static __u64
hash_rupasov(const unsigned char *name
/* name to hash */ ,
16 int len
/* @name's length */)
24 assert("nikita-672", name
!= NULL
);
25 assert("nikita-673", len
>= 0);
27 for (pow
= 1, i
= 1; i
< len
; ++i
)
33 a
= (name
[0] - 48) * pow
;
35 for (i
= 1; i
< len
; ++i
) {
37 for (pow
= 1, j
= i
; j
< len
- 1; ++j
)
43 for (pow
= 1, j
= i
; j
< len
- 1; ++j
)
48 for (; i
< 256; ++i
) {
50 for (pow
= 1, j
= i
; j
< len
- 1; ++j
)
60 static __u64
hash_r5(const unsigned char *name
/* name to hash */ ,
61 int len UNUSED_ARG
/* @name's length */)
65 assert("nikita-674", name
!= NULL
);
66 assert("nikita-675", len
>= 0);
77 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function
79 Hi = E Mi(Hi-1) + Hi-1
81 (see Applied Cryptography, 2nd edition, p448).
83 Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
85 Jeremy has agreed to the contents of reiserfs/README. -Hans
87 This code was blindly upgraded to __u64 by s/__u32/__u64/g.
89 static __u64
hash_tea(const unsigned char *name
/* name to hash */ ,
90 int len
/* @name's length */)
92 __u64 k
[] = { 0x9464a485u
, 0x542e1a94u
, 0x3e846bffu
, 0xb75bcfc3u
};
94 __u64 h0
= k
[0], h1
= k
[1];
99 assert("nikita-676", name
!= NULL
);
100 assert("nikita-677", len
>= 0);
102 #define DELTA 0x9E3779B9u
103 #define FULLROUNDS 10 /* 32 is overkill, 16 is strong crypto */
104 #define PARTROUNDS 6 /* 6 gets complete mixing */
106 /* a, b, c, d - data; h0, h1 - accumulated hash */
107 #define TEACORE(rounds) \
118 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); \
119 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); \
126 pad
= (__u64
) len
| ((__u64
) len
<< 8);
130 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
131 16 | (__u64
) name
[3] << 24;
132 b
= (__u64
) name
[4] | (__u64
) name
[5] << 8 | (__u64
) name
[6] <<
133 16 | (__u64
) name
[7] << 24;
134 c
= (__u64
) name
[8] | (__u64
) name
[9] << 8 | (__u64
) name
[10] <<
135 16 | (__u64
) name
[11] << 24;
136 d
= (__u64
) name
[12] | (__u64
) name
[13] << 8 | (__u64
) name
[14]
137 << 16 | (__u64
) name
[15] << 24;
146 /* assert(len < 16); */
150 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
151 16 | (__u64
) name
[3] << 24;
152 b
= (__u64
) name
[4] | (__u64
) name
[5] << 8 | (__u64
) name
[6] <<
153 16 | (__u64
) name
[7] << 24;
154 c
= (__u64
) name
[8] | (__u64
) name
[9] << 8 | (__u64
) name
[10] <<
155 16 | (__u64
) name
[11] << 24;
158 for (i
= 12; i
< len
; i
++) {
162 } else if (len
>= 8) {
163 /* assert(len < 12); */
166 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
167 16 | (__u64
) name
[3] << 24;
168 b
= (__u64
) name
[4] | (__u64
) name
[5] << 8 | (__u64
) name
[6] <<
169 16 | (__u64
) name
[7] << 24;
172 for (i
= 8; i
< len
; i
++) {
176 } else if (len
>= 4) {
177 /* assert(len < 8); */
180 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
181 16 | (__u64
) name
[3] << 24;
184 for (i
= 4; i
< len
; i
++) {
189 /* assert(len < 4); */
193 for (i
= 0; i
< len
; i
++) {
206 /* classical 64 bit Fowler/Noll/Vo-1 (FNV-1) hash.
208 See http://www.isthe.com/chongo/tech/comp/fnv/ for details.
212 FNV hashes are designed to be fast while maintaining a low collision
215 [This version also seems to preserve lexicographical order locally.]
217 FNV hash algorithms and source code have been released into the public
221 static __u64
hash_fnv1(const unsigned char *name
/* name to hash */ ,
222 int len UNUSED_ARG
/* @name's length */)
224 unsigned long long a
= 0xcbf29ce484222325ull
;
225 const unsigned long long fnv_64_prime
= 0x100000001b3ull
;
227 assert("nikita-678", name
!= NULL
);
228 assert("nikita-679", len
>= 0);
230 /* FNV-1 hash each octet in the buffer */
231 for (; *name
; ++name
) {
232 /* multiply by the 32 bit FNV magic prime mod 2^64 */
234 /* xor the bottom with the current octet */
235 a
^= (unsigned long long)(*name
);
237 /* return our new hash value */
241 /* degenerate hash function used to simplify testing of non-unique key
243 static __u64
hash_deg(const unsigned char *name UNUSED_ARG
/* name to hash */ ,
244 int len UNUSED_ARG
/* @name's length */)
246 return 0xc0c0c0c010101010ull
;
249 static int change_hash(struct inode
*inode
,
250 reiser4_plugin
* plugin
,
255 assert("nikita-3503", inode
!= NULL
);
256 assert("nikita-3504", plugin
!= NULL
);
258 assert("nikita-3505", is_reiser4_inode(inode
));
259 assert("nikita-3507", plugin
->h
.type_id
== REISER4_HASH_PLUGIN_TYPE
);
261 if (!plugin_of_group(inode_file_plugin(inode
), REISER4_DIRECTORY_FILE
))
262 return RETERR(-EINVAL
);
265 if (inode_hash_plugin(inode
) == NULL
||
266 inode_hash_plugin(inode
)->h
.id
!= plugin
->h
.id
) {
267 if (is_dir_empty(inode
) == 0)
268 result
= aset_set_unsafe(&reiser4_inode_data(inode
)->pset
,
271 result
= RETERR(-ENOTEMPTY
);
277 static reiser4_plugin_ops hash_plugin_ops
= {
282 .change
= change_hash
286 hash_plugin hash_plugins
[LAST_HASH_ID
] = {
287 [RUPASOV_HASH_ID
] = {
289 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
290 .id
= RUPASOV_HASH_ID
,
291 .pops
= &hash_plugin_ops
,
293 .desc
= "Original Yura's hash",
294 .linkage
= {NULL
, NULL
}
300 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
302 .pops
= &hash_plugin_ops
,
305 .linkage
= {NULL
, NULL
}
311 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
313 .pops
= &hash_plugin_ops
,
316 .linkage
= {NULL
, NULL
}
322 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
324 .pops
= &hash_plugin_ops
,
327 .linkage
= {NULL
, NULL
}
331 [DEGENERATE_HASH_ID
] = {
333 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
334 .id
= DEGENERATE_HASH_ID
,
335 .pops
= &hash_plugin_ops
,
336 .label
= "degenerate hash",
337 .desc
= "Degenerate hash: only for testing",
338 .linkage
= {NULL
, NULL
}
346 c-indentation-style: "K&R"