Add crc16 builtin

This commit is contained in:
cypress 2024-01-07 10:41:41 -05:00
parent fcbaf93c79
commit d267d568e4
3 changed files with 40 additions and 1 deletions

View file

@ -79,3 +79,24 @@ unsigned short CRC_Value(unsigned short crcvalue)
{
return crcvalue ^ CRC_XOR_VALUE;
}
//
// taken from darkplaces -- just a shortcut for initialization for
// use with string-hashing (just progs, likely). -- cypress (07 jan 2024)
//
unsigned short CRC_Block(const unsigned char *data, size_t size)
{
unsigned short crc = CRC_INIT_VALUE;
while (size--)
crc = (crc << 8) ^ crctable[(crc >> 8) ^ (*data++)];
return crc ^ CRC_XOR_VALUE;
}
unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size)
{
unsigned short crc = CRC_INIT_VALUE;
while (size--)
crc = (crc << 8) ^ crctable[(crc >> 8) ^ (tolower(*data++))];
return crc ^ CRC_XOR_VALUE;
}

View file

@ -22,3 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
void CRC_Init(unsigned short *crcvalue);
void CRC_ProcessByte(unsigned short *crcvalue, byte data);
unsigned short CRC_Value(unsigned short crcvalue);
unsigned short CRC_Block(const unsigned char *data, size_t size);
unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size);

View file

@ -1356,6 +1356,21 @@ void PF_strtolower(void)
G_INT(OFS_RETURN) = pr_string_temp - pr_strings;
}
/*
=================
PF_crc16
float crc16 (float, string)
=================
*/
void PF_crc16(void)
{
int insens = G_FLOAT(OFS_PARM0);
char *s = G_STRING(OFS_PARM1);
G_FLOAT(OFS_RETURN) = (unsigned short) ((insens ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
}
/*
=================
PF_substring
@ -3964,7 +3979,7 @@ PF_Fixme,
PF_Fixme,
PF_Fixme,
PF_Fixme,
PF_Fixme,
PF_crc16,
PF_Fixme,
PF_Fixme,
PF_Fixme,