diff options
author | Lenny Maiorani <lenny@serenityos.org> | 2022-02-18 11:20:29 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-26 17:49:47 +0000 |
commit | 6bd880c40408dc2f4354cb89275c3d36f6ae4387 (patch) | |
tree | 3d0255a33cb4a1b838891e6f6b66138c1b470bf0 /Userland/Libraries/LibCrypto/Checksum | |
parent | 0568229d810112e93573046590d8076dcf8c98e8 (diff) | |
download | serenity-6bd880c40408dc2f4354cb89275c3d36f6ae4387.zip |
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[]`.
Diffstat (limited to 'Userland/Libraries/LibCrypto/Checksum')
-rw-r--r-- | Userland/Libraries/LibCrypto/Checksum/CRC32.cpp | 24 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Checksum/CRC32.h | 31 |
2 files changed, 24 insertions, 31 deletions
diff --git a/Userland/Libraries/LibCrypto/Checksum/CRC32.cpp b/Userland/Libraries/LibCrypto/Checksum/CRC32.cpp index 84201aa724..0a0ed2e861 100644 --- a/Userland/Libraries/LibCrypto/Checksum/CRC32.cpp +++ b/Userland/Libraries/LibCrypto/Checksum/CRC32.cpp @@ -1,15 +1,37 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/Array.h> #include <AK/Span.h> #include <AK/Types.h> #include <LibCrypto/Checksum/CRC32.h> 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) { for (size_t i = 0; i < data.size(); i++) { diff --git a/Userland/Libraries/LibCrypto/Checksum/CRC32.h b/Userland/Libraries/LibCrypto/Checksum/CRC32.h index e2913527fd..8853c38a52 100644 --- a/Userland/Libraries/LibCrypto/Checksum/CRC32.h +++ b/Userland/Libraries/LibCrypto/Checksum/CRC32.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,35 +12,6 @@ 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> { public: CRC32() { } |