4 * Copyright (C) 2002 by Theodore Ts'o
6 * This file is released under the GPL v2.
8 * This file may be redistributed under the terms of the GNU Public
13 #include <linux/jbd.h>
14 #include <linux/ext3_fs.h>
15 #include <linux/cryptohash.h>
17 #define DELTA 0x9E3779B9
19 static void TEA_transform(__u32 buf
[4], __u32
const in
[])
22 __u32 b0
= buf
[0], b1
= buf
[1];
23 __u32 a
= in
[0], b
= in
[1], c
= in
[2], d
= in
[3];
28 b0
+= ((b1
<< 4)+a
) ^ (b1
+sum
) ^ ((b1
>> 5)+b
);
29 b1
+= ((b0
<< 4)+c
) ^ (b0
+sum
) ^ ((b0
>> 5)+d
);
37 /* The old legacy hash */
38 static __u32
dx_hack_hash (const char *name
, int len
)
40 __u32 hash0
= 0x12a3fe2d, hash1
= 0x37abe8f9;
42 __u32 hash
= hash1
+ (hash0
^ (*name
++ * 7152373));
44 if (hash
& 0x80000000) hash
-= 0x7fffffff;
51 static void str2hashbuf(const char *msg
, int len
, __u32
*buf
, int num
)
56 pad
= (__u32
)len
| ((__u32
)len
<< 8);
62 for (i
=0; i
< len
; i
++) {
65 val
= msg
[i
] + (val
<< 8);
79 * Returns the hash of a filename. If len is 0 and name is NULL, then
80 * this function can be used to test whether or not a hash version is
83 * The seed is an 4 longword (32 bits) "secret" which can be used to
84 * uniquify a hash. If the seed is all zero's, then some default seed
87 * A particular hash version specifies whether or not the seed is
88 * represented, and whether or not the returned hash is 32 bits or 64
89 * bits. 32 bit hashes will return 0 for the minor hash.
91 int ext3fs_dirhash(const char *name
, int len
, struct dx_hash_info
*hinfo
)
99 /* Initialize the default seed for the hash checksum functions */
105 /* Check to see if the seed is all zero's */
107 for (i
=0; i
< 4; i
++) {
112 memcpy(buf
, hinfo
->seed
, sizeof(buf
));
115 switch (hinfo
->hash_version
) {
117 hash
= dx_hack_hash(name
, len
);
119 case DX_HASH_HALF_MD4
:
122 str2hashbuf(p
, len
, in
, 8);
123 half_md4_transform(buf
, in
);
133 str2hashbuf(p
, len
, in
, 4);
134 TEA_transform(buf
, in
);
146 if (hash
== (EXT3_HTREE_EOF
<< 1))
147 hash
= (EXT3_HTREE_EOF
-1) << 1;
149 hinfo
->minor_hash
= minor_hash
;