- use a local byteswap header in the file system.

This commit is contained in:
Christoph Oelckers 2023-08-19 14:06:06 +02:00
parent 771fa2b51c
commit 454af06acf
12 changed files with 170 additions and 43 deletions

View file

@ -34,6 +34,9 @@
*/
#include "resourcefile.h"
#include "fs_swap.h"
using namespace fs_private;
//==========================================================================
//

View file

@ -33,7 +33,6 @@
*/
#include "resourcefile.h"
#include "cmdlib.h"
//==========================================================================
//

View file

@ -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;
} ;

View file

@ -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<int> (LumpSize, 256);
int cryptlen = std::min<int> (LumpSize, 256);
uint8_t *data = (uint8_t *)Cache;
for (int i = 0; i < cryptlen; ++i)

View file

@ -36,7 +36,9 @@
#include <ctype.h>
#include "resourcefile.h"
#include "filesystem.h"
#include "engineerrors.h"
#include "fs_swap.h"
using namespace fs_private;
struct wadinfo_t

View file

@ -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

View file

@ -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<uint32_t>(0xffff, FileSize);
uMaxBack = std::min<uint32_t>(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<uint32_t>((BUFREADCOMMENT + 4), (FileSize - uReadPos));
uReadSize = std::min<uint32_t>((BUFREADCOMMENT + 4), (FileSize - uReadPos));
if (fin.Seek(uReadPos, FileReader::SeekSet) != 0) break;

View file

@ -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<long>(offset, 0, Length);
FilePos = std::clamp<long>(offset, 0, Length);
return 0;
}

View file

@ -42,7 +42,7 @@
#include <stdarg.h>
#include <functional>
#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);

View file

@ -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<unsigned int>(len, pos+1);
unsigned int copy = std::min<unsigned int>(len, pos+1);
memcpy(Stream.InternalBuffer, copyStart, copy);
Stream.InternalBuffer += copy;
Stream.InternalOut += copy;
@ -623,7 +623,7 @@ public:
break;
}
unsigned int copy = min<unsigned int>(Stream.InternalOut, AvailOut);
unsigned int copy = std::min<unsigned int>(Stream.InternalOut, AvailOut);
if(copy > 0)
{
memcpy(Out, Stream.WindowData, copy);

View file

@ -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

View file

@ -0,0 +1,121 @@
//
// DESCRIPTION:
// Endianess handling, swapping 16bit and 32bit.
//
//-----------------------------------------------------------------------------
#ifndef __FS_SWAP_H__
#define __FS_SWAP_H__
#include <stdlib.h>
// Endianess handling.
// WAD files are stored little endian.
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#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__