]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
Generating checksums
authorWolfgang (Blub) Bumiller <blub@speed.at>
Tue, 2 Oct 2012 15:09:49 +0000 (17:09 +0200)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Tue, 2 Oct 2012 15:09:49 +0000 (17:09 +0200)
code.c
gmqcc.h
parser.c
util.c

diff --git a/code.c b/code.c
index bf46cc075709914b54d1aa1c56ec906879ae15be..e828ccc81b5da0c3d6964fa820162600f71922d1 100644 (file)
--- a/code.c
+++ b/code.c
@@ -72,7 +72,6 @@ void code_init() {
     prog_section_def       empty_def       = {0, 0, 0};
     int                    i               = 0;
 
-    code_crc = 0;
     code_entfields = 0;
 
     /* omit creation of null code */
diff --git a/gmqcc.h b/gmqcc.h
index 158463332e78a31ca40ab91d91f67336c2d574c0..40f8de0fe357ab34d297fb1c14f53f5fcc3b64dd 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -210,7 +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);
 
-uint32_t util_crc32(const char *, int, register const short);
+uint16_t util_crc16(const char *, int, const short);
+uint32_t util_crc32(const char *, int, const short);
 
 #ifdef NOTRACK
 #    define mem_a(x) malloc(x)
@@ -512,6 +513,7 @@ VECTOR_PROT(prog_section_field,     code_fields    );
 VECTOR_PROT(prog_section_function,  code_functions );
 VECTOR_PROT(int,                    code_globals   );
 VECTOR_PROT(char,                   code_chars     );
+extern uint16_t code_crc;
 
 typedef float   qcfloat;
 typedef int32_t qcint;
index 2d13537fc680010953fae1273a439921cdc01701..9740932834fd9b1643cd6e542cdefff061acf026 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -2899,6 +2899,73 @@ void parser_cleanup()
     mem_d(parser);
 }
 
+extern const uint16_t util_crc16_table[];
+static uint16_t stream_crc16(uint16_t old, const char *str)
+{
+    printf("%s", str);
+    while (*str) {
+        old = (old<<8) ^ util_crc16_table[(old >> 8) ^ ((unsigned char)*str)];
+        ++str;
+    }
+    return old;
+}
+
+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 = 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");
+       */
+       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;
+               default:
+                   crc = stream_crc16(crc, "\tint\t");
+                   break;
+           }
+           crc = stream_crc16(crc, parser->globals[i].name);
+           crc = stream_crc16(crc, ";\n");
+       }
+       crc = stream_crc16(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;
+               default:
+                   crc = stream_crc16(crc, "\tint\t");
+                   break;
+           }
+           crc = stream_crc16(crc, parser->fields[i].name);
+           crc = stream_crc16(crc, ";\n");
+       }
+       crc = stream_crc16(crc, "} entvars_t;\n\n");
+
+printf("GOT CRC %u\n", crc);
+       code_crc = crc;
+}
+
 bool parser_finish(const char *output)
 {
     size_t i;
@@ -2996,6 +3063,8 @@ bool parser_finish(const char *output)
             if (opts_dump)
                 ir_builder_dump(ir, printf);
 
+            generate_checksum(parser);
+
             if (!ir_builder_generate(ir, output)) {
                 printf("*** failed to generate output file\n");
                 ir_builder_delete(ir);
diff --git a/util.c b/util.c
index 04560602859715cf41e7e8030380069fad08ea9d..31865955dadacdd38fa1294278cb0bd19a27d690 100644 (file)
--- 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
 };
-static const uint16_t util_crc16_table[] = {
+const uint16_t util_crc16_table[] = {
     0x0000,     0x1021,     0x2042,     0x3063,     0x4084,     0x50A5,
     0x60C6,     0x70E7,     0x8108,     0x9129,     0xA14A,     0xB16B,
     0xC18C,     0xD1AD,     0xE1CE,     0xF1EF,     0x1231,     0x0210,