Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix endian issues with iff importer. #481

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions bitmap/iff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,22 @@ int bm_iff_get_sig(CFILE *f) {
int bm_iff_parse_bmhd(CFILE *ifile, uint32_t len, iff_bitmap_header *bmheader) {
len = len;

bmheader->w = cf_ReadShort(ifile);
bmheader->w = MOTOROLA_SHORT(bmheader->w);
bmheader->h = cf_ReadShort(ifile);
bmheader->h = MOTOROLA_SHORT(bmheader->h);
bmheader->x = cf_ReadShort(ifile);
bmheader->x = MOTOROLA_SHORT(bmheader->x);
bmheader->y = cf_ReadShort(ifile);
bmheader->y = MOTOROLA_SHORT(bmheader->y);
bmheader->w = cf_ReadShort(ifile, false);
bmheader->h = cf_ReadShort(ifile, false);
bmheader->x = cf_ReadShort(ifile, false);
bmheader->y = cf_ReadShort(ifile, false);

bmheader->nplanes = cf_ReadByte(ifile);
bmheader->masking = cf_ReadByte(ifile);
bmheader->compression = cf_ReadByte(ifile);
cf_ReadByte(ifile); /* skip pad */

bmheader->transparentcolor = cf_ReadShort(ifile);
bmheader->transparentcolor = MOTOROLA_SHORT(bmheader->transparentcolor);
bmheader->transparentcolor = cf_ReadShort(ifile, false);
bmheader->xaspect = cf_ReadByte(ifile);
bmheader->yaspect = cf_ReadByte(ifile);

bmheader->pagewidth = cf_ReadShort(ifile);
bmheader->pageheight = cf_ReadShort(ifile);
bmheader->pagewidth = cf_ReadShort(ifile, false);
bmheader->pageheight = cf_ReadShort(ifile, false);

iff_transparent_color = bmheader->transparentcolor;

Expand Down Expand Up @@ -383,8 +378,7 @@ int bm_iff_parse_file(CFILE *ifile, iff_bitmap_header *bmheader, iff_bitmap_head

sig = bm_iff_get_sig(ifile);

len = cf_ReadInt(ifile);
len = MOTOROLA_INT(len);
len = cf_ReadInt(ifile, false);

switch (sig) {
case IFF_SIG_FORM: {
Expand Down Expand Up @@ -568,7 +562,7 @@ int bm_iff_read_animbrush(const char *ifilename, int *bm_list) {
return -1;

sig = bm_iff_get_sig(ifile);
form_len = cf_ReadInt(ifile);
form_len = cf_ReadInt(ifile, false);

if (sig != IFF_SIG_FORM) {
mprintf(0, "Not a valid IFF file.\n");
Expand Down
8 changes: 4 additions & 4 deletions cfile/cfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,17 +909,17 @@ int cf_ReadBytes(uint8_t *buf, int count, CFILE *cfp) {
// to be present.
// Read and return an integer (32 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int32_t cf_ReadInt(CFILE *cfp) {
int32_t cf_ReadInt(CFILE *cfp, bool little_endian) {
int32_t i;
cf_ReadBytes((uint8_t *)&i, sizeof(i), cfp);
return INTEL_INT(i);
return little_endian ? D3::convert_le(i) : D3::convert_be(i);
}
// Read and return a int16_t (16 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int16_t cf_ReadShort(CFILE *cfp) {
int16_t cf_ReadShort(CFILE *cfp, bool little_endian) {
int16_t i;
cf_ReadBytes((uint8_t *)&i, sizeof(i), cfp);
return INTEL_SHORT(i);
return little_endian ? D3::convert_le(i) : D3::convert_be(i);
}
// Read and return a byte (8 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
Expand Down
4 changes: 2 additions & 2 deletions cfile/cfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ int cf_ReadBytes(uint8_t *buf, int count, CFILE *cfp);

// Read and return an integer (32 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int32_t cf_ReadInt(CFILE *cfp);
int32_t cf_ReadInt(CFILE *cfp, bool little_endian = true);

// Read and return a int16_t (16 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
int16_t cf_ReadShort(CFILE *cfp);
int16_t cf_ReadShort(CFILE *cfp, bool little_endian = true);

// Read and return a byte (8 bits)
// Throws an exception of type (cfile_error *) if the OS returns an error on read
Expand Down