From dc8703206e3f0f69605c56d0e1127f7e17f3476a Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Tue, 4 Jun 2019 14:34:27 +0200 Subject: Initial commit --- testing/yup-comm/utils/crc32q.c | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 testing/yup-comm/utils/crc32q.c (limited to 'testing/yup-comm/utils/crc32q.c') diff --git a/testing/yup-comm/utils/crc32q.c b/testing/yup-comm/utils/crc32q.c new file mode 100644 index 0000000..a0f0c46 --- /dev/null +++ b/testing/yup-comm/utils/crc32q.c @@ -0,0 +1,64 @@ +/* Author: Jan Sucan */ + +#include "crc32q.h" + +#include + +#define CRC32_TABLE_SIZE 256 + +#define CRC32_GENERATING_POLYNOME 0x814141ab + +static void crc32q_create_table(uint32_t * const tab, uint32_t gen_polynome); + +void +crc32q_create_table(uint32_t * const tab, uint32_t gen_polynome) +{ + int i, j; + uint32_t c; + uint32_t msbMask = 1U << ((sizeof(c) * 8) - 1); + + for (i = 0; i < CRC32_TABLE_SIZE; i++) { + c = i << 24; + for (j = 0; j < 8; j++) { + if (c & msbMask) { + c = (c << 1) ^ gen_polynome; + } else { + c = c << 1; + } + } + tab[i] = c; + } +} + +static uint32_t crc32_tab[CRC32_TABLE_SIZE]; + +static bool crc32q_table_created = false; + +void +crc32q_init(void) +{ + crc32q_create_table(crc32_tab, CRC32_GENERATING_POLYNOME); + crc32q_table_created = true; +} + +uint32_t +crc32q(const void * const buf, size_t size) +{ + // Vytvorenie tabulky pred prvym pouzitim + if (!crc32q_table_created) { + crc32q_create_table(crc32_tab, CRC32_GENERATING_POLYNOME); + crc32q_table_created = true; + } + + // Vypocet CRC-32 + const uint8_t *p; + + p = buf; + uint32_t crc = 0U; // Inicializacna CRC hodnota + + while (size--) { + crc = (crc << 8) ^ crc32_tab[(crc >> 24) ^ *p++]; + } + + return crc ^ 0U; +} -- cgit v1.2.3