Skip to content

Commit

Permalink
Add more CRC checksum variants to lib/nethash (#4550)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlstill authored Mar 21, 2024
1 parent aece6f9 commit aa71181
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/nethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,18 @@ uint16_t crc16(const uint8_t *buf, size_t len) {
return crcGeneric<uint16_t, 0, 0, table_crc16, Reflect>(buf, len);
}

uint16_t crc16ANSI(const uint8_t *buf, size_t len) {
return crcGeneric<uint16_t, 0, 0, table_crc16, Identity>(buf, len);
}

uint32_t crc32(const uint8_t *buf, size_t len) {
return crcGeneric<uint32_t, 0xffffffff, 0xffffffff, table_crc32, Reflect>(buf, len);
}

uint32_t crc32FCS(const uint8_t *buf, size_t len) {
return crcGeneric<uint32_t, 0xffffffff, 0xffffffff, table_crc32, Identity>(buf, len);
}

uint16_t crcCCITT(const uint8_t *buf, size_t len) {
return crcGeneric<uint16_t, 0xffff, 0, table_crcCCITT, Identity>(buf, len);
}
Expand Down
14 changes: 11 additions & 3 deletions lib/nethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
/// A collection of hashing functions commonly used in network protocols.
namespace NetHash {

/// CRC-16/CRC-16-IBM/CRC-16-ANSI (parameters: bit-reflection, polynomial 0x8005, init = 0 and
/// xor_out = 0).
/// CRC-16 used in BMv2 (parameters: bit-reflection, polynomial 0x8005, init = 0 and xor_out = 0).
uint16_t crc16(const uint8_t *buf, size_t len);

/// CRC-16-ANSI/CRC-16-IBM (parameters: NO bit-reflection, polynomial 0x8005, init = 0 and
/// xor_out = 0).
uint16_t crc16ANSI(const uint8_t *buf, size_t len);

/// CRC16-CCITT (parameters: NO bit-reflection, polynomial 0x1021, init = ~0 and xor_out = 0).
uint16_t crcCCITT(const uint8_t *buf, size_t len);

/// CRC-32 (parameters: bit-reflection, polynomial 0x04C11DB7, init = ~0 and xor_out = ~0).
/// CRC-32, used in BMv2 (parameters: bit-reflection, polynomial 0x04C11DB7, init = ~0 and
/// xor_out = ~0).
uint32_t crc32(const uint8_t *buf, size_t len);

/// CRC-32 used for Ethernet FCS (parameters: NO bit-reflection, polynomial 0x04C11DB7, init = ~0
/// and xor_out = ~0).
uint32_t crc32FCS(const uint8_t *buf, size_t len);

/// 16-bit ones' complement checksum (used in IP, TCP, UDP, ...).
uint16_t csum16(const uint8_t *buf, size_t len);

Expand Down
22 changes: 22 additions & 0 deletions test/gtest/nethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ TEST(NetHash, crc32) {
EXPECT_EQ(apply<crc32>("foobar%142qrs"), 0x95E1D00B_u32);
}

TEST(NetHash, crc32FCS) {
EXPECT_EQ(apply<crc32FCS>({}), 0x00000000_u32);
EXPECT_EQ(apply<crc32FCS>({0}), 0xB1F7404B_u32);
EXPECT_EQ(apply<crc32FCS>({0, 0, 0, 0, 0}), 0xB8EF4463_u32);
EXPECT_EQ(apply<crc32FCS>({0x0b, 0xb8, 0x1f, 0x90}), 0xEDA1EA6D_u32);
EXPECT_EQ(apply<crc32FCS>("fooo"), 0xCD961E19_u32);
EXPECT_EQ(apply<crc32FCS>("a"), 0x19939B6B_u32);
EXPECT_EQ(apply<crc32FCS>("foobar"), 0x52C03DC1_u32);
EXPECT_EQ(apply<crc32FCS>("foobar%142qrs"), 0xF3630DE4_u32);
}

TEST(NetHash, crc16) {
EXPECT_EQ(apply<crc16>({}), 0x0000_u16);
EXPECT_EQ(apply<crc16>({0}), 0x0000_u16);
Expand All @@ -59,6 +70,17 @@ TEST(NetHash, crc16) {
EXPECT_EQ(apply<crc16>("foobar%142qrs"), 0x3DA9_u16);
}

TEST(NetHash, crc16ANSI) {
EXPECT_EQ(apply<crc16ANSI>({}), 0x0000_u16);
EXPECT_EQ(apply<crc16ANSI>({0}), 0x0000_u16);
EXPECT_EQ(apply<crc16ANSI>({0, 0, 0, 0, 0}), 0x0000_u16);
EXPECT_EQ(apply<crc16ANSI>({0x0b, 0xb8, 0x1f, 0x90}), 0xD400_u16);
EXPECT_EQ(apply<crc16ANSI>("fooo"), 0x9C3A_u16);
EXPECT_EQ(apply<crc16ANSI>("a"), 0x8145_u16);
EXPECT_EQ(apply<crc16ANSI>("foobar"), 0x8F5B_u16);
EXPECT_EQ(apply<crc16ANSI>("foobar%142qrs"), 0x5A5E_u16);
}

TEST(NetHash, crcCCITT) {
EXPECT_EQ(apply<crcCCITT>({}), 0xFFFF_u16);
EXPECT_EQ(apply<crcCCITT>({0}), 0xE1F0_u16);
Expand Down

0 comments on commit aa71181

Please sign in to comment.