From 839570b6987be5f186ce7846e8dc6ef88c2a4a40 Mon Sep 17 00:00:00 2001 From: cypress Date: Sun, 7 Jan 2024 10:41:32 -0500 Subject: [PATCH] Add crc16 builtin --- source/crc.c | 21 +++++++++++++++++++++ source/crc.h | 3 +++ source/pr_cmds.c | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/source/crc.c b/source/crc.c index 0d2359a..f76844c 100644 --- a/source/crc.c +++ b/source/crc.c @@ -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; +} \ 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 31cfbdf..d1d7af8 100644 --- a/source/pr_cmds.c +++ b/source/pr_cmds.c @@ -1425,6 +1425,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_stov @@ -3729,6 +3744,7 @@ ebfs_builtin_t pr_ebfs_builtins[] = { 441, "tokenize", PF_tokenize }, { 442, "argv", PF_ArgV }, { 480, "strtolower", PF_strtolower }, + { 494, "crc16", PF_crc16 }, { 500, "songegg", PF_SongEgg }, { 501, "nzp_maxammo", PF_MaxAmmo },