From d27b934a06369794d21a3eeaf86c55f942518956 Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Tue, 31 Aug 2010 13:02:38 -0700 Subject: [PATCH] Update CRC-32 implementation. Invert the result of the CRC computation at the end. This will catch extra null bytes at the end of the buffer, but required updating the CRC validation. --- bluesky/bluesky-private.h | 1 + bluesky/crc32c.c | 6 +++++- bluesky/log.c | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bluesky/bluesky-private.h b/bluesky/bluesky-private.h index 837c6e5..01d8b88 100644 --- a/bluesky/bluesky-private.h +++ b/bluesky/bluesky-private.h @@ -246,6 +246,7 @@ void bluesky_cloudlog_flush(BlueSkyFS *fs); /* Logging infrastructure for ensuring operations are persistently recorded to * disk. */ #define BLUESKY_CRC32C_SEED (~(uint32_t)0) +#define BLUESKY_CRC32C_VALIDATOR ((uint32_t)0xb798b438UL) uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length); uint32_t crc32c_finalize(uint32_t crc); diff --git a/bluesky/crc32c.c b/bluesky/crc32c.c index 8e11bbf..40ce983 100644 --- a/bluesky/crc32c.c +++ b/bluesky/crc32c.c @@ -17,6 +17,10 @@ #include #include "bluesky-private.h" +/* Generator polynomial is 0x1edc6f41; input and output are reflected. The + * recommended starting value for the accumulator is ~0, and the bits of the + * CRC are inverted at the end as well. */ + static const uint32_t crc32c_table[256] = { 0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L, 0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL, @@ -101,5 +105,5 @@ uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length) uint32_t crc32c_finalize(uint32_t crc) { - return GUINT32_TO_LE(crc); + return ~GUINT32_TO_LE(crc); } diff --git a/bluesky/log.c b/bluesky/log.c index e79b21b..a3acf10 100644 --- a/bluesky/log.c +++ b/bluesky/log.c @@ -616,8 +616,11 @@ static gboolean validate_journal_item(const char *buf, size_t len, off_t offset) uint32_t crc = crc32c(BLUESKY_CRC32C_SEED, buf + offset, sizeof(struct log_header) + sizeof(struct log_footer) + size); - if (crc != 0) + if (crc != BLUESKY_CRC32C_VALIDATOR) { + g_warning("Journal entry failed to validate: CRC %08x != %08x", + crc, BLUESKY_CRC32C_VALIDATOR); return FALSE; + } return TRUE; } -- 2.20.1