2020-07-29 18:24:54 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-07-29 18:24:54 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-29 18:24:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Span.h>
|
|
|
|
#include <AK/Types.h>
|
2020-08-17 13:25:49 +00:00
|
|
|
#include <LibCrypto/Checksum/Adler32.h>
|
2020-07-29 18:24:54 +00:00
|
|
|
|
|
|
|
namespace Crypto::Checksum {
|
|
|
|
|
|
|
|
void Adler32::update(ReadonlyBytes data)
|
|
|
|
{
|
2024-05-19 17:43:37 +00:00
|
|
|
// See https://github.com/SerenityOS/serenity/pull/24408#discussion_r1609051678
|
|
|
|
constexpr size_t iterations_without_overflow = 380368439;
|
|
|
|
|
|
|
|
u64 state_a = m_state_a;
|
|
|
|
u64 state_b = m_state_b;
|
|
|
|
while (data.size()) {
|
|
|
|
// You can verify that no overflow will happen here during at least
|
|
|
|
// `iterations_without_overflow` iterations using the following Python script:
|
|
|
|
//
|
|
|
|
// state_a = 65520
|
|
|
|
// state_b = 65520
|
|
|
|
// for i in range(380368439):
|
|
|
|
// state_a += 255
|
|
|
|
// state_b += state_a
|
|
|
|
// print(state_b < 2 ** 64)
|
|
|
|
auto chunk = data.slice(0, min(data.size(), iterations_without_overflow));
|
|
|
|
for (u8 byte : chunk) {
|
|
|
|
state_a += byte;
|
|
|
|
state_b += state_a;
|
|
|
|
}
|
|
|
|
state_a %= 65521;
|
|
|
|
state_b %= 65521;
|
|
|
|
data = data.slice(chunk.size());
|
2020-07-29 18:24:54 +00:00
|
|
|
}
|
2024-05-19 17:43:37 +00:00
|
|
|
m_state_a = state_a;
|
|
|
|
m_state_b = state_b;
|
2023-07-08 02:48:11 +00:00
|
|
|
}
|
2020-07-29 18:24:54 +00:00
|
|
|
|
|
|
|
u32 Adler32::digest()
|
|
|
|
{
|
|
|
|
return (m_state_b << 16) | m_state_a;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|