Created
February 15, 2013 04:42
-
-
Save thesjg/4958618 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Author : Ross Williams ([email protected].). */ | |
/* 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 ([email protected].). 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