#include #include "crc32.h" #include #define LE_TABLE_SIZE (1 << CRC_LE_BITS) #define BE_TABLE_SIZE (1 << CRC_BE_BITS) static u_int32_t crc32table_le[LE_TABLE_SIZE]; static u_int32_t crc32table_be[BE_TABLE_SIZE]; /** * crc32init_le() - allocate and initialize LE table data * * crc is the crc of the byte i; other entries are filled in based on the * fact that crctable[i^j] = crctable[i] ^ crctable[j]. * */ static void crc32init_le(void) { unsigned i, j; u_int32_t crc = 1; crc32table_le[0] = 0; for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) { crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) crc32table_le[i + j] = crc ^ crc32table_le[j]; } } /** * crc32init_be() - allocate and initialize BE table data */ static void crc32init_be(void) { unsigned i, j; u_int32_t crc = 0x80000000; crc32table_be[0] = 0; for (i = 1; i < BE_TABLE_SIZE; i <<= 1) { crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0); for (j = 0; j < i; j++) crc32table_be[i + j] = crc ^ crc32table_be[j]; } } static void output_table(u_int32_t table[], int len) { int i; for (i = 0; i < len - 1; i++) { if (i % 6 == 0) printf("\n"); printf("0x%8.8xL, ", table[i]); } printf("0x%8.8xL\n", table[len - 1]); } static void output_table_endian(u_int32_t table[], int len, char endian_str[]) { int i; for (i = 0; i < len; i++) { if (i % 4 == 0) printf("\n"); printf("%s0x%8.8xL), ", endian_str, table[i]); } printf("\n"); } int main(int argc, char** argv) { printf("/* this file is generated - do not edit */\n\n"); if (CRC_LE_BITS > 1) { crc32init_le(); printf("const static u32 crc32table_le[] = {"); if(CRC_LE_BITS == 8){ printf("\n#define tole32(x) __constant_cpu_to_le32(x)"); output_table_endian(crc32table_le, BE_TABLE_SIZE, "tole32("); } else output_table(crc32table_le, LE_TABLE_SIZE); printf("};\n"); } if (CRC_BE_BITS > 1) { crc32init_be(); printf("const static u32 crc32table_be[] = {"); if(CRC_BE_BITS == 8){ printf("\n#define tobe32(x) __constant_cpu_to_be32(x)"); output_table_endian(crc32table_be, BE_TABLE_SIZE, "tobe32("); } else output_table(crc32table_be, BE_TABLE_SIZE); printf("};\n"); } return 0; }