forked from tpruvot/cpuminer-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haval_helper.c
195 lines (179 loc) · 4.59 KB
/
haval_helper.c
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
/* $Id: haval_helper.c 218 2010-06-08 17:06:34Z tp $ */
/*
* Helper code, included (three times !) by HAVAL implementation.
*
* TODO: try to merge this with md_helper.c.
*
* ==========================(LICENSE BEGIN)============================
*
* Copyright (c) 2007-2010 Projet RNRT SAPHIR
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* ===========================(LICENSE END)=============================
*
* @author Thomas Pornin <[email protected]>
*/
#undef SPH_XCAT
#define SPH_XCAT(a, b) SPH_XCAT_(a, b)
#undef SPH_XCAT_
#define SPH_XCAT_(a, b) a ## b
static void
#ifdef SPH_UPTR
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)
#else
SPH_XCAT(haval, PASSES)
#endif
(sph_haval_context *sc, const void *data, size_t len)
{
unsigned current;
#if SPH_64
current = (unsigned)sc->count & 127U;
#else
current = (unsigned)sc->count_low & 127U;
#endif
while (len > 0) {
unsigned clen;
#if !SPH_64
sph_u32 clow, clow2;
#endif
clen = 128U - current;
if (clen > len)
clen = (unsigned) len;
memcpy(sc->buf + current, data, clen);
data = (const unsigned char *)data + clen;
current += clen;
len -= clen;
if (current == 128U) {
DSTATE;
IN_PREPARE(sc->buf);
RSTATE;
SPH_XCAT(CORE, PASSES)(INW);
WSTATE;
current = 0;
}
#if SPH_64
sc->count += clen;
#else
clow = sc->count_low;
clow2 = SPH_T32(clow + clen);
sc->count_low = clow2;
if (clow2 < clow)
sc->count_high ++;
#endif
}
}
#ifdef SPH_UPTR
static void
SPH_XCAT(haval, PASSES)(sph_haval_context *sc, const void *data, size_t len)
{
unsigned current;
size_t orig_len;
#if !SPH_64
sph_u32 clow, clow2;
#endif
DSTATE;
if (len < 256U) {
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len);
return;
}
#if SPH_64
current = (unsigned)sc->count & 127U;
#else
current = (unsigned)sc->count_low & 127U;
#endif
if (current > 0) {
unsigned clen;
clen = 128U - current;
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, clen);
data = (const unsigned char *)data + clen;
len -= clen;
}
#if !SPH_UNALIGNED
if (((SPH_UPTR)data & 3U) != 0) {
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len);
return;
}
#endif
orig_len = len;
RSTATE;
while (len >= 128U) {
IN_PREPARE(data);
SPH_XCAT(CORE, PASSES)(INW);
data = (const unsigned char *)data + 128U;
len -= 128U;
}
WSTATE;
if (len > 0)
memcpy(sc->buf, data, len);
#if SPH_64
sc->count += (sph_u64)orig_len;
#else
clow = sc->count_low;
clow2 = SPH_T32(clow + orig_len);
sc->count_low = clow2;
if (clow2 < clow)
sc->count_high ++;
orig_len >>= 12;
orig_len >>= 10;
orig_len >>= 10;
sc->count_high += orig_len;
#endif
}
#endif
static void
SPH_XCAT(SPH_XCAT(haval, PASSES), _close)(sph_haval_context *sc,
unsigned ub, unsigned n, void *dst)
{
unsigned current;
DSTATE;
#if SPH_64
current = (unsigned)sc->count & 127U;
#else
current = (unsigned)sc->count_low & 127U;
#endif
sc->buf[current ++] = (0x01 << n) | ((ub & 0xFF) >> (8 - n));
RSTATE;
if (current > 118U) {
memset(sc->buf + current, 0, 128U - current);
do {
IN_PREPARE(sc->buf);
SPH_XCAT(CORE, PASSES)(INW);
} while (0);
current = 0;
}
memset(sc->buf + current, 0, 118U - current);
sc->buf[118] = 0x01 | (PASSES << 3);
sc->buf[119] = sc->olen << 3;
#if SPH_64
sph_enc64le_aligned(sc->buf + 120, SPH_T64(sc->count << 3));
#else
sph_enc32le_aligned(sc->buf + 120, SPH_T32(sc->count_low << 3));
sph_enc32le_aligned(sc->buf + 124,
SPH_T32((sc->count_high << 3) | (sc->count_low >> 29)));
#endif
do {
IN_PREPARE(sc->buf);
SPH_XCAT(CORE, PASSES)(INW);
} while (0);
WSTATE;
haval_out(sc, dst);
haval_init(sc, sc->olen, sc->passes);
}