Skip to content

Commit

Permalink
split crc code into python and library code
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Nov 19, 2024
1 parent 93d7c5c commit fc8a213
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 192 deletions.
24 changes: 10 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,48 @@ if(NOT "${SKBUILD_SABI_COMPONENT}" STREQUAL "")
python_add_library(crc
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c
WITH_SOABI USE_SABI 3.11)
python_add_library(p01
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p01.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c
WITH_SOABI USE_SABI 3.11)
python_add_library(p02
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p02.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c
WITH_SOABI USE_SABI 3.11)
python_add_library(p04
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p04.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c
WITH_SOABI USE_SABI 3.11)
python_add_library(p05
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p05.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c
WITH_SOABI USE_SABI 3.11)
else()
python_add_library(crc
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
WITH_SOABI)
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c)
python_add_library(p01
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p01.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
WITH_SOABI)
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c)
python_add_library(p02
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p02.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
WITH_SOABI)
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c)
python_add_library(p04
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p04.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
WITH_SOABI)
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c)
python_add_library(p05
MODULE
${CMAKE_SOURCE_DIR}/src/e2e/p05.c
${CMAKE_SOURCE_DIR}/src/e2e/crc.c
WITH_SOABI)
${CMAKE_SOURCE_DIR}/src/e2e/crclib.c)
endif()

install(TARGETS crc p01 p02 p04 p05 LIBRARY DESTINATION e2e)
175 changes: 1 addition & 174 deletions src/e2e/crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,180 +8,7 @@
#include <stdbool.h>
#include <stdint.h>

#include "crc.h"

uint8_t Crc_CalculateCRC8(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint8_t Crc_StartValue8,
bool Crc_IsFirstCall)
{
uint8_t crc;

if (Crc_IsFirstCall)
{
crc = CRC8_INITIAL_VALUE;
}
else
{
crc = (CRC8_XOR_VALUE ^ Crc_StartValue8);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc = CRC8_TABLE[crc ^ Crc_DataPtr[i]];
}

return (crc ^ CRC8_XOR_VALUE);
}

uint8_t Crc_CalculateCRC8H2F(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint8_t Crc_StartValue8H2F,
bool Crc_IsFirstCall)
{
uint8_t crc;

if (Crc_IsFirstCall)
{
crc = CRC8H2F_INITIAL_VALUE;
}
else
{
crc = (CRC8H2F_XOR_VALUE ^ Crc_StartValue8H2F);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc = CRC8H2F_TABLE[crc ^ Crc_DataPtr[i]];
}

return (crc ^ CRC8H2F_XOR_VALUE);
}

uint16_t Crc_CalculateCRC16(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint16_t Crc_StartValue16,
bool Crc_IsFirstCall)
{
uint16_t crc;

if (Crc_IsFirstCall)
{
crc = CRC16_INITIAL_VALUE;
}
else
{
crc = (CRC16_XOR_VALUE ^ Crc_StartValue16);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc = (crc << 8) ^ CRC16_TABLE[((crc >> 8) ^ Crc_DataPtr[i]) & 0xFFU];
}
return (crc ^ CRC16_XOR_VALUE);
}

uint16_t Crc_CalculateCRC16ARC(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint16_t Crc_StartValue16,
bool Crc_IsFirstCall)
{
uint16_t crc;

if (Crc_IsFirstCall)
{
crc = CRC16ARC_INITIAL_VALUE;
}
else
{
crc = (CRC16ARC_XOR_VALUE ^ Crc_StartValue16);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc = (crc >> 8) ^ CRC16ARC_TABLE[(crc ^ Crc_DataPtr[i]) & 0xFFu];
}
return (crc ^ CRC16ARC_XOR_VALUE);
}

uint32_t Crc_CalculateCRC32(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint32_t Crc_StartValue32,
bool Crc_IsFirstCall)
{
uint32_t crc;

if (Crc_IsFirstCall)
{
crc = CRC32_INITIAL_VALUE;
}
else
{
crc = (CRC32_XOR_VALUE ^ Crc_StartValue32);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc ^= (uint32_t)Crc_DataPtr[i];
crc = (crc >> 8u) ^ (CRC32_TABLE[crc & 0xFFu]);
}
return (crc ^ CRC32_XOR_VALUE);
}

uint32_t Crc_CalculateCRC32P4(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint32_t Crc_StartValue32,
bool Crc_IsFirstCall)
{
uint32_t crc;

if (Crc_IsFirstCall)
{
crc = CRC32P4_INITIAL_VALUE;
}
else
{
crc = (CRC32P4_XOR_VALUE ^ Crc_StartValue32);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc ^= (uint32_t)Crc_DataPtr[i];
crc = (crc >> 8u) ^ (CRC32P4_TABLE[crc & 0xFFu]);
}
return (crc ^ CRC32P4_XOR_VALUE);
}

uint64_t Crc_CalculateCRC64(
const uint8_t *Crc_DataPtr,
uint32_t Crc_Length,
uint64_t Crc_StartValue64,
bool Crc_IsFirstCall)
{
uint64_t crc;

if (Crc_IsFirstCall)
{
crc = CRC64_INITIAL_VALUE;
}
else
{
crc = (CRC64_XOR_VALUE ^ Crc_StartValue64);
}

for (size_t i = 0; i < Crc_Length; ++i)
{
crc ^= (uint64_t)Crc_DataPtr[i];
crc = (crc >> 8uLL) ^ (CRC64_TABLE[crc & 0xFFuLL]);
}
return (crc ^ CRC64_XOR_VALUE);
}
#include "crclib.h"

PyDoc_STRVAR(py_calculate_crc8_doc,
"calculate_crc8(data: bytes, start_value: int = 0xFF, first_call: bool = True) -> int\n"
Expand Down
Loading

0 comments on commit fc8a213

Please sign in to comment.