diff --git a/source/crc.c b/source/crc.c index ed4880c..f76844c 100644 --- a/source/crc.c +++ b/source/crc.c @@ -78,4 +78,25 @@ void CRC_ProcessByte(unsigned short *crcvalue, byte data) 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; } \ No newline at end of file diff --git a/source/crc.h b/source/crc.h index cad9772..bcaf295 100644 --- a/source/crc.h +++ b/source/crc.h @@ -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); \ No newline at end of file diff --git a/source/pr_cmds.c b/source/pr_cmds.c index 9bba1bd..2563fce 100644 --- a/source/pr_cmds.c +++ b/source/pr_cmds.c @@ -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,