Skip to content

Commit

Permalink
crc32: add re wrapper (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredh authored Sep 17, 2022
1 parent bc06310 commit e2a1525
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions include/re_crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@
*/


#ifdef USE_ZLIB
#include <zlib.h>
#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);
15 changes: 14 additions & 1 deletion src/crc32/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
#include <re_crc32.h>


#ifdef USE_ZLIB
#include <zlib.h>
#else


static const uint32_t crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
2 changes: 0 additions & 2 deletions src/crc32/mod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
# Copyright (C) 2010 Creytiv.com
#

ifeq ($(USE_ZLIB),)
SRCS += crc32/crc32.c
endif
2 changes: 1 addition & 1 deletion src/stun/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down

0 comments on commit e2a1525

Please sign in to comment.