-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash.h
236 lines (211 loc) · 5.35 KB
/
hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Simple hash number data type and handler functions
*
* Authors: Simon Kuenzer <simon.kuenzer@neclab.eu>
*
*
* Copyright (c) 2013-2017, NEC Europe Ltd., NEC Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef _HASH_H_
#define _HASH_H_
#ifndef __KERNEL__
#include <string.h>
#endif
typedef uint8_t hash512_t[64] __attribute__((aligned(8)));
static inline void hash_copy(hash512_t dst, const hash512_t src, uint8_t hlen)
{
#ifdef __x86_64
register uint8_t nbleft = hlen & 0x07; /* = mod by 8 */
register uint64_t mask64;
register uint64_t *p64_dst;
register uint64_t *p64_src;
register uint8_t i;
hlen -= nbleft;
i = 0;
for (; i < hlen; i += 8) {
p64_dst = (uint64_t *) &dst[i];
p64_src = (uint64_t *) &src[i];
*p64_dst = *p64_src;
}
if (nbleft) {
mask64 = ((uint64_t) 1 << (nbleft << 3)) - 1;
p64_dst = (uint64_t *) &dst[i];
p64_src = (uint64_t *) &src[i];
*p64_dst = *p64_src & mask64;
}
#else
memcpy(dst, src, hlen);
#endif
}
static inline int hash_compare(const hash512_t h0, const hash512_t h1, uint8_t hlen)
{
#ifdef __x86_64
register uint8_t nbleft = hlen & 0x07; /* = mod by 8 */
register uint64_t mask64;
register uint64_t *p64_0;
register uint64_t *p64_1;
register uint8_t i;
hlen -= nbleft;
i = 0;
for (; i < hlen; i += 8) {
p64_0 = (uint64_t *) &h0[i];
p64_1 = (uint64_t *) &h1[i];
if (*p64_0 != *p64_1)
return 1;
}
if (nbleft) {
mask64 = ((uint64_t) 1 << (nbleft << 3)) - 1;
p64_0 = (uint64_t *) &h0[i];
p64_1 = (uint64_t *) &h1[i];
if ((*p64_0 & mask64) != (*p64_1 & mask64))
return 1;
}
return 0;
#else
return (memcmp(h0, h1, hlen) != 0);
#endif
}
static inline int hash_is_zero(const hash512_t h, uint8_t hlen)
{
#ifdef __x86_64
register uint8_t nbleft = hlen & 0x07; /* = mod by 8 */
register uint64_t mask64;
register uint64_t *p64;
register uint8_t i;
hlen -= nbleft;
i = 0;
for (; i < hlen; i += 8) {
p64 = (uint64_t *) &h[i];
if (*p64 != 0)
return 0;
}
if (nbleft) {
mask64 = ((uint64_t) 1 << (nbleft << 3)) - 1;
p64 = (uint64_t *) &h[i];
if ((*p64 & mask64) != 0)
return 0;
}
return 1;
#else
uint8_t i;
for (i = 0; i < hlen; ++i)
if (h[i] != 0)
return 0;
return 1;
#endif
}
static inline int hash_is_max(const hash512_t h, uint8_t hlen)
{
#ifdef __x86_64
register uint8_t nbleft = hlen & 0x07; /* = mod by 8 */
register uint64_t mask64;
register uint64_t *p64;
register uint8_t i;
hlen -= nbleft;
i = 0;
for (; i < hlen; i += 8) {
p64 = (uint64_t *) &h[i];
if (*p64 != UINT64_MAX)
return 0;
}
if (nbleft) {
mask64 = ((uint64_t) 1 << (nbleft << 3)) - 1;
p64 = (uint64_t *) &h[i];
if ((*p64 & mask64) != mask64)
return 0;
}
return 1;
#else
uint8_t i;
for (i = 0; i < hlen; ++i)
if (h[i] != 0xFF)
return 0;
return 1;
#endif
}
static inline void hash_clear(hash512_t h, uint8_t hlen)
{
#ifdef __x86_64
register uint64_t *p64;
register uint8_t i;
for (i = 0; i < hlen; i += 8) {
p64 = (uint64_t *) &h[i];
*p64 = 0;
}
#else
memset(h, 0x00, hlen);
#endif
}
static inline int hash_parse(const char *in, hash512_t h, uint8_t hlen)
{
uint8_t strlen = hlen * 2;
uint8_t i, nu, nl;
for (i = 0; i < strlen; ++i) {
/* get upper nibble (4 bits) */
switch (in[i]) {
case '0' ... '9':
nu = in[i] - '0';
break;
case 'a' ... 'f':
nu = in[i] - 'a' + 10;
break;
#ifndef SHFS_HASH_PARSE_CASE_SENSITIVE
case 'A' ... 'F':
nu = in[i] - 'A' + 10;
break;
#endif
case '\0':
default:
return -1; /* unknown character or string ended unexpectedly */
}
/* get lower nibble (4 bits) */
++i;
switch (in[i]) {
case '0' ... '9':
nl = in[i] - '0';
break;
case 'a' ... 'f':
nl = in[i] - 'a' + 10;
break;
#ifndef SHFS_HASH_PARSE_CASE_SENSITIVE
case 'A' ... 'F':
nl = in[i] - 'A' + 10;
break;
case '\0':
#endif
default:
return -1;
}
h[i >> 1] = (nu << 4) | nl;
}
if (in[i] != '\0')
return -1;
return 0;
}
#endif /* _HASH_H_ */