hash.c 3.1 KB

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