Skip to content

Commit

Permalink
effects: Minor fixes for big-endian systems
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Nov 8, 2024
1 parent 01cc1f3 commit 5bc49c3
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions mojoshader_effects.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,40 @@ static uint32 readui32(const uint8 **_ptr, uint32 *_len)
return retval;
} // readui32

static uint16 readui16(const uint8 **_ptr, uint32 *_len)
{
uint16 retval = 0;
if (*_len < sizeof (retval))
*_len = 0;
else
{
const uint16 *ptr = (const uint16 *) *_ptr;
retval = SWAP16(*ptr);
*_ptr += sizeof (retval);
*_len -= sizeof (retval);
} // else
return retval;
} // readui16

static char *readstring(const uint8 *base,
const uint32 offset,
MOJOSHADER_malloc m,
void *d)
{
// !!! FIXME: sanity checks!
// !!! FIXME: verify this doesn't go past EOF looking for a null.
const char *str = ((const char *) base) + offset;
const uint32 len = *((const uint32 *) str);
char *strptr = NULL;
uint8 *pair = (uint8 *) (base + offset);
const uint32 *lenptr = (const uint32 *) pair;
char *strptr = (char *) (pair + sizeof(uint32));

const uint32 len = SWAP32(*lenptr);
char *result = NULL;

if (len == 0) return NULL; /* No length? No string. */
strptr = (char *) m(len, d);
memcpy(strptr, str + 4, len);
return strptr;

result = (char *) m(len, d);
memcpy(result, strptr, len);
return result;
} // readstring

static int findparameter(const MOJOSHADER_effectParam *params,
Expand Down Expand Up @@ -955,8 +975,9 @@ MOJOSHADER_effect *MOJOSHADER_compileEffect(const unsigned char *buf,

/* Read in header magic, seek to initial offset */
const uint8 *base = NULL;
uint32 header = readui32(&ptr, &len);
if (header == 0xBCF00BCF)
uint16 version = readui16(&ptr, &len);
uint16 header = readui16(&ptr, &len);
if ((header == 0xBCF0) && (version == 0x0BCF))
{
/* The Effect compiler provided with XNA4 adds some extra mess at the
* beginning of the file. It's useless though, so just skip it.
Expand All @@ -965,9 +986,10 @@ MOJOSHADER_effect *MOJOSHADER_compileEffect(const unsigned char *buf,
const uint32 skip = readui32(&ptr, &len) - 8;
ptr += skip;
len += skip;
header = readui32(&ptr, &len);
header = readui16(&ptr, &len);
version = readui16(&ptr, &len);
} // if
if (header != 0xFEFF0901)
if ((header != 0xFEFF) && (version != 0x0901))
{
MOJOSHADER_deleteEffect(retval);
return &MOJOSHADER_not_an_effect_effect;
Expand Down

0 comments on commit 5bc49c3

Please sign in to comment.