]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Big-endian: Byteswap only the field contents when writing progs.dat
authorJim Thoenen <the.resident@gmail.com>
Sun, 2 Feb 2014 06:57:02 +0000 (00:57 -0600)
committerJim Thoenen <the.resident@gmail.com>
Sun, 2 Feb 2014 06:57:02 +0000 (00:57 -0600)
The previous code swapped not just the fields' contents themselves, but
also field positions within several of the structs, resulting in a
non-working progs.dat when compiled on big endian (ppc in my case).
Swapping on a field-by-field basis now.

Also:
* Addresses weird swap size requests (30+ bytes in one case)

* Take a guess at the right way to log a weird swap request before dying

* Fix swap array length scaling

* Rename the main swap function to reflect its native->little-endian
  purpose.  Figued that was okay because progs.dat is required to be
  always little-endian...

* Add a non-array version of the swap function for convenience

code.c
gmqcc.h
pak.c
util.c

diff --git a/code.c b/code.c
index 0c3e5a36611c3a62ed4f3326570ac96e4737bcf2..920b49de8219aa41d296b1ed4f9d1d268b483268 100644 (file)
--- a/code.c
+++ b/code.c
@@ -260,25 +260,58 @@ static void code_create_header(code_t *code, prog_header_t *code_header, const c
     }
 
     /* ensure all data is in LE format */
-    util_endianswap(&code_header->version,    1, sizeof(code_header->version));
-    util_endianswap(&code_header->crc16,      1, sizeof(code_header->crc16));
-    util_endianswap(&code_header->statements, 2, sizeof(code_header->statements.offset));
-    util_endianswap(&code_header->defs,       2, sizeof(code_header->statements.offset));
-    util_endianswap(&code_header->fields,     2, sizeof(code_header->statements.offset));
-    util_endianswap(&code_header->functions,  2, sizeof(code_header->statements.offset));
-    util_endianswap(&code_header->strings,    2, sizeof(code_header->statements.offset));
-    util_endianswap(&code_header->globals,    2, sizeof(code_header->statements.offset));
-    util_endianswap(&code_header->entfield,   1, sizeof(code_header->entfield));
+    util_tolittleendian(&code_header->version,              sizeof(code_header->version));
+    util_tolittleendian(&code_header->crc16,                sizeof(code_header->crc16));
+    util_tolittleendian(&code_header->statements.offset,    sizeof(code_header->statements.offset));
+    util_tolittleendian(&code_header->statements.length,    sizeof(code_header->statements.length));
+    util_tolittleendian(&code_header->defs.offset,          sizeof(code_header->defs.offset));
+    util_tolittleendian(&code_header->defs.length,          sizeof(code_header->defs.length));
+    util_tolittleendian(&code_header->fields.offset,        sizeof(code_header->fields.offset));
+    util_tolittleendian(&code_header->fields.length,        sizeof(code_header->fields.length));
+    util_tolittleendian(&code_header->functions.offset,     sizeof(code_header->functions.offset));
+    util_tolittleendian(&code_header->functions.length,     sizeof(code_header->functions.length));
+    util_tolittleendian(&code_header->strings.offset,       sizeof(code_header->strings.offset));
+    util_tolittleendian(&code_header->strings.length,       sizeof(code_header->strings.length));
+    util_tolittleendian(&code_header->globals.offset,       sizeof(code_header->globals.offset));
+    util_tolittleendian(&code_header->globals.length,       sizeof(code_header->globals.length));
+    util_tolittleendian(&code_header->entfield,             sizeof(code_header->entfield));
 
     /*
      * These are not part of the header but we ensure LE format here to save on duplicated
      * code.
      */
