Commit 13feee4b authored by Benjamin Renard's avatar Benjamin Renard Committed by Facebook Github Bot

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
parent 4169222e
...@@ -179,4 +179,9 @@ uint32_t crc32(const uint8_t* data, size_t nbytes, uint32_t startingChecksum) { ...@@ -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 } // folly
...@@ -45,4 +45,16 @@ uint32_t crc32c(const uint8_t* data, size_t nbytes, ...@@ -45,4 +45,16 @@ uint32_t crc32c(const uint8_t* data, size_t nbytes,
uint32_t uint32_t
crc32(const uint8_t* data, size_t nbytes, uint32_t startingChecksum = ~0U); 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 } // folly
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include <folly/Checksum.h> #include <folly/Checksum.h>
#include <boost/crc.hpp>
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/Hash.h> #include <folly/Hash.h>
#include <folly/detail/ChecksumDetail.h> #include <folly/detail/ChecksumDetail.h>
...@@ -89,6 +89,17 @@ void testCRC32CContinuation( ...@@ -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 } // namespace
TEST(Checksum, crc32c_software) { TEST(Checksum, crc32c_software) {
...@@ -169,6 +180,11 @@ TEST(Checksum, crc32_continuation) { ...@@ -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) { void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
if (folly::detail::crc32c_hw_supported()) { if (folly::detail::crc32c_hw_supported()) {
uint32_t checksum; uint32_t checksum;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment