| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/boostorg/json
|
7 |
|
// Official repository: https://github.com/boostorg/json
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_JSON_DETAIL_DIGEST_HPP
|
10 |
|
#ifndef BOOST_JSON_DETAIL_DIGEST_HPP
|
| 11 |
|
#define BOOST_JSON_DETAIL_DIGEST_HPP
|
11 |
|
#define BOOST_JSON_DETAIL_DIGEST_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/json/detail/config.hpp>
|
13 |
|
#include <boost/json/detail/config.hpp>
|
| 14 |
|
|
14 |
|
|
| 15 |
|
|
15 |
|
|
| 16 |
|
|
16 |
|
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
// Calculate salted digest of string
|
22 |
|
// Calculate salted digest of string
|
| 23 |
|
template<class ForwardIterator>
|
23 |
|
template<class ForwardIterator>
|
| 24 |
|
|
24 |
|
|
| 25 |
|
digest(ForwardIterator b, ForwardIterator e, std::size_t salt) noexcept
|
25 |
|
digest(ForwardIterator b, ForwardIterator e, std::size_t salt) noexcept
|
| 26 |
|
|
26 |
|
|
| 27 |
|
std::size_t const len = std::distance(b, e);
|
27 |
|
std::size_t const len = std::distance(b, e);
|
| 28 |
|
|
28 |
|
|
| 29 |
|
#if BOOST_JSON_ARCH == 64
|
29 |
|
#if BOOST_JSON_ARCH == 64
|
| 30 |
|
|
30 |
|
|
| 31 |
|
using state_type = std::uint64_t;
|
31 |
|
using state_type = std::uint64_t;
|
| 32 |
|
state_type const m = 0xc6a4a7935bd1e995ULL;
|
32 |
|
state_type const m = 0xc6a4a7935bd1e995ULL;
|
| 33 |
|
|
33 |
|
|
| 34 |
|
state_type hash = salt ^ (len * m);
|
34 |
|
state_type hash = salt ^ (len * m);
|
| 35 |
|
|
35 |
|
|
| 36 |
|
constexpr std::size_t N = sizeof(state_type);
|
36 |
|
constexpr std::size_t N = sizeof(state_type);
|
| 37 |
|
e = std::next( b, len & ~std::size_t(N-1) );
|
37 |
|
e = std::next( b, len & ~std::size_t(N-1) );
|
| 38 |
|
for( ; b != e; std::advance(b, N) )
|
38 |
|
for( ; b != e; std::advance(b, N) )
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
|
42 |
|
|
| 43 |
|
# pragma warning(disable: 4996)
|
43 |
|
# pragma warning(disable: 4996)
|
| 44 |
|
|
44 |
|
|
| 45 |
|
std::copy_n( b, N, reinterpret_cast<unsigned char*>(&num) );
|
45 |
|
std::copy_n( b, N, reinterpret_cast<unsigned char*>(&num) );
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
|
48 |
|
|
| 49 |
|
|
49 |
|
|
| 50 |
|
|
50 |
|
|
| 51 |
|
|
51 |
|
|
| 52 |
|
|
52 |
|
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
|
57 |
|
|
| 58 |
|
|
58 |
|
|
| 59 |
|
case 7: hash ^= state_type( *std::next(b, 6) ) << 48; // fall through
|
59 |
|
case 7: hash ^= state_type( *std::next(b, 6) ) << 48; // fall through
|
| 60 |
|
case 6: hash ^= state_type( *std::next(b, 5) ) << 40; // fall through
|
60 |
|
case 6: hash ^= state_type( *std::next(b, 5) ) << 40; // fall through
|
| 61 |
|
case 5: hash ^= state_type( *std::next(b, 4) ) << 32; // fall through
|
61 |
|
case 5: hash ^= state_type( *std::next(b, 4) ) << 32; // fall through
|
| 62 |
|
case 4: hash ^= state_type( *std::next(b, 3) ) << 24; // fall through
|
62 |
|
case 4: hash ^= state_type( *std::next(b, 3) ) << 24; // fall through
|
| 63 |
|
case 3: hash ^= state_type( *std::next(b, 2) ) << 16; // fall through
|
63 |
|
case 3: hash ^= state_type( *std::next(b, 2) ) << 16; // fall through
|
| 64 |
|
case 2: hash ^= state_type( *std::next(b, 1) ) << 8; // fall through
|
64 |
|
case 2: hash ^= state_type( *std::next(b, 1) ) << 8; // fall through
|
| 65 |
|
case 1: hash ^= state_type( *std::next(b, 0) );
|
65 |
|
case 1: hash ^= state_type( *std::next(b, 0) );
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
|
70 |
|
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
|
73 |
|
|
| 74 |
|
|
74 |
|
|
| 75 |
|
using state_type = std::uint32_t;
|
75 |
|
using state_type = std::uint32_t;
|
| 76 |
|
state_type const m = 0x5bd1e995;
|
76 |
|
state_type const m = 0x5bd1e995;
|
| 77 |
|
|
77 |
|
|
| 78 |
|
state_type hash = salt ^ len;
|
78 |
|
state_type hash = salt ^ len;
|
| 79 |
|
|
79 |
|
|
| 80 |
|
constexpr std::size_t N = sizeof(state_type);
|
80 |
|
constexpr std::size_t N = sizeof(state_type);
|
| 81 |
|
e = std::next( b, len & ~std::size_t(N-1) );
|
81 |
|
e = std::next( b, len & ~std::size_t(N-1) );
|
| 82 |
|
for( ; b != e; std::advance(b, N) )
|
82 |
|
for( ; b != e; std::advance(b, N) )
|
| 83 |
|
|
83 |
|
|
| 84 |
|
|
84 |
|
|
| 85 |
|
std::copy_n( b, N, reinterpret_cast<unsigned char*>(&num) );
|
85 |
|
std::copy_n( b, N, reinterpret_cast<unsigned char*>(&num) );
|
| 86 |
|
|
86 |
|
|
| 87 |
|
|
87 |
|
|
| 88 |
|
|
88 |
|
|
| 89 |
|
|
89 |
|
|
| 90 |
|
|
90 |
|
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|
| 94 |
|
|
94 |
|
|
| 95 |
|
|
95 |
|
|
| 96 |
|
case 3: hash ^= state_type( *std::next(b, 2) ) << 16; // fall through
|
96 |
|
case 3: hash ^= state_type( *std::next(b, 2) ) << 16; // fall through
|
| 97 |
|
case 2: hash ^= state_type( *std::next(b, 1) ) << 8; // fall through
|
97 |
|
case 2: hash ^= state_type( *std::next(b, 1) ) << 8; // fall through
|
| 98 |
|
case 1: hash ^= state_type( *std::next(b, 0) );
|
98 |
|
case 1: hash ^= state_type( *std::next(b, 0) );
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
|
101 |
|
|
| 102 |
|
|
102 |
|
|
| 103 |
|
|
103 |
|
|
| 104 |
|
|
104 |
|
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
|
109 |
|
|
| 110 |
|
|
110 |
|
|
| 111 |
|
|
111 |
|
|
| 112 |
|
|
112 |
|
|
| 113 |
|
|
113 |
|
|
| 114 |
|
|
114 |
|
|
| 115 |
|
|
115 |
|
|