Files
Aaru.Checksums.Native/crc32.c

181 lines
5.5 KiB
C
Raw Normal View History

2021-09-21 23:51:30 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2022-12-01 23:06:20 +00:00
* Copyright (c) 2019-2023 Natalia Portillo.
2021-09-21 23:51:30 +01:00
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include "library.h"
2021-09-22 01:02:07 +01:00
#include "crc32.h"
2023-09-23 18:10:44 +01:00
/**
* @brief Initializes the CRC-32 checksum algorithm with the ISO polynomial.
*
* This function initializes the state variables required for the CRC-32
* checksum algorithm using the ISO polynomial. It prepares the algorithm
* to calculate the checksum for a new data set.
*
* @return Pointer to a structure containing the checksum state.
*/
2023-09-23 18:55:52 +01:00
AARU_EXPORT crc32_ctx *AARU_CALL crc32_init(void)
2021-09-21 23:51:30 +01:00
{
2023-09-23 18:55:52 +01:00
crc32_ctx *ctx = (crc32_ctx *)malloc(sizeof(crc32_ctx));
2021-09-21 23:51:30 +01:00
if(!ctx) return NULL;
ctx->crc = CRC32_ISO_SEED;
return ctx;
}
2023-09-23 18:10:44 +01:00
/**
* @brief Updates the CRC-32 checksum with new data.
*
* This function updates the CRC-32 checksum.
* The checksum is updated for the given data by using the ISO polynomial.
* The algorithm continues the checksum calculation from the previous state,
* so it can be used to update the checksum with new data as it is read.
*
* @param ctx Pointer to the CRC-32 context structure.
* @param data Pointer to the input data buffer.
* @param len The length of the input data buffer.
*
* @returns 0 on success, -1 on error.
*/
2023-09-23 18:55:52 +01:00
AARU_EXPORT int AARU_CALL crc32_update(crc32_ctx *ctx, const uint8_t *data, uint32_t len)
2021-09-21 23:51:30 +01:00
{
if(!ctx || !data) return -1;
2023-09-23 18:55:52 +01:00
#if defined(__x86_64__) || defined(__amd64) || defined(_M_AMD64) || defined(_M_X64) || defined(__I386__) || \
defined(__i386__) || defined(__THW_INTEL) || defined(_M_IX86)
if(have_clmul())
{
2021-10-13 03:07:04 +01:00
ctx->crc = ~crc32_clmul(~ctx->crc, data, (long)len);
return 0;
}
#endif
#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM)
#if __ARM_ARCH >= 7
if(have_arm_crc32())
{
ctx->crc = armv8_crc32_little(ctx->crc, data, len);
return 0;
}
2021-10-05 04:18:11 +01:00
#endif
if(have_neon())
{
2021-10-13 03:07:04 +01:00
ctx->crc = ~crc32_vmull(~ctx->crc, data, len);
return 0;
}
#endif
2021-10-05 02:41:28 +01:00
crc32_slicing(&ctx->crc, data, len);
return 0;
}
2023-09-23 18:10:44 +01:00
/**
* @brief Computes the CRC-32 checksum using slicing-by-8 algorithm.
*
* This function calculates the CRC-32 value for the given data using slicing-by-8 algorithm.
*
* @param previous_crc A pointer to the previous CRC-32 value, and where the updated value gets stored.
* @param data The pointer to the data buffer.
* @param len The length of the data in bytes.
*
* @note This function assumes little-endian byte order.
*/
2023-09-23 18:55:52 +01:00
AARU_EXPORT void AARU_CALL crc32_slicing(uint32_t *previous_crc, const uint8_t *data, long len)
2021-10-05 02:41:28 +01:00
{
2021-09-22 20:09:52 +01:00
// Unroll according to Intel slicing by uint8_t
// http://www.intel.com/technology/comms/perfnet/download/CRC_generators.pdf
// http://sourceforge.net/projects/slicing-by-8/
2024-04-30 15:12:48 +01:00
uint32_t c;
2023-09-23 18:55:52 +01:00
const uint32_t *current;
2024-04-30 15:12:48 +01:00
const uint8_t *current_char = data;
const size_t unroll = 4;
const size_t bytes_at_once = 8 * unroll;
uintptr_t unaligned_length = (4 - (((uintptr_t)current_char) & 3)) & 3;
2021-09-22 20:09:52 +01:00
2021-10-13 03:07:04 +01:00
c = *previous_crc;
2021-09-22 20:09:52 +01:00
while((len != 0) && (unaligned_length != 0))
{
2021-10-05 02:41:28 +01:00
c = (c >> 8) ^ crc32_table[0][(c & 0xFF) ^ *current_char++];
2021-09-22 20:09:52 +01:00
len--;
unaligned_length--;
}
2023-09-23 18:55:52 +01:00
current = (const uint32_t *)current_char;
2021-09-22 20:09:52 +01:00
while(len >= bytes_at_once)
{
size_t unrolling;
for(unrolling = 0; unrolling < unroll; unrolling++)
{
2021-10-05 02:41:28 +01:00
uint32_t one = *current++ ^ c;
2021-09-22 20:09:52 +01:00
uint32_t two = *current++;
// TODO: Big endian!
2024-04-30 15:12:48 +01:00
c = crc32_table[0][(two >> 24) & 0xFF] ^ crc32_table[1][(two >> 16) & 0xFF] ^
2021-10-05 02:41:28 +01:00
crc32_table[2][(two >> 8) & 0xFF] ^ crc32_table[3][two & 0xFF] ^ crc32_table[4][(one >> 24) & 0xFF] ^
crc32_table[5][(one >> 16) & 0xFF] ^ crc32_table[6][(one >> 8) & 0xFF] ^ crc32_table[7][one & 0xFF];
2021-09-22 20:09:52 +01:00
}
len -= bytes_at_once;
}
2023-09-23 18:55:52 +01:00
current_char = (const uint8_t *)current;
2021-09-22 20:09:52 +01:00
2021-10-05 02:41:28 +01:00
while(len-- != 0) c = (c >> 8) ^ crc32_table[0][(c & 0xFF) ^ *current_char++];
2021-09-21 23:51:30 +01:00
2021-10-13 03:07:04 +01:00
*previous_crc = c;
2021-09-21 23:51:30 +01:00
}
2023-09-23 18:10:44 +01:00
/**
* @brief Finalizes the calculation of the CRC-32 checksum.
*
* This function finalizes the calculation of the CRC-32 checksum and returns
* its value.
*
* @param[in] ctx Pointer to the CRC-32 context structure.
* @param[out] checksum Pointer to a 32-bit unsigned integer to store the checksum value.
*
* @returns 0 on success, -1 on error.
*/
2023-09-23 18:55:52 +01:00
AARU_EXPORT int AARU_CALL crc32_final(crc32_ctx *ctx, uint32_t *crc)
2021-09-21 23:51:30 +01:00
{
if(!ctx) return -1;
*crc = ctx->crc ^ CRC32_ISO_SEED;
return 0;
}
2023-09-23 18:10:44 +01:00
/**
* @brief Frees the resources allocated for the CRC-32 checksum context.
*
* This function should be called to release the memory used by the CRC-32 checksum
* context structure after it is no longer needed.
*
* @param ctx The CRC-32 checksum context structure, to be freed.
*/
2023-09-23 18:55:52 +01:00
AARU_EXPORT void AARU_CALL crc32_free(crc32_ctx *ctx)
2021-09-21 23:51:30 +01:00
{
if(ctx) free(ctx);
}