diff --git a/source/crc.c b/source/crc.c index 37696d5..c2b5e20 100644 --- a/source/crc.c +++ b/source/crc.c @@ -92,3 +92,24 @@ unsigned short CRC_Block (const byte *start, int count) return crc; } + +// +// taken from darkplaces -- just a shortcut for initialization for +// use with string-hashing (just progs, likely). -- cypress (07 jan 2024) +// + +unsigned short CRC_Block2(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; +} \ No newline at end of file diff --git a/source/crc.h b/source/crc.h index 421f522..6764859 100644 --- a/source/crc.h +++ b/source/crc.h @@ -29,5 +29,8 @@ void CRC_ProcessByte(unsigned short *crcvalue, byte data); unsigned short CRC_Value(unsigned short crcvalue); unsigned short CRC_Block (const byte *start, int count); //johnfitz -- texture crc +unsigned short CRC_Block2(const unsigned char *data, size_t size); +unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size); + #endif /* _QUAKE_CRC_H */ diff --git a/source/pr_cmds.c b/source/pr_cmds.c index a38130e..6cd362a 100644 --- a/source/pr_cmds.c +++ b/source/pr_cmds.c @@ -3241,6 +3241,21 @@ void PF_strtolower (void) G_INT (OFS_RETURN) = PR_SetEngineString (result); } +/* +================= +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_Block2) ((unsigned char *) s, strlen(s))); +} + /* ================= PF_strlen @@ -3937,7 +3952,7 @@ static builtin_t pr_builtin[] = NULL, // #491 NULL, // #492 NULL, // #493 - NULL, // #494 + PF_crc16, // #494 NULL, // #495 NULL, // #496 NULL, // #497