[util] Use uint16_t for crc16

This commit is contained in:
Bill Currie 2024-02-25 11:06:39 +09:00
parent 925dba732c
commit e89afafc8f
2 changed files with 14 additions and 14 deletions

View file

@ -35,11 +35,11 @@
#include "QF/qtypes.h"
void CRC_Init(unsigned short *crcvalue);
void CRC_ProcessByte(unsigned short *crcvalue, byte data);
void CRC_ProcessBlock (const byte *start, unsigned short *crcvalue, int count);
unsigned short CRC_Value(unsigned short crcvalue) __attribute__((const));
unsigned short CRC_Block (const byte *start, int count) __attribute__((pure));
void CRC_Init(uint16_t *crcvalue);
void CRC_ProcessByte(uint16_t *crcvalue, byte data);
void CRC_ProcessBlock (const byte *start, uint16_t *crcvalue, int count);
uint16_t CRC_Value(uint16_t crcvalue) __attribute__((const));
uint16_t CRC_Block (const byte *start, int count) __attribute__((pure));
///@}

View file

@ -37,7 +37,7 @@
#define CRC_INIT_VALUE 0xffff
#define CRC_XOR_VALUE 0x0000
static unsigned short crctable[256] = {
static uint16_t crctable[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
@ -74,36 +74,36 @@ static unsigned short crctable[256] = {
VISIBLE void
CRC_Init (unsigned short *crcvalue)
CRC_Init (uint16_t *crcvalue)
{
*crcvalue = CRC_INIT_VALUE;
}
VISIBLE void
CRC_ProcessByte (unsigned short *crcvalue, byte data)
CRC_ProcessByte (uint16_t *crcvalue, byte data)
{
*crcvalue = (*crcvalue << 8) ^ crctable[(*crcvalue >> 8) ^ data];
}
VISIBLE void
CRC_ProcessBlock (const byte *start, unsigned short *crcvalue, int count)
CRC_ProcessBlock (const byte *start, uint16_t *crcvalue, int count)
{
unsigned short crc = *crcvalue;
uint16_t crc = *crcvalue;
while (count--)
crc = (crc << 8) ^ crctable[(crc >> 8) ^ *start++];
*crcvalue = crc;
}
unsigned short
CRC_Value (unsigned short crcvalue)
uint16_t
CRC_Value (uint16_t crcvalue)
{
return crcvalue ^ CRC_XOR_VALUE;
}
VISIBLE unsigned short
VISIBLE uint16_t
CRC_Block (const byte *start, int count)
{
unsigned short crc;
uint16_t crc;
CRC_Init (&crc);
CRC_ProcessBlock (start, &crc, count);