DescentDevelopers / Descent3

Descent 3 by Outrage Entertainment
GNU General Public License v3.0
2.73k stars 231 forks source link

Fix endian issues with iff importer. #481

Closed MaddTheSane closed 1 day ago

MaddTheSane commented 2 days ago

Pull Request Type

Description

There were a couple of places that weren't byte-swapped on little-endian systems. And due to cf_ReadInt and cf_ReadShort automatically calling INTEL_INT which then byte-swaps the value on big-endian machines, the next calls to MOTOROLA_SHORT and MOTOROLA_INT don't swap the value because it's on a big-endian machine.

Checklist

Additional Comments

winterheart commented 2 days ago

I'd recommend implement additional cf_ReadShortBE() and cf_ReadIntBE() or extend current cf_ReadShort() and cf_ReadInt() with optional parameter little_endian in CFILE module like this:


// Declaration in cfile.h
int32_t cf_ReadInt(CFILE *cfp, bool little_endian = true);

// Implementation in cfile.cpp
int32_t cf_ReadInt(CFILE *cfp, bool little_endian) {
  int32_t i;
  cf_ReadBytes((uint8_t *)&i, sizeof(i), cfp);
  return (little_endian ? INTEL_INT(i) : MOTOROLA_INT(i));
}

After that you can use it without additional conversion code in iff