summaryrefslogtreecommitdiff
path: root/indra/llcommon/hbxxh.cpp
blob: 41d797a7e3b1181ff1c25ebf707e7ca5778f9a75 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * @file hbxxh.cpp
 * @brief High performances vectorized hashing based on xxHash.
 *
 * $LicenseInfo:firstyear=2023&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (c) 2023, Henri Beauchamp.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

// This define ensures that xxHash will be compiled within this module, with
// vectorized (*) and inlined functions (with no exported API symbol); our
// xxhash "pre-built library" package actually only contains the xxhash.h
// header (no library needed at link time).
// (*) SSE2 is normally used for x86(_64) builds, unless you enabled AVX2
// in your build, in which case the latter would be used instead. For ARM64
// builds, this would also automatically enable NEON vectorization.
#define XXH_INLINE_ALL
#include "xxhash/xxhash.h"

#include "hbxxh.h"

// How many bytes to grab at a time when hashing files or streams
constexpr size_t BLOCK_LEN = 4096;

///////////////////////////////////////////////////////////////////////////////
// HBXXH64 class
///////////////////////////////////////////////////////////////////////////////

//static
U64 HBXXH64::digest(const void* buffer, size_t len)
{
    return XXH3_64bits(buffer, len);
}

//static
U64 HBXXH64::digest(const char* str)
{
    return XXH3_64bits((const void*)str, strlen(str));
}

//static
U64 HBXXH64::digest(const std::string& str)
{
    return XXH3_64bits((const void*)str.c_str(), str.size());
}

// Must be called by all constructors.
void HBXXH64::init()
{
    mDigest = 0;
    mState = (void*)XXH3_createState();
    if (!mState || XXH3_64bits_reset((XXH3_state_t*)mState) != XXH_OK)
    {
        LL_WARNS() << "Failed to initialize state !" << LL_ENDL;
    }
}

HBXXH64::~HBXXH64()
{
    if (mState)
    {
        XXH3_freeState((XXH3_state_t*)mState);
    }
}

void HBXXH64::update(const void* buffer, size_t len)
{
    if (mState)
    {
        XXH3_64bits_update((XXH3_state_t*)mState, buffer, len);
    }
    else
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
    }
}

void HBXXH64::update(const std::string& str)
{
    if (mState)
    {
        XXH3_64bits_update((XXH3_state_t*)mState, (const void*)str.c_str(),
                           str.length());
    }
    else
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
    }
}

void HBXXH64::update(std::istream& stream)
{
    if (!mState)
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
        return;
    }

    char buffer[BLOCK_LEN];
    size_t len;
    while (stream.good())
    {
        stream.read(buffer, BLOCK_LEN);
        len = stream.gcount();
        XXH3_64bits_update((XXH3_state_t*)mState, (const void*)buffer, len);
    }
}

void HBXXH64::update(FILE* file)
{
    if (!mState)
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
        return;
    }

    char buffer[BLOCK_LEN];
    size_t len;
    while ((len = fread((void*)buffer, 1, BLOCK_LEN, file)))
    {
        XXH3_64bits_update((XXH3_state_t*)mState, (const void*)buffer, len);
    }
    fclose(file);
}

void HBXXH64::finalize()
{
    if (!mState)
    {
        LL_WARNS() << "Already finalized !" << LL_ENDL;
        return;
    }
    mDigest = XXH3_64bits_digest((XXH3_state_t*)mState);
    XXH3_freeState((XXH3_state_t*)mState);
    mState = NULL;
}

U64 HBXXH64::digest() const
{
    return mState ? XXH3_64bits_digest((XXH3_state_t*)mState) : mDigest;
}

std::ostream& operator<<(std::ostream& stream, HBXXH64 context)
{
    stream << context.digest();
    return stream;
}

///////////////////////////////////////////////////////////////////////////////
// HBXXH128 class
///////////////////////////////////////////////////////////////////////////////

//static
LLUUID HBXXH128::digest(const void* buffer, size_t len)
{
    XXH128_hash_t hash = XXH3_128bits(buffer, len);
    LLUUID id;
    U64* data = (U64*)id.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
    return id;
}

//static
LLUUID HBXXH128::digest(const char* str)
{
    XXH128_hash_t hash = XXH3_128bits((const void*)str, strlen(str));
    LLUUID id;
    U64* data = (U64*)id.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
    return id;
}

