split fill table endians, mark consts
This commit is contained in:
parent
4524ecba1a
commit
2ce38461bb
2 changed files with 35 additions and 12 deletions
42
crc32.c
42
crc32.c
|
@ -13,35 +13,55 @@ crc_reflect(uint32_t input)
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
crc_fill_table(uint32_t *table, int big, uint32_t polynomial)
|
crc_be_fill_table(uint32_t *table, uint32_t polynomial)
|
||||||
{
|
{
|
||||||
uint32_t lsb = (big) ? 1 << 31 : 1; /* least significant bit */
|
const uint32_t lsb = 1 << 31;
|
||||||
uint32_t poly = (big) ? polynomial : crc_reflect(polynomial);
|
uint32_t poly = polynomial;
|
||||||
|
|
||||||
for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) {
|
for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) {
|
||||||
*table = (big) ? c << 24 : c;
|
uint32_t v = c << 24;
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
if (*table & lsb) {
|
if (v & lsb) {
|
||||||
*table = (big) ? *table << 1 : *table >> 1;
|
v <<= 1;
|
||||||
*table ^= poly;
|
v ^= poly;
|
||||||
} else {
|
} else {
|
||||||
*table = (big) ? *table << 1 : *table >> 1;
|
v <<= 1;
|
||||||
}
|
}
|
||||||
*table &= 0xFFFFFFFF;
|
|
||||||
}
|
}
|
||||||
|
*table = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
crc_le_fill_table(uint32_t *table, uint32_t polynomial)
|
||||||
|
{
|
||||||
|
const uint32_t lsb = 1;
|
||||||
|
uint32_t poly = crc_reflect(polynomial);
|
||||||
|
|
||||||
|
for (int c = 0; c < CRC_TABLE_SIZE; c++, table++) {
|
||||||
|
uint32_t v = c;
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
if (v & lsb) {
|
||||||
|
v >>= 1;
|
||||||
|
v ^= poly;
|
||||||
|
} else {
|
||||||
|
v >>= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*table = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
crc_be_cycle(uint32_t *table, uint32_t *remainder, char c)
|
crc_be_cycle(uint32_t *table, uint32_t *remainder, char c)
|
||||||
{
|
{
|
||||||
uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xff];
|
const uint32_t byte = table[(((*remainder) >> 24) ^ c) & 0xFF];
|
||||||
*remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF;
|
*remainder = (((*remainder) << 8) ^ byte) & 0xFFFFFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
crc_le_cycle(uint32_t *table, uint32_t *remainder, char c)
|
crc_le_cycle(uint32_t *table, uint32_t *remainder, char c)
|
||||||
{
|
{
|
||||||
uint32_t byte = table[((*remainder) ^ c) & 0xFF];
|
const uint32_t byte = table[((*remainder) ^ c) & 0xFF];
|
||||||
*remainder = ((*remainder) >> 8) ^ byte;
|
*remainder = ((*remainder) >> 8) ^ byte;
|
||||||
}
|
}
|
||||||
|
|
5
main.c
5
main.c
|
@ -127,7 +127,10 @@ cycle_file(FILE *stream)
|
||||||
(big_endian) ? crc_be_cycle : crc_le_cycle;
|
(big_endian) ? crc_be_cycle : crc_le_cycle;
|
||||||
uint32_t table[CRC_TABLE_SIZE];
|
uint32_t table[CRC_TABLE_SIZE];
|
||||||
|
|
||||||
crc_fill_table(table, big_endian, polynomial);
|
if (big_endian)
|
||||||
|
crc_be_fill_table(table, polynomial);
|
||||||
|
else
|
||||||
|
crc_le_fill_table(table, polynomial);
|
||||||
do {
|
do {
|
||||||
int len = fread(buff, 1, BUFFER_SIZE, stream);
|
int len = fread(buff, 1, BUFFER_SIZE, stream);
|
||||||
if (ferror(stream)) {
|
if (ferror(stream)) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue