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/cryptohash.h>
15 #define DELTA 0x9E3779B9
17 static void TEA_transform(__u32 buf[4], __u32 const in[])
20 __u32 b0 = buf[0], b1 = buf[1];
21 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
26 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
27 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
35 /* The old legacy hash */
36 static __u32 dx_hack_hash_unsigned(const char *name, int len)
38 __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
39 const unsigned char *ucp = (const unsigned char *) name;
42 hash = hash1 + (hash0 ^ (((int) *ucp++) * 7152373));
44 if (hash & 0x80000000)
52 static __u32 dx_hack_hash_signed(const char *name, int len)
54 __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
55 const signed char *scp = (const signed char *) name;
58 hash = hash1 + (hash0 ^ (((int) *scp++) * 7152373));
60 if (hash & 0x80000000)
68 static void str2hashbuf_signed(const char *msg, int len, __u32 *buf, int num)
72 const signed char *scp = (const signed char *) msg;
74 pad = (__u32)len | ((__u32)len << 8);
80 for (i = 0; i < len; i++) {
83 val = ((int) scp[i]) + (val << 8);
96 static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)
100 const unsigned char *ucp = (const unsigned char *) msg;
102 pad = (__u32)len | ((__u32)len << 8);
108 for (i=0; i < len; i++) {
111 val = ((int) ucp[i]) + (val << 8);
125 * Returns the hash of a filename. If len is 0 and name is NULL, then
126 * this function can be used to test whether or not a hash version is
129 * The seed is an 4 longword (32 bits) "secret" which can be used to
130 * uniquify a hash. If the seed is all zero's, then some default seed
133 * A particular hash version specifies whether or not the seed is
134 * represented, and whether or not the returned hash is 32 bits or 64
135 * bits. 32 bit hashes will return 0 for the minor hash.
137 int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
140 __u32 minor_hash = 0;
144 void (*str2hashbuf)(const char *, int, __u32 *, int) =
147 /* Initialize the default seed for the hash checksum functions */
153 /* Check to see if the seed is all zero's */
155 for (i=0; i < 4; i++) {
160 memcpy(buf, hinfo->seed, sizeof(buf));
163 switch (hinfo->hash_version) {
164 case DX_HASH_LEGACY_UNSIGNED:
165 hash = dx_hack_hash_unsigned(name, len);
168 hash = dx_hack_hash_signed(name, len);
170 case DX_HASH_HALF_MD4_UNSIGNED:
171 str2hashbuf = str2hashbuf_unsigned;
172 case DX_HASH_HALF_MD4:
175 (*str2hashbuf)(p, len, in, 8);
176 half_md4_transform(buf, in);
183 case DX_HASH_TEA_UNSIGNED:
184 str2hashbuf = str2hashbuf_unsigned;
188 (*str2hashbuf)(p, len, in, 4);
189 TEA_transform(buf, in);
201 if (hash == (EXT3_HTREE_EOF_32BIT << 1))
202 hash = (EXT3_HTREE_EOF_32BIT - 1) << 1;
204 hinfo->minor_hash = minor_hash;