//static
LLUUID HBXXH128::digest(const std::string& str)
{
    XXH128_hash_t hash = XXH3_128bits((const void*)str.c_str(), str.size());
    LLUUID id;
    U64* data = (U64*)id.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
    return id;
}

//static
void HBXXH128::digest(LLUUID& result, const void* buffer, size_t len)
{
    XXH128_hash_t hash = XXH3_128bits(buffer, len);
    U64* data = (U64*)result.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
}

//static
void HBXXH128::digest(LLUUID& result, const char* str)
{
    XXH128_hash_t hash = XXH3_128bits((const void*)str, strlen(str));
    U64* data = (U64*)result.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
}

//static
void HBXXH128::digest(LLUUID& result, const std::string& str)
{
    XXH128_hash_t hash = XXH3_128bits((const void*)str.c_str(), str.size());
    U64* data = (U64*)result.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
}

// Must be called by all constructors.
void HBXXH128::init()
{
    mState = (void*)XXH3_createState();
    if (!mState || XXH3_128bits_reset((XXH3_state_t*)mState) != XXH_OK)
    {
        LL_WARNS() << "Failed to initialize state !" << LL_ENDL;
    }
}

HBXXH128::~HBXXH128()
{
    if (mState)
    {
        XXH3_freeState((XXH3_state_t*)mState);
    }
}

void HBXXH128::update(const void* buffer, size_t len)
{
    if (mState)
    {
        XXH3_128bits_update((XXH3_state_t*)mState, buffer, len);
    }
    else
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
    }
}

void HBXXH128::update(const std::string& str)
{
    if (mState)
    {
        XXH3_128bits_update((XXH3_state_t*)mState, (const void*)str.c_str(),
                           str.length());
    }
    else
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
    }
}

void HBXXH128::update(std::istream& stream)
{
    if (!mState)
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
        return;
    }

    char buffer[BLOCK_LEN];
    size_t len;
    while (stream.good())
    {
        stream.read(buffer, BLOCK_LEN);
        len = stream.gcount();
        XXH3_128bits_update((XXH3_state_t*)mState, (const void*)buffer, len);
    }
}

void HBXXH128::update(FILE* file)
{
    if (!mState)
    {
        LL_WARNS() << "Cannot update a finalized digest !" << LL_ENDL;
        return;
    }

    char buffer[BLOCK_LEN];
    size_t len;
    while ((len = fread((void*)buffer, 1, BLOCK_LEN, file)))
    {
        XXH3_128bits_update((XXH3_state_t*)mState, (const void*)buffer, len);
    }
    fclose(file);
}

void HBXXH128::finalize()
{
    if (!mState)
    {
        LL_WARNS() << "Already finalized !" << LL_ENDL;
        return;
    }
    XXH128_hash_t hash = XXH3_128bits_digest((XXH3_state_t*)mState);
    U64* data = (U64*)mDigest.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
    XXH3_freeState((XXH3_state_t*)mState);
    mState = NULL;
}

const LLUUID& HBXXH128::digest() const
{
    if (mState)
    {
        XXH128_hash_t hash = XXH3_128bits_digest((XXH3_state_t*)mState);
        // We cheat the const-ness of the method here, but this is OK, since
        // mDigest is private and cannot be accessed indirectly by other
        // methods than digest() ones, that do check for mState to decide
        // wether mDigest's current value may be provided as is or not. This
        // cheat saves us a temporary LLLUID copy.
        U64* data = (U64*)mDigest.mData;
        // Note: we do not check endianness here and we just store in the same
        // order as XXH128_hash_t, that is low word "first".
        data[0] = hash.low64;
        data[1] = hash.high64;
    }
    return mDigest;
}

void HBXXH128::digest(LLUUID& result) const
{
    if (!mState)
    {
        result = mDigest;
        return;
    }
    XXH128_hash_t hash = XXH3_128bits_digest((XXH3_state_t*)mState);
    U64* data = (U64*)result.mData;
    // Note: we do not check endianness here and we just store in the same
    // order as XXH128_hash_t, that is low word "first".
    data[0] = hash.low64;
    data[1] = hash.high64;
}

std::ostream& operator<<(std::ostream& stream, HBXXH128 context)
{
    stream << context.digest();
    return stream;
}