From: Dale Weiler Date: Sun, 2 Feb 2014 10:00:15 +0000 (-0800) Subject: Merge pull request #141 from CurrentResident/big_endian_swap_fix X-Git-Tag: xonotic-v0.8.0~13 X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=commitdiff_plain;h=360389638bc546a53bd577e43168c0674be3c354;hp=9cc4fe1ed246e3c37af7cbd7245b0b413372f3b9 Merge pull request #141 from CurrentResident/big_endian_swap_fix Big endian swap fix --- diff --git a/code.c b/code.c index 0c3e5a3..540093f 100644 --- 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_endianswap(&code_header->version, 1, sizeof(code_header->version)); + util_endianswap(&code_header->crc16, 1, sizeof(code_header->crc16)); + util_endianswap(&code_header->statements.offset, 1, sizeof(code_header->statements.offset)); + util_endianswap(&code_header->statements.length, 1, sizeof(code_header->statements.length)); + util_endianswap(&code_header->defs.offset, 1, sizeof(code_header->defs.offset)); + util_endianswap(&code_header->defs.length, 1, sizeof(code_header->defs.length)); + util_endianswap(&code_header->fields.offset, 1, sizeof(code_header->fields.offset)); + util_endianswap(&code_header->fields.length, 1, sizeof(code_header->fields.length)); + util_endianswap(&code_header->functions.offset, 1, sizeof(code_header->functions.offset)); + util_endianswap(&code_header->functions.length, 1, sizeof(code_header->functions.length)); + util_endianswap(&code_header->strings.offset, 1, sizeof(code_header->strings.offset)); + util_endianswap(&code_header->strings.length, 1, sizeof(code_header->strings.length)); + util_endianswap(&code_header->globals.offset, 1, sizeof(code_header->globals.offset)); + util_endianswap(&code_header->globals.length, 1, sizeof(code_header->globals.length)); + util_endianswap(&code_header->entfield, 1, 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_endianswap(&code->statements[i].opcode, 1, sizeof(code->statements[i].opcode)); + util_endianswap(&code->statements[i].o1, 1, sizeof(code->statements[i].o1)); + util_endianswap(&code->statements[i].o2, 1, sizeof(code->statements[i].o2)); + util_endianswap(&code->statements[i].o3, 1, sizeof(code->statements[i].o3)); + } + + for (i = 0; i < vec_size(code->defs); ++i) { + util_endianswap(&code->defs[i].type, 1, sizeof(code->defs[i].type)); + util_endianswap(&code->defs[i].offset, 1, sizeof(code->defs[i].offset)); + util_endianswap(&code->defs[i].name, 1, sizeof(code->defs[i].name)); + } + + for (i = 0; i < vec_size(code->fields); ++i) { + util_endianswap(&code->fields[i].type, 1, sizeof(code->fields[i].type)); + util_endianswap(&code->fields[i].offset, 1, sizeof(code->fields[i].offset)); + util_endianswap(&code->fields[i].name, 1, sizeof(code->fields[i].name)); + } + + for (i = 0; i < vec_size(code->functions); ++i) { + util_endianswap(&code->functions[i].entry, 1, sizeof(code->functions[i].entry)); + util_endianswap(&code->functions[i].firstlocal, 1, sizeof(code->functions[i].firstlocal)); + util_endianswap(&code->functions[i].locals, 1, sizeof(code->functions[i].locals)); + util_endianswap(&code->functions[i].profile, 1, sizeof(code->functions[i].profile)); + util_endianswap(&code->functions[i].name, 1, sizeof(code->functions[i].name)); + util_endianswap(&code->functions[i].file, 1, sizeof(code->functions[i].file)); + util_endianswap(&code->functions[i].nargs, 1, sizeof(code->functions[i].nargs)); + /* Don't swap argsize[] - it's just a byte array, which Quake uses only as such. */ + } + + util_endianswap(code->globals, vec_size(code->globals), sizeof(int32_t)); if (!OPTS_OPTION_BOOL(OPTION_QUIET)) { diff --git a/gmqcc.h b/gmqcc.h index 8925a09..97f0e4d 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -213,9 +213,10 @@ 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_endianswap(void *, size_t, 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 7de0ec6..bbe5322 100644 --- 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_endianswap(&pak->header.magic, 1, sizeof(pak->header.magic)); + util_endianswap(&pak->header.diroff, 1, sizeof(pak->header.diroff)); + util_endianswap(&pak->header.dirlen, 1, 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_endianswap(&dir.pos, 1, sizeof(dir.pos)); + util_endianswap(&dir.len, 1, sizeof(dir.len)); vec_push(pak->directories, dir); } diff --git a/util.c b/util.c index 4e1f4f0..030f54f 100644 --- a/util.c +++ b/util.c @@ -111,16 +111,18 @@ 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<<1); /* swap64 operates on 32 bit words, thus scale to that length. */ return; - default: exit(EXIT_FAILURE); /* please blow the fuck up! */ + default: + con_err ("util_endianswap: I don't know how to swap a %u byte structure!\n", typesize); + exit(EXIT_FAILURE); /* please blow the fuck up! */ } # endif #endif