From e2a152599906c1c1d7a13410e39fe6b7d93f10b5 Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Sat, 17 Sep 2022 10:19:08 +0200 Subject: [PATCH] crc32: add re wrapper (#526) --- CMakeLists.txt | 8 +++----- include/re_crc32.h | 6 +----- src/crc32/crc32.c | 15 ++++++++++++++- src/crc32/mod.mk | 2 -- src/stun/msg.c | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9034e8d49..b0f469bd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -452,11 +452,9 @@ elseif(UNIX) endif() endif() -if(NOT ZLIB_FOUND) - list(APPEND SRCS - src/crc32/crc32.c - ) -endif() +list(APPEND SRCS + src/crc32/crc32.c +) if(HAVE_THREADS) #Do nothing diff --git a/include/re_crc32.h b/include/re_crc32.h index 06bbce20d..a131761f2 100644 --- a/include/re_crc32.h +++ b/include/re_crc32.h @@ -5,8 +5,4 @@ */ -#ifdef USE_ZLIB -#include -#else -uint32_t crc32(uint32_t crc, const void *buf, uint32_t size); -#endif +uint32_t re_crc32(uint32_t crc, const void *buf, uint32_t size); diff --git a/src/crc32/crc32.c b/src/crc32/crc32.c index db8a65efd..a15bf49b1 100644 --- a/src/crc32/crc32.c +++ b/src/crc32/crc32.c @@ -47,6 +47,11 @@ #include +#ifdef USE_ZLIB +#include +#else + + static const uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, @@ -94,6 +99,9 @@ static const uint32_t crc32_tab[] = { }; +#endif + + /** * A function that calculates the CRC-32 based on the table above is * given below for documentation purposes. An equivalent implementation @@ -106,12 +114,17 @@ static const uint32_t crc32_tab[] = { * * @return CRC value */ -uint32_t crc32(uint32_t crc, const void *buf, uint32_t size) +uint32_t re_crc32(uint32_t crc, const void *buf, uint32_t size) { + +#ifdef USE_ZLIB + return (uint32_t)crc32(crc, buf, size); +#else const uint8_t *p = buf; crc = ~crc; while (size--) crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); return crc ^ ~0U; +#endif } diff --git a/src/crc32/mod.mk b/src/crc32/mod.mk index 528bb293e..5791f3780 100644 --- a/src/crc32/mod.mk +++ b/src/crc32/mod.mk @@ -4,6 +4,4 @@ # Copyright (C) 2010 Creytiv.com # -ifeq ($(USE_ZLIB),) SRCS += crc32/crc32.c -endif diff --git a/src/stun/msg.c b/src/stun/msg.c index b9b15ea31..0b736738c 100644 --- a/src/stun/msg.c +++ b/src/stun/msg.c @@ -52,7 +52,7 @@ struct stun_msg { static uint32_t fingerprint(const uint8_t *buf, size_t len) { - return (uint32_t)crc32(0, buf, (unsigned int)len) ^ 0x5354554e; + return re_crc32(0, buf, (unsigned int)len) ^ 0x5354554e; }