#include "amiCrc.h"

#define CRC32_POLYNOMIAL 0xedb88320

AM_LIB_C_HEADER(amiCrc, AMI_CRC)

void amiCrc32RCreateTable(unsigned int *table) {
    for (int i = 0; i < 256; i++) {
        unsigned int value = (~i & 1) - 1 & CRC32_POLYNOMIAL;
        value = ((i >> 1 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        value = ((i >> 2 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        value = ((i >> 3 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        value = ((i >> 4 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        value = ((i >> 5 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        value = ((i >> 6 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        table[0] = ((i >> 7 ^ ~value) & 1) - 1 & CRC32_POLYNOMIAL ^ value >> 1;
        table++;
    }
}

unsigned int amiCrc32RGet(unsigned int *table, int length, unsigned char *data,
                          unsigned int initial) {
    unsigned int value = ~initial;
    while (length > 0) {
        value = value >> 8 ^ table[(data[0] ^ value) & 0xff];
        length += -1;
        data++;
    }
    return ~value;
}
