From: Benjamin Renard Date: Tue, 11 Jul 2017 22:54:24 +0000 (-0700) Subject: add helper method to generate same crc32 results as php and boost::crc_32_type X-Git-Tag: v2017.07.17.00~22 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=13feee4bceed50023f6e6ba88b0f492dd3b4caf7;p=folly.git add helper method to generate same crc32 results as php and boost::crc_32_type Summary: This revision adds a helper method in folly/Checksum.h, that returns the same crc32 value as php's built-in implementationa and boost::crc_32_type. folly's default implementation only differs from the final xor (with 0xFFFFFFFF for boost and php, 0 for folly), which is added here. Reviewed By: djwatson Differential Revision: D5396671 fbshipit-source-id: 14874af2d5a80408c772875142de6e340ce01038 --- diff --git a/folly/Checksum.cpp b/folly/Checksum.cpp index 4e5f7768..e800b0d5 100644 --- a/folly/Checksum.cpp +++ b/folly/Checksum.cpp @@ -179,4 +179,9 @@ uint32_t crc32(const uint8_t* data, size_t nbytes, uint32_t startingChecksum) { } } +uint32_t +crc32_type(const uint8_t* data, size_t nbytes, uint32_t startingChecksum) { + return ~crc32(data, nbytes, startingChecksum); +} + } // folly diff --git a/folly/Checksum.h b/folly/Checksum.h index 642ad258..756d3730 100644 --- a/folly/Checksum.h +++ b/folly/Checksum.h @@ -45,4 +45,16 @@ uint32_t crc32c(const uint8_t* data, size_t nbytes, uint32_t crc32(const uint8_t* data, size_t nbytes, uint32_t startingChecksum = ~0U); +/** + * Compute the CRC-32 checksum of a buffer, using a hardware-accelerated + * implementation if available or a portable software implementation as + * a default. + * + * @note compared to crc32(), crc32_type() uses a different set of default + * parameters to match the results returned by boost::crc_32_type and + * php's built-in crc32 implementation + */ +uint32_t +crc32_type(const uint8_t* data, size_t nbytes, uint32_t startingChecksum = ~0U); + } // folly diff --git a/folly/test/ChecksumTest.cpp b/folly/test/ChecksumTest.cpp index 4c5039a2..0d7bde12 100644 --- a/folly/test/ChecksumTest.cpp +++ b/folly/test/ChecksumTest.cpp @@ -16,7 +16,7 @@ #include - +#include #include #include #include @@ -89,6 +89,17 @@ void testCRC32CContinuation( } } +void testMatchesBoost32Type() { + for (auto expected : expectedResults) { + boost::crc_32_type result; + result.process_bytes(buffer + expected.offset, expected.length); + const uint32_t boostResult = result.checksum(); + const uint32_t follyResult = + folly::crc32_type(buffer + expected.offset, expected.length); + EXPECT_EQ(follyResult, boostResult); + } +} + } // namespace TEST(Checksum, crc32c_software) { @@ -169,6 +180,11 @@ TEST(Checksum, crc32_continuation) { } } +TEST(Checksum, crc32_type) { + // Test that crc32_type matches boost::crc_32_type + testMatchesBoost32Type(); +} + void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) { if (folly::detail::crc32c_hw_supported()) { uint32_t checksum;