From 5dd8e23dfdcb7056c34f016a4774773405dc9001 Mon Sep 17 00:00:00 2001 From: "Wolfgang (Blub) Bumiller" Date: Tue, 2 Oct 2012 17:51:03 +0200 Subject: [PATCH] Cleaner crc implementation --- gmqcc.h | 4 +-- parser.c | 77 ++++++++++++++++++++++++++++++-------------------------- util.c | 11 ++++---- 3 files changed, 49 insertions(+), 43 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index 40f8de0..5e34c94 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -210,8 +210,8 @@ void util_endianswap (void *, int, int); size_t util_strtocmd (const char *, char *, size_t); size_t util_strtononcmd (const char *, char *, size_t); -uint16_t util_crc16(const char *, int, const short); -uint32_t util_crc32(const char *, int, const short); +uint16_t util_crc16(uint16_t crc, const char *data, size_t len); +uint32_t util_crc32(uint32_t crc, const char *data, size_t len); #ifdef NOTRACK # define mem_a(x) malloc(x) diff --git a/parser.c b/parser.c index 9740932..d865212 100644 --- a/parser.c +++ b/parser.c @@ -2899,14 +2899,20 @@ void parser_cleanup() mem_d(parser); } -extern const uint16_t util_crc16_table[]; -static uint16_t stream_crc16(uint16_t old, const char *str) +static uint16_t progdefs_crc_sum(uint16_t old, const char *str) { - printf("%s", str); - while (*str) { - old = (old<<8) ^ util_crc16_table[(old >> 8) ^ ((unsigned char)*str)]; - ++str; - } + return util_crc16(old, str, strlen(str)); +} + +static void progdefs_crc_file(const char *str) +{ + /* write to progdefs.h here */ +} + +static uint16_t progdefs_crc_both(uint16_t old, const char *str) +{ + old = progdefs_crc_sum(old, str); + progdefs_crc_file(str); return old; } @@ -2915,54 +2921,53 @@ static void generate_checksum(parser_t *parser) uint16_t crc = 0xFFFF; size_t i; - crc = stream_crc16(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{"); - crc = stream_crc16(crc, "\tint\tpad[28];\n"); + crc = progdefs_crc_both(crc, "\n/* file generated by qcc, do not modify */\n\ntypedef struct\n{"); + crc = progdefs_crc_sum(crc, "\tint\tpad[28];\n"); /* - crc = stream_crc16(crc, "\tint\tpad;\n"); - crc = stream_crc16(crc, "\tint\tofs_return[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm0[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm1[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm2[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm3[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm4[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm5[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm6[3];\n"); - crc = stream_crc16(crc, "\tint\tofs_parm7[3];\n"); + progdefs_crc_file("\tint\tpad;\n"); + progdefs_crc_file("\tint\tofs_return[3];\n"); + progdefs_crc_file("\tint\tofs_parm0[3];\n"); + progdefs_crc_file("\tint\tofs_parm1[3];\n"); + progdefs_crc_file("\tint\tofs_parm2[3];\n"); + progdefs_crc_file("\tint\tofs_parm3[3];\n"); + progdefs_crc_file("\tint\tofs_parm4[3];\n"); + progdefs_crc_file("\tint\tofs_parm5[3];\n"); + progdefs_crc_file("\tint\tofs_parm6[3];\n"); + progdefs_crc_file("\tint\tofs_parm7[3];\n"); */ for (i = 0; i < parser->crc_globals; ++i) { if (!ast_istype(parser->globals[i].var, ast_value)) continue; switch (parser->globals[i].var->expression.vtype) { - case TYPE_FLOAT: crc = stream_crc16(crc, "\tfloat\t"); break; - case TYPE_VECTOR: crc = stream_crc16(crc, "\tvec3_t\t"); break; - case TYPE_STRING: crc = stream_crc16(crc, "\tstring_t\t"); break; - case TYPE_FUNCTION: crc = stream_crc16(crc, "\tfunc_t\t"); break; + case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break; + case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break; + case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break; + case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break; default: - crc = stream_crc16(crc, "\tint\t"); + crc = progdefs_crc_both(crc, "\tint\t"); break; } - crc = stream_crc16(crc, parser->globals[i].name); - crc = stream_crc16(crc, ";\n"); + crc = progdefs_crc_both(crc, parser->globals[i].name); + crc = progdefs_crc_both(crc, ";\n"); } - crc = stream_crc16(crc, "} globalvars_t;\n\ntypedef struct\n{\n"); + crc = progdefs_crc_both(crc, "} globalvars_t;\n\ntypedef struct\n{\n"); for (i = 0; i < parser->crc_fields; ++i) { if (!ast_istype(parser->fields[i].var, ast_value)) continue; switch (parser->fields[i].var->expression.next->expression.vtype) { - case TYPE_FLOAT: crc = stream_crc16(crc, "\tfloat\t"); break; - case TYPE_VECTOR: crc = stream_crc16(crc, "\tvec3_t\t"); break; - case TYPE_STRING: crc = stream_crc16(crc, "\tstring_t\t"); break; - case TYPE_FUNCTION: crc = stream_crc16(crc, "\tfunc_t\t"); break; + case TYPE_FLOAT: crc = progdefs_crc_both(crc, "\tfloat\t"); break; + case TYPE_VECTOR: crc = progdefs_crc_both(crc, "\tvec3_t\t"); break; + case TYPE_STRING: crc = progdefs_crc_both(crc, "\tstring_t\t"); break; + case TYPE_FUNCTION: crc = progdefs_crc_both(crc, "\tfunc_t\t"); break; default: - crc = stream_crc16(crc, "\tint\t"); + crc = progdefs_crc_both(crc, "\tint\t"); break; } - crc = stream_crc16(crc, parser->fields[i].name); - crc = stream_crc16(crc, ";\n"); + crc = progdefs_crc_both(crc, parser->fields[i].name); + crc = progdefs_crc_both(crc, ";\n"); } - crc = stream_crc16(crc, "} entvars_t;\n\n"); + crc = progdefs_crc_both(crc, "} entvars_t;\n\n"); -printf("GOT CRC %u\n", crc); code_crc = crc; } diff --git a/util.c b/util.c index 3186595..e1db03f 100644 --- a/util.c +++ b/util.c @@ -285,7 +285,7 @@ static const uint32_t util_crc32_table[] = { 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }; -const uint16_t util_crc16_table[] = { +static const uint16_t util_crc16_table[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, @@ -334,13 +334,14 @@ const uint16_t util_crc16_table[] = { /* * Implements a CRC function for X worth bits using (uint[X]_t) * as type. and util_crc[X]_table. + * Streamable QCC compatible CRC functions. */ #define CRC(X) \ -uint##X##_t util_crc##X(const char *k, int len, const short clamp) { \ - register uint##X##_t h= (uint##X##_t)0xFFFFFFFF; \ +uint##X##_t util_crc##X(uint##X##_t current, const char *k, size_t len) { \ + register uint##X##_t h= current; \ for (; len; --len, ++k) \ - h = util_crc##X##_table[(h^((unsigned char)*k))&0xFF]^(h>>8); \ - return (~h)%clamp; \ + h = util_crc##X##_table[(h>>8)^((unsigned char)*k)]^(h<<8); \ + return h; \ } CRC(32) CRC(16) -- 2.39.2