Commit 2a9b3284 authored by Rusty Russell's avatar Rusty Russell

64-bit CRC code.

parent cd13fd53
......@@ -12,6 +12,17 @@
* Software Foundation; either version 2 of the License, or (at your option)
* any later version. */
/* crc64 code taken from Jacksum version 1.7.0 - checksum utility in Java
* E-mail: jonelo@jonelo.de
* Copyright (C) 2001-2006 Dipl.-Inf. (FH) Johann Nepomuk Loefflmann,
* All Rights Reserved, http://www.jonelo.de
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
#include "crc.h"
#include <stdbool.h>
#include <stdlib.h>
......@@ -173,3 +184,107 @@ const uint32_t *crc32_ieee_table(void)
{
return crc32_ieee_tab;
}
static uint64_t crc64_tab[] = {
0x0000000000000000ULL, 0x01b0000000000000ULL, 0x0360000000000000ULL,
0x02d0000000000000ULL, 0x06c0000000000000ULL, 0x0770000000000000ULL,
0x05a0000000000000ULL, 0x0410000000000000ULL, 0x0d80000000000000ULL,
0x0c30000000000000ULL, 0x0ee0000000000000ULL, 0x0f50000000000000ULL,
0x0b40000000000000ULL, 0x0af0000000000000ULL, 0x0820000000000000ULL,
0x0990000000000000ULL, 0x1b00000000000000ULL, 0x1ab0000000000000ULL,
0x1860000000000000ULL, 0x19d0000000000000ULL, 0x1dc0000000000000ULL,
0x1c70000000000000ULL, 0x1ea0000000000000ULL, 0x1f10000000000000ULL,
0x1680000000000000ULL, 0x1730000000000000ULL, 0x15e0000000000000ULL,
0x1450000000000000ULL, 0x1040000000000000ULL, 0x11f0000000000000ULL,
0x1320000000000000ULL, 0x1290000000000000ULL, 0x3600000000000000ULL,
0x37b0000000000000ULL, 0x3560000000000000ULL, 0x34d0000000000000ULL,
0x30c0000000000000ULL, 0x3170000000000000ULL, 0x33a0000000000000ULL,
0x3210000000000000ULL, 0x3b80000000000000ULL, 0x3a30000000000000ULL,
0x38e0000000000000ULL, 0x3950000000000000ULL, 0x3d40000000000000ULL,
0x3cf0000000000000ULL, 0x3e20000000000000ULL, 0x3f90000000000000ULL,
0x2d00000000000000ULL, 0x2cb0000000000000ULL, 0x2e60000000000000ULL,
0x2fd0000000000000ULL, 0x2bc0000000000000ULL, 0x2a70000000000000ULL,
0x28a0000000000000ULL, 0x2910000000000000ULL, 0x2080000000000000ULL,
0x2130000000000000ULL, 0x23e0000000000000ULL, 0x2250000000000000ULL,
0x2640000000000000ULL, 0x27f0000000000000ULL, 0x2520000000000000ULL,
0x2490000000000000ULL, 0x6c00000000000000ULL, 0x6db0000000000000ULL,
0x6f60000000000000ULL, 0x6ed0000000000000ULL, 0x6ac0000000000000ULL,
0x6b70000000000000ULL, 0x69a0000000000000ULL, 0x6810000000000000ULL,
0x6180000000000000ULL, 0x6030000000000000ULL, 0x62e0000000000000ULL,
0x6350000000000000ULL, 0x6740000000000000ULL, 0x66f0000000000000ULL,
0x6420000000000000ULL, 0x6590000000000000ULL, 0x7700000000000000ULL,
0x76b0000000000000ULL, 0x7460000000000000ULL, 0x75d0000000000000ULL,
0x71c0000000000000ULL, 0x7070000000000000ULL, 0x72a0000000000000ULL,
0x7310000000000000ULL, 0x7a80000000000000ULL, 0x7b30000000000000ULL,
0x79e0000000000000ULL, 0x7850000000000000ULL, 0x7c40000000000000ULL,
0x7df0000000000000ULL, 0x7f20000000000000ULL, 0x7e90000000000000ULL,
0x5a00000000000000ULL, 0x5bb0000000000000ULL, 0x5960000000000000ULL,
0x58d0000000000000ULL, 0x5cc0000000000000ULL, 0x5d70000000000000ULL,
0x5fa0000000000000ULL, 0x5e10000000000000ULL, 0x5780000000000000ULL,
0x5630000000000000ULL, 0x54e0000000000000ULL, 0x5550000000000000ULL,
0x5140000000000000ULL, 0x50f0000000000000ULL, 0x5220000000000000ULL,
0x5390000000000000ULL, 0x4100000000000000ULL, 0x40b0000000000000ULL,
0x4260000000000000ULL, 0x43d0000000000000ULL, 0x47c0000000000000ULL,
0x4670000000000000ULL, 0x44a0000000000000ULL, 0x4510000000000000ULL,
0x4c80000000000000ULL, 0x4d30000000000000ULL, 0x4fe0000000000000ULL,
0x4e50000000000000ULL, 0x4a40000000000000ULL, 0x4bf0000000000000ULL,
0x4920000000000000ULL, 0x4890000000000000ULL, 0xd800000000000000ULL,
0xd9b0000000000000ULL, 0xdb60000000000000ULL, 0xdad0000000000000ULL,
0xdec0000000000000ULL, 0xdf70000000000000ULL, 0xdda0000000000000ULL,
0xdc10000000000000ULL, 0xd580000000000000ULL, 0xd430000000000000ULL,
0xd6e0000000000000ULL, 0xd750000000000000ULL, 0xd340000000000000ULL,
0xd2f0000000000000ULL, 0xd020000000000000ULL, 0xd190000000000000ULL,
0xc300000000000000ULL, 0xc2b0000000000000ULL, 0xc060000000000000ULL,
0xc1d0000000000000ULL, 0xc5c0000000000000ULL, 0xc470000000000000ULL,
0xc6a0000000000000ULL, 0xc710000000000000ULL, 0xce80000000000000ULL,
0xcf30000000000000ULL, 0xcde0000000000000ULL, 0xcc50000000000000ULL,
0xc840000000000000ULL, 0xc9f0000000000000ULL, 0xcb20000000000000ULL,
0xca90000000000000ULL, 0xee00000000000000ULL, 0xefb0000000000000ULL,
0xed60000000000000ULL, 0xecd0000000000000ULL, 0xe8c0000000000000ULL,
0xe970000000000000ULL, 0xeba0000000000000ULL, 0xea10000000000000ULL,
0xe380000000000000ULL, 0xe230000000000000ULL, 0xe0e0000000000000ULL,
0xe150000000000000ULL, 0xe540000000000000ULL, 0xe4f0000000000000ULL,
0xe620000000000000ULL, 0xe790000000000000ULL, 0xf500000000000000ULL,
0xf4b0000000000000ULL, 0xf660000000000000ULL, 0xf7d0000000000000ULL,
0xf3c0000000000000ULL, 0xf270000000000000ULL, 0xf0a0000000000000ULL,
0xf110000000000000ULL, 0xf880000000000000ULL, 0xf930000000000000ULL,
0xfbe0000000000000ULL, 0xfa50000000000000ULL, 0xfe40000000000000ULL,
0xfff0000000000000ULL, 0xfd20000000000000ULL, 0xfc90000000000000ULL,
0xb400000000000000ULL, 0xb5b0000000000000ULL, 0xb760000000000000ULL,
0xb6d0000000000000ULL, 0xb2c0000000000000ULL, 0xb370000000000000ULL,
0xb1a0000000000000ULL, 0xb010000000000000ULL, 0xb980000000000000ULL,
0xb830000000000000ULL, 0xbae0000000000000ULL, 0xbb50000000000000ULL,
0xbf40000000000000ULL, 0xbef0000000000000ULL, 0xbc20000000000000ULL,
0xbd90000000000000ULL, 0xaf00000000000000ULL, 0xaeb0000000000000ULL,
0xac60000000000000ULL, 0xadd0000000000000ULL, 0xa9c0000000000000ULL,
0xa870000000000000ULL, 0xaaa0000000000000ULL, 0xab10000000000000ULL,
0xa280000000000000ULL, 0xa330000000000000ULL, 0xa1e0000000000000ULL,
0xa050000000000000ULL, 0xa440000000000000ULL, 0xa5f0000000000000ULL,
0xa720000000000000ULL, 0xa690000000000000ULL, 0x8200000000000000ULL,
0x83b0000000000000ULL, 0x8160000000000000ULL, 0x80d0000000000000ULL,
0x84c0000000000000ULL, 0x8570000000000000ULL, 0x87a0000000000000ULL,
0x8610000000000000ULL, 0x8f80000000000000ULL, 0x8e30000000000000ULL,
0x8ce0000000000000ULL, 0x8d50000000000000ULL, 0x8940000000000000ULL,
0x88f0000000000000ULL, 0x8a20000000000000ULL, 0x8b90000000000000ULL,
0x9900000000000000ULL, 0x98b0000000000000ULL, 0x9a60000000000000ULL,
0x9bd0000000000000ULL, 0x9fc0000000000000ULL, 0x9e70000000000000ULL,
0x9ca0000000000000ULL, 0x9d10000000000000ULL, 0x9480000000000000ULL,
0x9530000000000000ULL, 0x97e0000000000000ULL, 0x9650000000000000ULL,
0x9240000000000000ULL, 0x93f0000000000000ULL, 0x9120000000000000ULL,
0x9090000000000000ULL
};
uint64_t crc64_iso(uint64_t crc, const void *buf, size_t size)
{
const uint8_t *p = buf;
while (size--)
crc = crc64_tab[(crc ^ *p++) & 0xFFL] ^ (crc >> 8);
return crc;
}
const uint64_t *crc64_iso_table(void)
{
return crc64_tab;
}
......@@ -55,7 +55,7 @@ uint32_t crc32c(uint32_t start_crc, const void *buf, size_t size);
* void check_user_crc_table(const uint32_t *usertab)
* {
* const uint32_t *ctab = crc32c_table();
* if (!ieee_tab || memcmp(ieee_tab, usertab, 1024) != 0)
* if (!ctab || memcmp(ctab, usertab, 1024) != 0)
* abort();
* }
*/
......@@ -80,4 +80,25 @@ uint32_t crc32_ieee(uint32_t start_crc, const void *buf, size_t size);
* See crc32c_table() for details.
*/
const uint32_t *crc32_ieee_table(void);
/**
* crc64_iso - ISO 3309
* @start_crc: the initial crc (usually 0)
* @buf: pointer to bytes
* @size: length of buffer
*
* 64 bit CRC checksum using polynomial
* X^64 + X^4 + X^3 + X^1 + X^0
*
* See crc32c() for details.
*/
uint64_t crc64_iso(uint64_t start_crc, const void *buf, size_t size);
/**
* crc64_iso_table - Get the ISO 3309 CRC table
*
* See crc32c_table() for details.
*/
const uint64_t *crc64_iso_table(void);
#endif /* CCAN_CRC_H */
......@@ -238,12 +238,24 @@ static void test_crc32(uint32_t (*crc)(uint32_t, const void *, size_t),
ok1(memcmp(crc_table(), table_expect, 1024) == 0);
}
static void test_crc64(void)
{
/* according to http://swissknife.sourceforge.net/CRC64.html */
ok1(crc64_iso(0, "IHATEMATH", strlen("IHATEMATH"))
== 0xE3DCADD69B01ADD1ULL);
/* according to the CRC64 poly, http://sf.net/projects/jcrcgen */
ok1(crc64_iso(0, "123456789", strlen("123456789"))
== 0x46A5A9388A5BEFFEULL);
}
int main(int argc, char *argv[])
{
plan_tests(517 * 2);
plan_tests(517 * 2 + 2);
test_crc32(crc32c, crc32c_table,
crcc_zero_expect, crcc_expect, crc32c_tab);
test_crc32(crc32_ieee, crc32_ieee_table,
crc_ieee_zero_expect, crc_ieee_expect, crc32_ieee_tab);
test_crc64();
return exit_status();
}
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