1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:57:35 +00:00

LibCrypto: Simplify and move CRC32 table to cpp file

CRC32 table is generated at compile-time and put into a static
variable in the header file. This can be moved to be a function
instead of a class, be moved to the `.cpp` file` and generated as an
array instead of a class which only implements `operator[]`.
This commit is contained in:
Lenny Maiorani 2022-02-18 11:20:29 -07:00 committed by Linus Groh
parent 0568229d81
commit 6bd880c404
2 changed files with 24 additions and 31 deletions

View file

@ -1,15 +1,37 @@
/* /*
* Copyright (c) 2020, the SerenityOS developers. * Copyright (c) 2020-2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Array.h>
#include <AK/Span.h> #include <AK/Span.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <LibCrypto/Checksum/CRC32.h> #include <LibCrypto/Checksum/CRC32.h>
namespace Crypto::Checksum { namespace Crypto::Checksum {
static constexpr auto generate_table()
{
Array<u32, 256> data {};
for (auto i = 0u; i < data.size(); i++) {
u32 value = i;
for (auto j = 0; j < 8; j++) {
if (value & 1) {
value = 0xEDB88320 ^ (value >> 1);
} else {
value = value >> 1;
}
}
data[i] = value;
}
return data;
}
static constexpr auto table = generate_table();
void CRC32::update(ReadonlyBytes data) void CRC32::update(ReadonlyBytes data)
{ {
for (size_t i = 0; i < data.size(); i++) { for (size_t i = 0; i < data.size(); i++) {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, the SerenityOS developers. * Copyright (c) 2020-2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -12,35 +12,6 @@
namespace Crypto::Checksum { namespace Crypto::Checksum {
struct Table {
u32 data[256];
constexpr Table()
: data()
{
for (auto i = 0; i < 256; i++) {
u32 value = i;
for (auto j = 0; j < 8; j++) {
if (value & 1) {
value = 0xEDB88320 ^ (value >> 1);
} else {
value = value >> 1;
}
}
data[i] = value;
}
}
constexpr u32 operator[](int index) const
{
return data[index];
}
};
constexpr static auto table = Table();
class CRC32 : public ChecksumFunction<u32> { class CRC32 : public ChecksumFunction<u32> {
public: public:
CRC32() { } CRC32() { }