Fix endian aware load/save when SDL is disabled
This commit is contained in:
parent
161357d331
commit
3e32bc0c39
@ -1458,6 +1458,12 @@ void Load_IFF(T_IO_Context * context)
|
|||||||
{
|
{
|
||||||
nb_colors = section_size/3;
|
nb_colors = section_size/3;
|
||||||
|
|
||||||
|
if (nb_colors > 256)
|
||||||
|
{
|
||||||
|
File_error = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (current_frame != 0)
|
if (current_frame != 0)
|
||||||
Warning("One CMAP per frame is not supported");
|
Warning("One CMAP per frame is not supported");
|
||||||
if ((header.BitPlanes==6 && nb_colors==16) || (header.BitPlanes==8 && nb_colors==64))
|
if ((header.BitPlanes==6 && nb_colors==16) || (header.BitPlanes==8 && nb_colors==64))
|
||||||
|
|||||||
72
src/io.c
72
src/io.c
@ -98,82 +98,154 @@ int Write_bytes(FILE *file, void *src, size_t size)
|
|||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Read_word_le(FILE *file, word *dest)
|
int Read_word_le(FILE *file, word *dest)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
if (fread(dest, 1, sizeof(word), file) != sizeof(word))
|
if (fread(dest, 1, sizeof(word), file) != sizeof(word))
|
||||||
return 0;
|
return 0;
|
||||||
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
||||||
*dest = SDL_Swap16(*dest);
|
*dest = SDL_Swap16(*dest);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
|
#else
|
||||||
|
byte buffer[2];
|
||||||
|
if (fread(buffer, 1, 2, file) != 2)
|
||||||
|
return 0;
|
||||||
|
*dest = (word)buffer[0] | (word)buffer[1] << 8;
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// Ecrit un word (little-endian)
|
// Ecrit un word (little-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Write_word_le(FILE *file, word w)
|
int Write_word_le(FILE *file, word w)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
||||||
w = SDL_Swap16(w);
|
w = SDL_Swap16(w);
|
||||||
#endif
|
#endif
|
||||||
return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
|
return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
|
||||||
|
#else
|
||||||
|
if (fputc((w >> 0) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((w >> 8) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// Lit un word (big-endian)
|
// Lit un word (big-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Read_word_be(FILE *file, word *dest)
|
int Read_word_be(FILE *file, word *dest)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
if (fread(dest, 1, sizeof(word), file) != sizeof(word))
|
if (fread(dest, 1, sizeof(word), file) != sizeof(word))
|
||||||
return 0;
|
return 0;
|
||||||
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
||||||
*dest = SDL_Swap16(*dest);
|
*dest = SDL_Swap16(*dest);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
|
#else
|
||||||
|
byte buffer[2];
|
||||||
|
if (fread(buffer, 1, 2, file) != 2)
|
||||||
|
return 0;
|
||||||
|
*dest = (word)buffer[0] << 8 | (word)buffer[1];
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// Ecrit un word (big-endian)
|
// Ecrit un word (big-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Write_word_be(FILE *file, word w)
|
int Write_word_be(FILE *file, word w)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
||||||
w = SDL_Swap16(w);
|
w = SDL_Swap16(w);
|
||||||
#endif
|
#endif
|
||||||
return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
|
return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
|
||||||
|
#else
|
||||||
|
if (fputc((w >> 8) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((w >> 0) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// Lit un dword (little-endian)
|
// Lit un dword (little-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Read_dword_le(FILE *file, dword *dest)
|
int Read_dword_le(FILE *file, dword *dest)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
|
if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
|
||||||
return 0;
|
return 0;
|
||||||
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
||||||
*dest = SDL_Swap32(*dest);
|
*dest = SDL_Swap32(*dest);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
|
#else
|
||||||
|
byte buffer[4];
|
||||||
|
if (fread(buffer, 1, 4, file) != 4)
|
||||||
|
return 0;
|
||||||
|
*dest = (dword)buffer[0] | (dword)buffer[1] << 8 | (dword)buffer[2] << 16 | (dword)buffer[3] << 24;
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// Ecrit un dword (little-endian)
|
// Ecrit un dword (little-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Write_dword_le(FILE *file, dword dw)
|
int Write_dword_le(FILE *file, dword dw)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
||||||
dw = SDL_Swap32(dw);
|
dw = SDL_Swap32(dw);
|
||||||
#endif
|
#endif
|
||||||
return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
|
return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
|
||||||
|
#else
|
||||||
|
if (fputc((dw >> 0) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((dw >> 8) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((dw >> 16) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((dw >> 24) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lit un dword (big-endian)
|
// Lit un dword (big-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Read_dword_be(FILE *file, dword *dest)
|
int Read_dword_be(FILE *file, dword *dest)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
|
if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
|
||||||
return 0;
|
return 0;
|
||||||
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
||||||
*dest = SDL_Swap32(*dest);
|
*dest = SDL_Swap32(*dest);
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
|
#else
|
||||||
|
byte buffer[4];
|
||||||
|
if (fread(buffer, 1, 4, file) != 4)
|
||||||
|
return 0;
|
||||||
|
*dest = (dword)buffer[0] << 24 | (dword)buffer[1] << 16 | (dword)buffer[2] << 8 | (dword)buffer[3];
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
// Ecrit un dword (big-endian)
|
// Ecrit un dword (big-endian)
|
||||||
// Renvoie -1 si OK, 0 en cas d'erreur
|
// Renvoie -1 si OK, 0 en cas d'erreur
|
||||||
int Write_dword_be(FILE *file, dword dw)
|
int Write_dword_be(FILE *file, dword dw)
|
||||||
{
|
{
|
||||||
|
#if defined(USE_SDL) || defined(USE_SDL2)
|
||||||
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
|
||||||
dw = SDL_Swap32(dw);
|
dw = SDL_Swap32(dw);
|
||||||
#endif
|
#endif
|
||||||
return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
|
return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
|
||||||
|
#else
|
||||||
|
if (fputc((dw >> 24) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((dw >> 16) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((dw >> 8) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
if (fputc((dw >> 0) & 0xff, file) == EOF)
|
||||||
|
return 0;
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Détermine la position du dernier '/' ou '\\' dans une chaine,
|
// Détermine la position du dernier '/' ou '\\' dans une chaine,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user