hash.c 4.3 KB

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