PageRenderTime 140ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/add-ons/kernel/file_systems/userlandfs/r5/src/test/reiserfs/hashes.cpp

https://bitbucket.org/ddevine/haiku
C++ | 257 lines | 181 code | 38 blank | 38 comment | 35 complexity | 47ef76b6d440fa15d863b176ae5fe06a MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, ISC, BSD-3-Clause, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-3.0
  1. /*
  2. * Keyed 32-bit hash function using TEA in a Davis-Meyer function
  3. * H0 = Key
  4. * Hi = E Mi(Hi-1) + Hi-1
  5. *
  6. * (see Applied Cryptography, 2nd edition, p448).
  7. *
  8. * Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
  9. *
  10. * Jeremy has agreed to the contents of reiserfs/README. -Hans
  11. * Yura's function is added (04/07/2000)
  12. */
  13. // Modified by Ingo Weinhold (bonefish), Jan. 2003:
  14. // Fixed r5_hash() and added key_offset_for_name().
  15. //
  16. // keyed_hash
  17. // yura_hash
  18. // r5_hash
  19. //
  20. #include "hashes.h"
  21. #include "reiserfs.h"
  22. #define DELTA 0x9E3779B9
  23. #define FULLROUNDS 10 /* 32 is overkill, 16 is strong crypto */
  24. #define PARTROUNDS 6 /* 6 gets complete mixing */
  25. /* a, b, c, d - data; h0, h1 - accumulated hash */
  26. #define TEACORE(rounds) \
  27. do { \
  28. uint32 sum = 0; \
  29. int n = rounds; \
  30. uint32 b0, b1; \
  31. \
  32. b0 = h0; \
  33. b1 = h1; \
  34. \
  35. do \
  36. { \
  37. sum += DELTA; \
  38. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); \
  39. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); \
  40. } while(--n); \
  41. \
  42. h0 += b0; \
  43. h1 += b1; \
  44. } while(0)
  45. uint32 keyed_hash(const signed char *msg, int len)
  46. {
  47. uint32 k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3};
  48. uint32 h0 = k[0], h1 = k[1];
  49. uint32 a, b, c, d;
  50. uint32 pad;
  51. int i;
  52. // assert(len >= 0 && len < 256);
  53. pad = (uint32)len | ((uint32)len << 8);
  54. pad |= pad << 16;
  55. while(len >= 16)
  56. {
  57. a = (uint32)msg[ 0] |
  58. (uint32)msg[ 1] << 8 |
  59. (uint32)msg[ 2] << 16|
  60. (uint32)msg[ 3] << 24;
  61. b = (uint32)msg[ 4] |
  62. (uint32)msg[ 5] << 8 |
  63. (uint32)msg[ 6] << 16|
  64. (uint32)msg[ 7] << 24;
  65. c = (uint32)msg[ 8] |
  66. (uint32)msg[ 9] << 8 |
  67. (uint32)msg[10] << 16|
  68. (uint32)msg[11] << 24;
  69. d = (uint32)msg[12] |
  70. (uint32)msg[13] << 8 |
  71. (uint32)msg[14] << 16|
  72. (uint32)msg[15] << 24;
  73. TEACORE(PARTROUNDS);
  74. len -= 16;
  75. msg += 16;
  76. }
  77. if (len >= 12)
  78. {
  79. //assert(len < 16);
  80. if (len >= 16)
  81. *(int *)0 = 0;
  82. a = (uint32)msg[ 0] |
  83. (uint32)msg[ 1] << 8 |
  84. (uint32)msg[ 2] << 16|
  85. (uint32)msg[ 3] << 24;
  86. b = (uint32)msg[ 4] |
  87. (uint32)msg[ 5] << 8 |
  88. (uint32)msg[ 6] << 16|
  89. (uint32)msg[ 7] << 24;
  90. c = (uint32)msg[ 8] |
  91. (uint32)msg[ 9] << 8 |
  92. (uint32)msg[10] << 16|
  93. (uint32)msg[11] << 24;
  94. d = pad;
  95. for(i = 12; i < len; i++)
  96. {
  97. d <<= 8;
  98. d |= msg[i];
  99. }
  100. }
  101. else if (len >= 8)
  102. {
  103. //assert(len < 12);
  104. if (len >= 12)
  105. *(int *)0 = 0;
  106. a = (uint32)msg[ 0] |
  107. (uint32)msg[ 1] << 8 |
  108. (uint32)msg[ 2] << 16|
  109. (uint32)msg[ 3] << 24;
  110. b = (uint32)msg[ 4] |
  111. (uint32)msg[ 5] << 8 |
  112. (uint32)msg[ 6] << 16|
  113. (uint32)msg[ 7] << 24;
  114. c = d = pad;
  115. for(i = 8; i < len; i++)
  116. {
  117. c <<= 8;
  118. c |= msg[i];
  119. }
  120. }
  121. else if (len >= 4)
  122. {
  123. //assert(len < 8);
  124. if (len >= 8)
  125. *(int *)0 = 0;
  126. a = (uint32)msg[ 0] |
  127. (uint32)msg[ 1] << 8 |
  128. (uint32)msg[ 2] << 16|
  129. (uint32)msg[ 3] << 24;
  130. b = c = d = pad;
  131. for(i = 4; i < len; i++)
  132. {
  133. b <<= 8;
  134. b |= msg[i];
  135. }
  136. }
  137. else
  138. {
  139. //assert(len < 4);
  140. if (len >= 4)
  141. *(int *)0 = 0;
  142. a = b = c = d = pad;
  143. for(i = 0; i < len; i++)
  144. {
  145. a <<= 8;
  146. a |= msg[i];
  147. }
  148. }
  149. TEACORE(FULLROUNDS);
  150. /* return 0;*/
  151. return h0^h1;
  152. }
  153. /* What follows in this file is copyright 2000 by Hans Reiser, and the
  154. * licensing of what follows is governed by reiserfs/README */
  155. uint32 yura_hash (const signed char *msg, int len)
  156. {
  157. int j, pow;
  158. uint32 a, c;
  159. int i;
  160. for (pow=1,i=1; i < len; i++) pow = pow * 10;
  161. if (len == 1)
  162. a = msg[0]-48;
  163. else
  164. a = (msg[0] - 48) * pow;
  165. for (i=1; i < len; i++) {
  166. c = msg[i] - 48;
  167. for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
  168. a = a + c * pow;
  169. }
  170. for (; i < 40; i++) {
  171. c = '0' - 48;
  172. for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
  173. a = a + c * pow;
  174. }
  175. for (; i < 256; i++) {
  176. c = i;
  177. for (pow=1,j=i; j < len-1; j++) pow = pow * 10;
  178. a = a + c * pow;
  179. }
  180. a = a << 7;
  181. return a;
  182. }
  183. uint32 r5_hash (const signed char *msg, int len)
  184. {
  185. uint32 a=0;
  186. // bonefish: len was ignored
  187. // while(*msg) {
  188. while(len--) {
  189. a += *msg << 4;
  190. a += *msg >> 4;
  191. a *= 11;
  192. msg++;
  193. }
  194. return a;
  195. }
  196. // bonefish:
  197. // from reiserfs_fs.h:
  198. /* hash value occupies bits from 7 up to 30 */
  199. #define GET_HASH_VALUE(offset) ((offset) & 0x7fffff80LL)
  200. #define MAX_GENERATION_NUMBER 127
  201. // bonefish:
  202. // derived from name.c: get_third_component()
  203. uint32
  204. key_offset_for_name(hash_function_t hash, const char *name, int len)
  205. {
  206. uint32 res;
  207. if (!len || (len == 1 && name[0] == '.'))
  208. return DOT_OFFSET;
  209. if (len == 2 && name[0] == '.' && name[1] == '.')
  210. return DOT_DOT_OFFSET;
  211. res = (*hash)((const signed char*)name, len);
  212. // take bits from 7-th to 30-th including both bounds
  213. res = GET_HASH_VALUE(res);
  214. if (res == 0)
  215. // needed to have no names before "." and ".." those have hash
  216. // value == 0 and generation conters 1 and 2 accordingly
  217. res = 128;
  218. return res + MAX_GENERATION_NUMBER;
  219. }