Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kubo
Created October 16, 2019 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubo/c6bc9f038e3dd66bfb37da4f8596d119 to your computer and use it in GitHub Desktop.
Save kubo/c6bc9f038e3dd66bfb37da4f8596d119 to your computer and use it in GitHub Desktop.
/*
* Copy this file to the root directory of snzip source tree and compile it as follows:
*
* gcc -o crc32_test crc32_test.c crc32.c crc32_sse4_2.c -msse4.2
*/
#include <stdio.h>
#include "crc32.h"
/* See https://tools.ietf.org/html/rfc3720#appendix-B.4 */
typedef struct {
const char *name;
size_t data_len;
const unsigned char *data;
union {
unsigned char crc_bytes[4];
unsigned int crc;
};
} crc_data_t;
static const crc_data_t crc_data[] = {
{"32 bytes of zeroes",
32,
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00",
"\xaa\x36\x91\x8a"},
{"32 bytes of ones",
32,
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff",
"\x43\xab\xa8\x62"},
{"32 bytes of incrementing 00..1f",
32,
"\x00\x01\x02\x03"
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f"
"\x10\x11\x12\x13"
"\x14\x15\x16\x17"
"\x18\x19\x1a\x1b"
"\x1c\x1d\x1e\x1f",
"\x4e\x79\xdd\x46"},
{"32 bytes of decrementing 1f..00",
32,
"\x1f\x1e\x1d\x1c"
"\x1b\x1a\x19\x18"
"\x17\x16\x15\x14"
"\x13\x12\x11\x10"
"\x0f\x0e\x0d\x0c"
"\x0b\x0a\x09\x08"
"\x07\x06\x05\x04"
"\x03\x02\x01\x00",
"\x5c\xdb\x3f\x11"},
{"An iSCSI - SCSI Read (10) Command PDU",
48,
"\x01\xc0\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x14\x00\x00\x00"
"\x00\x00\x04\x00"
"\x00\x00\x00\x14"
"\x00\x00\x00\x18"
"\x28\x00\x00\x00"
"\x00\x00\x00\x00"
"\x02\x00\x00\x00"
"\x00\x00\x00\x00",
"\x56\x3a\x96\xd9"},
};
#define NUM_CRC_DATA (sizeof(crc_data)/sizeof(crc_data[0]))
int main()
{
int i;
for (i = 0; i < NUM_CRC_DATA; i++) {
unsigned int crc = ~calculate_crc32c(~0, crc_data[i].data, crc_data[i].data_len);
if (crc_data[i].crc == crc) {
printf("%s: OK\n", crc_data[i].name);
} else {
printf("%s: expect 0x%x but 0x%x\n", crc_data[i].name, crc_data[i].crc, crc);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment