446 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
|
|
|
3 |
*
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
|
6 |
* the Free Software Foundation; either version 3 of the License, or
|
|
|
7 |
* (at your option) any later version.
|
|
|
8 |
*
|
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
* GNU General Public License for more details.
|
|
|
13 |
*
|
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
|
15 |
* along with this program; if not, write to the Free Software Foundation,
|
|
|
16 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
#include "tcrc32.h"
|
|
|
20 |
#include "tresources.h"
|
|
|
21 |
#include "terror.h"
|
|
|
22 |
|
|
|
23 |
using std::vector;
|
|
|
24 |
|
|
|
25 |
TCrc32::TCrc32(const vector<uint8_t>& bytes)
|
|
|
26 |
{
|
|
|
27 |
DECL_TRACER("TCrc32::TCrc32(const vector<uint8_t>& bytes)");
|
|
|
28 |
|
|
|
29 |
uint32_t salt = 0;
|
|
|
30 |
size_t pos = 0;
|
|
|
31 |
|
|
|
32 |
for (size_t i = 0; i < bytes.size(); ++i)
|
|
|
33 |
{
|
|
|
34 |
uint32_t b = static_cast<uint32_t>(bytes[i]);
|
|
|
35 |
|
|
|
36 |
if (b == 0 && bytes.size() > 4)
|
|
|
37 |
continue;
|
|
|
38 |
|
|
|
39 |
salt |= (b << (pos * 8));
|
|
|
40 |
|
|
|
41 |
if (pos >= 4)
|
|
|
42 |
break;
|
|
|
43 |
|
|
|
44 |
pos++;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
mCrc32 = calculate_crc32c(salt, bytes.data(), bytes.size());
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
uint32_t TCrc32::crc32c_sb8_64_bit(uint32_t crc, const unsigned char *p_buf, uint32_t length, uint32_t init_bytes)
|
|
|
51 |
{
|
|
|
52 |
DECL_TRACER("TCrc32::crc32c_sb8_64_bit(uint32_t crc, const unsigned char *p_buf, uint32_t length, uint32_t init_bytes)");
|
|
|
53 |
|
|
|
54 |
uint32_t li;
|
|
|
55 |
uint32_t term1, term2;
|
|
|
56 |
uint32_t running_length;
|
|
|
57 |
uint32_t end_bytes;
|
|
|
58 |
|
|
|
59 |
running_length = ((length - init_bytes) / 8) * 8;
|
|
|
60 |
end_bytes = length - init_bytes - running_length;
|
|
|
61 |
|
|
|
62 |
for (li = 0; li < init_bytes; li++)
|
|
|
63 |
crc = sctp_crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8);
|
|
|
64 |
|
|
|
65 |
for (li = 0; li < running_length / 8; li++)
|
|
|
66 |
{
|
|
|
67 |
if (isBigEndian())
|
|
|
68 |
{
|
|
|
69 |
crc ^= *p_buf++;
|
|
|
70 |
crc ^= (*p_buf++) << 8;
|
|
|
71 |
crc ^= (*p_buf++) << 16;
|
|
|
72 |
crc ^= (*p_buf++) << 24;
|
|
|
73 |
}
|
|
|
74 |
else
|
|
|
75 |
{
|
|
|
76 |
crc ^= *(const uint32_t *) p_buf;
|
|
|
77 |
p_buf += 4;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
term1 = sctp_crc_tableil8_o88[crc & 0x000000FF] ^ sctp_crc_tableil8_o80[(crc >> 8) & 0x000000FF];
|
|
|
81 |
term2 = crc >> 16;
|
|
|
82 |
crc = term1 ^ sctp_crc_tableil8_o72[term2 & 0x000000FF] ^ sctp_crc_tableil8_o64[(term2 >> 8) & 0x000000FF];
|
|
|
83 |
|
|
|
84 |
if (isBigEndian())
|
|
|
85 |
{
|
|
|
86 |
crc ^= sctp_crc_tableil8_o56[*p_buf++];
|
|
|
87 |
crc ^= sctp_crc_tableil8_o48[*p_buf++];
|
|
|
88 |
crc ^= sctp_crc_tableil8_o40[*p_buf++];
|
|
|
89 |
crc ^= sctp_crc_tableil8_o32[*p_buf++];
|
|
|
90 |
}
|
|
|
91 |
else
|
|
|
92 |
{
|
|
|
93 |
term1 = sctp_crc_tableil8_o56[(*(const uint32_t *) p_buf) & 0x000000FF] ^ sctp_crc_tableil8_o48[((*(const uint32_t *) p_buf) >> 8) & 0x000000FF];
|
|
|
94 |
|
|
|
95 |
term2 = (*(const uint32_t *) p_buf) >> 16;
|
|
|
96 |
crc = crc ^ term1 ^ sctp_crc_tableil8_o40[term2 & 0x000000FF] ^ sctp_crc_tableil8_o32[(term2 >> 8) & 0x000000FF];
|
|
|
97 |
p_buf += 4;
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
for (li = 0; li < end_bytes; li++)
|
|
|
102 |
crc = sctp_crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8);
|
|
|
103 |
|
|
|
104 |
return crc;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
uint32_t TCrc32::multitable_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)
|
|
|
108 |
{
|
|
|
109 |
DECL_TRACER("TCrc32::multitable_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)");
|
|
|
110 |
|
|
|
111 |
uint32_t to_even_word;
|
|
|
112 |
|
|
|
113 |
if (length == 0)
|
|
|
114 |
return (crc32c);
|
|
|
115 |
|
|
|
116 |
to_even_word = (4 - (((uintptr_t) buffer) & 0x3));
|
|
|
117 |
return (crc32c_sb8_64_bit(crc32c, buffer, length, to_even_word));
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
uint32_t TCrc32::singletable_crc32c(uint32_t crc, const void *buf, size_t size)
|
|
|
121 |
{
|
|
|
122 |
DECL_TRACER("TCrc32::singletable_crc32c(uint32_t crc, const void *buf, size_t size)");
|
|
|
123 |
|
|
|
124 |
const uint8_t *p = (uint8_t *)buf;
|
|
|
125 |
|
|
|
126 |
while (size--)
|
|
|
127 |
crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
|
|
|
128 |
|
|
|
129 |
return crc;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
uint32_t TCrc32::calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)
|
|
|
133 |
{
|
|
|
134 |
DECL_TRACER("TCrc32::calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)");
|
|
|
135 |
|
|
|
136 |
if (!buffer || !length)
|
|
|
137 |
return 0;
|
|
|
138 |
|
|
|
139 |
std::stringstream s;
|
|
|
140 |
s << std::setw(8) << std::setfill('0') << std::hex << crc32c;
|
|
|
141 |
MSG_DEBUG("Calculating CRC32 with the salt " << s.str() << " and a length of " << length << " bytes.");
|
|
|
142 |
|
|
|
143 |
if (length < 4)
|
|
|
144 |
return (singletable_crc32c(crc32c, buffer, length));
|
|
|
145 |
else
|
|
|
146 |
return (multitable_crc32c(crc32c, buffer, length));
|
|
|
147 |
}
|