From 454af06acfb04fc3214459a06b43dd027117e259 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 19 Aug 2023 14:06:06 +0200 Subject: [PATCH] - use a local byteswap header in the file system. --- src/common/filesystem/file_grp.cpp | 3 + src/common/filesystem/file_lump.cpp | 1 - src/common/filesystem/file_pak.cpp | 10 +- src/common/filesystem/file_rff.cpp | 5 +- src/common/filesystem/file_wad.cpp | 4 +- src/common/filesystem/file_whres.cpp | 16 +-- src/common/filesystem/file_zip.cpp | 8 +- src/common/filesystem/files.cpp | 4 +- src/common/filesystem/files.h | 33 +++--- src/common/filesystem/files_decompress.cpp | 6 +- src/common/filesystem/fs_findfile.cpp | 2 +- src/common/filesystem/fs_swap.h | 121 +++++++++++++++++++++ 12 files changed, 170 insertions(+), 43 deletions(-) create mode 100644 src/common/filesystem/fs_swap.h diff --git a/src/common/filesystem/file_grp.cpp b/src/common/filesystem/file_grp.cpp index 7ed922263..4765bcde0 100644 --- a/src/common/filesystem/file_grp.cpp +++ b/src/common/filesystem/file_grp.cpp @@ -34,6 +34,9 @@ */ #include "resourcefile.h" +#include "fs_swap.h" + +using namespace fs_private; //========================================================================== // diff --git a/src/common/filesystem/file_lump.cpp b/src/common/filesystem/file_lump.cpp index f16fefaab..60c957081 100644 --- a/src/common/filesystem/file_lump.cpp +++ b/src/common/filesystem/file_lump.cpp @@ -33,7 +33,6 @@ */ #include "resourcefile.h" -#include "cmdlib.h" //========================================================================== // diff --git a/src/common/filesystem/file_pak.cpp b/src/common/filesystem/file_pak.cpp index f9e96eed6..4003ce396 100644 --- a/src/common/filesystem/file_pak.cpp +++ b/src/common/filesystem/file_pak.cpp @@ -34,6 +34,8 @@ #include "resourcefile.h" +using namespace fs_private; + //========================================================================== // // @@ -43,14 +45,14 @@ struct dpackfile_t { char name[56]; - int filepos, filelen; + uint32_t filepos, filelen; } ; struct dpackheader_t { - int ident; // == IDPAKHEADER - int dirofs; - int dirlen; + uint32_t ident; // == IDPAKHEADER + uint32_t dirofs; + uint32_t dirlen; } ; diff --git a/src/common/filesystem/file_rff.cpp b/src/common/filesystem/file_rff.cpp index 67c26def1..723d184e6 100644 --- a/src/common/filesystem/file_rff.cpp +++ b/src/common/filesystem/file_rff.cpp @@ -34,6 +34,9 @@ */ #include "resourcefile.h" +#include "fs_swap.h" + +using namespace fs_private; //========================================================================== // @@ -217,7 +220,7 @@ int FRFFLump::FillCache() if (Flags & LUMPF_COMPRESSED) { - int cryptlen = min (LumpSize, 256); + int cryptlen = std::min (LumpSize, 256); uint8_t *data = (uint8_t *)Cache; for (int i = 0; i < cryptlen; ++i) diff --git a/src/common/filesystem/file_wad.cpp b/src/common/filesystem/file_wad.cpp index 1a256db65..beab51476 100644 --- a/src/common/filesystem/file_wad.cpp +++ b/src/common/filesystem/file_wad.cpp @@ -36,7 +36,9 @@ #include #include "resourcefile.h" #include "filesystem.h" -#include "engineerrors.h" +#include "fs_swap.h" + +using namespace fs_private; struct wadinfo_t diff --git a/src/common/filesystem/file_whres.cpp b/src/common/filesystem/file_whres.cpp index cc37ec26c..2d5223041 100644 --- a/src/common/filesystem/file_whres.cpp +++ b/src/common/filesystem/file_whres.cpp @@ -37,25 +37,15 @@ #include "resourcefile.h" #include "cmdlib.h" +#include "fs_swap.h" + +using namespace fs_private; //========================================================================== // // // //========================================================================== -struct whresentry -{ - int filepospage, filelen, priority; -} ; - -struct dpackheader_t -{ - int ident; // == IDPAKHEADER - int dirofs; - int dirlen; -} ; - - //========================================================================== // // Wad file diff --git a/src/common/filesystem/file_zip.cpp b/src/common/filesystem/file_zip.cpp index 55e4081fe..d5680ba3e 100644 --- a/src/common/filesystem/file_zip.cpp +++ b/src/common/filesystem/file_zip.cpp @@ -41,6 +41,10 @@ #include "w_zip.h" #include "ancientzip.h" +#include "fs_findfile.h" +#include "fs_swap.h" + +using namespace fs_private; #define BUFREADCOMMENT (0x400) @@ -120,7 +124,7 @@ static uint32_t Zip_FindCentralDir(FileReader &fin, bool* zip64) uint32_t uPosFound=0; FileSize = (uint32_t)fin.GetLength(); - uMaxBack = min(0xffff, FileSize); + uMaxBack = std::min(0xffff, FileSize); uBackRead = 4; while (uBackRead < uMaxBack) @@ -133,7 +137,7 @@ static uint32_t Zip_FindCentralDir(FileReader &fin, bool* zip64) uBackRead += BUFREADCOMMENT; uReadPos = FileSize - uBackRead; - uReadSize = min((BUFREADCOMMENT + 4), (FileSize - uReadPos)); + uReadSize = std::min((BUFREADCOMMENT + 4), (FileSize - uReadPos)); if (fin.Seek(uReadPos, FileReader::SeekSet) != 0) break; diff --git a/src/common/filesystem/files.cpp b/src/common/filesystem/files.cpp index 094bd8a99..88a66a59e 100644 --- a/src/common/filesystem/files.cpp +++ b/src/common/filesystem/files.cpp @@ -37,6 +37,8 @@ #include "files.h" #include "stb_sprintf.h" +using namespace fs_private; + #ifdef _WIN32 std::wstring toWide(const char* str); #endif @@ -274,7 +276,7 @@ long MemoryReader::Seek(long offset, int origin) } if (offset < 0 || offset > Length) return -1; - FilePos = clamp(offset, 0, Length); + FilePos = std::clamp(offset, 0, Length); return 0; } diff --git a/src/common/filesystem/files.h b/src/common/filesystem/files.h index 4cfe74382..eb4b202ac 100644 --- a/src/common/filesystem/files.h +++ b/src/common/filesystem/files.h @@ -42,7 +42,7 @@ #include #include #include "basics.h" -#include "m_swap.h" +#include "fs_swap.h" #include "tarray.h" class FileSystemException : public std::exception @@ -268,53 +268,53 @@ public: return v; } + uint16_t ReadUInt16() { uint16_t v = 0; Read(&v, 2); - return LittleShort(v); + return fs_private::LittleShort(v); } int16_t ReadInt16() { - int16_t v = 0; + return (int16_t)ReadUInt16(); + } + + int16_t ReadUInt16BE() + { + uint16_t v = 0; Read(&v, 2); - return LittleShort(v); + return fs_private::BigShort(v); } int16_t ReadInt16BE() { - int16_t v = 0; - Read(&v, 2); - return BigShort(v); + return (int16_t)ReadUInt16BE(); } uint32_t ReadUInt32() { uint32_t v = 0; Read(&v, 4); - return LittleLong(v); + return fs_private::LittleLong(v); } int32_t ReadInt32() { - int32_t v = 0; - Read(&v, 4); - return LittleLong(v); + return (int32_t)ReadUInt32(); } uint32_t ReadUInt32BE() { uint32_t v = 0; Read(&v, 4); - return BigLong(v); + return fs_private::BigLong(v); } int32_t ReadInt32BE() { - int32_t v = 0; - Read(&v, 4); - return BigLong(v); + return (int32_t)ReadUInt32BE(); } uint64_t ReadUInt64() @@ -368,7 +368,8 @@ public: virtual size_t Write(const void *buffer, size_t len); virtual long Tell(); virtual long Seek(long offset, int mode); - size_t Printf(const char *fmt, ...) GCCPRINTF(2,3); + size_t Printf(const char *fmt, ...); + virtual void Close() { if (File != NULL) fclose(File); diff --git a/src/common/filesystem/files_decompress.cpp b/src/common/filesystem/files_decompress.cpp index 3e636d1bd..d38dfe25b 100644 --- a/src/common/filesystem/files_decompress.cpp +++ b/src/common/filesystem/files_decompress.cpp @@ -527,7 +527,7 @@ class DecompressorLZSS : public DecompressorBase return false; Stream.AvailIn -= 2; - uint16_t pos = BigShort(*(uint16_t*)Stream.In); + uint16_t pos = fs_private::BigShort(*(uint16_t*)Stream.In); uint8_t len = (pos & 0xF)+1; pos >>= 4; Stream.In += 2; @@ -551,7 +551,7 @@ class DecompressorLZSS : public DecompressorBase // Partial overlap: Copy in 2 or 3 chunks. do { - unsigned int copy = min(len, pos+1); + unsigned int copy = std::min(len, pos+1); memcpy(Stream.InternalBuffer, copyStart, copy); Stream.InternalBuffer += copy; Stream.InternalOut += copy; @@ -623,7 +623,7 @@ public: break; } - unsigned int copy = min(Stream.InternalOut, AvailOut); + unsigned int copy = std::min(Stream.InternalOut, AvailOut); if(copy > 0) { memcpy(Out, Stream.WindowData, copy); diff --git a/src/common/filesystem/fs_findfile.cpp b/src/common/filesystem/fs_findfile.cpp index 5830d2fca..021494644 100644 --- a/src/common/filesystem/fs_findfile.cpp +++ b/src/common/filesystem/fs_findfile.cpp @@ -106,7 +106,7 @@ static void *FS_FindFirst(const char *const filespec, findstate_t *const fileinf if (slash) { pattern = slash + 1; - fileinfo->path = std::string(filespec, slash - filespec + 1); + fileinfo->path = std::string(filespec, 0, slash - filespec + 1); dir = fileinfo->path.c_str(); } else diff --git a/src/common/filesystem/fs_swap.h b/src/common/filesystem/fs_swap.h new file mode 100644 index 000000000..1e8bbef83 --- /dev/null +++ b/src/common/filesystem/fs_swap.h @@ -0,0 +1,121 @@ +// +// DESCRIPTION: +// Endianess handling, swapping 16bit and 32bit. +// +//----------------------------------------------------------------------------- + + +#ifndef __FS_SWAP_H__ +#define __FS_SWAP_H__ + +#include + +// Endianess handling. +// WAD files are stored little endian. + +#ifdef __APPLE__ +#include +#endif + +namespace fs_private +{ + +#ifdef __APPLE__ + +inline unsigned short LittleShort(unsigned short x) +{ + return OSSwapLittleToHostInt16(x); +} + +inline unsigned int LittleLong(unsigned int x) +{ + return OSSwapLittleToHostInt32(x); +} + +inline unsigned short BigShort(unsigned short x) +{ + return OSSwapBigToHostInt16(x); +} + +inline unsigned int BigLong(unsigned int x) +{ + return OSSwapBigToHostInt32(x); +} + + +#elif defined __BIG_ENDIAN__ + +// Swap 16bit, that is, MSB and LSB byte. +// No masking with 0xFF should be necessary. +inline unsigned short LittleShort (unsigned short x) +{ + return (unsigned short)((x>>8) | (x<<8)); +} + +inline unsigned int LittleLong (unsigned int x) +{ + return (unsigned int)( + (x>>24) + | ((x>>8) & 0xff00) + | ((x<<8) & 0xff0000) + | (x<<24)); +} + +inline unsigned short BigShort(unsigned short x) +{ + return x; +} + +inline unsigned int BigLong(unsigned int x) +{ + return x; +} + +#else + +inline unsigned short LittleShort(unsigned short x) +{ + return x; +} + +inline unsigned int LittleLong(unsigned int x) +{ + return x; +} + +#ifdef _MSC_VER + +inline unsigned short BigShort(unsigned short x) +{ + return _byteswap_ushort(x); +} + +inline unsigned int BigLong(unsigned int x) +{ + return (unsigned int)_byteswap_ulong((unsigned long)x); +} + +#pragma warning (default: 4035) + +#else + +inline unsigned short BigShort (unsigned short x) +{ + return (unsigned short)((x>>8) | (x<<8)); +} + +inline unsigned int BigLong (unsigned int x) +{ + return (unsigned int)( + (x>>24) + | ((x>>8) & 0xff00) + | ((x<<8) & 0xff0000) + | (x<<24)); +} + +#endif + +#endif // __BIG_ENDIAN__ +} + +#endif // __M_SWAP_H__