add helper method to generate same crc32 results as php and boost::crc_32_type
authorBenjamin Renard <benj@fb.com>
Tue, 11 Jul 2017 22:54:24 +0000 (15:54 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 11 Jul 2017 23:07:14 +0000 (16:07 -0700)
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

folly/Checksum.cpp
folly/Checksum.h
folly/test/ChecksumTest.cpp

index 4e5f7768c1979c8d4b858cd219fd237cc57b6e94..e800b0d53c96da1ef4a9901c6ce52af8e74db39d 100644 (file)
@@ -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
index 642ad2586f75402dc99f955a2516240df35a911f..756d3730659ffe532909798313c70191d3af8098 100644 (file)
@@ -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
index 4c5039a2198dc0285186b010eeab899f910a9d8e..0d7bde12a20fda6bf9f6a74c4e6e124c66f20d78 100644 (file)
@@ -16,7 +16,7 @@
 
 #include <folly/Checksum.h>
 
-
+#include <boost/crc.hpp>
 #include <folly/Benchmark.h>
 #include <folly/Hash.h>
 #include <folly/detail/ChecksumDetail.h>
@@ -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;