Skip to content

Commit

Permalink
Fix SonarQube issue
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Aug 27, 2023
1 parent e376dd8 commit 04889fe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
10 changes: 5 additions & 5 deletions cpp/devices/ctapdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,11 @@ bool CTapDriver::PendingPackets() const
}

// See https://stackoverflow.com/questions/21001659/crc32-algorithm-implementation-in-c-without-a-look-up-table-and-with-a-public-li
uint32_t CTapDriver::Crc32(const uint8_t *buf, int length) {
uint32_t CTapDriver::Crc32(span<const uint8_t> data) {
uint32_t crc = 0xffffffff;
for (int i = 0; i < length; i++) {
crc ^= buf[i];
for (int j = 0; j < 8; j++) {
for (const auto d: data) {
crc ^= d;
for (int i = 0; i < 8; i++) {
const uint32_t mask = -(static_cast<int>(crc) & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
Expand Down Expand Up @@ -466,7 +466,7 @@ int CTapDriver::Receive(uint8_t *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.
const int crc = Crc32(buf, dwReceived);
const int crc = Crc32(span(buf, dwReceived));

buf[dwReceived + 0] = (uint8_t)((crc >> 0) & 0xFF);
buf[dwReceived + 1] = (uint8_t)((crc >> 8) & 0xFF);
Expand Down
3 changes: 2 additions & 1 deletion cpp/devices/ctapdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vector>
#include <string>
#include <array>
#include <span>

using namespace std;

Expand All @@ -41,7 +42,7 @@ class CTapDriver
bool Disable() const; // Disable the piscsi0 interface
void Flush(); // Purge all of the packets that are waiting to be processed

static uint32_t Crc32(const uint8_t *, int);
static uint32_t Crc32(span<const uint8_t>);

private:
array<byte, 6> m_MacAddr; // MAC Address
Expand Down
16 changes: 8 additions & 8 deletions cpp/test/ctapdriver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022 Uwe Seimet
// Copyright (C) 2022-2023 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand All @@ -15,27 +15,27 @@ TEST(CTapDriverTest, Crc32)
array<uint8_t, ETH_FRAME_LEN> buf;

buf.fill(0x00);
EXPECT_EQ(0xe3d887bb, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0xe3d887bb, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));

buf.fill(0xff);
EXPECT_EQ(0x814765f4, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0x814765f4, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));

buf.fill(0x10);
EXPECT_EQ(0xb7288Cd3, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0xb7288Cd3, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));

buf.fill(0x7f);
EXPECT_EQ(0x4b543477, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0x4b543477, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));

buf.fill(0x80);
EXPECT_EQ(0x29cbd638, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0x29cbd638, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));

for (size_t i = 0; i < buf.size(); i++) {
buf[i] = (uint8_t)i;
}
EXPECT_EQ(0xe7870705, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0xe7870705, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));

for (size_t i = buf.size() - 1; i > 0; i--) {
buf[i] = (uint8_t)i;
}
EXPECT_EQ(0xe7870705, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN));
EXPECT_EQ(0xe7870705, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN)));
}

0 comments on commit 04889fe

Please sign in to comment.