xxhash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. xxHash - Fast Hash algorithm
  3. Copyright (C) 2012-2014, Yann Collet.
  4. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - xxHash source repository : http://code.google.com/p/xxhash/
  27. */
  28. #include "archive_platform.h"
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "archive_xxhash.h"
  32. #ifdef HAVE_LIBLZ4
  33. /***************************************
  34. ** Tuning parameters
  35. ****************************************/
  36. /* Unaligned memory access is automatically enabled for "common" CPU, such as x86.
  37. ** For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected.
  38. ** If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance.
  39. ** You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32).
  40. */
  41. #if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  42. # define XXH_USE_UNALIGNED_ACCESS 1
  43. #endif
  44. /* XXH_ACCEPT_NULL_INPUT_POINTER :
  45. ** If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
  46. ** When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
  47. ** This option has a very small performance cost (only measurable on small inputs).
  48. ** By default, this option is disabled. To enable it, uncomment below define :
  49. ** #define XXH_ACCEPT_NULL_INPUT_POINTER 1
  50. ** XXH_FORCE_NATIVE_FORMAT :
  51. ** By default, xxHash library provides endian-independent Hash values, based on little-endian convention.
  52. ** Results are therefore identical for little-endian and big-endian CPU.
  53. ** This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  54. ** Should endian-independence be of no importance for your application, you may set the #define below to 1.
  55. ** It will improve speed for Big-endian CPU.
  56. ** This option has no impact on Little_Endian CPU.
  57. */
  58. #define XXH_FORCE_NATIVE_FORMAT 0
  59. /***************************************
  60. ** Compiler Specific Options
  61. ****************************************/
  62. /* Disable some Visual warning messages */
  63. #ifdef _MSC_VER /* Visual Studio */
  64. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  65. #endif
  66. #ifdef _MSC_VER /* Visual Studio */
  67. # define FORCE_INLINE __forceinline
  68. #else
  69. # ifdef __GNUC__
  70. # define FORCE_INLINE inline __attribute__((always_inline))
  71. # else
  72. # define FORCE_INLINE inline
  73. # endif
  74. #endif
  75. /***************************************
  76. ** Includes & Memory related functions
  77. ****************************************/
  78. #define XXH_malloc malloc
  79. #define XXH_free free
  80. #define XXH_memcpy memcpy
  81. static unsigned int XXH32 (const void*, unsigned int, unsigned int);
  82. static void* XXH32_init (unsigned int);
  83. static XXH_errorcode XXH32_update (void*, const void*, unsigned int);
  84. static unsigned int XXH32_digest (void*);
  85. /*static int XXH32_sizeofState(void);*/
  86. static XXH_errorcode XXH32_resetState(void*, unsigned int);
  87. #define XXH32_SIZEOFSTATE 48
  88. typedef struct { long long ll[(XXH32_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH32_stateSpace_t;
  89. static unsigned int XXH32_intermediateDigest (void*);
  90. /***************************************
  91. ** Basic Types
  92. ****************************************/
  93. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  94. # include <stdint.h>
  95. typedef uint8_t BYTE;
  96. typedef uint16_t U16;
  97. typedef uint32_t U32;
  98. typedef int32_t S32;
  99. typedef uint64_t U64;
  100. #else
  101. typedef unsigned char BYTE;
  102. typedef unsigned short U16;
  103. typedef unsigned int U32;
  104. typedef signed int S32;
  105. typedef unsigned long long U64;
  106. #endif
  107. #if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS)
  108. # define _PACKED __attribute__ ((packed))
  109. #else
  110. # define _PACKED
  111. #endif
  112. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  113. # ifdef __IBMC__
  114. # pragma pack(1)
  115. # else
  116. # pragma pack(push, 1)
  117. # endif
  118. #endif
  119. typedef struct _U32_S { U32 v; } _PACKED U32_S;
  120. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  121. # pragma pack(pop)
  122. #endif
  123. /****************************************
  124. ** Compiler-specific Functions and Macros
  125. *****************************************/
  126. #define GCC_VERSION ((__GNUC__-0) * 100 + (__GNUC_MINOR__ - 0))
  127. #if GCC_VERSION >= 409
  128. __attribute__((__no_sanitize_undefined__))
  129. #endif
  130. static inline U32 A32(const void * x)
  131. {
  132. return (((const U32_S *)(x))->v);
  133. }
  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. #else
  138. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  139. #endif
  140. #if defined(_MSC_VER) /* Visual Studio */
  141. # define XXH_swap32 _byteswap_ulong
  142. #elif GCC_VERSION >= 403
  143. # define XXH_swap32 __builtin_bswap32
  144. #else
  145. static inline U32 XXH_swap32 (U32 x) {
  146. return ((x << 24) & 0xff000000 ) |
  147. ((x << 8) & 0x00ff0000 ) |
  148. ((x >> 8) & 0x0000ff00 ) |
  149. ((x >> 24) & 0x000000ff );}
  150. #endif
  151. /***************************************
  152. ** Constants
  153. ****************************************/
  154. #define PRIME32_1 2654435761U
  155. #define PRIME32_2 2246822519U
  156. #define PRIME32_3 3266489917U
  157. #define PRIME32_4 668265263U
  158. #define PRIME32_5 374761393U
  159. /***************************************
  160. ** Architecture Macros
  161. ****************************************/
  162. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  163. #ifndef XXH_CPU_LITTLE_ENDIAN /* It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch */
  164. static const int one = 1;
  165. # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&one))
  166. #endif
  167. /***************************************
  168. ** Macros
  169. ****************************************/
  170. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } /* use only *after* variable declarations */
  171. /*****************************
  172. ** Memory reads
  173. ******************************/
  174. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  175. static
  176. FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align)
  177. {
  178. if (align==XXH_unaligned)
  179. return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
  180. else
  181. return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
  182. }
  183. static
  184. FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); }
  185. /*****************************
  186. ** Simple Hash Functions
  187. ******************************/
  188. static
  189. FORCE_INLINE U32 XXH32_endian_align(const void* input, unsigned int len, U32 seed, XXH_endianess endian, XXH_alignment align)
  190. {
  191. const BYTE* p = (const BYTE*)input;
  192. const BYTE* bEnd = p + len;
  193. U32 h32;
  194. #define XXH_get32bits(p) XXH_readLE32_align((const U32*)p, endian, align)
  195. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  196. if (p==NULL) { len=0; bEnd=p=(const BYTE*)(size_t)16; }
  197. #endif
  198. if (len>=16)
  199. {
  200. const BYTE* const limit = bEnd - 16;
  201. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  202. U32 v2 = seed + PRIME32_2;
  203. U32 v3 = seed + 0;
  204. U32 v4 = seed - PRIME32_1;
  205. do
  206. {
  207. v1 += XXH_get32bits(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
  208. v2 += XXH_get32bits(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
  209. v3 += XXH_get32bits(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
  210. v4 += XXH_get32bits(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
  211. } while (p<=limit);
  212. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  213. }
  214. else
  215. {
  216. h32 = seed + PRIME32_5;
  217. }
  218. h32 += (U32) len;
  219. while (p<=bEnd-4)
  220. {
  221. h32 += XXH_get32bits(p) * PRIME32_3;
  222. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  223. p+=4;
  224. }
  225. while (p<bEnd)
  226. {
  227. h32 += (*p) * PRIME32_5;
  228. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  229. p++;
  230. }
  231. h32 ^= h32 >> 15;
  232. h32 *= PRIME32_2;
  233. h32 ^= h32 >> 13;
  234. h32 *= PRIME32_3;
  235. h32 ^= h32 >> 16;
  236. return h32;
  237. }
  238. U32 XXH32(const void* input, unsigned int len, U32 seed)
  239. {
  240. #if 0
  241. // Simple version, good for code maintenance, but unfortunately slow for small inputs
  242. void* state = XXH32_init(seed);
  243. XXH32_update(state, input, len);
  244. return XXH32_digest(state);
  245. #else
  246. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  247. # if !defined(XXH_USE_UNALIGNED_ACCESS)
  248. if ((((size_t)input) & 3) == 0) /* Input is aligned, let's leverage the speed advantage */
  249. {
  250. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  251. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  252. else
  253. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  254. }
  255. # endif
  256. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  257. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  258. else
  259. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  260. #endif
  261. }
  262. /*****************************
  263. ** Advanced Hash Functions
  264. ******************************/
  265. struct XXH_state32_t
  266. {
  267. U64 total_len;
  268. U32 seed;
  269. U32 v1;
  270. U32 v2;
  271. U32 v3;
  272. U32 v4;
  273. int memsize;
  274. char memory[16];
  275. };
  276. #if 0
  277. static
  278. int XXH32_sizeofState(void)
  279. {
  280. XXH_STATIC_ASSERT(XXH32_SIZEOFSTATE >= sizeof(struct XXH_state32_t)); /* A compilation error here means XXH32_SIZEOFSTATE is not large enough */
  281. return sizeof(struct XXH_state32_t);
  282. }
  283. #endif
  284. static
  285. XXH_errorcode XXH32_resetState(void* state_in, U32 seed)
  286. {
  287. struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
  288. state->seed = seed;
  289. state->v1 = seed + PRIME32_1 + PRIME32_2;
  290. state->v2 = seed + PRIME32_2;
  291. state->v3 = seed + 0;
  292. state->v4 = seed - PRIME32_1;
  293. state->total_len = 0;
  294. state->memsize = 0;
  295. return XXH_OK;
  296. }
  297. static
  298. void* XXH32_init (U32 seed)
  299. {
  300. void* state = XXH_malloc (sizeof(struct XXH_state32_t));
  301. XXH32_resetState(state, seed);
  302. return state;
  303. }
  304. static
  305. FORCE_INLINE XXH_errorcode XXH32_update_endian (void* state_in, const void* input, int len, XXH_endianess endian)
  306. {
  307. struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
  308. const BYTE* p = (const BYTE*)input;
  309. const BYTE* const bEnd = p + len;
  310. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  311. if (input==NULL) return XXH_ERROR;
  312. #endif
  313. state->total_len += len;
  314. if (state->memsize + len < 16) /* fill in tmp buffer */
  315. {
  316. XXH_memcpy(state->memory + state->memsize, input, len);
  317. state->memsize += len;
  318. return XXH_OK;
  319. }
  320. if (state->memsize) /* some data left from previous update */
  321. {
  322. XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize);
  323. {
  324. const U32* p32 = (const U32*)state->memory;
  325. state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++;
  326. state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++;
  327. state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++;
  328. state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++;
  329. }
  330. p += 16-state->memsize;
  331. state->memsize = 0;
  332. }
  333. if (p <= bEnd-16)
  334. {
  335. const BYTE* const limit = bEnd - 16;
  336. U32 v1 = state->v1;
  337. U32 v2 = state->v2;
  338. U32 v3 = state->v3;
  339. U32 v4 = state->v4;
  340. do
  341. {
  342. v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
  343. v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
  344. v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
  345. v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
  346. } while (p<=limit);
  347. state->v1 = v1;
  348. state->v2 = v2;
  349. state->v3 = v3;
  350. state->v4 = v4;
  351. }
  352. if (p < bEnd)
  353. {
  354. XXH_memcpy(state->memory, p, bEnd-p);
  355. state->memsize = (int)(bEnd-p);
  356. }
  357. return XXH_OK;
  358. }
  359. static
  360. XXH_errorcode XXH32_update (void* state_in, const void* input, unsigned int len)
  361. {
  362. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  363. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  364. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  365. else
  366. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  367. }
  368. static
  369. FORCE_INLINE U32 XXH32_intermediateDigest_endian (void* state_in, XXH_endianess endian)
  370. {
  371. struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
  372. const BYTE * p = (const BYTE*)state->memory;
  373. BYTE* bEnd = (BYTE*)state->memory + state->memsize;
  374. U32 h32;
  375. if (state->total_len >= 16)
  376. {
  377. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  378. }
  379. else
  380. {
  381. h32 = state->seed + PRIME32_5;
  382. }
  383. h32 += (U32) state->total_len;
  384. while (p<=bEnd-4)
  385. {
  386. h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3;
  387. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  388. p+=4;
  389. }
  390. while (p<bEnd)
  391. {
  392. h32 += (*p) * PRIME32_5;
  393. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  394. p++;
  395. }
  396. h32 ^= h32 >> 15;
  397. h32 *= PRIME32_2;
  398. h32 ^= h32 >> 13;
  399. h32 *= PRIME32_3;
  400. h32 ^= h32 >> 16;
  401. return h32;
  402. }
  403. static
  404. U32 XXH32_intermediateDigest (void* state_in)
  405. {
  406. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  407. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  408. return XXH32_intermediateDigest_endian(state_in, XXH_littleEndian);
  409. else
  410. return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian);
  411. }
  412. static
  413. U32 XXH32_digest (void* state_in)
  414. {
  415. U32 h32 = XXH32_intermediateDigest(state_in);
  416. XXH_free(state_in);
  417. return h32;
  418. }
  419. const
  420. struct archive_xxhash __archive_xxhash = {
  421. XXH32,
  422. XXH32_init,
  423. XXH32_update,
  424. XXH32_digest
  425. };
  426. #else
  427. /*
  428. * Define an empty version of the struct if we aren't using the LZ4 library.
  429. */
  430. const
  431. struct archive_xxhash __archive_xxhash = {
  432. NULL,
  433. NULL,
  434. NULL,
  435. NULL
  436. };
  437. #endif /* HAVE_LIBLZ4 */