hash.c 3.1 KB

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