/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>

typedef unsigned char DOTNETBYTE;

DOTNETBYTE table[256];
DOTNETBYTE poly = 0xD5;

void createtable()
{
    int i, j;
    for(i = 0; i < 256; i++)
    {
        int temp = i;
        for(j = 0; j < 8; j++)
        {
            if((temp & 0x80) != 0)
            {
                temp = (temp << 1) ^ poly;
            }
            else
            {
                temp <<= 1 ;
            }
        }
        table[i] = (DOTNETBYTE)temp;
    }
}

DOTNETBYTE computechecksum(DOTNETBYTE *hej, int n)
{
    int i;
    DOTNETBYTE crc = 0;
    if(hej != NULL && n > 0)
    {
        for(i = 0; i < n; i++)
        {
            crc = table[crc ^ hej[i]];
        }
    }
    return crc;
}

int main()
{
    unsigned char b[] = { 1, 2, 3, 4 };
    createtable();
    printf("%d\n", computechecksum(b, sizeof(b)));
    return 0;
}
