Skip to content

Instantly share code, notes, and snippets.

@thesjg
Created February 15, 2013 04:42
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 thesjg/4958618 to your computer and use it in GitHub Desktop.
Save thesjg/4958618 to your computer and use it in GitHub Desktop.
/* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
/* Date : 3 June 1993. */
/* Version : 1.0. */
/* Status : Public domain. */
/* statically configured to produce any table covered by the Rocksoft^tm */
/* Model CRC Algorithm. For more information on the Rocksoft^tm Model CRC */
/* Algorithm, see the document titled "A Painless Guide to CRC Error */
/* Detection Algorithms" by Ross Williams (ross@guest.adelaide.edu.au.). This */
/* document is likely to be in "ftp.adelaide.edu.au/pub/rocksoft". */
/* */
/* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
#include <stdio.h>
#include <stdlib.h>
#define TB_BITS 32
#define TB_POLY 0x1EDC6F41L
#define TB_TOPBIT 1L << (TB_BITS-1)
static void gentable(uint32_t *table);
static uint32_t cm_tab(int index);
static uint32_t reflect(uint32_t v, int b);
int
main(int argc, char *argv[])
{
uint32_t iscsiCrc32Table1[256];
uint32_t *table = &iscsiCrc32Table1[0];
gentable(table);
}
static void
gentable(uint32_t *table)
{
int i;
for (i = 0; i < 256; ++i) {
table[i] = cm_tab(i);
}
}
static uint32_t
cm_tab(int index)
{
int i;
uint32_t r;
uint32_t inbyte = (ulong)index;
inbyte = reflect(inbyte, 8);
r = inbyte << (TB_BITS-8);
for (i = 0; i < 8; ++i) {
if (r & TB_TOPBIT)
r = (r << 1) ^ TB_POLY;
else
r <<= 1;
}
r = reflect(r, TB_BITS);
return (r);
}
/*
* Returns the value v with the bottom b [0,32] bits reflected.
* Example: reflect(0x3e23L,3) == 0x3e26
*/
static uint32_t
reflect(uint32_t v, int b)
{
int i;
uint32_t t = v;
for (i=0; i<b; i++) {
if (t & 1L)
v |= 1L << ((b-1) - i);
else
v &= ~(1L << ((b-1) - i));
t >>= 1;
}
return (v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment