From 5cfffbfa62aaf425ec6b309f7054b8b3c7d6ef28 Mon Sep 17 00:00:00 2001 From: "Paul A. Patience" Date: Fri, 10 Jun 2016 11:41:11 -0400 Subject: [PATCH] crc64: fix error --- include/crc64.h | 22 ++++++++++++++++++++++ libc/misc/lib_crc64.c | 15 +++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/include/crc64.h b/include/crc64.h index 8db458ad66..e08183327a 100644 --- a/include/crc64.h +++ b/include/crc64.h @@ -43,6 +43,28 @@ #include #include +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* + * CRC64_CHECK is the CRC64 of the string "123456789" without the null byte. + * + * const uint8_t checkbuf[] = + * { + * '1', '2', '3', '4', '5', '6', '7', '8', '9' + * }; + * + * assert(crc64(checkbuf, sizeof(checkbuf)) == CRC64_CHECK); + */ + +/* CRC-64/WE */ + +#define CRC64_POLY ((uint64_t)0x42f0e1eba9ea3693) +#define CRC64_INIT ((uint64_t)0xffffffffffffffff) +#define CRC64_XOROUT ((uint64_t)0xffffffffffffffff) +#define CRC64_CHECK ((uint64_t)0x62ec59e3f1a4f00a) + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/libc/misc/lib_crc64.c b/libc/misc/lib_crc64.c index 4b0374c554..8faf0193d1 100644 --- a/libc/misc/lib_crc64.c +++ b/libc/misc/lib_crc64.c @@ -54,8 +54,8 @@ * #include * #include * - * #define CRC64_POLYNOMIAL ((uint64_t)0x42f0e1eba9ea3693) - * #define CRC64_TABLEN ((size_t)256) + * #define CRC64_POLY ((uint64_t)0x42f0e1eba9ea3693) + * #define CRC64_TABLEN ((size_t)256) * * int main(void) * { @@ -73,7 +73,7 @@ * { * if ((crc64val & ((uint64_t)1 << 63)) != 0) * { - * crc64val = (crc64val << 1) ^ CRC64_POLYNOMIAL; + * crc64val = (crc64val << 1) ^ CRC64_POLY; * } * else * { @@ -234,8 +234,6 @@ static const uint64_t crc64_tab[256] = 0x5dedc41a34bbeeb2, 0x1f1d25f19d51d821, 0xd80c07cd676f8394, 0x9afce626ce85b507 }; -#else -# define CRC64_POLYNOMIAL ((uint64_t)0x42f0e1eba9ea3693) #endif /**************************************************************************** @@ -257,7 +255,8 @@ uint64_t crc64part(FAR const uint8_t *src, size_t len, uint64_t crc64val) for (i = 0; i < len; i++) { - crc64val = crc64_tab[((crc64val >> 56) & 0xff) ^ src[i]] ^ (crc64val << 8); + crc64val = crc64_tab[((crc64val >> 56) & 0xff) ^ src[i]] ^ + (crc64val << 8); } return crc64val; @@ -275,7 +274,7 @@ uint64_t crc64part(FAR const uint8_t *src, size_t len, uint64_t crc64val) { if ((crc64val & ((uint64_t)1 << 63)) != 0) { - crc64val = (crc64val << 1) ^ CRC64_POLYNOMIAL; + crc64val = (crc64val << 1) ^ CRC64_POLY; } else { @@ -298,5 +297,5 @@ uint64_t crc64part(FAR const uint8_t *src, size_t len, uint64_t crc64val) uint64_t crc64(FAR const uint8_t *src, size_t len) { - return crc64part(src, len, ~(uint64_t)0); + return crc64part(src, len, CRC64_INIT) ^ CRC64_XOROUT; }