This commit is contained in:
Raimo 2025-10-08 02:04:37 +02:00 committed by GitHub
commit b96216a712
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 45 additions and 9 deletions

View File

@ -51,17 +51,53 @@ CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)
uint64_t t = tmp;
uint8_t c = count;
while (data.size() > 0) {
t |= uint64_t{data.front()} << (8 * (c % 8));
const unsigned char* ptr = data.data();
size_t size = data.size();
// Handle initial unaligned bytes to reach 8-byte boundary
uint8_t initial_bytes = std::min(size, size_t(8 - (c & 7)) & 7);
const bool had_leftover_bytes = (initial_bytes > 0);
size -= initial_bytes;
while (initial_bytes > 0) {
t |= uint64_t{*ptr} << (8 * (c & 7));
ptr++;
c++;
if ((c & 7) == 0) {
v3 ^= t;
SIPROUND;
SIPROUND;
v0 ^= t;
t = 0;
initial_bytes--;
}
if (had_leftover_bytes && (c & 7) == 0) {
v3 ^= t;
SIPROUND;
SIPROUND;
v0 ^= t;
t = 0;
}
// Process aligned 8-byte chunks directly
while (size >= 8) {
uint64_t chunk;
std::memcpy(&chunk, ptr, 8);
if constexpr (std::endian::native == std::endian::big) {
chunk = internal_bswap_64(chunk);
}
data = data.subspan(1);
v3 ^= chunk;
SIPROUND;
SIPROUND;
v0 ^= chunk;
ptr += 8;
size -= 8;
c += 8;
}
//Handle remaining unaligned bytes
while (size > 0) {
t |= uint64_t{*ptr} << (8 * (c & 7));
ptr++;
c++;
size--;
}
v[0] = v0;