hash.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * linux/fs/ext3/hash.c
  3. *
  4. * Copyright (C) 2002 by Theodore Ts'o
  5. *
  6. * This file is released under the GPL v2.
  7. *
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. */
  11. #include "ext3.h"
  12. #include <linux/cryptohash.h>
  13. #define DELTA 0x9E3779B9
  14. static void TEA_transform(__u32 buf[4], __u32 const in[])
  15. {
  16. __u32 sum = 0;
  17. __u32 b0 = buf[0], b1 = buf[1];
  18. __u32 a = in[0], b = in[1], c = in[2], d = in[3];
  19. int n = 16;
  20. do {
  21. sum += DELTA;
  22. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  23. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  24. } while(--n);
  25. buf[0] += b0;
  26. buf[1] += b1;
  27. }
  28. /* The old legacy hash */
  29. static __u32 dx_hack_hash_unsigned(const char *name, int len)
  30. {
  31. __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
  32. const unsigned char *ucp = (const unsigned char *) name;
  33. while (len--) {
  34. hash = hash1 + (hash0 ^ (((int) *ucp++) * 7152373));
  35. if (hash & 0x80000000)
  36. hash -= 0x7fffffff;
  37. hash1 = hash0;
  38. hash0 = hash;
  39. }
  40. return hash0 << 1;
  41. }
  42. static __u32 dx_hack_hash_signed(const char *name, int len)
  43. {
  44. __u32 hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
  45. const signed char *scp = (const signed char *) name;
  46. while (len--) {
  47. hash = hash1 + (hash0 ^ (((int) *scp++) * 7152373));
  48. if (hash & 0x80000000)
  49. hash -= 0x7fffffff;
  50. hash1 = hash0;
  51. hash0 = hash;
  52. }
  53. return hash0 << 1;
  54. }
  55. static void str2hashbuf_signed(const char *msg, int len, __u32 *buf, int num)
  56. {
  57. __u32 pad, val;
  58. int i;
  59. const signed char *scp = (const signed char *) msg;
  60. pad = (__u32)len | ((__u32)len << 8);
  61. pad |= pad << 16;
  62. val = pad;
  63. if (len > num*4)
  64. len = num * 4;
  65. for (i = 0; i < len; i++) {
  66. if ((i % 4) == 0)
  67. val = pad;
  68. val = ((int) scp[i]) + (val << 8);
  69. if ((i % 4) == 3) {
  70. *buf++ = val;
  71. val = pad;
  72. num--;
  73. }
  74. }
  75. if (--num >= 0)
  76. *buf++ = val;
  77. while (--num >= 0)
  78. *buf++ = pad;
  79. }
  80. static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num)
  81. {
  82. __u32 pad, val;
  83. int i;
  84. const unsigned char *ucp = (const unsigned char *) msg;
  85. pad = (__u32)len | ((__u32)len << 8);
  86. pad |= pad << 16;
  87. val = pad;
  88. if (len > num*4)
  89. len = num * 4;
  90. for (i=0; i < len; i++) {
  91. if ((i % 4) == 0)
  92. val = pad;
  93. val = ((int) ucp[i]) + (val << 8);
  94. if ((i % 4) == 3) {
  95. *buf++ = val;
  96. val = pad;
  97. num--;
  98. }
  99. }
  100. if (--num >= 0)
  101. *buf++ = val;
  102. while (--num >= 0)
  103. *buf++ = pad;
  104. }
  105. /*
  106. * Returns the hash of a filename. If len is 0 and name is NULL, then
  107. * this function can be used to test whether or not a hash version is
  108. * supported.
  109. *
  110. * The seed is an 4 longword (32 bits) "secret" which can be used to
  111. * uniquify a hash. If the seed is all zero's, then some default seed
  112. * may be used.
  113. *
  114. * A particular hash version specifies whether or not the seed is
  115. * represented, and whether or not the returned hash is 32 bits or 64
  116. * bits. 32 bit hashes will return 0 for the minor hash.
  117. */
  118. int ext3fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
  119. {
  120. __u32 hash;
  121. __u32 minor_hash = 0;
  122. const char *p;
  123. int i;
  124. __u32 in[8], buf[4];
  125. void (*str2hashbuf)(const char *, int, __u32 *, int) =
  126. str2hashbuf_signed;
  127. /* Initialize the default seed for the hash checksum functions */
  128. buf[0] = 0x67452301;
  129. buf[1] = 0xefcdab89;
  130. buf[2] = 0x98badcfe;
  131. buf[3] = 0x10325476;
  132. /* Check to see if the seed is all zero's */
  133. if (hinfo->seed) {
  134. for (i=0; i < 4; i++) {
  135. if (hinfo->seed[i])
  136. break;
  137. }
  138. if (i < 4)
  139. memcpy(buf, hinfo->seed, sizeof(buf));
  140. }
  141. switch (hinfo->hash_version) {
  142. case DX_HASH_LEGACY_UNSIGNED:
  143. hash = dx_hack_hash_unsigned(name, len);
  144. break;
  145. case DX_HASH_LEGACY:
  146. hash = dx_hack_hash_signed(name, len);
  147. break;
  148. case DX_HASH_HALF_MD4_UNSIGNED:
  149. str2hashbuf = str2hashbuf_unsigned;
  150. case DX_HASH_HALF_MD4:
  151. p = name;
  152. while (len > 0) {
  153. (*str2hashbuf)(p, len, in, 8);
  154. half_md4_transform(buf, in);
  155. len -= 32;
  156. p += 32;
  157. }
  158. minor_hash = buf[2];
  159. hash = buf[1];
  160. break;
  161. case DX_HASH_TEA_UNSIGNED:
  162. str2hashbuf = str2hashbuf_unsigned;
  163. case DX_HASH_TEA:
  164. p = name;
  165. while (len > 0) {
  166. (*str2hashbuf)(p, len, in, 4);
  167. TEA_transform(buf, in);
  168. len -= 16;
  169. p += 16;
  170. }
  171. hash = buf[0];
  172. minor_hash = buf[1];
  173. break;
  174. default:
  175. hinfo->hash = 0;
  176. return -1;
  177. }
  178. hash = hash & ~1;
  179. if (hash == (EXT3_HTREE_EOF_32BIT << 1))
  180. hash = (EXT3_HTREE_EOF_32BIT - 1) << 1;
  181. hinfo->hash = hash;
  182. hinfo->minor_hash = minor_hash;
  183. return 0;
  184. }