From 33bfe42017dba7a14e993860bff4d6ea7990391e Mon Sep 17 00:00:00 2001 From: Uwe Seimet <48174652+uweseimet@users.noreply.github.com> Date: Sun, 27 Feb 2022 22:24:50 +0100 Subject: [PATCH] Replaced zlib by internal crc32() method (#698) --- src/raspberrypi/Makefile | 2 +- src/raspberrypi/devices/ctapdriver.cpp | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/raspberrypi/Makefile b/src/raspberrypi/Makefile index e3c47d87..a623b12a 100644 --- a/src/raspberrypi/Makefile +++ b/src/raspberrypi/Makefile @@ -183,7 +183,7 @@ ALL: all docs: $(DOC_DIR)/rascsi_man_page.txt $(DOC_DIR)/rasctl_man_page.txt $(DOC_DIR)/scsimon_man_page.txt $(BINDIR)/$(RASCSI): $(SRC_PROTOBUF) $(OBJ_RASCSI) | $(BINDIR) - $(CXX) $(CXXFLAGS) -o $@ $(OBJ_RASCSI) -lpthread -lz -lpcap -lprotobuf -lstdc++fs + $(CXX) $(CXXFLAGS) -o $@ $(OBJ_RASCSI) -lpthread -lpcap -lprotobuf -lstdc++fs $(BINDIR)/$(RASCTL): $(SRC_PROTOBUF) $(OBJ_RASCTL) | $(BINDIR) $(CXX) $(CXXFLAGS) -o $@ $(OBJ_RASCTL) -lpthread -lprotobuf -lstdc++fs diff --git a/src/raspberrypi/devices/ctapdriver.cpp b/src/raspberrypi/devices/ctapdriver.cpp index 01a540e7..3a328339 100644 --- a/src/raspberrypi/devices/ctapdriver.cpp +++ b/src/raspberrypi/devices/ctapdriver.cpp @@ -18,8 +18,6 @@ #include #include #endif -// TODO Try to get rid of zlib, there is only one operation using it -#include // For crc32() #include "os.h" #include "ctapdriver.h" #include "log.h" @@ -447,6 +445,19 @@ bool CTapDriver::PendingPackets() } } +// See https://stackoverflow.com/questions/21001659/crc32-algorithm-implementation-in-c-without-a-look-up-table-and-with-a-public-li +uint32_t crc32(BYTE *buf, int length) { + uint32_t crc = 0xffffffff; + for (int i = 0; i < length; i++) { + crc ^= buf[i]; + for (int j = 0; j < 8; j++) { + uint32_t mask = -(crc & 1); + crc = (crc >> 1) ^ (0xEDB88320 & mask); + } + } + return ~crc; +} + //--------------------------------------------------------------------------- // // Receive @@ -457,7 +468,7 @@ int CTapDriver::Rx(BYTE *buf) ASSERT(m_hTAP != -1); // Check if there is data that can be received - if(!PendingPackets()){ + if (!PendingPackets()) { return 0; } @@ -473,11 +484,7 @@ int CTapDriver::Rx(BYTE *buf) // We need to add the Frame Check Status (FCS) CRC back onto the end of the packet. // The Linux network subsystem removes it, since most software apps shouldn't ever // need it. - - // Initialize the CRC - DWORD crc = crc32(0L, Z_NULL, 0); - // Calculate the CRC - crc = crc32(crc, buf, dwReceived); + int crc = crc32(buf, dwReceived); buf[dwReceived + 0] = (BYTE)((crc >> 0) & 0xFF); buf[dwReceived + 1] = (BYTE)((crc >> 8) & 0xFF);