Added a helper function for auto-detecting value endianness

This commit is contained in:
NicknineTheEagle 2018-07-17 23:54:24 +03:00
parent 454488b56c
commit e2160f4e09
3 changed files with 12 additions and 2 deletions

View File

@ -89,7 +89,7 @@ VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE *streamFile) {
* 0x0c(4): some sub-offset? (0x20, found when @0x01 is set) */
/* use start_offset as endianness flag */
if ((uint32_t)read_32bitLE(0x08,streamFile) > 0x0000FFFF) {
if (guess_endianness32bit(0x08,streamFile)) {
read_32bit = read_32bitBE;
} else {
read_32bit = read_32bitLE;

View File

@ -149,7 +149,7 @@ VGMSTREAM * init_vgmstream_ea_bnk(STREAMFILE *streamFile) {
goto fail;
/* use header size as endianness flag */
if ((uint32_t)read_32bitLE(0x08,streamFile) > 0x000F0000) { /* todo not very accurate */
if (guess_endianness32bit(offset + 0x08,streamFile)) {
read_32bit = read_32bitBE;
read_16bit = read_16bitBE;
} else {

View File

@ -171,6 +171,16 @@ static inline int8_t read_8bit(off_t offset, STREAMFILE * streamfile) {
return buf[0];
}
/* guess byte endianness from a given value, return true if big endian and false if little endian */
/* TODO: possibly improve */
static inline int guess_endianness16bit(off_t offset, STREAMFILE * streamfile) {
return ((uint16_t)read_16bitLE(offset,streamfile) > (uint16_t)read_16bitBE(offset,streamfile)) ? 1 : 0;
}
static inline int guess_endianness32bit(off_t offset, STREAMFILE * streamfile) {
return ((uint32_t)read_32bitLE(offset,streamfile) > (uint32_t)read_32bitBE(offset,streamfile)) ? 1 : 0;
}
/* various STREAMFILE helpers functions */
size_t get_streamfile_text_line(int dst_length, char * dst, off_t offset, STREAMFILE * streamfile, int *line_done_ptr);