1 /*
2  * List of hashes for the brute force collision tester
3  * Part of xxHash project
4  * Copyright (C) 2019-2020 Yann Collet
5  *
6  * GPL v2 License
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * You can contact the author at:
23  * - xxHash homepage: https://www.xxhash.com
24  * - xxHash source repository: https://github.com/Cyan4973/xxHash
25  */
26 
27 #ifndef HASHES_H_1235465
28 #define HASHES_H_1235465
29 
30 #include <stddef.h>      /* size_t */
31 #include <stdint.h>      /* uint64_t */
32 #define XXH_INLINE_ALL   /* XXH128_hash_t */
33 #include "xxhash.h"
34 
35 
36 /* return type */
37 
38 typedef union {
39     uint64_t       h64;
40     XXH128_hash_t h128;
41 } UniHash;
42 
uniHash32(uint64_t v32)43 UniHash uniHash32(uint64_t v32)
44 {   UniHash unih;
45     unih.h64 = v32;
46     return unih;
47 }
48 
uniHash64(uint64_t v64)49 UniHash uniHash64(uint64_t v64)
50 {   UniHash unih;
51     unih.h64 = v64;
52     return unih;
53 }
54 
uniHash128(XXH128_hash_t v128)55 UniHash uniHash128(XXH128_hash_t v128)
56 {   UniHash unih;
57     unih.h128 = v128;
58     return unih;
59 }
60 
61 
62 /* ===  xxHash  === */
63 
XXH3_wrapper(const void * data,size_t size)64 UniHash XXH3_wrapper (const void* data, size_t size)
65 {
66     return uniHash64( XXH3_64bits(data, size) );
67 }
68 
XXH128_wrapper(const void * data,size_t size)69 UniHash XXH128_wrapper (const void* data, size_t size)
70 {
71     return uniHash128( XXH3_128bits(data, size) );
72 }
73 
XXH128l_wrapper(const void * data,size_t size)74 UniHash XXH128l_wrapper (const void* data, size_t size)
75 {
76     return uniHash64( XXH3_128bits(data, size).low64 );
77 }
78 
XXH128h_wrapper(const void * data,size_t size)79 UniHash XXH128h_wrapper (const void* data, size_t size)
80 {
81     return uniHash64( XXH3_128bits(data, size).high64 );
82 }
83 
XXH64_wrapper(const void * data,size_t size)84 UniHash XXH64_wrapper (const void* data, size_t size)
85 {
86     return uniHash64 ( XXH64(data, size, 0) );
87 }
88 
XXH32_wrapper(const void * data,size_t size)89 UniHash XXH32_wrapper (const void* data, size_t size)
90 {
91     return uniHash32( XXH32(data, size, 0) );
92 }
93 
94 /* ===  Dummy integration example  === */
95 
96 #include "dummy.h"
97 
badsum32_wrapper(const void * data,size_t size)98 UniHash badsum32_wrapper (const void* data, size_t size)
99 {
100     return uniHash32( badsum32(data, size, 0) );
101 }
102 
103 
104 
105 /* ===  Table  === */
106 
107 typedef UniHash (*hashfn) (const void* data, size_t size);
108 
109 typedef struct {
110     const char* name;
111     hashfn fn;
112     int bits;
113 } hashDescription;
114 
115 #define HASH_FN_TOTAL 7
116 
117 hashDescription hashfnTable[HASH_FN_TOTAL] = {
118     { "xxh3"  ,  XXH3_wrapper,     64 },
119     { "xxh64" ,  XXH64_wrapper,    64 },
120     { "xxh128",  XXH128_wrapper,  128 },
121     { "xxh128l", XXH128l_wrapper,  64 },
122     { "xxh128h", XXH128h_wrapper,  64 },
123     { "xxh32" ,  XXH32_wrapper,    32 },
124     { "badsum32",badsum32_wrapper, 32 },
125 };
126 
127 #endif   /* HASHES_H_1235465 */
128