-    util_endianswap(code->statements, vec_size(code->statements), sizeof(prog_section_statement_t));
-    util_endianswap(code->defs,       vec_size(code->defs),       sizeof(prog_section_def_t));
-    util_endianswap(code->fields,     vec_size(code->fields),     sizeof(prog_section_field_t));
-    util_endianswap(code->functions,  vec_size(code->functions),  sizeof(prog_section_function_t));
-    util_endianswap(code->globals,    vec_size(code->globals),    sizeof(int32_t));
+
+    for (i = 0; i < vec_size(code->statements); ++i) {
+        util_tolittleendian(& code->statements[i].opcode,   sizeof(code->statements[i].opcode));
+        util_tolittleendian(& code->statements[i].o1,       sizeof(code->statements[i].o1));
+        util_tolittleendian(& code->statements[i].o2,       sizeof(code->statements[i].o2));
+        util_tolittleendian(& code->statements[i].o3,       sizeof(code->statements[i].o3));
+    }
+
+    for (i = 0; i < vec_size(code->defs); ++i) {
+        util_tolittleendian(& code->defs[i].type,   sizeof(code->defs[i].type));
+        util_tolittleendian(& code->defs[i].offset, sizeof(code->defs[i].offset));
+        util_tolittleendian(& code->defs[i].name,   sizeof(code->defs[i].name));
+    }
+
+    for (i = 0; i < vec_size(code->fields); ++i) {
+        util_tolittleendian(& code->fields[i].type,   sizeof(code->fields[i].type));
+        util_tolittleendian(& code->fields[i].offset, sizeof(code->fields[i].offset));
+        util_tolittleendian(& code->fields[i].name,   sizeof(code->fields[i].name));
+    }
+
+    for (i = 0; i < vec_size(code->functions); ++i) {
+        util_tolittleendian(& code->functions[i].entry,         sizeof(code->functions[i].entry));
+        util_tolittleendian(& code->functions[i].firstlocal,    sizeof(code->functions[i].firstlocal));
+        util_tolittleendian(& code->functions[i].locals,        sizeof(code->functions[i].locals));
+        util_tolittleendian(& code->functions[i].profile,       sizeof(code->functions[i].profile));
+        util_tolittleendian(& code->functions[i].name,          sizeof(code->functions[i].name));
+        util_tolittleendian(& code->functions[i].file,          sizeof(code->functions[i].file));
+        util_tolittleendian(& code->functions[i].nargs,         sizeof(code->functions[i].nargs));
+        /* Don't swap argsize[] - it's just a byte array, which Quake uses only as such. */
+    }
+
+    util_tolittleendianarray(code->globals, vec_size(code->globals), sizeof(int32_t));
 
 
     if (!OPTS_OPTION_BOOL(OPTION_QUIET)) {
@@ -412,9 +445,9 @@ bool code_write(code_t *code, const char *filename, const char *lnofile) {
         if (!fp)
             return false;
 
-        util_endianswap(&version,         1,                          sizeof(version));
-        util_endianswap(code->linenums,   vec_size(code->linenums),   sizeof(code->linenums[0]));
-        util_endianswap(code->columnnums, vec_size(code->columnnums), sizeof(code->columnnums[0]));
+        util_tolittleendian     (&version,                                     sizeof(version));
+        util_tolittleendianarray(code->linenums,   vec_size(code->linenums),   sizeof(code->linenums[0]));
+        util_tolittleendianarray(code->columnnums, vec_size(code->columnnums), sizeof(code->columnnums[0]));
 
         if (fs_file_write("LNOF",                          4,                                      1,                          fp) != 1 ||
             fs_file_write(&version,                        sizeof(version),                        1,                          fp) != 1 ||
diff --git a/gmqcc.h b/gmqcc.h
index 8925a09f23d34d8ead889d7efc0857246993fb39..367065f40ff574c18cd8e5993e1463dcbc2acf87 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -213,9 +213,11 @@ void *stat_mem_allocate  (size_t, size_t, const char *, const char *);
 #define util_isprint(a) (((unsigned)(a)-0x20) < 0x5F)
 #define util_isspace(a) (((a) >= 9 && (a) <= 13) || (a) == ' ')
 
-bool  util_strupper      (const char *);
-bool  util_strdigit      (const char *);
-void  util_endianswap    (void *, size_t, unsigned int);
+bool  util_strupper (const char *);
+bool  util_strdigit (const char *);
+
+void  util_tolittleendianarray  (void *, size_t, unsigned int);
+void  util_tolittleendian       (void *,         unsigned int);
 
 size_t util_strtocmd         (const char *, char *, size_t);
 size_t util_strtononcmd      (const char *, char *, size_t);
diff --git a/pak.c b/pak.c
index 7de0ec6008c6d2a41beb868ea2aad4e9cf57da21..eb523967ec40169fd808877764ad8f071d2018ca 100644 (file)
--- a/pak.c
+++ b/pak.c
@@ -145,7 +145,10 @@ static pak_file_t *pak_open_read(const char *file) {
 
     memset         (&pak->header, 0, sizeof(pak_header_t));
     fs_file_read   (&pak->header,    sizeof(pak_header_t), 1, pak->handle);
-    util_endianswap(&pak->header, 1, sizeof(pak_header_t));
+
+    util_tolittleendian(&pak->header.magic,  sizeof(pak->header.magic));
+    util_tolittleendian(&pak->header.diroff, sizeof(pak->header.diroff));
+    util_tolittleendian(&pak->header.dirlen, sizeof(pak->header.dirlen));
 
     /*
      * Every PAK file has "PACK" stored as FOURCC data in the
@@ -171,7 +174,10 @@ static pak_file_t *pak_open_read(const char *file) {
     for (itr = 0; itr < pak->header.dirlen / 64; itr++) {
         pak_directory_t dir;
         fs_file_read   (&dir,    sizeof(pak_directory_t), 1, pak->handle);
-        util_endianswap(&dir, 1, sizeof(pak_directory_t));
+
+        /* Don't translate name - it's just an array of bytes. */
+        util_tolittleendian(&dir.pos, sizeof(dir.pos));
+        util_tolittleendian(&dir.len, sizeof(dir.len));
 
         vec_push(pak->directories, dir);
     }
@@ -212,7 +218,7 @@ static pak_file_t *pak_open_write(const char *file) {
     pak->header.magic = PAK_FOURCC;
 
     /* on BE systems we need to swap the byte order of the FOURCC */
-    util_endianswap(&pak->header.magic, 1, sizeof(uint32_t));
+    util_tolittleendian(&pak->header.magic, sizeof(uint32_t));
 
     /*
      * We need to write out the header since files will be wrote out to
diff --git a/util.c b/util.c
index 4e1f4f013074b4f93928d8b55d196f7ea6211df1..bcffdcbf9fa43df7be0bb4925f33240a065e1ad1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -95,7 +95,7 @@ const char *util_instr_str[VINSTR_END] = {
     }
 #endif
 
-void util_endianswap(void *_data, size_t length, unsigned int typesize) {
+void util_tolittleendianarray(void *_data, size_t length, unsigned int typesize) {
 #   if PLATFORM_BYTE_ORDER == -1 /* runtime check */
     if (*((char*)&typesize))
         return;
@@ -111,21 +111,27 @@ void util_endianswap(void *_data, size_t length, unsigned int typesize) {
         switch (typesize) {
             case 1: return;
             case 2:
-                util_swap16((uint16_t*)_data, length>>1);
+                util_swap16((uint16_t*)_data, length);
                 return;
             case 4:
-                util_swap32((uint32_t*)_data, length>>2);
+                util_swap32((uint32_t*)_data, length);
                 return;
             case 8:
-                util_swap64((uint32_t*)_data, length>>3);
+                util_swap64((uint32_t*)_data, length);
                 return;
 
-            default: exit(EXIT_FAILURE); /* please blow the fuck up! */
+            default:
+                con_err ("util_tolittleendianarray: I don't know how to swap a %d byte structure!\n", typesize);
+                exit(EXIT_FAILURE); /* please blow the fuck up! */
         }
 #   endif
 #endif
 }
 
+void util_tolittleendian(void *_data, unsigned int typesize) {
+    util_tolittleendianarray(_data, 1, typesize);
+}
+
 /*
 * Based On:
 *   Slicing-by-8 algorithms by Michael E.