Hash.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. #include "Hash.h"
  2. #pragma warning(disable:4804)
  3. /*
  4. xxHash - Fast Hash algorithm
  5. Copyright (C) 2012-2014, Yann Collet.
  6. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the following disclaimer
  14. in the documentation and/or other materials provided with the
  15. distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. You can contact the author at :
  28. - xxHash source repository : http://code.google.com/p/xxhash/
  29. - public discussion board : https://groups.google.com/forum/#!forum/lz4c
  30. */
  31. //**************************************
  32. // Tuning parameters
  33. //**************************************
  34. // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
  35. // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected.
  36. // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance.
  37. // You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32).
  38. #if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  39. # define XXH_USE_UNALIGNED_ACCESS 1
  40. #endif
  41. // XXH_ACCEPT_NULL_INPUT_POINTER :
  42. // If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
  43. // When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
  44. // This option has a very small performance cost (only measurable on small inputs).
  45. // By default, this option is disabled. To enable it, uncomment below define :
  46. // #define XXH_ACCEPT_NULL_INPUT_POINTER 1
  47. // XXH_FORCE_NATIVE_FORMAT :
  48. // By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
  49. // Results are therefore identical for little-endian and big-endian CPU.
  50. // This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  51. // Should endian-independance be of no importance for your application, you may set the #define below to 1.
  52. // It will improve speed for Big-endian CPU.
  53. // This option has no impact on Little_Endian CPU.
  54. #define XXH_FORCE_NATIVE_FORMAT 0
  55. //**************************************
  56. // Compiler Specific Options
  57. //**************************************
  58. // Disable some Visual warning messages
  59. #ifdef _MSC_VER // Visual Studio
  60. # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
  61. #endif
  62. #ifdef _MSC_VER // Visual Studio
  63. # define FORCE_INLINE static __forceinline
  64. #else
  65. # ifdef __GNUC__
  66. # define FORCE_INLINE static inline __attribute__((always_inline))
  67. # else
  68. # define FORCE_INLINE static inline
  69. # endif
  70. #endif
  71. //**************************************
  72. // Includes & Memory related functions
  73. //**************************************
  74. typedef struct { long long ll[6]; } XXH32_state_t;
  75. typedef struct { long long ll[11]; } XXH64_state_t;
  76. typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
  77. // Modify the local functions below should you wish to use some other memory routines
  78. // for malloc(), free()
  79. #include <stdlib.h>
  80. FORCE_INLINE void* XXH_malloc(size_t s) { return malloc(s); }
  81. FORCE_INLINE void XXH_free (void* p) { free(p); }
  82. // for memcpy()
  83. #include <string.h>
  84. FORCE_INLINE void* XXH_memcpy(void* dest, const void* src, size_t size)
  85. {
  86. return memcpy(dest,src,size);
  87. }
  88. //**************************************
  89. // Basic Types
  90. //**************************************
  91. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
  92. # include <stdint.h>
  93. typedef uint8_t BYTE;
  94. typedef uint16_t U16;
  95. typedef uint32_t U32;
  96. typedef int32_t S32;
  97. typedef uint64_t U64;
  98. #else
  99. typedef unsigned char BYTE;
  100. typedef unsigned short U16;
  101. typedef unsigned int U32;
  102. typedef signed int S32;
  103. typedef unsigned long long U64;
  104. #endif
  105. #if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS)
  106. # define _PACKED __attribute__ ((packed))
  107. #else
  108. # define _PACKED
  109. #endif
  110. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  111. # ifdef __IBMC__
  112. # pragma pack(1)
  113. # else
  114. # pragma pack(push, 1)
  115. # endif
  116. #endif
  117. typedef struct _U32_S
  118. {
  119. U32 v;
  120. } _PACKED U32_S;
  121. typedef struct _U64_S
  122. {
  123. U64 v;
  124. } _PACKED U64_S;
  125. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  126. # pragma pack(pop)
  127. #endif
  128. #define A32(x) (((U32_S *)(x))->v)
  129. #define A64(x) (((U64_S *)(x))->v)
  130. //***************************************
  131. // Compiler-specific Functions and Macros
  132. //***************************************
  133. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  134. // Note : although _rotl exists for minGW (GCC under windows), performance seems poor
  135. #if defined(_MSC_VER)
  136. # define XXH_rotl32(x,r) _rotl(x,r)
  137. # define XXH_rotl64(x,r) _rotl64(x,r)
  138. #else
  139. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  140. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  141. #endif
  142. #if defined(_MSC_VER) // Visual Studio
  143. # define XXH_swap32 _byteswap_ulong
  144. # define XXH_swap64 _byteswap_uint64
  145. #elif GCC_VERSION >= 403
  146. # define XXH_swap32 __builtin_bswap32
  147. # define XXH_swap64 __builtin_bswap64
  148. #else
  149. static inline U32 XXH_swap32 (U32 x)
  150. {
  151. return ((x << 24) & 0xff000000 ) |
  152. ((x << 8) & 0x00ff0000 ) |
  153. ((x >> 8) & 0x0000ff00 ) |
  154. ((x >> 24) & 0x000000ff );
  155. }
  156. static inline U64 XXH_swap64 (U64 x)
  157. {
  158. return ((x << 56) & 0xff00000000000000ULL) |
  159. ((x << 40) & 0x00ff000000000000ULL) |
  160. ((x << 24) & 0x0000ff0000000000ULL) |
  161. ((x << 8) & 0x000000ff00000000ULL) |
  162. ((x >> 8) & 0x00000000ff000000ULL) |
  163. ((x >> 24) & 0x0000000000ff0000ULL) |
  164. ((x >> 40) & 0x000000000000ff00ULL) |
  165. ((x >> 56) & 0x00000000000000ffULL);
  166. }
  167. #endif
  168. //**************************************
  169. // Constants
  170. //**************************************
  171. #define PRIME32_1 2654435761U
  172. #define PRIME32_2 2246822519U
  173. #define PRIME32_3 3266489917U
  174. #define PRIME32_4 668265263U
  175. #define PRIME32_5 374761393U
  176. #define PRIME64_1 11400714785074694791ULL
  177. #define PRIME64_2 14029467366897019727ULL
  178. #define PRIME64_3 1609587929392839161ULL
  179. #define PRIME64_4 9650029242287828579ULL
  180. #define PRIME64_5 2870177450012600261ULL
  181. //**************************************
  182. // Architecture Macros
  183. //**************************************
  184. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  185. #ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch
  186. static const int one = 1;
  187. # define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one))
  188. #endif
  189. //**************************************
  190. // Macros
  191. //**************************************
  192. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations
  193. //****************************
  194. // Memory reads
  195. //****************************
  196. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  197. FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align)
  198. {
  199. if (align==XXH_unaligned)
  200. return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
  201. else
  202. return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
  203. }
  204. FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian)
  205. {
  206. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  207. }
  208. FORCE_INLINE U64 XXH_readLE64_align(const U64* ptr, XXH_endianess endian, XXH_alignment align)
  209. {
  210. if (align==XXH_unaligned)
  211. return endian==XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr));
  212. else
  213. return endian==XXH_littleEndian ? *ptr : XXH_swap64(*ptr);
  214. }
  215. FORCE_INLINE U64 XXH_readLE64(const U64* ptr, XXH_endianess endian)
  216. {
  217. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  218. }
  219. //****************************
  220. // Simple Hash Functions
  221. //****************************
  222. FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
  223. {
  224. const BYTE* p = (const BYTE*)input;
  225. const BYTE* bEnd = p + len;
  226. U32 h32;
  227. #define XXH_get32bits(p) XXH_readLE32_align((const U32*)p, endian, align)
  228. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  229. if (p==NULL)
  230. {
  231. len=0;
  232. bEnd=p=(const BYTE*)(size_t)16;
  233. }
  234. #endif
  235. if (len>=16)
  236. {
  237. const BYTE* const limit = bEnd - 16;
  238. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  239. U32 v2 = seed + PRIME32_2;
  240. U32 v3 = seed + 0;
  241. U32 v4 = seed - PRIME32_1;
  242. do
  243. {
  244. v1 += XXH_get32bits(p) * PRIME32_2;
  245. v1 = XXH_rotl32(v1, 13);
  246. v1 *= PRIME32_1;
  247. p+=4;
  248. v2 += XXH_get32bits(p) * PRIME32_2;
  249. v2 = XXH_rotl32(v2, 13);
  250. v2 *= PRIME32_1;
  251. p+=4;
  252. v3 += XXH_get32bits(p) * PRIME32_2;
  253. v3 = XXH_rotl32(v3, 13);
  254. v3 *= PRIME32_1;
  255. p+=4;
  256. v4 += XXH_get32bits(p) * PRIME32_2;
  257. v4 = XXH_rotl32(v4, 13);
  258. v4 *= PRIME32_1;
  259. p+=4;
  260. }
  261. while (p<=limit);
  262. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  263. }
  264. else
  265. {
  266. h32 = seed + PRIME32_5;
  267. }
  268. h32 += (U32) len;
  269. while (p+4<=bEnd)
  270. {
  271. h32 += XXH_get32bits(p) * PRIME32_3;
  272. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  273. p+=4;
  274. }
  275. while (p<bEnd)
  276. {
  277. h32 += (*p) * PRIME32_5;
  278. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  279. p++;
  280. }
  281. h32 ^= h32 >> 15;
  282. h32 *= PRIME32_2;
  283. h32 ^= h32 >> 13;
  284. h32 *= PRIME32_3;
  285. h32 ^= h32 >> 16;
  286. return h32;
  287. }
  288. unsigned int XXH32 (const void* input, size_t len, unsigned seed)
  289. {
  290. #if 0
  291. // Simple version, good for code maintenance, but unfortunately slow for small inputs
  292. XXH32_state_t state;
  293. XXH32_reset(&state, seed);
  294. XXH32_update(&state, input, len);
  295. return XXH32_digest(&state);
  296. #else
  297. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  298. # if !defined(XXH_USE_UNALIGNED_ACCESS)
  299. if ((((size_t)input) & 3) == 0) // Input is aligned, let's leverage the speed advantage
  300. {
  301. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  302. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  303. else
  304. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  305. }
  306. # endif
  307. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  308. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  309. else
  310. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  311. #endif
  312. }
  313. FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
  314. {
  315. const BYTE* p = (const BYTE*)input;
  316. const BYTE* bEnd = p + len;
  317. U64 h64;
  318. #define XXH_get64bits(p) XXH_readLE64_align((const U64*)p, endian, align)
  319. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  320. if (p==NULL)
  321. {
  322. len=0;
  323. bEnd=p=(const BYTE*)(size_t)32;
  324. }
  325. #endif
  326. if (len>=32)
  327. {
  328. const BYTE* const limit = bEnd - 32;
  329. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  330. U64 v2 = seed + PRIME64_2;
  331. U64 v3 = seed + 0;
  332. U64 v4 = seed - PRIME64_1;
  333. do
  334. {
  335. v1 += XXH_get64bits(p) * PRIME64_2;
  336. p+=8;
  337. v1 = XXH_rotl64(v1, 31);
  338. v1 *= PRIME64_1;
  339. v2 += XXH_get64bits(p) * PRIME64_2;
  340. p+=8;
  341. v2 = XXH_rotl64(v2, 31);
  342. v2 *= PRIME64_1;
  343. v3 += XXH_get64bits(p) * PRIME64_2;
  344. p+=8;
  345. v3 = XXH_rotl64(v3, 31);
  346. v3 *= PRIME64_1;
  347. v4 += XXH_get64bits(p) * PRIME64_2;
  348. p+=8;
  349. v4 = XXH_rotl64(v4, 31);
  350. v4 *= PRIME64_1;
  351. }
  352. while (p<=limit);
  353. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  354. v1 *= PRIME64_2;
  355. v1 = XXH_rotl64(v1, 31);
  356. v1 *= PRIME64_1;
  357. h64 ^= v1;
  358. h64 = h64 * PRIME64_1 + PRIME64_4;
  359. v2 *= PRIME64_2;
  360. v2 = XXH_rotl64(v2, 31);
  361. v2 *= PRIME64_1;
  362. h64 ^= v2;
  363. h64 = h64 * PRIME64_1 + PRIME64_4;
  364. v3 *= PRIME64_2;
  365. v3 = XXH_rotl64(v3, 31);
  366. v3 *= PRIME64_1;
  367. h64 ^= v3;
  368. h64 = h64 * PRIME64_1 + PRIME64_4;
  369. v4 *= PRIME64_2;
  370. v4 = XXH_rotl64(v4, 31);
  371. v4 *= PRIME64_1;
  372. h64 ^= v4;
  373. h64 = h64 * PRIME64_1 + PRIME64_4;
  374. }
  375. else
  376. {
  377. h64 = seed + PRIME64_5;
  378. }
  379. h64 += (U64) len;
  380. while (p+8<=bEnd)
  381. {
  382. U64 k1 = XXH_get64bits(p);
  383. k1 *= PRIME64_2;
  384. k1 = XXH_rotl64(k1,31);
  385. k1 *= PRIME64_1;
  386. h64 ^= k1;
  387. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  388. p+=8;
  389. }
  390. if (p+4<=bEnd)
  391. {
  392. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
  393. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  394. p+=4;
  395. }
  396. while (p<bEnd)
  397. {
  398. h64 ^= (*p) * PRIME64_5;
  399. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  400. p++;
  401. }
  402. h64 ^= h64 >> 33;
  403. h64 *= PRIME64_2;
  404. h64 ^= h64 >> 29;
  405. h64 *= PRIME64_3;
  406. h64 ^= h64 >> 32;
  407. return h64;
  408. }
  409. unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  410. {
  411. #if 0
  412. // Simple version, good for code maintenance, but unfortunately slow for small inputs
  413. XXH64_state_t state;
  414. XXH64_reset(&state, seed);
  415. XXH64_update(&state, input, len);
  416. return XXH64_digest(&state);
  417. #else
  418. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  419. # if !defined(XXH_USE_UNALIGNED_ACCESS)
  420. if ((((size_t)input) & 7)==0) // Input is aligned, let's leverage the speed advantage
  421. {
  422. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  423. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  424. else
  425. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  426. }
  427. # endif
  428. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  429. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  430. else
  431. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  432. #endif
  433. }
  434. /****************************************************
  435. * Advanced Hash Functions
  436. ****************************************************/
  437. /*** Allocation ***/
  438. typedef struct
  439. {
  440. U64 total_len;
  441. U32 seed;
  442. U32 v1;
  443. U32 v2;
  444. U32 v3;
  445. U32 v4;
  446. U32 memsize;
  447. char memory[16];
  448. } XXH_istate32_t;
  449. typedef struct
  450. {
  451. U64 total_len;
  452. U64 seed;
  453. U64 v1;
  454. U64 v2;
  455. U64 v3;
  456. U64 v4;
  457. U32 memsize;
  458. char memory[32];
  459. } XXH_istate64_t;
  460. XXH32_state_t* XXH32_createState(void)
  461. {
  462. XXH_STATIC_ASSERT(sizeof(XXH32_state_t) >= sizeof(XXH_istate32_t)); // A compilation error here means XXH32_state_t is not large enough
  463. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  464. }
  465. XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  466. {
  467. XXH_free(statePtr);
  468. return XXH_OK;
  469. };
  470. XXH64_state_t* XXH64_createState(void)
  471. {
  472. XXH_STATIC_ASSERT(sizeof(XXH64_state_t) >= sizeof(XXH_istate64_t)); // A compilation error here means XXH64_state_t is not large enough
  473. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  474. }
  475. XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  476. {
  477. XXH_free(statePtr);
  478. return XXH_OK;
  479. };
  480. /*** Hash feed ***/
  481. XXH_errorcode XXH32_reset(XXH32_state_t* state_in, U32 seed)
  482. {
  483. XXH_istate32_t* state = (XXH_istate32_t*) state_in;
  484. state->seed = seed;
  485. state->v1 = seed + PRIME32_1 + PRIME32_2;
  486. state->v2 = seed + PRIME32_2;
  487. state->v3 = seed + 0;
  488. state->v4 = seed - PRIME32_1;
  489. state->total_len = 0;
  490. state->memsize = 0;
  491. return XXH_OK;
  492. }
  493. XXH_errorcode XXH64_reset(XXH64_state_t* state_in, unsigned long long seed)
  494. {
  495. XXH_istate64_t* state = (XXH_istate64_t*) state_in;
  496. state->seed = seed;
  497. state->v1 = seed + PRIME64_1 + PRIME64_2;
  498. state->v2 = seed + PRIME64_2;
  499. state->v3 = seed + 0;
  500. state->v4 = seed - PRIME64_1;
  501. state->total_len = 0;
  502. state->memsize = 0;
  503. return XXH_OK;
  504. }
  505. FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const void* input, size_t len, XXH_endianess endian)
  506. {
  507. XXH_istate32_t* state = (XXH_istate32_t *) state_in;
  508. const BYTE* p = (const BYTE*)input;
  509. const BYTE* const bEnd = p + len;
  510. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  511. if (input==NULL) return XXH_ERROR;
  512. #endif
  513. state->total_len += len;
  514. if (state->memsize + len < 16) // fill in tmp buffer
  515. {
  516. XXH_memcpy(state->memory + state->memsize, input, len);
  517. state->memsize += (U32)len;
  518. return XXH_OK;
  519. }
  520. if (state->memsize) // some data left from previous update
  521. {
  522. XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize);
  523. {
  524. const U32* p32 = (const U32*)state->memory;
  525. state->v1 += XXH_readLE32(p32, endian) * PRIME32_2;
  526. state->v1 = XXH_rotl32(state->v1, 13);
  527. state->v1 *= PRIME32_1;
  528. p32++;
  529. state->v2 += XXH_readLE32(p32, endian) * PRIME32_2;
  530. state->v2 = XXH_rotl32(state->v2, 13);
  531. state->v2 *= PRIME32_1;
  532. p32++;
  533. state->v3 += XXH_readLE32(p32, endian) * PRIME32_2;
  534. state->v3 = XXH_rotl32(state->v3, 13);
  535. state->v3 *= PRIME32_1;
  536. p32++;
  537. state->v4 += XXH_readLE32(p32, endian) * PRIME32_2;
  538. state->v4 = XXH_rotl32(state->v4, 13);
  539. state->v4 *= PRIME32_1;
  540. p32++;
  541. }
  542. p += 16-state->memsize;
  543. state->memsize = 0;
  544. }
  545. if (p <= bEnd-16)
  546. {
  547. const BYTE* const limit = bEnd - 16;
  548. U32 v1 = state->v1;
  549. U32 v2 = state->v2;
  550. U32 v3 = state->v3;
  551. U32 v4 = state->v4;
  552. do
  553. {
  554. v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  555. v1 = XXH_rotl32(v1, 13);
  556. v1 *= PRIME32_1;
  557. p+=4;
  558. v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  559. v2 = XXH_rotl32(v2, 13);
  560. v2 *= PRIME32_1;
  561. p+=4;
  562. v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  563. v3 = XXH_rotl32(v3, 13);
  564. v3 *= PRIME32_1;
  565. p+=4;
  566. v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  567. v4 = XXH_rotl32(v4, 13);
  568. v4 *= PRIME32_1;
  569. p+=4;
  570. }
  571. while (p<=limit);
  572. state->v1 = v1;
  573. state->v2 = v2;
  574. state->v3 = v3;
  575. state->v4 = v4;
  576. }
  577. if (p < bEnd)
  578. {
  579. XXH_memcpy(state->memory, p, bEnd-p);
  580. state->memsize = (int)(bEnd-p);
  581. }
  582. return XXH_OK;
  583. }
  584. XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
  585. {
  586. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  587. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  588. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  589. else
  590. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  591. }
  592. FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state_in, XXH_endianess endian)
  593. {
  594. XXH_istate32_t* state = (XXH_istate32_t*) state_in;
  595. const BYTE * p = (const BYTE*)state->memory;
  596. BYTE* bEnd = (BYTE*)state->memory + state->memsize;
  597. U32 h32;
  598. if (state->total_len >= 16)
  599. {
  600. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  601. }
  602. else
  603. {
  604. h32 = state->seed + PRIME32_5;
  605. }
  606. h32 += (U32) state->total_len;
  607. while (p+4<=bEnd)
  608. {
  609. h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3;
  610. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  611. p+=4;
  612. }
  613. while (p<bEnd)
  614. {
  615. h32 += (*p) * PRIME32_5;
  616. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  617. p++;
  618. }
  619. h32 ^= h32 >> 15;
  620. h32 *= PRIME32_2;
  621. h32 ^= h32 >> 13;
  622. h32 *= PRIME32_3;
  623. h32 ^= h32 >> 16;
  624. return h32;
  625. }
  626. U32 XXH32_digest (const XXH32_state_t* state_in)
  627. {
  628. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  629. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  630. return XXH32_digest_endian(state_in, XXH_littleEndian);
  631. else
  632. return XXH32_digest_endian(state_in, XXH_bigEndian);
  633. }
  634. FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state_in, const void* input, size_t len, XXH_endianess endian)
  635. {
  636. XXH_istate64_t * state = (XXH_istate64_t *) state_in;
  637. const BYTE* p = (const BYTE*)input;
  638. const BYTE* const bEnd = p + len;
  639. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  640. if (input==NULL) return XXH_ERROR;
  641. #endif
  642. state->total_len += len;
  643. if (state->memsize + len < 32) // fill in tmp buffer
  644. {
  645. XXH_memcpy(state->memory + state->memsize, input, len);
  646. state->memsize += (U32)len;
  647. return XXH_OK;
  648. }
  649. if (state->memsize) // some data left from previous update
  650. {
  651. XXH_memcpy(state->memory + state->memsize, input, 32-state->memsize);
  652. {
  653. const U64* p64 = (const U64*)state->memory;
  654. state->v1 += XXH_readLE64(p64, endian) * PRIME64_2;
  655. state->v1 = XXH_rotl64(state->v1, 31);
  656. state->v1 *= PRIME64_1;
  657. p64++;
  658. state->v2 += XXH_readLE64(p64, endian) * PRIME64_2;
  659. state->v2 = XXH_rotl64(state->v2, 31);
  660. state->v2 *= PRIME64_1;
  661. p64++;
  662. state->v3 += XXH_readLE64(p64, endian) * PRIME64_2;
  663. state->v3 = XXH_rotl64(state->v3, 31);
  664. state->v3 *= PRIME64_1;
  665. p64++;
  666. state->v4 += XXH_readLE64(p64, endian) * PRIME64_2;
  667. state->v4 = XXH_rotl64(state->v4, 31);
  668. state->v4 *= PRIME64_1;
  669. p64++;
  670. }
  671. p += 32-state->memsize;
  672. state->memsize = 0;
  673. }
  674. if (p+32 <= bEnd)
  675. {
  676. const BYTE* const limit = bEnd - 32;
  677. U64 v1 = state->v1;
  678. U64 v2 = state->v2;
  679. U64 v3 = state->v3;
  680. U64 v4 = state->v4;
  681. do
  682. {
  683. v1 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  684. v1 = XXH_rotl64(v1, 31);
  685. v1 *= PRIME64_1;
  686. p+=8;
  687. v2 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  688. v2 = XXH_rotl64(v2, 31);
  689. v2 *= PRIME64_1;
  690. p+=8;
  691. v3 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  692. v3 = XXH_rotl64(v3, 31);
  693. v3 *= PRIME64_1;
  694. p+=8;
  695. v4 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  696. v4 = XXH_rotl64(v4, 31);
  697. v4 *= PRIME64_1;
  698. p+=8;
  699. }
  700. while (p<=limit);
  701. state->v1 = v1;
  702. state->v2 = v2;
  703. state->v3 = v3;
  704. state->v4 = v4;
  705. }
  706. if (p < bEnd)
  707. {
  708. XXH_memcpy(state->memory, p, bEnd-p);
  709. state->memsize = (int)(bEnd-p);
  710. }
  711. return XXH_OK;
  712. }
  713. XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  714. {
  715. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  716. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  717. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  718. else
  719. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  720. }
  721. FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state_in, XXH_endianess endian)
  722. {
  723. XXH_istate64_t * state = (XXH_istate64_t *) state_in;
  724. const BYTE * p = (const BYTE*)state->memory;
  725. BYTE* bEnd = (BYTE*)state->memory + state->memsize;
  726. U64 h64;
  727. if (state->total_len >= 32)
  728. {
  729. U64 v1 = state->v1;
  730. U64 v2 = state->v2;
  731. U64 v3 = state->v3;
  732. U64 v4 = state->v4;
  733. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  734. v1 *= PRIME64_2;
  735. v1 = XXH_rotl64(v1, 31);
  736. v1 *= PRIME64_1;
  737. h64 ^= v1;
  738. h64 = h64*PRIME64_1 + PRIME64_4;
  739. v2 *= PRIME64_2;
  740. v2 = XXH_rotl64(v2, 31);
  741. v2 *= PRIME64_1;
  742. h64 ^= v2;
  743. h64 = h64*PRIME64_1 + PRIME64_4;
  744. v3 *= PRIME64_2;
  745. v3 = XXH_rotl64(v3, 31);
  746. v3 *= PRIME64_1;
  747. h64 ^= v3;
  748. h64 = h64*PRIME64_1 + PRIME64_4;
  749. v4 *= PRIME64_2;
  750. v4 = XXH_rotl64(v4, 31);
  751. v4 *= PRIME64_1;
  752. h64 ^= v4;
  753. h64 = h64*PRIME64_1 + PRIME64_4;
  754. }
  755. else
  756. {
  757. h64 = state->seed + PRIME64_5;
  758. }
  759. h64 += (U64) state->total_len;
  760. while (p+8<=bEnd)
  761. {
  762. U64 k1 = XXH_readLE64((const U64*)p, endian);
  763. k1 *= PRIME64_2;
  764. k1 = XXH_rotl64(k1,31);
  765. k1 *= PRIME64_1;
  766. h64 ^= k1;
  767. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  768. p+=8;
  769. }
  770. if (p+4<=bEnd)
  771. {
  772. h64 ^= (U64)(XXH_readLE32((const U32*)p, endian)) * PRIME64_1;
  773. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  774. p+=4;
  775. }
  776. while (p<bEnd)
  777. {
  778. h64 ^= (*p) * PRIME64_5;
  779. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  780. p++;
  781. }
  782. h64 ^= h64 >> 33;
  783. h64 *= PRIME64_2;
  784. h64 ^= h64 >> 29;
  785. h64 *= PRIME64_3;
  786. h64 ^= h64 >> 32;
  787. return h64;
  788. }
  789. unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  790. {
  791. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  792. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  793. return XXH64_digest_endian(state_in, XXH_littleEndian);
  794. else
  795. return XXH64_digest_endian(state_in, XXH_bigEndian);
  796. }
  797. //////////////////////////////////////////////////////////////////////////
  798. //-----------------------------------------------------------------------------
  799. // MurmurHash3 was written by Austin Appleby, and is placed in the public
  800. // domain. The author hereby disclaims copyright to this source code.
  801. // Note - The x86 and x64 versions do _not_ produce the same results, as the
  802. // algorithms are optimized for their respective platforms. You can still
  803. // compile and run any of them on any platform, but your performance with the
  804. // non-native version will be less than optimal.
  805. //-----------------------------------------------------------------------------
  806. // Platform-specific functions and macros
  807. // Microsoft Visual Studio
  808. #if defined(_MSC_VER)
  809. #ifndef FORCE_INLINE
  810. #define FORCE_INLINE __forceinline
  811. #endif
  812. #include <stdlib.h>
  813. #define ROTL32(x,y) _rotl(x,y)
  814. #define ROTL64(x,y) _rotl64(x,y)
  815. #define BIG_CONSTANT(x) (x)
  816. // Other compilers
  817. #else // defined(_MSC_VER)
  818. //#define FORCE_INLINE inline __attribute__((always_inline))
  819. inline uint32_t rotl32(uint32_t x, int8_t r)
  820. {
  821. return (x << r) | (x >> (32 - r));
  822. }
  823. inline uint64_t rotl64(uint64_t x, int8_t r)
  824. {
  825. return (x << r) | (x >> (64 - r));
  826. }
  827. #define ROTL32(x,y) rotl32(x,y)
  828. #define ROTL64(x,y) rotl64(x,y)
  829. #define BIG_CONSTANT(x) (x##LLU)
  830. #endif // !defined(_MSC_VER)
  831. //-----------------------------------------------------------------------------
  832. // Block read - if your platform needs to do endian-swapping or can only
  833. // handle aligned reads, do the conversion here
  834. FORCE_INLINE uint32_t getblock32(const uint32_t * p, int i)
  835. {
  836. return p[i];
  837. }
  838. FORCE_INLINE uint64_t getblock64(const uint64_t * p, int i)
  839. {
  840. return p[i];
  841. }
  842. //-----------------------------------------------------------------------------
  843. // Finalization mix - force all bits of a hash block to avalanche
  844. FORCE_INLINE uint32_t fmix32(uint32_t h)
  845. {
  846. h ^= h >> 16;
  847. h *= 0x85ebca6b;
  848. h ^= h >> 13;
  849. h *= 0xc2b2ae35;
  850. h ^= h >> 16;
  851. return h;
  852. }
  853. //----------
  854. FORCE_INLINE uint64_t fmix64(uint64_t k)
  855. {
  856. k ^= k >> 33;
  857. k *= BIG_CONSTANT(0xff51afd7ed558ccd);
  858. k ^= k >> 33;
  859. k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
  860. k ^= k >> 33;
  861. return k;
  862. }
  863. //-----------------------------------------------------------------------------
  864. void MurmurHash3_x86_32(const void * key, int len,
  865. uint32_t seed, void * out)
  866. {
  867. const uint8_t * data = (const uint8_t*) key;
  868. const int nblocks = len / 4;
  869. uint32_t h1 = seed;
  870. const uint32_t c1 = 0xcc9e2d51;
  871. const uint32_t c2 = 0x1b873593;
  872. //----------
  873. // body
  874. const uint32_t * blocks = (const uint32_t *) (data + nblocks * 4);
  875. for (int i = -nblocks; i; i++)
  876. {
  877. uint32_t k1 = getblock32(blocks, i);
  878. k1 *= c1;
  879. k1 = ROTL32(k1, 15);
  880. k1 *= c2;
  881. h1 ^= k1;
  882. h1 = ROTL32(h1, 13);
  883. h1 = h1 * 5 + 0xe6546b64;
  884. }
  885. //----------
  886. // tail
  887. const uint8_t * tail = (const uint8_t*) (data + nblocks * 4);
  888. uint32_t k1 = 0;
  889. switch (len & 3)
  890. {
  891. case 3: k1 ^= tail[2] << 16;
  892. case 2: k1 ^= tail[1] << 8;
  893. case 1: k1 ^= tail[0];
  894. k1 *= c1; k1 = ROTL32(k1, 15); k1 *= c2; h1 ^= k1;
  895. };
  896. //----------
  897. // finalization
  898. h1 ^= len;
  899. h1 = fmix32(h1);
  900. *(uint32_t*) out = h1;
  901. }
  902. //-----------------------------------------------------------------------------
  903. void MurmurHash3_x86_128(const void * key, const int len,
  904. uint32_t seed, void * out)
  905. {
  906. const uint8_t * data = (const uint8_t*) key;
  907. const int nblocks = len / 16;
  908. uint32_t h1 = seed;
  909. uint32_t h2 = seed;
  910. uint32_t h3 = seed;
  911. uint32_t h4 = seed;
  912. const uint32_t c1 = 0x239b961b;
  913. const uint32_t c2 = 0xab0e9789;
  914. const uint32_t c3 = 0x38b34ae5;
  915. const uint32_t c4 = 0xa1e38b93;
  916. //----------
  917. // body
  918. const uint32_t * blocks = (const uint32_t *) (data + nblocks * 16);
  919. for (int i = -nblocks; i; i++)
  920. {
  921. uint32_t k1 = getblock32(blocks, i * 4 + 0);
  922. uint32_t k2 = getblock32(blocks, i * 4 + 1);
  923. uint32_t k3 = getblock32(blocks, i * 4 + 2);
  924. uint32_t k4 = getblock32(blocks, i * 4 + 3);
  925. k1 *= c1; k1 = ROTL32(k1, 15); k1 *= c2; h1 ^= k1;
  926. h1 = ROTL32(h1, 19); h1 += h2; h1 = h1 * 5 + 0x561ccd1b;
  927. k2 *= c2; k2 = ROTL32(k2, 16); k2 *= c3; h2 ^= k2;
  928. h2 = ROTL32(h2, 17); h2 += h3; h2 = h2 * 5 + 0x0bcaa747;
  929. k3 *= c3; k3 = ROTL32(k3, 17); k3 *= c4; h3 ^= k3;
  930. h3 = ROTL32(h3, 15); h3 += h4; h3 = h3 * 5 + 0x96cd1c35;
  931. k4 *= c4; k4 = ROTL32(k4, 18); k4 *= c1; h4 ^= k4;
  932. h4 = ROTL32(h4, 13); h4 += h1; h4 = h4 * 5 + 0x32ac3b17;
  933. }
  934. //----------
  935. // tail
  936. const uint8_t * tail = (const uint8_t*) (data + nblocks * 16);
  937. uint32_t k1 = 0;
  938. uint32_t k2 = 0;
  939. uint32_t k3 = 0;
  940. uint32_t k4 = 0;
  941. switch (len & 15)
  942. {
  943. case 15: k4 ^= tail[14] << 16;
  944. case 14: k4 ^= tail[13] << 8;
  945. case 13: k4 ^= tail[12] << 0;
  946. k4 *= c4; k4 = ROTL32(k4, 18); k4 *= c1; h4 ^= k4;
  947. case 12: k3 ^= tail[11] << 24;
  948. case 11: k3 ^= tail[10] << 16;
  949. case 10: k3 ^= tail[9] << 8;
  950. case 9: k3 ^= tail[8] << 0;
  951. k3 *= c3; k3 = ROTL32(k3, 17); k3 *= c4; h3 ^= k3;
  952. case 8: k2 ^= tail[7] << 24;
  953. case 7: k2 ^= tail[6] << 16;
  954. case 6: k2 ^= tail[5] << 8;
  955. case 5: k2 ^= tail[4] << 0;
  956. k2 *= c2; k2 = ROTL32(k2, 16); k2 *= c3; h2 ^= k2;
  957. case 4: k1 ^= tail[3] << 24;
  958. case 3: k1 ^= tail[2] << 16;
  959. case 2: k1 ^= tail[1] << 8;
  960. case 1: k1 ^= tail[0] << 0;
  961. k1 *= c1; k1 = ROTL32(k1, 15); k1 *= c2; h1 ^= k1;
  962. };
  963. //----------
  964. // finalization
  965. h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
  966. h1 += h2; h1 += h3; h1 += h4;
  967. h2 += h1; h3 += h1; h4 += h1;
  968. h1 = fmix32(h1);
  969. h2 = fmix32(h2);
  970. h3 = fmix32(h3);
  971. h4 = fmix32(h4);
  972. h1 += h2; h1 += h3; h1 += h4;
  973. h2 += h1; h3 += h1; h4 += h1;
  974. ((uint32_t*) out)[0] = h1;
  975. ((uint32_t*) out)[1] = h2;
  976. ((uint32_t*) out)[2] = h3;
  977. ((uint32_t*) out)[3] = h4;
  978. }
  979. //-----------------------------------------------------------------------------
  980. void MurmurHash3_x64_128(const void * key, const int len,
  981. const uint32_t seed, void * out)
  982. {
  983. const uint8_t * data = (const uint8_t*) key;
  984. const int nblocks = len / 16;
  985. uint64_t h1 = seed;
  986. uint64_t h2 = seed;
  987. const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
  988. const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
  989. //----------
  990. // body
  991. const uint64_t * blocks = (const uint64_t *) (data);
  992. for (int i = 0; i < nblocks; i++)
  993. {
  994. uint64_t k1 = getblock64(blocks, i * 2 + 0);
  995. uint64_t k2 = getblock64(blocks, i * 2 + 1);
  996. k1 *= c1; k1 = ROTL64(k1, 31); k1 *= c2; h1 ^= k1;
  997. h1 = ROTL64(h1, 27); h1 += h2; h1 = h1 * 5 + 0x52dce729;
  998. k2 *= c2; k2 = ROTL64(k2, 33); k2 *= c1; h2 ^= k2;
  999. h2 = ROTL64(h2, 31); h2 += h1; h2 = h2 * 5 + 0x38495ab5;
  1000. }
  1001. //----------
  1002. // tail
  1003. const uint8_t * tail = (const uint8_t*) (data + nblocks * 16);
  1004. uint64_t k1 = 0;
  1005. uint64_t k2 = 0;
  1006. switch (len & 15)
  1007. {
  1008. case 15: k2 ^= ((uint64_t) tail[14]) << 48;
  1009. case 14: k2 ^= ((uint64_t) tail[13]) << 40;
  1010. case 13: k2 ^= ((uint64_t) tail[12]) << 32;
  1011. case 12: k2 ^= ((uint64_t) tail[11]) << 24;
  1012. case 11: k2 ^= ((uint64_t) tail[10]) << 16;
  1013. case 10: k2 ^= ((uint64_t) tail[9]) << 8;
  1014. case 9: k2 ^= ((uint64_t) tail[8]) << 0;
  1015. k2 *= c2; k2 = ROTL64(k2, 33); k2 *= c1; h2 ^= k2;
  1016. case 8: k1 ^= ((uint64_t) tail[7]) << 56;
  1017. case 7: k1 ^= ((uint64_t) tail[6]) << 48;
  1018. case 6: k1 ^= ((uint64_t) tail[5]) << 40;
  1019. case 5: k1 ^= ((uint64_t) tail[4]) << 32;
  1020. case 4: k1 ^= ((uint64_t) tail[3]) << 24;
  1021. case 3: k1 ^= ((uint64_t) tail[2]) << 16;
  1022. case 2: k1 ^= ((uint64_t) tail[1]) << 8;
  1023. case 1: k1 ^= ((uint64_t) tail[0]) << 0;
  1024. k1 *= c1; k1 = ROTL64(k1, 31); k1 *= c2; h1 ^= k1;
  1025. };
  1026. //----------
  1027. // finalization
  1028. h1 ^= len; h2 ^= len;
  1029. h1 += h2;
  1030. h2 += h1;
  1031. h1 = fmix64(h1);
  1032. h2 = fmix64(h2);
  1033. h1 += h2;
  1034. h2 += h1;
  1035. ((uint64_t*) out)[0] = h1;
  1036. ((uint64_t*) out)[1] = h2;
  1037. }
  1038. //////////////////////////////////////////////////////////////////////////
  1039. /*
  1040. * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
  1041. * MD5 Message-Digest Algorithm (RFC 1321).
  1042. *
  1043. * Homepage:
  1044. * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
  1045. *
  1046. * Author:
  1047. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  1048. *
  1049. * This software was written by Alexander Peslyak in 2001. No copyright is
  1050. * claimed, and the software is hereby placed in the public domain.
  1051. * In case this attempt to disclaim copyright and place the software in the
  1052. * public domain is deemed null and void, then the software is
  1053. * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
  1054. * general public under the following terms:
  1055. *
  1056. * Redistribution and use in source and binary forms, with or without
  1057. * modification, are permitted.
  1058. *
  1059. * There's ABSOLUTELY NO WARRANTY, express or implied.
  1060. *
  1061. * (This is a heavily cut-down "BSD license".)
  1062. *
  1063. * This differs from Colin Plumb's older public domain implementation in that
  1064. * no exactly 32-bit integer data type is required (any 32-bit or wider
  1065. * unsigned integer data type will do), there's no compile-time endianness
  1066. * configuration, and the function prototypes match OpenSSL's. No code from
  1067. * Colin Plumb's implementation has been reused; this comment merely compares
  1068. * the properties of the two independent implementations.
  1069. *
  1070. * The primary goals of this implementation are portability and ease of use.
  1071. * It is meant to be fast, but not as fast as possible. Some known
  1072. * optimizations are not included to reduce source code size and avoid
  1073. * compile-time configuration.
  1074. */
  1075. /* Any 32-bit or wider unsigned integer data type will do */
  1076. typedef unsigned int MD5_u32plus;
  1077. typedef struct {
  1078. MD5_u32plus lo, hi;
  1079. MD5_u32plus a, b, c, d;
  1080. unsigned char buffer[64];
  1081. MD5_u32plus block[16];
  1082. } MD5_CTX;
  1083. /*
  1084. * The basic MD5 functions.
  1085. *
  1086. * F and G are optimized compared to their RFC 1321 definitions for
  1087. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  1088. * implementation.
  1089. */
  1090. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  1091. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  1092. #define H(x, y, z) (((x) ^ (y)) ^ (z))
  1093. #define H2(x, y, z) ((x) ^ ((y) ^ (z)))
  1094. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  1095. /*
  1096. * The MD5 transformation for all four rounds.
  1097. */
  1098. #define STEP(f, a, b, c, d, x, t, s) \
  1099. (a) += f((b), (c), (d)) + (x) + (t); \
  1100. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  1101. (a) += (b);
  1102. /*
  1103. * SET reads 4 input bytes in little-endian byte order and stores them in a
  1104. * properly aligned word in host byte order.
  1105. *
  1106. * The check for little-endian architectures that tolerate unaligned memory
  1107. * accesses is just an optimization. Nothing will break if it fails to detect
  1108. * a suitable architecture.
  1109. *
  1110. * Unfortunately, this optimization may be a C strict aliasing rules violation
  1111. * if the caller's data buffer has effective type that cannot be aliased by
  1112. * MD5_u32plus. In practice, this problem may occur if these MD5 routines are
  1113. * inlined into a calling function, or with future and dangerously advanced
  1114. * link-time optimizations. For the time being, keeping these MD5 routines in
  1115. * their own translation unit avoids the problem.
  1116. */
  1117. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  1118. #define SET(n) \
  1119. (*(MD5_u32plus *)&ptr[(n) * 4])
  1120. #define GET(n) \
  1121. SET(n)
  1122. #else
  1123. #define SET(n) \
  1124. (ctx->block[(n)] = \
  1125. (MD5_u32plus)ptr[(n) * 4] | \
  1126. ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
  1127. ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
  1128. ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
  1129. #define GET(n) \
  1130. (ctx->block[(n)])
  1131. #endif
  1132. /*
  1133. * This processes one or more 64-byte data blocks, but does NOT update the bit
  1134. * counters. There are no alignment requirements.
  1135. */
  1136. static const void *body(MD5_CTX *ctx, const void *data, unsigned long size)
  1137. {
  1138. const unsigned char *ptr;
  1139. MD5_u32plus a, b, c, d;
  1140. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  1141. ptr = (const unsigned char *)data;
  1142. a = ctx->a;
  1143. b = ctx->b;
  1144. c = ctx->c;
  1145. d = ctx->d;
  1146. do {
  1147. saved_a = a;
  1148. saved_b = b;
  1149. saved_c = c;
  1150. saved_d = d;
  1151. /* Round 1 */
  1152. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  1153. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  1154. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  1155. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  1156. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  1157. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  1158. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  1159. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  1160. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  1161. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  1162. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  1163. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  1164. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  1165. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  1166. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  1167. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  1168. /* Round 2 */
  1169. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  1170. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  1171. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  1172. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  1173. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  1174. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  1175. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  1176. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  1177. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  1178. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  1179. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  1180. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  1181. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  1182. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  1183. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  1184. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  1185. /* Round 3 */
  1186. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  1187. STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
  1188. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  1189. STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
  1190. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  1191. STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  1192. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  1193. STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
  1194. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  1195. STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
  1196. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  1197. STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
  1198. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  1199. STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
  1200. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  1201. STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
  1202. /* Round 4 */
  1203. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  1204. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  1205. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  1206. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  1207. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  1208. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  1209. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  1210. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  1211. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  1212. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  1213. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  1214. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  1215. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  1216. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  1217. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  1218. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  1219. a += saved_a;
  1220. b += saved_b;
  1221. c += saved_c;
  1222. d += saved_d;
  1223. ptr += 64;
  1224. } while (size -= 64);
  1225. ctx->a = a;
  1226. ctx->b = b;
  1227. ctx->c = c;
  1228. ctx->d = d;
  1229. return ptr;
  1230. }
  1231. void MD5_Init(MD5_CTX *ctx)
  1232. {
  1233. ctx->a = 0x67452301;
  1234. ctx->b = 0xefcdab89;
  1235. ctx->c = 0x98badcfe;
  1236. ctx->d = 0x10325476;
  1237. ctx->lo = 0;
  1238. ctx->hi = 0;
  1239. }
  1240. void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
  1241. {
  1242. MD5_u32plus saved_lo;
  1243. unsigned long used, available;
  1244. saved_lo = ctx->lo;
  1245. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  1246. ctx->hi++;
  1247. ctx->hi += size >> 29;
  1248. used = saved_lo & 0x3f;
  1249. if (used) {
  1250. available = 64 - used;
  1251. if (size < available) {
  1252. memcpy(&ctx->buffer[used], data, size);
  1253. return;
  1254. }
  1255. memcpy(&ctx->buffer[used], data, available);
  1256. data = (const unsigned char *)data + available;
  1257. size -= available;
  1258. body(ctx, ctx->buffer, 64);
  1259. }
  1260. if (size >= 64) {
  1261. data = body(ctx, data, size & ~(unsigned long)0x3f);
  1262. size &= 0x3f;
  1263. }
  1264. memcpy(ctx->buffer, data, size);
  1265. }
  1266. #define MD5_OUT(dst, src) \
  1267. (dst)[0] = (unsigned char)(src); \
  1268. (dst)[1] = (unsigned char)((src) >> 8); \
  1269. (dst)[2] = (unsigned char)((src) >> 16); \
  1270. (dst)[3] = (unsigned char)((src) >> 24);
  1271. void MD5_Final(unsigned char *result, MD5_CTX *ctx)
  1272. {
  1273. unsigned long used, available;
  1274. used = ctx->lo & 0x3f;
  1275. ctx->buffer[used++] = 0x80;
  1276. available = 64 - used;
  1277. if (available < 8) {
  1278. memset(&ctx->buffer[used], 0, available);
  1279. body(ctx, ctx->buffer, 64);
  1280. used = 0;
  1281. available = 64;
  1282. }
  1283. memset(&ctx->buffer[used], 0, available - 8);
  1284. ctx->lo <<= 3;
  1285. MD5_OUT(&ctx->buffer[56], ctx->lo)
  1286. MD5_OUT(&ctx->buffer[60], ctx->hi)
  1287. body(ctx, ctx->buffer, 64);
  1288. MD5_OUT(&result[0], ctx->a)
  1289. MD5_OUT(&result[4], ctx->b)
  1290. MD5_OUT(&result[8], ctx->c)
  1291. MD5_OUT(&result[12], ctx->d)
  1292. memset(ctx, 0, sizeof(*ctx));
  1293. }
  1294. //////////////////////////////////////////////////////////////////////////
  1295. USING_NS_BF;
  1296. uint64 Beefy::Hash64(const void* data, int length, uint64 seed)
  1297. {
  1298. return XXH64(data, length, seed);
  1299. }
  1300. uint64 Beefy::Hash64(uint64 hash, uint64 seed)
  1301. {
  1302. //return XXH64((const void*)&hash, sizeof(uint64), seed);
  1303. return fmix64(seed) ^ hash;
  1304. }
  1305. Val128 Beefy::Hash128(const void* data, int length)
  1306. {
  1307. Val128 hashVal;
  1308. MurmurHash3_x64_128(data, length, 0, &hashVal);
  1309. return hashVal;
  1310. }
  1311. Val128 Beefy::Hash128(const void* data, int length, const Val128& seed)
  1312. {
  1313. if (length < 8)
  1314. {
  1315. uint64 iVal = 0;
  1316. memcpy(&iVal, data, length);
  1317. Val128 val;
  1318. val.mLow = fmix64(seed.mLow) ^ iVal;
  1319. val.mHigh = val.mLow ^ seed.mHigh;
  1320. return val;
  1321. }
  1322. Val128 hashVal;
  1323. MurmurHash3_x64_128(data, length, (uint32)seed.mLow, &hashVal);
  1324. hashVal.mHigh ^= fmix64(seed.mHigh);
  1325. hashVal.mLow ^= fmix64(seed.mLow);
  1326. return hashVal;
  1327. }
  1328. Val128 Beefy::HashMD5(const void* data, int length)
  1329. {
  1330. Val128 result;
  1331. MD5_CTX ctx;
  1332. MD5_Init(&ctx);
  1333. MD5_Update(&ctx, data, length);
  1334. MD5_Final((unsigned char*)&result, &ctx);
  1335. return result;
  1336. }
  1337. //////////////////////////////////////////////////////////////////////////
  1338. // Only 63 chars - skip zero
  1339. static const char cHash64bToChar[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  1340. 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
  1341. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  1342. 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' };
  1343. String Beefy::HashEncode64(uint64 val)
  1344. {
  1345. String outStr;
  1346. if ((int64)val < 0)
  1347. {
  1348. uint64 flippedNum = (uint64)-(int64)val;
  1349. // Only flip if the encoded result would actually be shorter
  1350. if (flippedNum <= 0x00FFFFFFFFFFFFFFLL)
  1351. {
  1352. val = flippedNum;
  1353. outStr.Append('_');
  1354. }
  1355. }
  1356. for (int i = 0; i < 10; i++)
  1357. {
  1358. int charIdx = (int)((val >> (i * 6)) & 0x3F) - 1;
  1359. if (charIdx != -1)
  1360. outStr.Append(cHash64bToChar[charIdx]);
  1361. }
  1362. return outStr;
  1363. }
  1364. StringT<21> Beefy::HashEncode128(Val128 val)
  1365. {
  1366. StringT<21> str;
  1367. str += HashEncode64(val.mHigh);
  1368. str += HashEncode64(val.mLow);
  1369. return str;
  1370. }
  1371. //////////////////////////////////////////////////////////////////////////
  1372. static int gDbgVizIdx = 0;
  1373. HashContext::~HashContext()
  1374. {
  1375. #ifdef BF_PLATFORM_WINDOWS
  1376. delete mDbgVizStream;
  1377. #endif
  1378. }
  1379. void HashContext::Reset()
  1380. {
  1381. mBufSize = 0;
  1382. mBufOffset = 0;
  1383. }
  1384. void HashContext::Mixin(const void* data, int size)
  1385. {
  1386. if (size >= 1024)
  1387. {
  1388. Val128 hashVal = Hash128(data, size);
  1389. Mixin(&hashVal, 16);
  1390. return;
  1391. }
  1392. while (size > 0)
  1393. {
  1394. int addBytes = std::min(size, 1024 - mBufSize);
  1395. #ifdef BF_PLATFORM_WINDOWS
  1396. if (mDbgViz)
  1397. {
  1398. int findIdx = 0x2cc159;
  1399. if ((mBufOffset + mBufSize <= findIdx) && (mBufOffset + mBufSize + addBytes > findIdx))
  1400. {
  1401. NOP;
  1402. }
  1403. }
  1404. #endif
  1405. memcpy(&mBuf[mBufSize], data, addBytes);
  1406. mBufSize += addBytes;
  1407. size -= addBytes;
  1408. data = (uint8*)data + addBytes;
  1409. if (mBufSize == 1024)
  1410. {
  1411. Val128 val = Finish128();
  1412. *(Val128*)mBuf = val;
  1413. mBufSize = 16;
  1414. }
  1415. }
  1416. }
  1417. void HashContext::MixinHashContext(HashContext& ctx)
  1418. {
  1419. Mixin(ctx.mBufSize);
  1420. Mixin(ctx.mBuf, ctx.mBufSize);
  1421. }
  1422. void HashContext::MixinStr(const char* str)
  1423. {
  1424. int len = (int)strlen(str);
  1425. Mixin(len);
  1426. Mixin(str, len);
  1427. }
  1428. void HashContext::MixinStr(const StringImpl& str)
  1429. {
  1430. int len = (int)str.length();
  1431. Mixin(len);
  1432. Mixin(str.c_str(), len);
  1433. }
  1434. Val128 HashContext::Finish128()
  1435. {
  1436. #ifdef BF_PLATFORM_WINDOWS
  1437. if (mDbgViz)
  1438. {
  1439. // String dbg = "HashContext Dbg";
  1440. // for (int i = 0; i < mBufSize; i++)
  1441. // dbg += StrFormat(" %X:%02X", i, mBuf[i]);
  1442. // dbg += "\n";
  1443. // OutputDebugStr(dbg);
  1444. if (mDbgVizStream == NULL)
  1445. {
  1446. String filePath = StrFormat("c:\\temp\\hash%d.bin", gDbgVizIdx++);
  1447. mDbgVizStream = new FileStream();
  1448. if (mDbgVizStream->Open(filePath, "wb"))
  1449. {
  1450. OutputDebugStrF("Creating dbg hash: %s\n", filePath.c_str());
  1451. }
  1452. else
  1453. {
  1454. OutputDebugStrF("FAILED creating dbg hash: %s\n", filePath.c_str());
  1455. }
  1456. }
  1457. if ((mDbgVizStream != NULL) && (mDbgVizStream->IsOpen()))
  1458. mDbgVizStream->Write(mBuf, mBufSize);
  1459. }
  1460. #endif
  1461. if (mBufSize <= 16)
  1462. {
  1463. // We do this copy because 'result' gets zero-initialized and then we copy in any applicable data
  1464. Val128 result;
  1465. memcpy(&result, mBuf, mBufSize);
  1466. mBufOffset += mBufSize;
  1467. return result;
  1468. }
  1469. Val128 val = Hash128(mBuf, mBufSize);
  1470. mBufOffset += mBufSize;
  1471. mBufSize = 0;
  1472. return val;
  1473. }
  1474. uint64 HashContext::Finish64()
  1475. {
  1476. #ifdef BF_PLATFORM_WINDOWS
  1477. if (mDbgViz)
  1478. {
  1479. // String dbg = "HashContext Dbg";
  1480. // for (int i = 0; i < mBufSize; i++)
  1481. // dbg += StrFormat(" %X:%02X", i, mBuf[i]);
  1482. // dbg += "\n";
  1483. // OutputDebugStr(dbg);
  1484. if (mDbgVizStream == NULL)
  1485. {
  1486. String filePath = StrFormat("c:\\temp\\hash%d.bin", gDbgVizIdx++);
  1487. mDbgVizStream = new FileStream();
  1488. mDbgVizStream->Open(filePath, "wb");
  1489. OutputDebugStrF("Creating dbg hash: %s\n", filePath.c_str());
  1490. }
  1491. mDbgVizStream->Write(mBuf, mBufSize);
  1492. }
  1493. #endif
  1494. if (mBufSize <= 8)
  1495. {
  1496. // We do this copy because 'result' gets zero-initialized and then we copy in any applicable data
  1497. uint64 result = 0;
  1498. memcpy(&result, mBuf, mBufSize);
  1499. mBufOffset += mBufSize;
  1500. return result;
  1501. }
  1502. uint64 val = Hash64(mBuf, mBufSize);
  1503. mBufOffset += mBufSize;
  1504. mBufSize = 0;
  1505. return val;
  1506. }