Add crc16 builtin

This commit is contained in:
cypress 2024-01-07 10:41:32 -05:00
parent 23a9dc0b9a
commit 839570b698
3 changed files with 40 additions and 0 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

@ -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 },